Fdisk formatting of disk having bs=1K fails

2011-04-08 Thread Hans Petter Selasky
Hi,

It appears that src/sbin/fdisk.c can only read the MBR of disks having a 
blocksize different than 512 bytes. When writing a new MBR, the below check 
fails. Can someone having knowledge into fdisk, fix this issue and MFC to 8-
stable? Also I'm curious about the #ifdef __ia64__ .

if ((mboot.bootinst_size = sb.st_size) % secsize != 0)

secsize = 1024;
sb.st_size = sizeof(/boot/mbr) = 512;

--HPS

My attempt to fix this issue:

--- fdisk.c (revision 220305)
+++ fdisk.c (local)
@@ -508,22 +508,29 @@
const char *fname;
int fdesc, n;
struct stat sb;
+   off_t align_size;
 
fname = b_flag ? b_flag : /boot/mbr;
if ((fdesc = open(fname, O_RDONLY)) == -1 ||
fstat(fdesc, sb) == -1)
err(1, %s, fname);
-   if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
-   errx(1, %s: length must be a multiple of sector size, 
fname);
+
+   align_size = (sb.st_size + secsize - 1);
+   align_size -= align_size % secsize;
+   if (align_size == 0)
+   errx(1, %s: length must be non-zero, fname);
+   mboot.bootinst_size = align_size;
if (mboot.bootinst != NULL)
free(mboot.bootinst);
-   if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == 
NULL)
+   if ((mboot.bootinst = malloc(align_size)) == NULL)
errx(1, %s: unable to allocate read buffer, fname);
-   if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
+   if ((n = read(fdesc, mboot.bootinst, sb.st_size)) == -1 ||
close(fdesc))
err(1, %s, fname);
-   if (n != mboot.bootinst_size)
+   if (n != sb.st_size)
errx(1, %s: short read, fname);
+   if (align_size != n)
+   memset(mboot.bootinst + sb.st_size, 0, align_size - 
sb.st_size);
 #else
if (mboot.bootinst != NULL)
free(mboot.bootinst);
___
freebsd-current@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to freebsd-current-unsubscr...@freebsd.org


Re: Fdisk formatting of disk having bs=1K fails

2011-04-08 Thread Marcel Moolenaar

On Apr 8, 2011, at 12:34 PM, Hans Petter Selasky wrote:

 Hi,
 
 It appears that src/sbin/fdisk.c can only read the MBR of disks having a 
 blocksize different than 512 bytes. When writing a new MBR, the below check 
 fails. Can someone having knowledge into fdisk, fix this issue and MFC to 8-
 stable? Also I'm curious about the #ifdef __ia64__ .

You can eliminate the __ia64__ conditional if you want. From the
commit log:


r95860 | peter | 2002-05-01 06:48:29 + (Wed, 01 May 2002) | 4 lines

Add a hack so that fdisk(8) can initialize an ia64 disk.  There is
no /boot/mbr to read the boot code from (ia64 does not *have* bootblocks!).
fdisk depended on magic in the /boot/mbr file to initialize some fields.


fdisk is not compiled for ia64 anymore since the introduction of gpart.
The same holds for bsdlabel. So, that means that the hack is not needed
anymore.

FYI,

-- 
Marcel Moolenaar
xcl...@mac.com



___
freebsd-current@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to freebsd-current-unsubscr...@freebsd.org