]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
partx: convert hard sector size to 512-byte sectors
authorKarel Zak <kzak@redhat.com>
Thu, 26 Feb 2009 13:23:06 +0000 (14:23 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 26 Feb 2009 14:58:28 +0000 (15:58 +0100)
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 <kzak@redhat.com>
partx/Makefile.am
partx/dos.c
partx/partx.8

index 798cfea7205e3faed2160fb4bdc8feb5b9d770f9..5fc5db6c7e7e01a44178d3fc3a9af5072d7bad72 100644 (file)
@@ -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
 
index c4fb28a9077c7e7ff9fb8c13e2b4b76a66ebdc0f..ebc4ce0a762a7b9b4ec4ca8aaf0d709b04733a71 100644 (file)
@@ -1,4 +1,7 @@
 #include <stdio.h>
+
+#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;
index 61ece52f5aa8cb82af1ffc7b1229800f6cbee9ea..23a247f3d65770b1339c1db69270146868d7b394 100644 (file)
@@ -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.