From: Karel Zak Date: Thu, 26 Feb 2009 13:23:06 +0000 (+0100) Subject: partx: convert hard sector size to 512-byte sectors X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=176303e08d7f1a7ab1211e0b4f3292d2a4648a07;p=util-linux partx: convert hard sector size to 512-byte sectors The msdos PT depends on a sector size (BLKSSZGET), but partx(8) counts internally with 512-byte sectors only. The dos.c has to convert start and size to 512-byte sectors. sysfs (kernel uses 512-byte sectors only): # cat /sys/block/sdb/sdb1/{start,size} 256 16128 (note that 16128 * 512 = 8257536; 8Mb) old version: # partx /dev/sdb 1: 32- 2047 ( 2016 sectors, 1 MB) ^^^^ start, end and sectors are correct, but in 4KiB sectors The size in MB is completely wrong. new version: # partx -l /dev/sdb 1: 256- 16383 ( 16128 sectors, 8 MB) start, end and sectors are converted to 512-byte sectors. The size in MB is correct now. Note that this change is important, because "partx -a" counts the size of a new partition in 512-byte sectors for all PT formats (sun, gpt, ...). Signed-off-by: Karel Zak --- diff --git a/partx/Makefile.am b/partx/Makefile.am index 798cfea7..5fc5db6c 100644 --- a/partx/Makefile.am +++ b/partx/Makefile.am @@ -4,7 +4,11 @@ if BUILD_PARTX usrsbinexec_PROGRAMS = addpart delpart partx partx_SOURCES = bsd.c dos.c partx.c solaris.c unixware.c gpt.c crc32.c \ - efi.h gpt.h crc32.h partx.h dos.h + efi.h gpt.h crc32.h partx.h dos.h ../lib/blkdev.c + +if LINUX +partx_SOURCES += ../lib/linux_version.c +endif dist_man_MANS = addpart.8 delpart.8 partx.8 diff --git a/partx/dos.c b/partx/dos.c index c4fb28a9..ebc4ce0a 100644 --- a/partx/dos.c +++ b/partx/dos.c @@ -1,4 +1,7 @@ #include + +#include "blkdev.h" + #include "partx.h" #include "dos.h" @@ -25,7 +28,7 @@ partition_size(struct partition *p) { static int read_extended_partition(int fd, struct partition *ep, - struct slice *sp, int ns) + struct slice *sp, int ns, int ssf) { struct partition *p; unsigned long start, here; @@ -54,8 +57,8 @@ read_extended_partition(int fd, struct partition *ep, if (partition_size(p) == 0 || is_extended(p->sys_type)) continue; if (n < ns) { - sp[n].start = here + partition_start(p); - sp[n].size = partition_size(p); + sp[n].start = (here + partition_start(p)) * ssf; + sp[n].size = partition_size(p) * ssf; n++; } else { fprintf(stderr, @@ -89,6 +92,7 @@ read_dos_pt(int fd, struct slice all, struct slice *sp, int ns) { unsigned long offset = all.start; int i, n=0; unsigned char *bp; + int ssf; bp = getblock(fd, offset); if (bp == NULL) @@ -97,6 +101,13 @@ read_dos_pt(int fd, struct slice all, struct slice *sp, int ns) { if (bp[510] != 0x55 || bp[511] != 0xaa) return -1; + /* msdos PT depends sector size... */ + if (blkdev_get_sector_size(fd, &ssf) != 0) + ssf = DEFAULT_SECTOR_SIZE; + + /* ... but partx counts everything in 512-byte sectors */ + ssf /= 512; + p = (struct partition *) (bp + 0x1be); for (i=0; i<4; i++) { if (is_gpt(p->sys_type)) @@ -107,8 +118,8 @@ read_dos_pt(int fd, struct slice all, struct slice *sp, int ns) { for (i=0; i<4; i++) { /* always add, even if zero length */ if (n < ns) { - sp[n].start = partition_start(p); - sp[n].size = partition_size(p); + sp[n].start = partition_start(p) * ssf; + sp[n].size = partition_size(p) * ssf; n++; } else { fprintf(stderr, @@ -120,7 +131,7 @@ read_dos_pt(int fd, struct slice all, struct slice *sp, int ns) { p = (struct partition *) (bp + 0x1be); for (i=0; i<4; i++) { if (is_extended(p->sys_type)) - n += read_extended_partition(fd, p, sp+n, ns-n); + n += read_extended_partition(fd, p, sp+n, ns-n, ssf); p++; } return n; diff --git a/partx/partx.8 b/partx/partx.8 index 61ece52f..23a247f3 100644 --- a/partx/partx.8 +++ b/partx/partx.8 @@ -29,7 +29,7 @@ add specified partitions or read disk and add all partitions delete specified or all partitions .TP .B \-l -list partitions +list partitions. Note that the all numbers are in 512-byte sectors. .TP .BI --type " TYPE" Specify the partition type -- dos, bsd, solaris, unixware or gpt.