Skip to contentSkip to conversation

grub-probe support for NetBSD

PATCH In review Grégoire Sutre 3 participants 8 messages Dec 27, 2009 – Jan 4, 2010 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 grub-probe support for NetBSD

Hi,

I finished adding NetBSD specific code for grub-probe. A patch and a changelog entry are attached. A few notes:

- devices are required to be character (raw) devices. In NetBSD, those are of the form "/dev/r[wsc]d[0-9]+[a-z]" for hard disk drives and CD-ROM drives, as far as I know.

- in grub_util_biosdisk_open, the disk size is obtained from the disk label.

- in grub_util_biosdisk_get_grub_dev, the code is derived from the Linux
code: we get the start sector of the partition from the disk label, and we compare it with each partition GRUB recognizes. I don't think we can do better, as device names correspond to disklabel entries, and those can be quite arbitrary.

- in make_device_name, I fixed what I believe to be a typo, but I may be wrong.

Please let me know if I missed something,

Grégoire

2009-12-27 Gregoire Sutre <[email protected]>

Add grub-probe support for NetBSD.

* util/getroot.c (find_root_device): Convert block device to character device on NetBSD.
* util/probe.c (probe): Require character device on NetBSD.
* util/hostdisk.c (grub_util_biosdisk_open): NetBSD specific code. (convert_system_partition_to_system_disk): Likewise. (grub_util_biosdisk_get_grub_dev): Likewise. (make_device_name): Fixed a typo in bsd_part_str.

util/getroot.c +6 -0
diff -Naurp grub2_2009-12-27/util/getroot.c grub2/util/getroot.c
@@ -266,8 +266,14 @@ find_root_device (const char *dir, dev_t
 	  char *cwd;
 
 	  cwd = xgetcwd ();
+#if defined(__NetBSD__)
+	  /* Convert this block device to its character (raw) device */
+	  res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
+	  sprintf (res, "%s/r%s", cwd, ent->d_name);
+#else
 	  res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 2);
 	  sprintf (res, "%s/%s", cwd, ent->d_name);
+#endif
 	  strip_extra_slashes (res);
 	  free (cwd);
 
util/grub-probe.c +1 -1
diff -Naurp grub2_2009-12-27/util/grub-probe.c grub2/util/grub-probe.c
@@ -111,7 +111,7 @@ probe (const char *path, char *device_na
 
   if (path == NULL)
     {
-#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
       if (! grub_util_check_char_device (device_name))
         grub_util_error ("%s is not a character device.\n", device_name);
 #else
util/hostdisk.c +168 -7

236 lines not on this page · raw message

#1 lore raw

Hi,

Here is a new version of the patch (the change log remains the same as in the original email). The new version does not attempt to read from a floppy device in grub_util_biosdisk_open. Indeed, on NetBSD, reading from the floppy device (e.g. cat /dev/fd0a) takes a _long_ time to abort when there is no floppy in the drive.

With this patch (and with a few other changes [1] discussed here or on bug-grub), I could successfully install grub with grub-install on a USB stick (and then boot from it) on NetBSD 5.0 i386 and amd64.

Regards,

Grégoire

[1] http://pkgsrc-wip.cvs.sourceforge.net/viewvc/pkgsrc-wip/wip/grub2-current/patches/

getroot.c renamed from getroot.c.orig +6 -0
@@ -266,8 +266,14 @@ find_root_device (const char *dir, dev_t
 	  char *cwd;
 
 	  cwd = xgetcwd ();
+#if defined(__NetBSD__)
+	  /* Convert this block device to its character (raw) device */
+	  res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
+	  sprintf (res, "%s/r%s", cwd, ent->d_name);
+#else
 	  res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 2);
 	  sprintf (res, "%s/%s", cwd, ent->d_name);
+#endif
 	  strip_extra_slashes (res);
 	  free (cwd);
 
grub-probe.c renamed from grub-probe.c.orig +1 -1
@@ -111,7 +111,7 @@ probe (const char *path, char *device_na
 
   if (path == NULL)
     {
-#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
       if (! grub_util_check_char_device (device_name))
         grub_util_error ("%s is not a character device.\n", device_name);
 #else
hostdisk.c renamed from hostdisk.c.orig +174 -7

242 lines not on this page · raw message

#2 lore raw

getroot.c find_root_device (const char *dir, dev_t · line 276

Quoted 8 lines · Grégoire Sutre
getroot.c renamed from getroot.c.orig +6 -0
+#if defined(__NetBSD__)
+	  /* Convert this block device to its character (raw) device */
+	  res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
+	  sprintf (res, "%s/r%s", cwd, ent->d_name);
+#else
 	  res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 2);
 	  sprintf (res, "%s/%s", cwd, ent->d_name);
+#endif

Can you avoid code duplication here? Something like:

#ifdef __NetBSD__
const char *template = "%s/r%s";
#else
const char *template = "%s/%s";
#endif

Signature · 3 lines
Robert Millan

  "Be the change you want to see in the world" -- Gandhi
#3 lore raw
Quoted 12 lines · Robert Millan

getroot.c find_root_device (const char *dir, dev_t · line 276

Quoted 8 lines · Grégoire Sutre
getroot.c renamed from getroot.c.orig +6 -0
+#if defined(__NetBSD__)
+	  /* Convert this block device to its character (raw) device */
+	  res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
+	  sprintf (res, "%s/r%s", cwd, ent->d_name);
+#else
 	  res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 2);
 	  sprintf (res, "%s/%s", cwd, ent->d_name);
+#endif

Can you avoid code duplication here? Something like:

Quoted 5 lines

#ifdef __NetBSD__
const char *template = "%s/r%s";
#else
const char *template = "%s/%s";
#endif

Indeed, it's better. But we also need a variable for the extra length (3 for NetBSD and 2 otherwise). I have updated the patch.

http://pkgsrc-wip.cvs.sourceforge.net/viewvc/*checkout*/pkgsrc-wip/wip/grub2-current/patches/patch-grub-probe-netbsd

The patch is not finished yet: as discussed on IRC, I'll try to factorize Linux and NetBSD code in grub_util_biosdisk_get_grub_dev, and to fix the floppy problem.

Grégoire

#4 lore raw
Quoted 13 lines · Grégoire Sutre
Quoted 12 lines · Robert Millan

getroot.c find_root_device (const char *dir, dev_t · line 276

Quoted 8 lines · Grégoire Sutre
getroot.c renamed from getroot.c.orig +6 -0
+#if defined(__NetBSD__)
+      /* Convert this block device to its character (raw) device */
+      res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
+      sprintf (res, "%s/r%s", cwd, ent->d_name);
+#else
       res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 2);
       sprintf (res, "%s/%s", cwd, ent->d_name);
+#endif

Can you avoid code duplication here? Something like:

Quoted 8 lines
Quoted 5 lines

#ifdef __NetBSD__
const char *template = "%s/r%s";
#else
const char *template = "%s/%s";
#endif

Indeed, it's better. But we also need a variable for the extra length (3 for NetBSD and 2 otherwise).

One byte is cheap. No need for gimmicks just to save one byte. You can always allocate with +3

Quoted 17 lines

I have updated the patch.

http://pkgsrc-wip.cvs.sourceforge.net/viewvc/*checkout*/pkgsrc-wip/wip/grub2-current/patches/patch-grub-probe-netbsd

The patch is not finished yet: as discussed on IRC, I'll try to factorize Linux and NetBSD code in grub_util_biosdisk_get_grub_dev, and to fix the floppy problem.

Grégoire

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Regards
Vladimir 'φ-coder/phcoder' Serbinenko
#5 lore raw
Quoted 2 lines · Vladimir 'φ-coder/phcoder' Serbinenko

One byte is cheap. No need for gimmicks just to save one byte. You can always allocate with +3

Ok :-)

Grégoire

#6 lore raw
In reply to Grégoire Sutre
Quoted 10 lines · Grégoire Sutre
Quoted 7 lines

Can you avoid code duplication here? Something like:

#ifdef __NetBSD__
const char *template = "%s/r%s";
#else
const char *template = "%s/%s";
#endif

Indeed, it's better. But we also need a variable for the extra length (3 for NetBSD and 2 otherwise). I have updated the patch.

I understand; don't worry about that, GCC is smart and space is cheap in util/ anyway.

Quoted 1 line

http://pkgsrc-wip.cvs.sourceforge.net/viewvc/*checkout*/pkgsrc-wip/wip/grub2-current/patches/patch-grub-probe-netbsd

Would you please send patches as attachments? URLs don't make it easy for review.

Thanks!

Signature · 3 lines
Robert Millan

  "Be the change you want to see in the world" -- Gandhi
#7 lore raw
In reply to Grégoire Sutre

Hi,

This the new version of the patch adding grub-probe (and grub-setup) support for NetBSD. The main changes are:

- in function grub_util_biosdisk_get_grub_dev(), merge of the new NetBSD code with the original Linux code.

- re-enabled disk size detection for floppy. This is done as for the other devices via the disklabel. Hence, the floppy must have been labeled before, which is the recommended usage according to [1]. I couldn't find another way to get the disk size for floppies. For the record, an example of successfull installation on a floppy (with grub-install) is also attached.

- as the floppy driver takes a long time to abort when the floppy drive is empty, the option NORETRY is set on the floppy driver. The running time of grub-probe gets divided by 6 (30s instead of 3 minutes). It is still long, but is also due to the fact that grub-probe tries to read the floppy 4 times. Of course, this is only if the device map contains a floppy device, and grub-install --no-floppy does not suffer from this problem.

Thanks,

Grégoire

[1] http://www.netbsd.org/docs/guide/en/chap-rmmedia.html

Description: adds support for NetBSD in GRUB utilities.
See: http://lists.gnu.org/archive/html/grub-devel/2009-12/msg00432.html

Change log
----------
2010-??-?? Gregoire Sutre <[email protected]>

Add grub-probe support for NetBSD.

* util/getroot.c (find_root_device): Convert block device to character device on NetBSD.
* util/probe.c (probe): Require character device on NetBSD.
* util/hostdisk.c: NetBSD specific headers. (configure_device_driver): new function to tune device driver parameters (currently only for NetBSD floppy driver). (grub_util_biosdisk_open): NetBSD specific code (get disk size via disklabel ioctl).
(open_device): call configure_device_driver on NetBSD. (convert_system_partition_to_system_disk): NetBSD specific code. (device_is_wholedisk): Likewise. (grub_util_biosdisk_get_grub_dev): Likewise. (make_device_name): Fixed a typo in bsd_part_str.
* configure.ac: check for opendisk() and getrawpartition() on NetBSD and set LIBUTIL.
* Makefile.in: add LIBUTIL to LIBS.

getroot.c renamed from getroot.c.orig +9 -2
@@ -264,10 +264,17 @@ find_root_device (const char *dir, dev_t
 	  /* Found!  */
 	  char *res;
 	  char *cwd;
+#if defined(__NetBSD__)
+	  /* Convert this block device to its character (raw) device.  */
+	  const char *template = "%s/r%s";
+#else
+	  /* Keep the device name as it is.  */
+	  const char *template = "%s/%s";
+#endif
 
 	  cwd = xgetcwd ();
-	  res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 2);
-	  sprintf (res, "%s/%s", cwd, ent->d_name);
+	  res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
+	  sprintf (res, template, cwd, ent->d_name);
 	  strip_extra_slashes (res);
 	  free (cwd);
 
grub-probe.c renamed from grub-probe.c.orig +1 -1
@@ -111,7 +111,7 @@ probe (const char *path, char *device_na
 
   if (path == NULL)
     {
-#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
       if (! grub_util_check_char_device (device_name))
         grub_util_error ("%s is not a character device.\n", device_name);
 #else
hostdisk.c renamed from hostdisk.c.orig +142 -14

278 lines not on this page · raw message

configure.ac renamed from configure.ac.orig +14 -0
@@ -208,6 +208,20 @@ AC_CHECK_FUNCS(memmove sbrk strdup lstat
 AC_CHECK_HEADERS(sys/mkdev.h sys/sysmacros.h malloc.h termios.h sys/types.h)
 AC_CHECK_HEADERS(unistd.h string.h strings.h sys/stat.h sys/fcntl.h)
 
+# For opendisk() and getrawpartition() on NetBSD.
+# Used in util/deviceiter.c and in util/hostdisk.c.
+AC_CHECK_HEADER([util.h], [
+  AC_CHECK_LIB([util], [opendisk], [
+    LIBUTIL="-lutil"
+    AC_DEFINE(HAVE_OPENDISK, 1, [Define if opendisk() in -lutil can be used])
+  ])
+  AC_CHECK_LIB([util], [getrawpartition], [
+    LIBUTIL="-lutil"
+    AC_DEFINE(HAVE_GETRAWPARTITION, 1, [Define if getrawpartition() in -lutil can be used])
+  ])
+])
+AC_SUBST([LIBUTIL])
+
 #
 # Check for target programs.
 #
Makefile.in renamed from Makefile.in.orig +4 -1
@@ -44,6 +44,9 @@ pkglibdir =  $(libdir)/`echo @PACKAGE_TA
 # Internationalization library.
 LIBINTL = @LIBINTL@
 
+# Util library.
+LIBUTIL = @LIBUTIL@
+
 XGETTEXT = @XGETTEXT@
 MSGMERGE = @MSGMERGE@
 MSGFMT = @MSGFMT@
@@ -75,7 +78,7 @@ MKDIR_P = @MKDIR_P@
 
 mkinstalldirs = $(srcdir)/mkinstalldirs
 
-LIBS = @LIBS@ $(LIBINTL)
+LIBS = @LIBS@ $(LIBINTL) $(LIBUTIL)
 
 CC = @CC@
 CFLAGS = @CFLAGS@

Example Installation of GRUB 2 on a common 1440 KB floppy *with grub-install* on NetBSD 5.0 i386/amd64

Format the floppy with:

$ fdformat -f /dev/rfd0a

Install a standard floppy disk label with:

$ disklabel -rw fd0a floppy

Create a filesystem on the floppy with:

$ newfs -m 0 /dev/rfd0a

Mount this filesystem e.g. on /mnt with:

$ mount /dev/fd0a /mnt

Finally, install grub with:

$ grub2-install --force --root-directory=/mnt/grub /dev/rfd0a