grub-probe && FreeBSD
PATCH 1/1 grub-probe && FreeBSD
I finished to make grub-probe functionnal on FreeBSD system. I also added a few useful command-line options on the utility.
1) grub-probe support multiple output formats and targets. Optionnal patch. (util_grub-probe.c.diff)
2) Added support for resolution of mounted filesystem device. (util_getroot.c.diff)
3) Added support for resolution of device total sector size. (util_biosdisk.d.diff)
4) Added capacity of generating a valid grub device string. (util_biosdisk.d.diff)
5) Modified grub_strncpy so it does not skip the terminating nul-byte of the source string. It was making grub_ufs_dir() to mis- duplicate the path parameter. Further testing are required to know if it does not break anything but I'm confident it will not.
(kern_misc.c.diff)
6) Added GRUB_ERR_IOCTL to include/grub/err.h
(include_grub_err.h.diff)
Patches have been tested on a source tree fetched 2008/01/21 20:43 EST (10 minutes ago).
I will continue to work on the sources so grub-setup and grub-install works. The other utilities will come after.
Also, if someone can email-me how to uses diff so I can create patch in a single file, it would be greatly appreciated. :)
Francis Gendreau
54c54,55
< GRUB_ERR_MENU
---
Quoted 2 lines
GRUB_ERR_MENU,
GRUB_ERR_IOCTL
58d57
<
61d59
<
69,72c67,68
<
< while ((*p++ = *src++) != '\0' && --c)
< ;
<
---
Quoted 2 lines
while ((*p++ = *src++) != '\0' && c)
--c;579c575
< /* Skip the slow computation if 32-bit arithmetic is possible. */
---
Quoted 1 line
/* Skip the slow computation, if 32-bit arithmetics are possible. */
4c4
< * Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008 Free Software Foundation, Inc.
---
Quoted 1 line
* Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007 Free Software Foundation, Inc.
37a38
Quoted 1 line
#include <
regex.h>
78a80,85
Quoted 6 lines
#ifdef
__FreeBSD__# include <sys/types.h> # include <sys/ioctl.h> # include <sys/disk.h> #endif
105c112
< if ((name[0] != 'f' && name[0] != 'h') || name[1] != 'd')
---
Quoted 1 line
if ((name[0] != 'f' && name[0] != 'h') || name[1] != 'd') // allow only 'fd' or 'hd'
196a204,254
Quoted 49 lines
#elif defined (
__FreeBSD__)
/* To get the amound of sector from FreeBSD, we must uses ioctl(2).
**
** DIOCGSECTORSIZE is for the sector size of the unit (unsigned int).
** DIOCGMEDIASIZE is for the total amound of bytes on the media(off_t or unsigned long long. **total_sectors=media_sizedevided bysector_size
*/{ unsigned long long media; unsigned int sector; int fd; char *msg; grub_errno = GRUB_ERR_NONE; fd = open (map[drive], O_RDONLY); if ( fd == -1 ) (void) grub_fatal ("Failed to open `%s' associated to `%s'. %s\n", map[drive], name, strerror(errno)); if ( ioctl (fd, DIOCGSECTORSIZE, §or) ) { grub_errno = GRUB_ERR_IOCTL; msg = "Failed to get the media's sector size."; } else if ( ioctl (fd, DIOCGMEDIASIZE, &media) ) { grub_errno = GRUB_ERR_IOCTL; msg = "Failed to get the media's size."; } else if ( media % sector != 0 ) { grub_errno = GRUB_ERR_BAD_DEVICE; msg = "Unaligned device size."; } else disk->total_sectors = media / sector; close (fd); if ( grub_errno != GRUB_ERR_NONE ) { char *str = NULL; if ( grub_errno == GRUB_ERR_IOCTL ) str = strerror (errno); grub_error (grub_errno, "Unable to open %s correctly. %s %s", map[drive], msg, str?str:""); } grub_util_info ("the size of %s is %llu sectors", name, disk->total_sectors); return grub_errno;
Quoted 2 lines
}
428c486
< /* Work around a bug in Linux ez remapping. Linux remaps all
---
Quoted 1 line
/* Work around a bug in linux's ez remapping. Linux remaps all
612c670
< if (strncmp ("ide/", p, 4) == 0)
---
Quoted 1 line
if (strncmp ("/dev/ide/", p, 9) == 0)622c680
< if (strncmp ("scsi/", p, 5) == 0)
---
Quoted 1 line
if (strncmp ("/dev/scsi/", p, 10) == 0)683c741
< if (map[i] && strcmp (map[i], os_disk) == 0)
---
Quoted 1 line
if (map[i] && strncmp (map[i], os_disk, strlen (map[i])) == 0)
712c770
<
---
Quoted 1 line
#ifndef __FreeBSD__
715c773
<
---
Quoted 1 line
#endif
852a911,955
Quoted 40 lines
#elif
defined(__FreeBSD__)
/* FreeBSD uses: /.+/([a-zA-Z0-9]+)([0-9]+)s([1234])([a-zA-Z])where: $1 = driver name (ad sd fd ...) $2 = driver unit $3 = slice unit (1-4) $4 = label unit (a-z) We must extract $3 and $4. */ { int ret, dos_part = -1, bsd_part = -1; regex_t re; char *re_str = "/.+/([-a-zA-Z0-9_]+)([0-9]+)s([1-4])([a-zA-Z])"; #define RE_EBUF_SIZE 4096 char ebuf[RE_EBUF_SIZE]; #ifdef N_MATCHES # define SAVED_N_MATCHES N_MATCHES # undef N_MATCHES #endif #define N_MATCHES 5 regmatch_t matches[N_MATCHES]; ret = regcomp (&re, re_str, REG_EXTENDED); if ( ret ) { regerror (ret, &re, ebuf, RE_EBUF_SIZE); grub_util_error ("Std1003.2 Regular Expression compilation on `%s': %s", re_str, ebuf); } ret = regexec (&re, os_dev, N_MATCHES, matches, 0); if ( ret ) { regerror (ret, &re, ebuf, RE_EBUF_SIZE); grub_util_error ("Std1003.2 Regular Expression execution on `%s': %s", os_dev, ebuf); } regfree (&re); dos_part = strtol (os_dev+matches[3].rm_so, NULL, 10) -1; bsd_part = *(os_dev+matches[4].rm_so) - 'a'; return make_device_name (drive, dos_part, bsd_part);
Quoted 5 lines
}
#undefN_MATCHES
# ifdefSAVED_N_MATCHES
# defineN_MATCHESSAVED_N_MATCHES
#endif
125a126
Quoted 1 line
#ifndef
__FreeBSD__
240a242,289
Quoted 45 lines
#else /* defined
__FreeBSD__*/
/* Makegrub_guess_root_device()to use statfs(2) on FreeBSD system ** in order to locate the device associated to a mount point. ** The diactivated section of the function make the function skip ** the slice and bsdlabel portion of the device name.
*/
#include <sys/param.h>
#include <sys/mount.h>
#include <stdlib.h>
#include <errno.h>
char *grub_guess_root_device(const char *path)
{
char *ret = NULL;
struct statfs sfsb;if ( !statfs (path, &sfsb) ) {/* char *endp = sfsb.f_mntfromname, *s = path; size_t l; for (l = 0; l < MNAMELEN && *s; ++l, ++s); endp += l - 1; while ( *endp != '/' ) // emulate basename(3) --endp; while ( *endp < '0' || *endp > '9' ) // skip driver name ++endp; while ( *endp >= '0' && *endp <= '9' ) // skip driver unit ++endp; l = endp - sfsb.f_mntfromname; // adjust length ret = xmalloc (l+1); if ( ret ) { strncpy (ret, sfsb.f_mntfromname, l); ret[l] = '\0'; } */ ret = xmalloc (MNAMELEN); strncpy (ret, sfsb.f_mntfromname, MNAMELEN); } else { grub_util_error ("Cannot stat `%s'", path); }
Quoted 3 lines
return ret;
}
#endif /*__FreeBSD__*/
42,45c42,53
< #define PRINT_FS 0
< #define PRINT_DRIVE 1
< #define PRINT_DEVICE 2
< #define PRINT_PARTMAP 3
---
Quoted 9 lines
#define GRUB_PRINT_DEFAULT 0x00 #define GRUB_PRINT_DRIVE 0x01 #define GRUB_PRINT_DEVICE 0x02 #define GRUB_PRINT_PARTMAP 0x04 #define GRUB_PRINT_FS 0x08 #define GRUB_PRINT_INVAL 0x10 #define GRUB_PRINT_SHVAR 0x20 #define GRUB_PRINT_PREFIX 0x40
Quoted 3 lines
static int
grub_parse_target(const char *arg, intold_fl); static void usage (int status);
static voiddump_target_flags(int fl);
47c55,58
< int print = PRINT_FS;
---
Quoted 4 lines
static char
*output_prefix= NULL;
static voidprint_target_value( inttarget_fl, int target, const char *value);static void probe (const char *path, int
target_fl);
72,138d82
< static void
< probe (const char *path)
< {
< char *device_name; < char *drive_name = NULL; < grub_device_t dev; < grub_fs_t fs;
<
< device_name = grub_guess_root_device (path);
< if (! device_name)
< grub_util_error ("cannot find a device for %s.\n", path);
<
< if (print == PRINT_DEVICE)
< {
< printf ("%s\n", device_name);
< goto end;
< }
<
< drive_name = grub_util_get_grub_dev (device_name);
< if (! drive_name)
< grub_util_error ("cannot find a GRUB drive for %s.\n", device_name);
<
< if (print == PRINT_DRIVE)
< {
< printf ("(%s)\n", drive_name);
< goto end;
< }
<
< grub_util_info ("opening %s", drive_name);
< dev = grub_device_open (drive_name);
< if (! dev)< grub_util_error ("%s", grub_errmsg);
<
< if (print == PRINT_PARTMAP)
< {
< if (dev->disk->partition == NULL)
< grub_util_error ("Cannot detect partition map for %s", drive_name);
<
< if (strcmp (dev->disk->partition->partmap->name, "amiga_partition_map") == 0)
< printf ("amiga\n");
< else if (strcmp (dev->disk->partition->partmap->name, "apple_partition_map") == 0)
< printf ("apple\n");
< else if (strcmp (dev->disk->partition->partmap->name, "gpt_partition_map") == 0)
< printf ("gpt\n");
< else if (strcmp (dev->disk->partition->partmap->name, "pc_partition_map") == 0)
< printf ("pc\n");
< else if (strcmp (dev->disk->partition->partmap->name, "sun_partition_map") == 0)
< printf ("sun\n");
< else
< grub_util_error ("Unknown partition map %s", dev->disk->partition->partmap->name);
< goto end;
< }
<
< fs = grub_fs_probe (dev);
< if (! fs)
< grub_util_error ("%s", grub_errmsg);
<
< printf ("%s\n", fs->name);
<
< grub_device_close (dev);
<
< end:
<
< free (device_name);
< free (drive_name);
< }
<
145a90,91
Quoted 2 lines
{"prefix-prefix", required_argument, 0, 'P'},
{"prefix-shvar", required_argument, 0, 'S'},149,174d94
< static void
< usage (int status)
< {
< if (status)
< fprintf (stderr,
< "Try ``grub-probe --help'' for more information.\n");
< else
< printf ("\
< Usage: grub-probe [OPTION]... PATH\n\
< \n\
< Probe device information for a given path.\n\
< \n\
< -m, --device-map=FILE use FILE as the device map [default=%s]\n\
< -t, --target=(fs|drive|device|partmap)\n\
< print filesystem module, GRUB drive, system device or partition map module [default=fs]\n\ < -h, --help display this message and exit\n\ < -V, --version print version information and exit\n\ < -v, --verbose print verbose messages\n\
< \n\
< Report bugs to <%s>.\n\
< ",
< DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
<
< exit (status);
< }
<
179a100
Quoted 1 line
int target_fl = GRUB_PRINT_DEFAULT;
186c107
< int c = getopt_long (argc, argv, "m:t:hVv", options, 0);
---
Quoted 1 line
int c = getopt_long (argc, argv, "m:t:hVvP:S:A", options, 0);
201,210c122
< if (!strcmp (optarg, "fs"))
< print = PRINT_FS;
< else if (!strcmp (optarg, "drive"))
< print = PRINT_DRIVE;
< else if (!strcmp (optarg, "device"))
< print = PRINT_DEVICE;
< else if (!strcmp (optarg, "partmap"))
< print = PRINT_PARTMAP;
< else
< usage (1);
---
Quoted 1 line
target_fl = grub_parse_target (optarg, target_fl);
212a125,139
Quoted 6 lines
case 'S':
output_prefix = optarg;
target_fl |= GRUB_PRINT_SHVAR;
break;
case 'P':Quoted 9 lines
output_prefix = optarg;
target_fl |= GRUB_PRINT_PREFIX;
break;
case 'A':
target_fl |= GRUB_PRINT_FS|GRUB_PRINT_PARTMAP
|GRUB_PRINT_DEVICE|GRUB_PRINT_DRIVE;
break;245a173,185
Quoted 10 lines
if (target_fl & GRUB_PRINT_INVAL)
{
if ((target_fl & ~GRUB_PRINT_INVAL) == GRUB_PRINT_DEFAULT)
{
fputs ("No targets provided are valid.\n", stderr);
usage(1);
}
target_fl = target_fl & ~GRUB_PRINT_INVAL;
}Quoted 3 lines
if ((
target_fl&GRUB_PRINT_PREFIX) && (target_fl& (GRUB_PRINT_SHVAR)))grub_util_error ("targets `prefix' and `shvar' should not be used together!");
253c193
< probe (path);
---
Quoted 1 line
probe (path, target_fl);
261a202,452
Quoted 251 lines
}
static void
probe (const char *path, inttarget_fl)
{
char*device_name= NULL;
char*drive_name= NULL;
grub_device_tdev;
grub_fs_tfs;if ((target_fl & ~(GRUB_PRINT_PREFIX|GRUB_PRINT_SHVAR)) == GRUB_PRINT_DEFAULT) target_fl |= GRUB_PRINT_FS; //dump_target_flags (target_fl); device_name = grub_guess_root_device (path); if (! device_name) grub_util_error ("cannot find a device for %s.\n", path); grub_util_info ("Using device `%s' for `%s'", device_name, path); if (target_fl & GRUB_PRINT_DEVICE) print_target_value (target_fl, GRUB_PRINT_DEVICE, device_name); if (target_fl & (GRUB_PRINT_FS|GRUB_PRINT_PARTMAP|GRUB_PRINT_DRIVE)) { drive_name = grub_util_get_grub_dev (device_name); if (! drive_name) grub_util_error ("cannot find a GRUB drive for %s.\n", device_name);if (target_fl & GRUB_PRINT_DRIVE) print_target_value (target_fl, GRUB_PRINT_DRIVE, drive_name); if (target_fl & (GRUB_PRINT_FS|GRUB_PRINT_PARTMAP)) { dev = grub_device_open (drive_name); if (! dev) grub_util_error ("%s", grub_errmsg); if (target_fl & GRUB_PRINT_PARTMAP) { char *value; if (dev->disk->partition == NULL) grub_util_error ("Cannot detect partition map for %s", drive_name); if (strcmp (dev->disk->partition->partmap->name, "amiga_partition_map") == 0) value = "amiga"; else if (strcmp (dev->disk->partition->partmap->name, "apple_partition_map") == 0) value = "apple"; else if (strcmp (dev->disk->partition->partmap->name, "gpt_partition_map") == 0) value = "gpt"; else if (strcmp (dev->disk->partition->partmap->name, "pc_partition_map") == 0) value = "pc"; else if (strcmp (dev->disk->partition->partmap->name, "sun_partition_map") == 0) value = "sun"; else { if (!(target_fl & (GRUB_PRINT_PREFIX|GRUB_PRINT_SHVAR))) grub_util_error ("Unknown partition map %s", dev->disk->partition->partmap->name); else value = ""; } print_target_value (target_fl, GRUB_PRINT_PARTMAP, value); } if (target_fl & GRUB_PRINT_FS) { fs = grub_fs_probe (dev); if (! fs) grub_util_error ("%s", grub_errmsg); print_target_value (target_fl, GRUB_PRINT_FS, fs->name); } grub_device_close (dev); } } if (device_name) free (device_name); if (drive_name) free (drive_name); }static void
print_target_value( inttarget_fl, int target, const char *value) {
char*target_str= NULL;if (!value) value = ""; if (target == GRUB_PRINT_FS) target_str = "fs"; else if (target == GRUB_PRINT_DRIVE) target_str = "drive"; else if (target == GRUB_PRINT_DEVICE) target_str = "device"; else if (target == GRUB_PRINT_PARTMAP) target_str = "partmap"; else { fprintf (stderr, "INTERNAL ERROR: invalid target int print_target_value()\n"); exit(1); }if (
target_fl& (GRUB_PRINT_SHVAR|GRUB_PRINT_PREFIX)){ if (target_fl & GRUB_PRINT_SHVAR) { if (output_prefix) fprintf (stdout, "%s%s=\"%s\"\n", output_prefix, target_str, value); else fprintf (stdout, "%s=\"%s\"\n", target_str, value); } if (target_fl & GRUB_PRINT_PREFIX) { if (output_prefix) fprintf (stdout, "%s%s: %s\n", output_prefix, target_str, value); else fprintf (stdout, "%s: %s\n", target_str, value); } } else { puts (value);}
}static int
grub_parse_target(const char *arg, intold_fl)
{
char *argv[16];
int i;
int fl =GRUB_PRINT_DEFAULT;if ( arg )
{ char *next = (char*) arg; for ( i = 0; i < 16 && next && *next ; ++i ) { arg = (const char*)next; next = strchr( next, ','); if ( next ) { *next = 0; ++next; } argv[i] = (char*)arg; } while (i > 0) { --i; if (!strcmp ("all", argv[i])) fl |= GRUB_PRINT_FS|GRUB_PRINT_DRIVE|GRUB_PRINT_DEVICE|GRUB_PRINT_PARTMAP; else if (!strcmp ("fs", argv[i])) fl |= GRUB_PRINT_FS; else if (!strcmp ("drive", argv[i])) fl |= GRUB_PRINT_DRIVE; else if (!strcmp ("device", argv[i])) fl |= GRUB_PRINT_DEVICE; else if (!strcmp ("partmap", argv[i])) fl |= GRUB_PRINT_PARTMAP; else if (!strcmp ("prefix", argv[i])) fl |= GRUB_PRINT_PREFIX; else if (!strcmp ("shvar", argv[i])) fl |= GRUB_PRINT_SHVAR; else { fl |= GRUB_PRINT_INVAL; grub_util_info ("grub-probe: warning: Invalid target `%s'\n", argv[i]); } }}
return fl |old_fl;
}static void
usage (int status)
{
if (status)fprintf (stderr, "Try ``grub-probe --help'' for more information.\n"); else printf ("\Usage: grub-probe [OPTION]... PATH\n\
\n\
Probe device information for a given path.\n\
\n\
-m, --device-map=FILE use FILE as the device map [default=%s]\n\-t, --target=(all,fs,drive,device,partmap,prefix,shvar)\n\ print filesystem module, GRUB drive, system\n\ device and/or partition map module [default=fs].\n\ It is possible to add multiple targets by separating\n\ them using commas and/or adding multiple -t on\n\ the command line. `all' can be used to set\n\ `fs', `drive', `device' and `partmap' targets.\n\n\ If no -t parameters are specified, grub-probe\n\ will print the default target.\n\n\ On the other hand, if targets are specified and none\n\ contain valid targets, the application terminates\n\ after emitting an error message. If some of the targets\n\ are valid, the application will warn the user about\n\ invalid targets and print the values for the valid\n\ ones.\n\n\ There is also two pseudo-targets which cannot co-exist:\n\n\ \t`prefix' will print the target name before the\n\ \t\t value, separating them with the sequence\n\ \t\t semi-colon_space (: ).\n\ \t\tex:\ttarget: value\\n\n\n\ \t`shvar'\t prints values in this format:\n\ \t\tex:\ttarget=\"value\"\\n\n\n\ If `shvar' is used, value can be imported into Bourne\n\ Shell scripts using:\n\ \t\teval `grub-probe -t shvar <path>`\n\ -P, --prefix-prefix=\"prefix\"\n\ Prefixes the output variable name with \"prefix\" in \n\ `prefix' output mode and activate it.\n\ -S, --prefix-shvar=\"prefix\"\n\ Prefix outputs variable name with \"prefix\" in \n\ `shvar' output mode and activate it.\n\ -A, --all Same as `-t all'\n\ -h, --help display this message and exit\n\ -V, --version print version information and exit\n\ -v, --verbose print verbose messages\n\ \n\ Report bugs to <%s>.\n\ ", DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);exit (status);
}static void
dump_target_flags(int fl)
{
fputs ("grub-probe: DUMP: target flags = ", stderr); if ( fl &GRUB_PRINT_INVAL)fputs ("GRUB_PRINT_INVAL ", stderr); if ( fl & GRUB_PRINT_DRIVE ) fputs ("GRUB_PRINT_DRIVE ", stderr); if ( fl & GRUB_PRINT_DEVICE ) fputs ("GRUB_PRINT_DEVICE ", stderr); if ( fl & GRUB_PRINT_PARTMAP ) fputs ("GRUB_PRINT_PARTMAP ", stderr); if ( fl & GRUB_PRINT_FS ) fputs ("GRUB_PRINT_FS ", stderr); if ( fl & GRUB_PRINT_SHVAR ) fputs ("GRUB_PRINT_SHVAR ", stderr); if ( fl & GRUB_PRINT_PREFIX ) fputs ("GRUB_PRINT_PREFIX ", stderr);fputs ("\n", stderr);
Quoted 3 lines · Francis Gendreau
Also, if someone can email-me how to uses diff so I can create patch in a single file, it would be greatly appreciated. :)
diff -urp grub.old grub.new
-r for recursive.
-u is needed to prevent everyone's eyes from bleeding (for some odd reason, everyone prefers this but is still not the default).
-p is often preferred in this list to track which functions you changed.
Please resend your patch with the new params, it'll make it easier to review it.
Also, remember to include a ChangeLog entry describing it.
Thanks!
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What use is a phone call, if you are unable to speak? (as seen on /.)
Quoted 12 lines · Robert Millan
Quoted 3 lines · Francis Gendreau
Also, if someone can email-me how to uses diff so I can create patch in a single file, it would be greatly appreciated. :)
diff -urp grub.old grub.new
(quoted diff) +0
-2-r for recursive. -u is needed to prevent everyone's eyes from bleeding (for some odd reason,everyone prefers this but is still not the default).
Quoted 6 lines
-p is often preferred in this list to track which functions you changed.
Please resend your patch with the new params, it'll make it easier to review it.
Also, remember to include a ChangeLog entry describing it.
No patch yet, or did I miss something?
--
Marco
Quoted 14 lines · Marco Gerards
Quoted 12 lines · Robert Millan
Quoted 3 lines · Francis Gendreau
Also, if someone can email-me how to uses diff so I can create patch in a single file, it would be greatly appreciated. :)
diff -urp grub.old grub.new
(quoted diff) +0
-2-r for recursive. -u is needed to prevent everyone's eyes from bleeding (for some odd reason,everyone prefers this but is still not the default).
Quoted 8 lines
Quoted 6 lines
-p is often preferred in this list to track which functions you changed.
Please resend your patch with the new params, it'll make it easier to review it.
Also, remember to include a ChangeLog entry describing it.
No patch yet, or did I miss something?
No _new_ patch. There's a patch, but it's split in several files and run without -u.
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What use is a phone call… if you are unable to speak? (as seen on /.)
Sorry,
I've been busy at other things. I'll update the patches for the current tree and send them back.
Francis
Quoted 32 lines · Marco Gerards
Quoted 12 lines · Robert Millan
Quoted 3 lines · Francis Gendreau
Also, if someone can email-me how to uses diff so I can create patch in a single file, it would be greatly appreciated. :)
diff -urp grub.old grub.new
(quoted diff) +0
-2-r for recursive. -u is needed to prevent everyone's eyes from bleeding (for some odd reason,everyone prefers this but is still not the default).
Quoted 6 lines
-p is often preferred in this list to track which functions you changed.
Please resend your patch with the new params, it'll make it easier to review it.
Also, remember to include a ChangeLog entry describing it.
No patch yet, or did I miss something?
--
Marco_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel