Nikos Skalkotos
2013-Nov-05 10:15 UTC
[Libguestfs] [PATCH stable-1.24] Fix fstab block device resolution for FreeBSD
The device name prefix for IDE hard drives used to be `ad' but now is `ada' (http://www.freebsd.org/doc/handbook/disks-naming.html). For virtio hard drives it is `vtbd'. Under an MBR partition table a slice will be used, so the name of the first partitions will be either `ada0s1a' or `vtbd0s1a'. Under a GPT partition table, where no slice is needed, the name of the first partition will be either `ada0p1' or `vtbd0p1'. Signed-off-by: Nikos Skalkotos <skalkoto@grnet.gr> --- src/guestfs-internal.h | 2 ++ src/inspect-fs-unix.c | 27 +++++++++++++++++++++++---- src/match.c | 25 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index 9936c84..5356920 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -630,12 +630,14 @@ extern int guestfs___match (guestfs_h *g, const char *str, const pcre *re); extern char *guestfs___match1 (guestfs_h *g, const char *str, const pcre *re); extern int guestfs___match2 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2); extern int guestfs___match3 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2, char **ret3); +extern int guestfs___match4 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2, char **ret3, char **ret4); extern int guestfs___match6 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2, char **ret3, char **ret4, char **ret5, char **ret6); #define match guestfs___match #define match1 guestfs___match1 #define match2 guestfs___match2 #define match3 guestfs___match3 +#define match4 guestfs___match4 #define match6 guestfs___match6 /* stringsbuf.c */ diff --git a/src/inspect-fs-unix.c b/src/inspect-fs-unix.c index 60b081d..8e0f135 100644 --- a/src/inspect-fs-unix.c +++ b/src/inspect-fs-unix.c @@ -64,7 +64,8 @@ static pcre *re_major_minor; static pcre *re_xdev; static pcre *re_cciss; static pcre *re_mdN; -static pcre *re_freebsd; +static pcre *re_freebsd_mbr; +static pcre *re_freebsd_gpt; static pcre *re_diskbyid; static pcre *re_netbsd; static pcre *re_opensuse; @@ -115,7 +116,8 @@ compile_regexps (void) COMPILE (re_xdev, "^/dev/(h|s|v|xv)d([a-z]+)(\\d*)$", 0); COMPILE (re_cciss, "^/dev/(cciss/c\\d+d\\d+)(?:p(\\d+))?$", 0); COMPILE (re_mdN, "^(/dev/md\\d+)$", 0); - COMPILE (re_freebsd, "^/dev/ad(\\d+)s(\\d+)([a-z])$", 0); + COMPILE (re_freebsd_mbr, "^/dev/(ada{0,1}|vtbd)(\\d+)s(\\d+)([a-z])$", 0); + COMPILE (re_freebsd_gpt, "^/dev/(ada{0,1}|vtbd)(\\d+)p(\\d+)$", 0); COMPILE (re_diskbyid, "^/dev/disk/by-id/.*-part(\\d+)$", 0); COMPILE (re_netbsd, "^NetBSD (\\d+)\\.(\\d+)", 0); COMPILE (re_opensuse, "^(openSUSE|SuSE Linux|SUSE LINUX) ", 0); @@ -143,7 +145,8 @@ free_regexps (void) pcre_free (re_xdev); pcre_free (re_cciss); pcre_free (re_mdN); - pcre_free (re_freebsd); + pcre_free (re_freebsd_mbr); + pcre_free (re_freebsd_gpt); pcre_free (re_diskbyid); pcre_free (re_netbsd); pcre_free (re_opensuse); @@ -1464,7 +1467,22 @@ resolve_fstab_device (guestfs_h *g, const char *spec, Hash_table *md_map) free (disk); } - else if (match3 (g, spec, re_freebsd, &disk, &slice, &part)) { + else if (match3 (g, spec, re_freebsd_gpt, &type, &disk, &part)) { + /* If the FreeBSD disk contains GPT partitions, the translation to Linux + * device names is straight forward. Partitions on a virtio disk are + * prefixed with vtbd. IDE hard drives used to be prefixed with ad and now + * are with ada. + */ + int disk_i = guestfs___parse_unsigned_int (g, disk); + int part_i = guestfs___parse_unsigned_int (g, part); + free (type); + free (disk); + free (part); + + if (disk_i != -1 && disk_i <= 26 && part_i > 0 && part_i <= 128) + device = safe_asprintf (g, "/dev/sd%c%d", disk_i + 'a', part_i); + } + else if (match4 (g, spec, re_freebsd_mbr, &type, &disk, &slice, &part)) { /* FreeBSD disks are organized quite differently. See: * http://www.freebsd.org/doc/handbook/disk-organization.html * FreeBSD "partitions" are exposed as quasi-extended partitions @@ -1474,6 +1492,7 @@ resolve_fstab_device (guestfs_h *g, const char *spec, Hash_table *md_map) int disk_i = guestfs___parse_unsigned_int (g, disk); int slice_i = guestfs___parse_unsigned_int (g, slice); int part_i = part[0] - 'a' /* counting from 0 */; + free (type); free (disk); free (slice); free (part); diff --git a/src/match.c b/src/match.c index 86c0a31..2a3e5a9 100644 --- a/src/match.c +++ b/src/match.c @@ -104,6 +104,31 @@ guestfs___match3 (guestfs_h *g, const char *str, const pcre *re, return 1; } +/* Match a regular expression which contains exactly four captures. */ +int +guestfs___match4 (guestfs_h *g, const char *str, const pcre *re, + char **ret1, char **ret2, char **ret3, char **ret4) +{ + size_t len = strlen (str); + int vec[30], r; + + r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30); + if (r == PCRE_ERROR_NOMATCH) + return 0; + + *ret1 = NULL; + *ret2 = NULL; + *ret3 = NULL; + *ret4 = NULL; + + if (r > 1) *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]); + if (r > 2) *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]); + if (r > 3) *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]); + if (r > 4) *ret4 = safe_strndup (g, &str[vec[8]], vec[9]-vec[8]); + + return 1; +} + /* Match a regular expression which contains exactly six captures. */ int guestfs___match6 (guestfs_h *g, const char *str, const pcre *re, -- 1.8.4.2
Richard W.M. Jones
2013-Nov-05 11:42 UTC
Re: [Libguestfs] [PATCH stable-1.24] Fix fstab block device resolution for FreeBSD
On Tue, Nov 05, 2013 at 12:15:47PM +0200, Nikos Skalkotos wrote:> The device name prefix for IDE hard drives used to be `ad' but now > is `ada' (http://www.freebsd.org/doc/handbook/disks-naming.html). > For virtio hard drives it is `vtbd'. > > Under an MBR partition table a slice will be used, so the name of > the first partitions will be either `ada0s1a' or `vtbd0s1a'. Under a > GPT partition table, where no slice is needed, the name of the first > partition will be either `ada0p1' or `vtbd0p1'. > > Signed-off-by: Nikos Skalkotos <skalkoto@grnet.gr>I've applied this. I also applied the attached patch, which readds support for the old format, on the assumption that old FreeBSD guests will still exist for some time to come. Hope this is OK. Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Nikos Skalkotos
2013-Nov-05 11:59 UTC
Re: [Libguestfs] [PATCH stable-1.24] Fix fstab block device resolution for FreeBSD
Hello Richard, I think my patch is sufficient. I don't thing you need this patch at all. The regular expression /dev/(ada{0,1}|vtbd) will match both: /dev/ada and /dev/ad. This is why I use "{0,1}" (=zero or one occurrence of the character a) Nikos On Tue 05 Nov 2013 01:42:44 PM EET, Richard W.M. Jones wrote:> On Tue, Nov 05, 2013 at 12:15:47PM +0200, Nikos Skalkotos wrote: >> The device name prefix for IDE hard drives used to be `ad' but now >> is `ada' (http://www.freebsd.org/doc/handbook/disks-naming.html). >> For virtio hard drives it is `vtbd'. >> >> Under an MBR partition table a slice will be used, so the name of >> the first partitions will be either `ada0s1a' or `vtbd0s1a'. Under a >> GPT partition table, where no slice is needed, the name of the first >> partition will be either `ada0p1' or `vtbd0p1'. >> >> Signed-off-by: Nikos Skalkotos <skalkoto@grnet.gr> > > I've applied this. > > I also applied the attached patch, which readds support for the old > format, on the assumption that old FreeBSD guests will still exist for > some time to come. Hope this is OK. > > Thanks, > > Rich. >
Apparently Analagous Threads
- [PATCH stable-1.24] Fix fstab block device resolution for FreeBSD
- Re: [PATCH stable-1.24] Fix fstab block device resolution for FreeBSD
- [PATCH stable-1.24] Fix fstab block device resolution for FreeBSD
- Re: [PATCH stable-1.24] Fix fstab block device resolution for FreeBSD
- [PATCH 4/5] Fix fstab block device resolution for FreeBSD