fdisk behavior hack

M. Warner Losh imp at bsdimp.com
Wed Nov 4 17:20:48 UTC 2009


Greetings,

Enclosed please find a hack to the behavior of fdisk.  On !x86, !ia64
platforms, you can't do a "fdisk -I da0" because there's not
/boot/mbr.  It is perfectly valid to make disks that don't have any
boot code on them (say if you are an ARM or MIPS embedded machine).

This patch changes things.  Currently, for all patforms except ia64 we
open the specified file (defaulting to /boot/mbr).  If it doesn't
exist or we can't read it, we die.  Then we do a bunch of sanity
checking on the MBR that was read in.  On ia64 we just make a fake one
and return (we don't use the -b argument at all!).

I'd like to propose something simpler.

I'd like to propose that for !i386 and !amd64 we allow the open and/or
stat to fail.  If the user specified the -b flag, the same thing as
the !ia64 case will happen as today: they get an error and the program
refuses to work.  If no -b flag was specified, then we'll use the
current ia64-only code to fake up a 'good enough' MBR and proceed.

This will allow non-x86 platforms that install fdisk to still
initialize a disk in the face of the missing template mbr file.

Comments?

Code Review?

Warner
-------------- next part --------------
Index: fdisk.c
===================================================================
--- fdisk.c	(revision 198865)
+++ fdisk.c	(working copy)
@@ -501,15 +501,37 @@
 static void
 init_boot(void)
 {
-#ifndef __ia64__
 	const char *fname;
 	int fdesc, n;
 	struct stat sb;
 
+	/*
+	 * Open the mbr file and sanity check it.  If we're not on a x86 box,
+	 * then /boot/mbr doesn't exist.  In that case, allow the user to
+	 * continue with an MBR that just has the right signature, but no
+	 * actual code.  If it does exist, then we sanity check it on all
+	 * platforms.  If there's a problem with the -b argument, always
+	 * whine.
+	 */
 	fname = b_flag ? b_flag : "/boot/mbr";
 	if ((fdesc = open(fname, O_RDONLY)) == -1 ||
-	    fstat(fdesc, &sb) == -1)
+	    fstat(fdesc, &sb) == -1) {
+#if !defined(__i386__) && !defined(__amd64__)
+		if (b_flag)
+			err(1, "%s", fname);
+		warn("%s", fname);
+		if (mboot.bootinst != NULL)
+			free(mboot.bootinst);
+		mboot.bootinst_size = secsize;
+		if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL)
+			errx(1, "unable to allocate boot block buffer");
+		memset(mboot.bootinst, 0, mboot.bootinst_size);
+		le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
+		return;
+#else
 		err(1, "%s", fname);
+#endif
+	}
 	if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
 		errx(1, "%s: length must be a multiple of sector size", fname);
 	if (mboot.bootinst != NULL)
@@ -521,15 +543,6 @@
 		err(1, "%s", fname);
 	if (n != mboot.bootinst_size)
 		errx(1, "%s: short read", fname);
-#else
-	if (mboot.bootinst != NULL)
-		free(mboot.bootinst);
-	mboot.bootinst_size = secsize;
-	if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL)
-		errx(1, "unable to allocate boot block buffer");
-	memset(mboot.bootinst, 0, mboot.bootinst_size);
-	le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
-#endif
 }
 
 


More information about the freebsd-arch mailing list