2 * sfdisk version 3.0 - aeb - 950813
4 * Copyright (C) 1995 Andries E. Brouwer (aeb@cwi.nl)
6 * This program is free software. You can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation: either Version 1
9 * or (at your option) any later version.
11 * A.V. Le Blanc (LeBlanc@mcc.ac.uk) wrote Linux fdisk 1992-1994,
12 * patched by various people (faith@cs.unc.edu, martin@cs.unc.edu,
13 * leisner@sdsp.mc.xerox.com, esr@snark.thyrsus.com, aeb@cwi.nl)
14 * 1993-1995, with version numbers (as far as I have seen) 0.93 - 2.0e.
15 * This program had (head,sector,cylinder) as basic unit, and was
16 * (therefore) broken in several ways for the use on larger disks -
17 * for example, my last patch (from 2.0d to 2.0e) was required
18 * to allow a partition to cross cylinder 8064, and to write an
19 * extended partition past the 4GB mark.
21 * The current program is a rewrite from scratch, and I started a
22 * version numbering at 3.0.
23 * Andries Brouwer, aeb@cwi.nl, 950813
25 * Well, a good user interface is still lacking. On the other hand,
26 * many configurations cannot be handled by any other fdisk.
27 * I changed the name to sfdisk to prevent confusion. - aeb, 970501
30 #define PROGNAME "sfdisk"
33 #include <stdlib.h> /* atoi, free */
34 #include <stdarg.h> /* varargs */
35 #include <unistd.h> /* read, write */
36 #include <fcntl.h> /* O_RDWR */
37 #include <errno.h> /* ERANGE */
38 #include <string.h> /* strchr(), strrchr() */
41 #include <sys/ioctl.h>
43 #include <sys/utsname.h>
50 #include "linux_version.h"
52 #include "wholedisk.h"
53 #include "pathnames.h"
54 #include "canonicalize.h"
56 #include "closestream.h"
64 static struct systypes i386_sys_types
[] = {
65 #include "dos_part_types.h"
72 * C. About heads, sectors and cylinders
75 * F. The standard input
77 * H. Listing the current situation
78 * I. Writing the new situation
82 int force
= 0; /* 1: do what I say, even if it is stupid ... */
83 int quiet
= 0; /* 1: suppress all warnings */
84 /* IA-64 gcc spec file currently does -DLinux... */
86 int Linux
= 0; /* 1: suppress warnings irrelevant for Linux */
87 int DOS
= 0; /* 1: shift extended partitions by #sectors, not 1 */
88 int DOS_extended
= 0; /* 1: use starting cylinder boundary of extd partn */
89 int dump
= 0; /* 1: list in a format suitable for later input */
90 int verify
= 0; /* 1: check that listed partition is reasonable */
91 int no_write
= 0; /* 1: do not actually write to disk */
92 int no_reread
= 0; /* 1: skip the BLKRRPART ioctl test at startup */
93 int leave_last
= 0; /* 1: don't allocate the last cylinder */
95 char *save_sector_file
= NULL
;
96 char *restore_sector_file
= NULL
;
99 my_warn(char *s
, ...) {
105 vfprintf(stderr
, s
, p
);
112 error(char *s
, ...) {
117 fprintf(stderr
, "\n" PROGNAME
": ");
118 vfprintf(stderr
, s
, p
);
128 * sseek: seek to specified sector - return 0 on failure
130 * Note: we use 512-byte sectors here, irrespective of the hardware ss.
134 sseek(char *dev
, int fd
, unsigned long s
) {
136 in
= ((off_t
) s
<< 9);
138 if ((out
= lseek(fd
, in
, SEEK_SET
)) != in
) {
140 error(_("seek error on %s - cannot seek to %lu\n"), dev
, s
);
145 error(_("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n"),
146 (unsigned int)(in
>> 32), (unsigned int)(in
& 0xffffffff),
147 (unsigned int)(out
>> 32), (unsigned int)(out
& 0xffffffff));
158 * We preserve all sectors read in a chain - some of these will
159 * have to be modified and written back.
163 unsigned long long sectornumber
;
174 sectorhead
= s
->next
;
179 static struct sector
*
180 get_sector(char *dev
, int fd
, unsigned long long sno
) {
183 for (s
= sectorhead
; s
; s
= s
->next
)
184 if (s
->sectornumber
== sno
)
187 if (!sseek(dev
, fd
, sno
))
190 s
= xmalloc(sizeof(struct sector
));
192 if (read(fd
, s
->data
, sizeof(s
->data
)) != sizeof(s
->data
)) {
193 if (errno
) /* 0 in case we read past end-of-disk */
195 error(_("read error on %s - cannot read sector %lu\n"), dev
, sno
);
200 s
->next
= sectorhead
;
202 s
->sectornumber
= sno
;
203 s
->to_be_written
= 0;
209 msdos_signature(struct sector
*s
) {
210 unsigned char *data
= (unsigned char *)s
->data
;
211 if (data
[510] == 0x55 && data
[511] == 0xaa)
217 write_sectors(char *dev
, int fd
) {
220 for (s
= sectorhead
; s
; s
= s
->next
)
221 if (s
->to_be_written
) {
222 if (!sseek(dev
, fd
, s
->sectornumber
))
224 if (write(fd
, s
->data
, sizeof(s
->data
)) != sizeof(s
->data
)) {
226 error(_("write error on %s - cannot write sector %lu\n"),
227 dev
, s
->sectornumber
);
230 s
->to_be_written
= 0;
236 ulong_to_chars(unsigned long u
, char *uu
) {
239 for (i
= 0; i
< 4; i
++) {
246 chars_to_ulong(unsigned char *uu
) {
250 for (i
= 3; i
>= 0; i
--)
251 u
= (u
<< 8) | uu
[i
];
256 save_sectors(char *dev
, int fdin
) {
261 fdout
= open(save_sector_file
, O_WRONLY
| O_CREAT
, 0444);
263 perror(save_sector_file
);
264 error(_("cannot open partition sector save file (%s)\n"),
269 for (s
= sectorhead
; s
; s
= s
->next
)
270 if (s
->to_be_written
) {
271 ulong_to_chars(s
->sectornumber
, ss
);
272 if (!sseek(dev
, fdin
, s
->sectornumber
))
274 if (read(fdin
, ss
+ 4, 512) != 512) {
276 error(_("read error on %s - cannot read sector %lu\n"),
277 dev
, s
->sectornumber
);
280 if (write(fdout
, ss
, sizeof(ss
)) != sizeof(ss
)) {
282 error(_("write error on %s\n"), save_sector_file
);
296 static int reread_disk_partition(char *dev
, int fd
);
299 restore_sectors(char *dev
) {
300 int fdin
= -1, fdout
= -1;
303 char *ss0
= NULL
, *ss
;
306 if (stat(restore_sector_file
, &statbuf
) < 0) {
307 perror(restore_sector_file
);
308 error(_("cannot stat partition restore file (%s)\n"),
309 restore_sector_file
);
312 if (statbuf
.st_size
% 516) {
313 error(_("partition restore file has wrong size - not restoring\n"));
317 ss0
= xmalloc(statbuf
.st_size
);
320 fdin
= open(restore_sector_file
, O_RDONLY
);
322 perror(restore_sector_file
);
323 error(_("cannot open partition restore file (%s)\n"),
324 restore_sector_file
);
327 if (read(fdin
, ss
, statbuf
.st_size
) != statbuf
.st_size
) {
329 error(_("error reading %s\n"), restore_sector_file
);
333 fdout
= open(dev
, O_WRONLY
);
336 error(_("cannot open device %s for writing\n"), dev
);
340 ct
= statbuf
.st_size
/ 516;
342 sno
= chars_to_ulong((unsigned char *)ss
);
343 if (!sseek(dev
, fdout
, sno
))
345 if (write(fdout
, ss
+ 4, 512) != 512) {
347 error(_("error writing sector %lu on %s\n"), sno
, dev
);
355 if (!reread_disk_partition(dev
, fdout
)) /* closes fdout */
372 * C. About heads, sectors and cylinders
376 * <linux/hdreg.h> defines HDIO_GETGEO and
377 * struct hd_geometry {
378 * unsigned char heads;
379 * unsigned char sectors;
380 * unsigned short cylinders;
381 * unsigned long start;
384 * For large disks g.cylinders is truncated, so we use BLKGETSIZE.
388 * We consider several geometries for a disk:
389 * B - the BIOS geometry, gotten from the kernel via HDIO_GETGEO
390 * F - the fdisk geometry
391 * U - the user-specified geometry
393 * 0 means unspecified / unknown
396 unsigned long long total_size
; /* in sectors */
397 unsigned long cylindersize
; /* in sectors */
398 unsigned long heads
, sectors
, cylinders
;
402 static struct geometry
403 get_geometry(char *dev
, int fd
, int silent
) {
404 struct hd_geometry g
;
406 unsigned long long sectors
;
410 if (ioctl(fd
, HDIO_GETGEO
, &g
))
413 g
.heads
= g
.sectors
= g
.cylinders
= g
.start
= 0;
415 warnx(_("Disk %s: cannot get geometry\n"), dev
);
420 R
.sectors
= g
.sectors
;
421 R
.cylindersize
= R
.heads
* R
.sectors
;
425 if (blkdev_get_sectors(fd
, §ors
) == -1) {
426 /* maybe an ordinary file */
429 if (fstat(fd
, &s
) == 0 && S_ISREG(s
.st_mode
))
430 R
.total_size
= (s
.st_size
>> 9);
432 warnx(_("Disk %s: cannot get size\n"), dev
);
434 R
.total_size
= sectors
;
436 if (R
.cylindersize
&& R
.total_size
) {
437 sectors
/= R
.cylindersize
;
448 get_cylindersize(char *dev
, int fd
, int silent
) {
451 R
= get_geometry(dev
, fd
, silent
);
453 B
.heads
= (U
.heads
? U
.heads
: R
.heads
? R
.heads
: 255);
454 B
.sectors
= (U
.sectors
? U
.sectors
: R
.sectors
? R
.sectors
: 63);
455 B
.cylinders
= (U
.cylinders
? U
.cylinders
: R
.cylinders
);
457 B
.cylindersize
= B
.heads
* B
.sectors
;
458 B
.total_size
= R
.total_size
;
460 if (B
.cylinders
== 0 && B
.cylindersize
!= 0)
461 B
.cylinders
= B
.total_size
/ B
.cylindersize
;
463 if (R
.start
&& !force
) {
464 my_warn(_("Warning: start=%lu - this looks like a partition rather than\n"
465 "the entire disk. Using fdisk on it is probably meaningless.\n"
466 "[Use the --force option if you really want this]\n"),
471 if (R
.heads
&& B
.heads
!= R
.heads
)
472 my_warn(_("Warning: HDIO_GETGEO says that there are %lu heads\n"),
474 if (R
.sectors
&& B
.sectors
!= R
.sectors
)
475 my_warn(_("Warning: HDIO_GETGEO says that there are %lu sectors\n"),
477 if (R
.cylinders
&& B
.cylinders
!= R
.cylinders
478 && B
.cylinders
< 65536 && R
.cylinders
< 65536)
479 my_warn(_("Warning: BLKGETSIZE/HDIO_GETGEO says that there are %lu cylinders\n"),
484 my_warn(_("Warning: unlikely number of sectors (%lu) - usually at most 63\n"
485 "This will give problems with all software that uses C/H/S addressing.\n"),
488 printf(_("\nDisk %s: %lu cylinders, %lu heads, %lu sectors/track\n"),
489 dev
, B
.cylinders
, B
.heads
, B
.sectors
);
493 unsigned char h
, s
, c
;
494 } __attribute__ ((packed
)) chs
; /* has some c bits in s */
495 chs zero_chs
= { 0, 0, 0 };
498 unsigned long h
, s
, c
;
500 longchs zero_longchs
;
503 longchs_to_chs(longchs aa
, struct geometry G
) {
506 if (aa
.h
< 256 && aa
.s
< 64 && aa
.c
< 1024) {
508 a
.s
= aa
.s
| ((aa
.c
>> 2) & 0xc0);
510 } else if (G
.heads
&& G
.sectors
) {
512 a
.s
= G
.sectors
| 0xc0;
520 chs_to_longchs(chs a
) {
526 aa
.c
= (aa
.c
<< 2) + a
.c
;
531 ulong_to_longchs(unsigned long sno
, struct geometry G
) {
534 if (G
.heads
&& G
.sectors
&& G
.cylindersize
) {
535 aa
.s
= 1 + sno
% G
.sectors
;
536 aa
.h
= (sno
/ G
.sectors
) % G
.heads
;
537 aa
.c
= sno
/ G
.cylindersize
;
545 ulong_to_chs(unsigned long sno
, struct geometry G
) {
546 return longchs_to_chs(ulong_to_longchs(sno
, G
), G
);
551 longchs_to_ulong(longchs aa
, struct geometry G
) {
552 return (aa
.c
* G
.cylindersize
+ aa
.h
* G
.sectors
+ aa
.s
- 1);
556 chs_to_ulong(chs a
, struct geometry G
) {
557 return longchs_to_ulong(chs_to_longchs(a
), G
);
562 is_equal_chs(chs a
, chs b
) {
563 return (a
.h
== b
.h
&& a
.s
== b
.s
&& a
.c
== b
.c
);
567 chs_ok(chs a
, char *v
, char *w
) {
568 longchs aa
= chs_to_longchs(a
);
571 if (is_equal_chs(a
, zero_chs
))
573 if (B
.heads
&& aa
.h
>= B
.heads
) {
574 my_warn(_("%s of partition %s has impossible value for head: "
575 "%lu (should be in 0-%lu)\n"), w
, v
, aa
.h
, B
.heads
- 1);
578 if (B
.sectors
&& (aa
.s
== 0 || aa
.s
> B
.sectors
)) {
579 my_warn(_("%s of partition %s has impossible value for sector: "
580 "%lu (should be in 1-%lu)\n"), w
, v
, aa
.s
, B
.sectors
);
583 if (B
.cylinders
&& aa
.c
>= B
.cylinders
) {
584 my_warn(_("%s of partition %s has impossible value for cylinders: "
585 "%lu (should be in 0-%lu)\n"), w
, v
, aa
.c
, B
.cylinders
- 1);
592 * D. About system Ids
595 #define EMPTY_PARTITION 0
596 #define EXTENDED_PARTITION 5
597 #define WIN98_EXTENDED 0x0f
598 #define DM6_AUX1PARTITION 0x51
599 #define DM6_AUX3PARTITION 0x53
600 #define DM6_PARTITION 0x54
601 #define EZD_PARTITION 0x55
602 #define LINUX_SWAP 0x82
603 #define LINUX_NATIVE 0x83
604 #define LINUX_EXTENDED 0x85
605 #define BSD_PARTITION 0xa5
606 #define NETBSD_PARTITION 0xa9
608 /* List of partition types */
611 sysname(unsigned char type
) {
614 for (s
= i386_sys_types
; s
->name
; s
++)
624 printf(_("Id Name\n\n"));
625 for (s
= i386_sys_types
; s
->name
; s
++)
626 printf("%2x %s\n", s
->type
, _(s
->name
));
630 is_extended(unsigned char type
) {
631 return (type
== EXTENDED_PARTITION
632 || type
== LINUX_EXTENDED
|| type
== WIN98_EXTENDED
);
636 is_bsd(unsigned char type
) {
637 return (type
== BSD_PARTITION
|| type
== NETBSD_PARTITION
);
641 * E. About partitions
644 /* MS/DOS partition */
647 unsigned char bootable
; /* 0 or 0x80 */
649 unsigned char sys_type
;
651 unsigned int start_sect
; /* starting sector counting from 0 */
652 unsigned int nr_sects
; /* nr of sectors in partition */
653 } __attribute__ ((packed
));
655 /* Unfortunately, partitions are not aligned, and non-Intel machines
656 are unhappy with non-aligned integers. So, we need a copy by hand. */
658 copy_to_int(unsigned char *cp
) {
669 copy_from_int(int m
, char *cp
) {
680 copy_to_part(char *cp
, struct partition
*p
) {
682 p
->begin_chs
.h
= *cp
++;
683 p
->begin_chs
.s
= *cp
++;
684 p
->begin_chs
.c
= *cp
++;
686 p
->end_chs
.h
= *cp
++;
687 p
->end_chs
.s
= *cp
++;
688 p
->end_chs
.c
= *cp
++;
689 p
->start_sect
= copy_to_int((unsigned char *)cp
);
690 p
->nr_sects
= copy_to_int((unsigned char *)cp
+ 4);
694 copy_from_part(struct partition
*p
, char *cp
) {
696 *cp
++ = p
->begin_chs
.h
;
697 *cp
++ = p
->begin_chs
.s
;
698 *cp
++ = p
->begin_chs
.c
;
700 *cp
++ = p
->end_chs
.h
;
701 *cp
++ = p
->end_chs
.s
;
702 *cp
++ = p
->end_chs
.c
;
703 copy_from_int(p
->start_sect
, cp
);
704 copy_from_int(p
->nr_sects
, cp
+ 4);
707 /* Roughly speaking, Linux doesn't use any of the above fields except
708 for partition type, start sector and number of sectors. (However,
709 see also linux/drivers/scsi/fdomain.c.)
710 The only way partition type is used (in the kernel) is the comparison
711 for equality with EXTENDED_PARTITION (and these Disk Manager types). */
714 unsigned long long start
;
715 unsigned long long size
;
716 unsigned long long sector
, offset
; /* disk location of this info */
718 struct part_desc
*ep
; /* extended partition containing this one */
724 static struct part_desc
*
725 outer_extended_partition(struct part_desc
*p
) {
732 is_parent(struct part_desc
*pp
, struct part_desc
*p
) {
742 struct part_desc partitions
[512];
746 /* determine where on the disk this information goes */
748 add_sector_and_offset(struct disk_desc
*z
) {
752 for (pno
= 0; pno
< z
->partno
; pno
++) {
753 p
= &(z
->partitions
[pno
]);
754 p
->offset
= 0x1be + (pno
% 4) * sizeof(struct partition
);
755 p
->sector
= (p
->ep
? p
->ep
->start
: 0);
759 /* tell the kernel to reread the partition tables */
761 reread_ioctl(int fd
) {
763 if (ioctl(fd
, BLKRRPART
))
768 /* perror might change errno */
773 /* 2.6.8 returns EIO for a zero table */
780 /* reread after writing */
782 reread_disk_partition(char *dev
, int fd
) {
783 printf(_("Re-reading the partition table ...\n"));
787 if (reread_ioctl(fd
) && is_blkdev(fd
)) {
788 warnx(_("The command to re-read the partition table failed.\n"
789 "Run partprobe(8), kpartx(8) or reboot your system now,\n"
790 "before using mkfs\n"));
794 if (fsync(fd
) || close(fd
)) {
796 warnx(_("Error closing %s\n"), dev
);
804 /* find Linux name of this partition, assuming that it will have a name */
806 index_to_linux(int pno
, struct disk_desc
*z
) {
808 struct part_desc
*p
= &(z
->partitions
[0]);
809 for (i
= 0; i
< pno
; i
++, p
++)
810 if (i
< 4 || (p
->size
> 0 && !is_extended(p
->p
.sys_type
)))
816 linux_to_index(int lpno
, struct disk_desc
*z
) {
818 struct part_desc
*p
= &(z
->partitions
[0]);
819 for (i
= 0; i
< z
->partno
&& ct
< lpno
; i
++, p
++)
820 if ((i
< 4 || (p
->size
> 0 && !is_extended(p
->p
.sys_type
)))
827 asc_to_index(char *pnam
, struct disk_desc
*z
) {
831 pno
= atoi(pnam
+ 1);
834 pno
= linux_to_index(pnum
, z
);
836 if (!(pno
>= 0 && pno
< z
->partno
))
837 errx(EXIT_FAILURE
, _("%s: no such partition\n"), pnam
);
842 * List partitions - in terms of sectors, blocks or cylinders
849 int default_format
= F_MEGABYTE
;
850 int specified_format
= 0;
851 int show_extended
= 0;
860 warnx(_("unrecognized format - using sectors\n"));
863 specified_format
= F_SECTOR
;
866 specified_format
= F_BLOCK
;
869 specified_format
= F_CYLINDER
;
872 specified_format
= F_MEGABYTE
;
878 unitsize(int format
) {
879 default_format
= (B
.cylindersize
? F_CYLINDER
: F_MEGABYTE
);
880 if (!format
&& !(format
= specified_format
))
881 format
= default_format
;
887 return B
.cylindersize
;
898 static unsigned long long
899 get_disksize(int format
) {
900 if (B
.total_size
&& leave_last
)
901 /* don't use last cylinder (--leave-last option) */
902 return (B
.total_size
- B
.cylindersize
) / unitsize(format
);
904 return B
.total_size
/ unitsize(format
);
908 out_partition_header(char *dev
, int format
, struct geometry G
) {
910 printf("# partition table of %s\n", dev
);
911 printf("unit: sectors\n\n");
915 default_format
= (G
.cylindersize
? F_CYLINDER
: F_MEGABYTE
);
916 if (!format
&& !(format
= specified_format
))
917 format
= default_format
;
921 warnx(_("unimplemented format - using %s\n"),
922 G
.cylindersize
? _("cylinders") : _("sectors"));
925 if (G
.cylindersize
) {
926 printf(_("Units: cylinders of %lu bytes, blocks of 1024 bytes"
927 ", counting from %d\n\n"), G
.cylindersize
<< 9, increment
);
928 printf(_(" Device Boot Start End #cyls #blocks Id System\n"));
933 printf(_("Units: sectors of 512 bytes, counting from %d\n\n"),
935 printf(_(" Device Boot Start End #sectors Id System\n"));
938 printf(_("Units: blocks of 1024 bytes, counting from %d\n\n"),
940 printf(_(" Device Boot Start End #blocks Id System\n"));
943 printf(_("Units: 1MiB = 1024*1024 bytes, blocks of 1024 bytes"
944 ", counting from %d\n\n"), increment
);
945 printf(_(" Device Boot Start End MiB #blocks Id System\n"));
951 out_rounddown(int width
, unsigned long long n
, unsigned long unit
, int inc
) {
952 printf("%*llu", width
, inc
+ n
/ unit
);
954 putchar((n
% unit
) ? '+' : ' ');
959 out_roundup(int width
, unsigned long long n
, unsigned long unit
, int inc
) {
960 if (n
== (unsigned long long)(-1))
961 printf("%*s", width
, "-");
963 printf("%*llu", width
, inc
+ n
/ unit
);
965 putchar(((n
+ 1) % unit
) ? '-' : ' ');
970 out_roundup_size(int width
, unsigned long long n
, unsigned long unit
) {
971 printf("%*llu", width
, (n
+ unit
- 1) / unit
);
973 putchar((n
% unit
) ? '-' : ' ');
977 static struct geometry
978 get_fdisk_geometry_one(struct part_desc
*p
) {
981 memset(&G
, 0, sizeof(struct geometry
));
982 chs b
= p
->p
.end_chs
;
983 longchs bb
= chs_to_longchs(b
);
986 G
.cylindersize
= G
.heads
* G
.sectors
;
991 get_fdisk_geometry(struct disk_desc
*z
) {
994 struct geometry G0
, G
;
996 memset(&G0
, 0, sizeof(struct geometry
));
998 for (pno
= 0; pno
< z
->partno
; pno
++) {
999 p
= &(z
->partitions
[pno
]);
1000 if (p
->size
!= 0 && p
->p
.sys_type
!= 0) {
1001 G
= get_fdisk_geometry_one(p
);
1005 } else if (G
.heads
!= G0
.heads
|| G
.sectors
!= G0
.sectors
) {
1011 F
= (agree
? G0
: B
);
1012 return (F
.sectors
!= B
.sectors
|| F
.heads
!= B
.heads
);
1016 out_partition(char *dev
, int format
, struct part_desc
*p
,
1017 struct disk_desc
*z
, struct geometry G
) {
1018 unsigned long long start
, end
, size
;
1021 if (!format
&& !(format
= specified_format
))
1022 format
= default_format
;
1024 pno
= p
- &(z
->partitions
[0]); /* our index */
1025 lpno
= index_to_linux(pno
, z
); /* name of next one that has a name */
1026 if (pno
== linux_to_index(lpno
, z
)) /* was that us? */
1027 printf("%s", partname(dev
, lpno
, 10)); /* yes */
1028 else if (show_extended
)
1032 putchar(dump
? ':' : ' ');
1035 end
= p
->start
+ p
->size
- 1;
1039 printf(" start=%9llu", start
);
1040 printf(", size=%9llu", size
);
1041 if (p
->ptype
== DOS_TYPE
) {
1042 printf(", Id=%2x", p
->p
.sys_type
);
1043 if (p
->p
.bootable
== 0x80)
1044 printf(", bootable");
1050 if (p
->ptype
!= DOS_TYPE
|| p
->p
.bootable
== 0)
1052 else if (p
->p
.bootable
== 0x80)
1055 printf(" ? "); /* garbage */
1059 if (G
.cylindersize
) {
1060 out_rounddown(6, start
, G
.cylindersize
, increment
);
1061 out_roundup(6, end
, G
.cylindersize
, increment
);
1062 out_roundup_size(6, size
, G
.cylindersize
);
1063 out_rounddown(9, size
, 2, 0);
1069 out_rounddown(9, start
, 1, increment
);
1070 out_roundup(9, end
, 1, increment
);
1071 out_rounddown(10, size
, 1, 0);
1075 printf("%8lu,%3lu ",
1076 p
->sector
/ 2, ((p
->sector
& 1) ? 512 : 0) + p
->offset
);
1078 out_rounddown(8, start
, 2, increment
);
1079 out_roundup(8, end
, 2, increment
);
1080 out_rounddown(9, size
, 2, 0);
1083 out_rounddown(5, start
, 2048, increment
);
1084 out_roundup(5, end
, 2048, increment
);
1085 out_roundup_size(5, size
, 2048);
1086 out_rounddown(9, size
, 2, 0);
1089 if (p
->ptype
== DOS_TYPE
) {
1090 printf(" %2x %s\n", p
->p
.sys_type
, sysname(p
->p
.sys_type
));
1095 /* Is chs as we expect? */
1096 if (!quiet
&& p
->ptype
== DOS_TYPE
) {
1099 a
= (size
? ulong_to_chs(start
, G
) : zero_chs
);
1101 aa
= chs_to_longchs(a
);
1102 bb
= chs_to_longchs(b
);
1103 if (a
.s
&& !is_equal_chs(a
, b
))
1104 warnx(_("\t\tstart: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1105 aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1106 a
= (size
? ulong_to_chs(end
, G
) : zero_chs
);
1108 aa
= chs_to_longchs(a
);
1109 bb
= chs_to_longchs(b
);
1110 if (a
.s
&& !is_equal_chs(a
, b
))
1111 warnx(_("\t\tend: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1112 aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1113 if (G
.cylinders
&& G
.cylinders
< 1024 && bb
.c
> G
.cylinders
)
1114 warnx(_("partition ends on cylinder %ld, beyond the end of the disk\n"),
1120 out_partitions(char *dev
, struct disk_desc
*z
) {
1121 int pno
, format
= 0;
1123 if (z
->partno
== 0) {
1125 warnx(_("No partitions found\n"));
1127 if (get_fdisk_geometry(z
) && !dump
) {
1128 warnx(_("Warning: The partition table looks like it was made\n"
1129 " for C/H/S=*/%ld/%ld (instead of %ld/%ld/%ld).\n"
1130 "For this listing I'll assume that geometry.\n"),
1131 F
.heads
, F
.sectors
, B
.cylinders
, B
.heads
, B
.sectors
);
1134 out_partition_header(dev
, format
, F
);
1135 for (pno
= 0; pno
< z
->partno
; pno
++) {
1136 out_partition(dev
, format
, &(z
->partitions
[pno
]), z
, F
);
1137 if (show_extended
&& pno
% 4 == 3)
1144 disj(struct part_desc
*p
, struct part_desc
*q
) {
1145 return ((p
->start
+ p
->size
<= q
->start
)
1146 || (is_extended(p
->p
.sys_type
)
1147 && q
->start
+ q
->size
<= p
->start
+ p
->size
));
1151 pnumber(struct part_desc
*p
, struct disk_desc
*z
) {
1152 static char buf
[20];
1154 struct part_desc
*p0
= &(z
->partitions
[0]);
1156 this = index_to_linux(p
- p0
, z
);
1157 next
= index_to_linux(p
- p0
+ 1, z
);
1160 sprintf(buf
, "%d", this);
1162 sprintf(buf
, "[%d]", this);
1167 partitions_ok(int fd
, struct disk_desc
*z
) {
1168 struct part_desc
*partitions
= &(z
->partitions
[0]), *p
, *q
;
1169 int partno
= z
->partno
;
1171 #define PNO(p) pnumber(p, z)
1173 /* Have at least 4 partitions been defined? */
1176 errx(EXIT_FAILURE
, _("no partition table present."));
1178 errx(EXIT_FAILURE
, _("strange, only %d partitions defined."), partno
);
1182 /* Are the partitions of size 0 marked empty?
1183 And do they have start = 0? And bootable = 0? */
1184 for (p
= partitions
; p
- partitions
< partno
; p
++)
1186 if (p
->p
.sys_type
!= EMPTY_PARTITION
)
1187 my_warn(_("Warning: partition %s has size 0 but is not marked Empty\n"),
1189 else if (p
->p
.bootable
!= 0)
1190 my_warn(_("Warning: partition %s has size 0 and is bootable\n"),
1192 else if (p
->p
.start_sect
!= 0)
1193 my_warn(_("Warning: partition %s has size 0 and nonzero start\n"),
1195 /* all this is probably harmless, no error return */
1198 /* Are the logical partitions contained in their extended partitions? */
1199 for (p
= partitions
+ 4; p
< partitions
+ partno
; p
++)
1200 if (p
->ptype
== DOS_TYPE
)
1201 if (p
->size
&& !is_extended(p
->p
.sys_type
)) {
1203 if (p
->start
< q
->start
1204 || p
->start
+ p
->size
> q
->start
+ q
->size
) {
1205 my_warn(_("Warning: partition %s is not contained in "
1206 "partition %s\n"), PNO(p
), PNO(q
));
1211 /* Are the data partitions mutually disjoint? */
1212 for (p
= partitions
; p
< partitions
+ partno
; p
++)
1213 if (p
->size
&& !is_extended(p
->p
.sys_type
))
1214 for (q
= p
+ 1; q
< partitions
+ partno
; q
++)
1215 if (q
->size
&& !is_extended(q
->p
.sys_type
))
1216 if (!((p
->start
> q
->start
) ? disj(q
, p
) : disj(p
, q
))) {
1217 my_warn(_("Warning: partitions %s and %s overlap\n"),
1222 /* Are the data partitions and the extended partition
1223 table sectors disjoint? */
1224 for (p
= partitions
; p
< partitions
+ partno
; p
++)
1225 if (p
->size
&& !is_extended(p
->p
.sys_type
))
1226 for (q
= partitions
; q
< partitions
+ partno
; q
++)
1227 if (is_extended(q
->p
.sys_type
))
1228 if (p
->start
<= q
->start
&& p
->start
+ p
->size
> q
->start
) {
1229 my_warn(_("Warning: partition %s contains part of "
1230 "the partition table (sector %llu),\n"
1231 "and will destroy it when filled\n"),
1236 /* Do they start past zero and end before end-of-disk? */
1238 unsigned long long ds
= get_disksize(F_SECTOR
);
1239 for (p
= partitions
; p
< partitions
+ partno
; p
++)
1241 if (p
->start
== 0) {
1242 my_warn(_("Warning: partition %s starts at sector 0\n"),
1246 if (p
->size
&& p
->start
+ p
->size
> ds
) {
1247 my_warn(_("Warning: partition %s extends past end of disk\n"),
1255 if (blkdev_get_sector_size(fd
, §or_size
) == -1)
1256 sector_size
= DEFAULT_SECTOR_SIZE
;
1258 /* Is the size of partitions less than 2^32 sectors limit? */
1259 for (p
= partitions
; p
< partitions
+ partno
; p
++)
1260 if (p
->size
> UINT_MAX
) {
1261 unsigned long long bytes
= p
->size
* sector_size
;
1262 int giga
= bytes
/ 1000000000;
1263 int hectogiga
= (giga
+ 50) / 100;
1264 my_warn(_("Warning: partition %s has size %d.%d TB (%llu bytes),\n"
1265 "which is larger than the %llu bytes limit imposed\n"
1266 "by the DOS partition table for %d-byte sectors\n"),
1267 PNO(p
), hectogiga
/ 10, hectogiga
% 10,
1269 (unsigned long long) UINT_MAX
* sector_size
,
1274 /* Do the partitions start below the 2^32 sectors limit? */
1275 for (p
= partitions
; p
< partitions
+ partno
; p
++)
1276 if (p
->start
> UINT_MAX
) {
1277 unsigned long long bytes
= p
->start
* sector_size
;
1278 int giga
= bytes
/ 1000000000;
1279 int hectogiga
= (giga
+ 50) / 100;
1280 my_warn(_("Warning: partition %s starts at sector %llu (%d.%d TB for %d-byte sectors),\n"
1281 "which exceeds the DOS partition table limit of %llu sectors\n"),
1287 (unsigned long long) UINT_MAX
);
1291 /* At most one chain of DOS extended partitions ? */
1292 /* It seems that the OS/2 fdisk has the additional requirement
1293 that the extended partition must be the fourth one */
1296 for (p
= partitions
; p
< partitions
+ 4; p
++)
1297 if (p
->p
.sys_type
== EXTENDED_PARTITION
)
1299 if (ect
> 1 && !Linux
) {
1300 my_warn(_("Among the primary partitions, at most one can be extended\n"
1301 " (although this is not a problem under Linux)\n"));
1307 * Do all partitions start at a cylinder boundary ?
1308 * (this is not required for Linux)
1309 * The first partition starts after MBR.
1310 * Logical partitions start slightly after the containing extended partn.
1312 if (B
.cylindersize
&& !Linux
) {
1313 for (p
= partitions
; p
< partitions
+ partno
; p
++)
1315 if (p
->start
% B
.cylindersize
!= 0
1317 || p
->start
/ B
.cylindersize
!=
1318 p
->ep
->start
/ B
.cylindersize
)
1319 && (p
->p
.start_sect
>= B
.cylindersize
)) {
1320 my_warn(_("Warning: partition %s does not start "
1321 "at a cylinder boundary\n"), PNO(p
));
1322 if (specified_format
== F_CYLINDER
)
1325 if ((p
->start
+ p
->size
) % B
.cylindersize
) {
1326 my_warn(_("Warning: partition %s does not end "
1327 "at a cylinder boundary\n"), PNO(p
));
1328 if (specified_format
== F_CYLINDER
)
1334 /* Usually, one can boot only from primary partitions. */
1335 /* In fact, from a unique one only. */
1336 /* do not warn about bootable extended partitions -
1337 often LILO is there */
1340 for (p
= partitions
; p
< partitions
+ partno
; p
++)
1341 if (p
->p
.bootable
) {
1343 pno
= p
- partitions
;
1344 else if (p
- partitions
< 4) {
1345 my_warn(_("Warning: more than one primary partition is marked "
1346 "bootable (active)\n"
1347 "This does not matter for LILO, but the DOS MBR will "
1348 "not boot this disk.\n"));
1351 if (p
- partitions
>= 4) {
1352 my_warn(_("Warning: usually one can boot from primary partitions "
1353 "only\nLILO disregards the `bootable' flag.\n"));
1357 if (pno
== -1 || pno
>= 4)
1358 my_warn(_("Warning: no primary partition is marked bootable (active)\n"
1359 "This does not matter for LILO, but the DOS MBR will "
1360 "not boot this disk.\n"));
1363 /* Is chs as we expect? */
1364 for (p
= partitions
; p
< partitions
+ partno
; p
++)
1365 if (p
->ptype
== DOS_TYPE
) {
1368 a
= p
->size
? ulong_to_chs(p
->start
, B
) : zero_chs
;
1370 aa
= chs_to_longchs(a
);
1371 bb
= chs_to_longchs(b
);
1372 if (!Linux
&& !chs_ok(b
, PNO(p
), _("start")))
1374 if (a
.s
&& !is_equal_chs(a
, b
))
1375 my_warn(_("partition %s: start: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1376 PNO(p
), aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1377 a
= p
->size
? ulong_to_chs(p
->start
+ p
->size
- 1, B
) : zero_chs
;
1379 aa
= chs_to_longchs(a
);
1380 bb
= chs_to_longchs(b
);
1381 if (!Linux
&& !chs_ok(b
, PNO(p
), _("end")))
1383 if (a
.s
&& !is_equal_chs(a
, b
))
1384 my_warn(_("partition %s: end: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1385 PNO(p
), aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1386 if (B
.cylinders
&& B
.cylinders
< 1024 && bb
.c
> B
.cylinders
)
1387 my_warn(_("partition %s ends on cylinder %ld, beyond the end of the disk\n"),
1397 extended_partition(char *dev
, int fd
, struct part_desc
*ep
, struct disk_desc
*z
) {
1400 unsigned long long start
, here
, next
;
1401 int i
, moretodo
= 1;
1403 struct part_desc
*partitions
= &(z
->partitions
[0]);
1404 size_t pno
= z
->partno
;
1406 here
= start
= ep
->start
;
1408 if (B
.cylindersize
&& start
% B
.cylindersize
) {
1411 here
= start
-= (start
% B
.cylindersize
);
1412 warnx(_("Warning: shifted start of the extd partition "
1413 "from %lld to %lld\n"
1414 "(For listing purposes only. "
1415 "Do not change its contents.)\n"), ep
->start
, start
);
1417 warnx(_("Warning: extended partition does not start at a "
1418 "cylinder boundary.\n"
1419 "DOS and Linux will interpret the contents differently.\n"));
1426 if (!(s
= get_sector(dev
, fd
, here
)))
1429 if (!msdos_signature(s
)) {
1430 error(_("ERROR: sector %lu does not have an msdos signature\n"),
1434 cp
= s
->data
+ 0x1be;
1436 if (pno
+ 4 >= ARRAY_SIZE(z
->partitions
)) {
1437 warnx(_("too many partitions - ignoring those past nr (%zu)\n"),
1444 for (i
= 0; i
< 4; i
++, cp
+= sizeof(struct partition
)) {
1445 partitions
[pno
].sector
= here
;
1446 partitions
[pno
].offset
= cp
- s
->data
;
1447 partitions
[pno
].ep
= ep
;
1448 copy_to_part(cp
, &p
);
1449 if (is_extended(p
.sys_type
)) {
1450 partitions
[pno
].start
= start
+ p
.start_sect
;
1452 warnx(_("tree of partitions?\n"));
1454 next
= partitions
[pno
].start
; /* follow `upper' branch */
1457 partitions
[pno
].start
= here
+ p
.start_sect
;
1459 partitions
[pno
].size
= p
.nr_sects
;
1460 partitions
[pno
].ptype
= DOS_TYPE
;
1461 partitions
[pno
].p
= p
;
1470 #define BSD_DISKMAGIC (0x82564557UL)
1471 #define BSD_MAXPARTITIONS 16
1472 #define BSD_FS_UNUSED 0
1473 typedef unsigned char u8
;
1474 typedef unsigned short u16
;
1475 typedef unsigned int u32
;
1476 struct bsd_disklabel
{
1479 char d_typename
[16];
1480 char d_packname
[16];
1484 u16 d_npartitions
; /* number of partitions in following */
1486 struct bsd_partition
{ /* the partition table */
1487 u32 p_size
; /* number of sectors in partition */
1488 u32 p_offset
; /* starting sector */
1489 u32 p_fsize
; /* filesystem basic fragment size */
1490 u8 p_fstype
; /* filesystem type, see below */
1491 u8 p_frag
; /* filesystem fragments per block */
1492 u16 p_cpg
; /* filesystem cylinders per group */
1493 } d_partitions
[BSD_MAXPARTITIONS
]; /* actually may be more */
1497 bsd_partition(char *dev
, int fd
, struct part_desc
*ep
, struct disk_desc
*z
) {
1498 struct bsd_disklabel
*l
;
1499 struct bsd_partition
*bp
, *bp0
;
1500 unsigned long long start
= ep
->start
;
1502 struct part_desc
*partitions
= &(z
->partitions
[0]);
1503 size_t pno
= z
->partno
;
1505 if (!(s
= get_sector(dev
, fd
, start
+ 1)))
1507 l
= (struct bsd_disklabel
*)(s
->data
);
1508 if (l
->d_magic
!= BSD_DISKMAGIC
|| l
->d_magic2
!= BSD_DISKMAGIC
)
1511 bp
= bp0
= &l
->d_partitions
[0];
1512 while (bp
- bp0
< BSD_MAXPARTITIONS
&& bp
- bp0
< l
->d_npartitions
) {
1513 if (pno
+ 1 >= ARRAY_SIZE(z
->partitions
)) {
1514 warnx(_("too many partitions - ignoring those "
1515 "past nr (%zu)\n"), pno
- 1);
1518 if (bp
->p_fstype
!= BSD_FS_UNUSED
) {
1519 partitions
[pno
].start
= bp
->p_offset
;
1520 partitions
[pno
].size
= bp
->p_size
;
1521 partitions
[pno
].sector
= start
+ 1;
1522 partitions
[pno
].offset
= (char *)bp
- (char *)bp0
;
1523 partitions
[pno
].ep
= 0;
1524 partitions
[pno
].ptype
= BSD_TYPE
;
1533 msdos_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1536 struct partition pt
;
1538 struct part_desc
*partitions
= &(z
->partitions
[0]);
1539 int pno
= z
->partno
;
1542 bsd_later
= (get_linux_version() >= KERNEL_VERSION(2, 3, 40));
1545 if (!(s
= get_sector(dev
, fd
, start
)))
1548 if (!msdos_signature(s
))
1551 cp
= s
->data
+ 0x1be;
1552 copy_to_part(cp
, &pt
);
1554 /* If I am not mistaken, recent kernels will hide this from us,
1555 so we will never actually see traces of a Disk Manager */
1556 if (pt
.sys_type
== DM6_PARTITION
1557 || pt
.sys_type
== EZD_PARTITION
1558 || pt
.sys_type
== DM6_AUX1PARTITION
1559 || pt
.sys_type
== DM6_AUX3PARTITION
) {
1560 warnx(_("detected Disk Manager - unable to handle that\n"));
1564 unsigned short sig
, magic
;
1565 memcpy(&sig
, s
->data
+ 2, sizeof(sig
));
1567 memcpy(&magic
, s
->data
+ sig
, sizeof(magic
));
1568 if (magic
== 0x55aa && (1 & *(unsigned char *)(s
->data
+ sig
+ 2))) {
1569 warnx(_("DM6 signature found - giving up\n"));
1574 for (pno
= 0; pno
< 4; pno
++, cp
+= sizeof(struct partition
)) {
1575 partitions
[pno
].sector
= start
;
1576 partitions
[pno
].offset
= cp
- s
->data
;
1577 copy_to_part(cp
, &pt
);
1578 partitions
[pno
].start
= start
+ pt
.start_sect
;
1579 partitions
[pno
].size
= pt
.nr_sects
;
1580 partitions
[pno
].ep
= 0;
1581 partitions
[pno
].p
= pt
;
1586 for (i
= 0; i
< 4; i
++) {
1587 if (is_extended(partitions
[i
].p
.sys_type
)) {
1588 if (!partitions
[i
].size
) {
1589 warnx(_("strange..., an extended partition of size 0?\n"));
1592 extended_partition(dev
, fd
, &partitions
[i
], z
);
1594 if (!bsd_later
&& is_bsd(partitions
[i
].p
.sys_type
)) {
1595 if (!partitions
[i
].size
) {
1596 warnx(_("strange..., a BSD partition of size 0?\n"));
1599 bsd_partition(dev
, fd
, &partitions
[i
], z
);
1604 for (i
= 0; i
< 4; i
++) {
1605 if (is_bsd(partitions
[i
].p
.sys_type
)) {
1606 if (!partitions
[i
].size
) {
1607 warnx(_("strange..., a BSD partition of size 0?\n"));
1610 bsd_partition(dev
, fd
, &partitions
[i
], z
);
1619 osf_partition(char *dev
__attribute__ ((__unused__
)),
1620 int fd
__attribute__ ((__unused__
)),
1621 unsigned long start
__attribute__ ((__unused__
)),
1622 struct disk_desc
*z
__attribute__ ((__unused__
))) {
1627 sun_partition(char *dev
__attribute__ ((__unused__
)),
1628 int fd
__attribute__ ((__unused__
)),
1629 unsigned long start
__attribute__ ((__unused__
)),
1630 struct disk_desc
*z
__attribute__ ((__unused__
))) {
1635 amiga_partition(char *dev
__attribute__ ((__unused__
)),
1636 int fd
__attribute__ ((__unused__
)),
1637 unsigned long start
__attribute__ ((__unused__
)),
1638 struct disk_desc
*z
__attribute__ ((__unused__
))) {
1643 get_partitions(char *dev
, int fd
, struct disk_desc
*z
) {
1646 if (!msdos_partition(dev
, fd
, 0, z
)
1647 && !osf_partition(dev
, fd
, 0, z
)
1648 && !sun_partition(dev
, fd
, 0, z
)
1649 && !amiga_partition(dev
, fd
, 0, z
)) {
1651 warnx(_(" %s: unrecognized partition table type\n"), dev
);
1657 write_partitions(char *dev
, int fd
, struct disk_desc
*z
) {
1659 struct part_desc
*partitions
= &(z
->partitions
[0]), *p
;
1660 int pno
= z
->partno
;
1663 warnx(_("-n flag was given: Nothing changed\n"));
1667 for (p
= partitions
; p
< partitions
+ pno
; p
++) {
1668 s
= get_sector(dev
, fd
, p
->sector
);
1671 s
->to_be_written
= 1;
1672 if (p
->ptype
== DOS_TYPE
) {
1673 copy_from_part(&(p
->p
), s
->data
+ p
->offset
);
1674 s
->data
[510] = 0x55;
1675 s
->data
[511] = (unsigned char)0xaa;
1678 if (save_sector_file
) {
1679 if (!save_sectors(dev
, fd
)) {
1680 errx(EXIT_FAILURE
, _("Failed saving the old sectors - aborting\n"));
1684 if (!write_sectors(dev
, fd
)) {
1685 error(_("Failed writing the partition on %s\n"), dev
);
1690 error(_("Failed writing the partition on %s\n"), dev
);
1697 * F. The standard input
1702 * <start> <size> <type> <bootable> <c,h,s> <c,h,s>
1703 * Fields are separated by whitespace or comma or semicolon possibly
1704 * followed by whitespace; initial and trailing whitespace is ignored.
1705 * Numbers can be octal, decimal or hexadecimal, decimal is default
1706 * The <c,h,s> parts can (and probably should) be omitted.
1707 * Bootable is specified as [*|-], with as default not-bootable.
1708 * Type is given in hex, without the 0x prefix, or is [E|S|L|X], where
1709 * L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), and E
1710 * is EXTENDED_PARTITION (5), X is LINUX_EXTENDED (85).
1711 * The default value of start is the first nonassigned sector/cylinder/...
1712 * The default value of size is as much as possible (until next
1713 * partition or end-of-disk).
1714 * .: end of chain of extended partitions.
1716 * On interactive input an empty line means: all defaults.
1717 * Otherwise empty lines are ignored.
1731 3, "bootable", 1}, {
1741 * Read a line, split it into fields
1743 * (some primitive handwork, but a more elaborate parser seems
1750 read_stdin(char **fields
, char *line
, int fieldssize
, int linesize
) {
1754 /* boolean true and empty string at start */
1757 for (fno
= 0; fno
< fieldssize
; fno
++)
1758 fields
[fno
] = line
+ 1;
1761 /* read a line from stdin */
1762 lp
= fgets(line
+ 2, linesize
- 2, stdin
);
1767 if (!(lp
= strchr(lp
, '\n')))
1768 errx(EXIT_FAILURE
, _("long or incomplete input line - quitting"));
1771 /* remove comments, if any */
1772 if ((lp
= strchr(line
+ 2, '#')) != 0)
1775 /* recognize a few commands - to be expanded */
1776 if (!strcmp(line
+ 2, "unit: sectors")) {
1777 specified_format
= F_SECTOR
;
1781 /* dump style? - then bad input is fatal */
1782 if ((ip
= strchr(line
+ 2, ':')) != 0) {
1787 while (isspace(*ip
))
1791 for (d
= dumpflds
; (size_t) (d
- dumpflds
) < ARRAY_SIZE(dumpflds
); d
++) {
1792 if (!strncmp(ip
, d
->fldname
, strlen(d
->fldname
))) {
1793 ip
+= strlen(d
->fldname
);
1794 while (isspace(*ip
))
1797 fields
[d
->fldno
] = line
;
1798 else if (*ip
== '=') {
1799 while (isspace(*++ip
)) ;
1800 fields
[d
->fldno
] = ip
;
1801 while (isalnum(*ip
)) /* 0x07FF */
1804 errx(EXIT_FAILURE
, _("input error: `=' expected after %s field"),
1806 if (fno
<= d
->fldno
)
1810 if (*ip
!= ',' && *ip
!= ';')
1811 errx(EXIT_FAILURE
, _("input error: unexpected character %c after %s field"),
1817 errx(EXIT_FAILURE
, _("unrecognized input: %s"), ip
);
1820 /* split line into fields */
1823 while ((c
= *ip
++) != 0) {
1824 if (!lp
[-1] && (c
== '\t' || c
== ' ')) ;
1825 else if (c
== '\t' || c
== ' ' || c
== ',' || c
== ';') {
1827 if (fno
< fieldssize
)
1834 if (lp
== fields
[fno
- 1])
1839 /* read a number, use default if absent */
1840 /* a sign gives an offset from the default */
1842 get_ul(char *u
, unsigned long *up
, unsigned long def
, int base
) {
1850 } else if (*u
== '-') {
1856 val
= strtoul(u
, &nu
, base
);
1857 if (errno
== ERANGE
) {
1858 warnx(_("number too big\n"));
1862 warnx(_("trailing junk after number\n"));
1867 else if (sign
== -1)
1876 /* read a number, use default if absent */
1877 /* a sign gives an offset from the default */
1879 get_ull(char *u
, unsigned long long *up
, unsigned long long def
, int base
) {
1882 unsigned long long val
;
1887 } else if (*u
== '-') {
1893 val
= strtoull(u
, &nu
, base
);
1894 if (errno
== ERANGE
) {
1895 warnx(_("number too big\n"));
1899 warnx(_("trailing junk after number\n"));
1904 else if (sign
== -1)
1913 /* There are two common ways to structure extended partitions:
1914 as nested boxes, and as a chain. Sometimes the partitions
1915 must be given in order. Sometimes all logical partitions
1916 must lie inside the outermost extended partition.
1917 NESTED: every partition is contained in the surrounding partitions
1918 and is disjoint from all others.
1919 CHAINED: every data partition is contained in the surrounding partitions
1920 and disjoint from all others, but extended partitions may lie outside
1921 (insofar as allowed by all_logicals_inside_outermost_extended).
1922 ONESECTOR: all data partitions are mutually disjoint; extended partitions
1923 each use one sector only (except perhaps for the outermost one).
1925 int partitions_in_order
= 0;
1926 int all_logicals_inside_outermost_extended
= 1;
1927 enum { NESTED
, CHAINED
, ONESECTOR
} boxes
= NESTED
;
1929 /* find the default value for <start> - assuming entire units */
1930 static unsigned long long
1931 first_free(int pno
, int is_extended
, struct part_desc
*ep
, int format
,
1932 unsigned long long mid
, struct disk_desc
*z
) {
1933 unsigned long long ff
, fff
;
1934 unsigned long unit
= unitsize(format
);
1935 struct part_desc
*partitions
= &(z
->partitions
[0]), *pp
= 0;
1937 /* if containing ep undefined, look at its container */
1938 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
)
1942 if (boxes
== NESTED
|| (boxes
== CHAINED
&& !is_extended
))
1944 else if (all_logicals_inside_outermost_extended
)
1945 pp
= outer_extended_partition(ep
);
1948 ff
= pp
? (pp
->start
+ unit
- 1) / unit
: 0;
1950 /* rounding up wastes almost an entire cylinder - round down
1951 and leave it to compute_start_sect() to fix the difference */
1952 ff
= pp
? pp
->start
/ unit
: 0;
1954 /* MBR and 1st sector of an extended partition are never free */
1959 for (pp
= partitions
; pp
< partitions
+ pno
; pp
++) {
1960 if (!is_parent(pp
, ep
) && pp
->size
> 0) {
1961 if ((partitions_in_order
|| pp
->start
/ unit
<= ff
1962 || (mid
&& pp
->start
/ unit
<= mid
))
1963 && (fff
= (pp
->start
+ pp
->size
+ unit
- 1) / unit
) > ff
) {
1973 /* find the default value for <size> - assuming entire units */
1974 static unsigned long long
1975 max_length(int pno
, int is_extended
, struct part_desc
*ep
, int format
,
1976 unsigned long long start
, struct disk_desc
*z
) {
1977 unsigned long long fu
;
1978 unsigned long unit
= unitsize(format
);
1979 struct part_desc
*partitions
= &(z
->partitions
[0]), *pp
= 0;
1981 /* if containing ep undefined, look at its container */
1982 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
)
1986 if (boxes
== NESTED
|| (boxes
== CHAINED
&& !is_extended
))
1988 else if (all_logicals_inside_outermost_extended
)
1989 pp
= outer_extended_partition(ep
);
1991 fu
= pp
? (pp
->start
+ pp
->size
) / unit
: get_disksize(format
);
1993 for (pp
= partitions
; pp
< partitions
+ pno
; pp
++)
1994 if (!is_parent(pp
, ep
) && pp
->size
> 0
1995 && pp
->start
/ unit
>= start
&& pp
->start
/ unit
< fu
)
1996 fu
= pp
->start
/ unit
;
1998 return (fu
> start
) ? fu
- start
: 0;
2001 /* compute starting sector of a partition inside an extended one */
2002 /* return 0 on failure */
2003 /* ep is 0 or points to surrounding extended partition */
2005 compute_start_sect(struct part_desc
*p
, struct part_desc
*ep
) {
2006 unsigned long long base
;
2007 int inc
= (DOS
&& B
.sectors
) ? B
.sectors
: 1;
2010 if (ep
&& p
->start
+ p
->size
>= ep
->start
+ 1)
2011 delta
= p
->start
- ep
->start
- inc
;
2012 else if (p
->start
== 0 && p
->size
> 0)
2018 unsigned long long old_size
= p
->size
;
2021 if (is_extended(p
->p
.sys_type
) && boxes
== ONESECTOR
)
2023 else if ((long long) old_size
<= -delta
) {
2024 my_warn(_("no room for partition descriptor\n"));
2029 : (is_extended(p
->p
.sys_type
) ?
2030 outer_extended_partition(ep
) : ep
)->start
);
2032 if (p
->p
.sys_type
== EMPTY_PARTITION
&& p
->size
== 0) {
2033 p
->p
.start_sect
= 0;
2034 p
->p
.begin_chs
= zero_chs
;
2035 p
->p
.end_chs
= zero_chs
;
2037 p
->p
.start_sect
= p
->start
- base
;
2038 p
->p
.begin_chs
= ulong_to_chs(p
->start
, B
);
2039 p
->p
.end_chs
= ulong_to_chs(p
->start
+ p
->size
- 1, B
);
2041 p
->p
.nr_sects
= p
->size
;
2045 /* build the extended partition surrounding a given logical partition */
2047 build_surrounding_extended(struct part_desc
*p
, struct part_desc
*ep
,
2048 struct disk_desc
*z
) {
2049 int inc
= (DOS
&& B
.sectors
) ? B
.sectors
: 1;
2050 int format
= F_SECTOR
;
2051 struct part_desc
*p0
= &(z
->partitions
[0]), *eep
= ep
->ep
;
2053 if (boxes
== NESTED
) {
2054 ep
->start
= first_free(ep
- p0
, 1, eep
, format
, p
->start
, z
);
2055 ep
->size
= max_length(ep
- p0
, 1, eep
, format
, ep
->start
, z
);
2056 if (ep
->start
> p
->start
|| ep
->start
+ ep
->size
< p
->start
+ p
->size
) {
2057 my_warn(_("cannot build surrounding extended partition\n"));
2061 ep
->start
= p
->start
;
2062 if (boxes
== CHAINED
)
2068 ep
->p
.nr_sects
= ep
->size
;
2070 ep
->p
.sys_type
= EXTENDED_PARTITION
;
2071 if (!compute_start_sect(ep
, eep
) || !compute_start_sect(p
, ep
)) {
2072 ep
->p
.sys_type
= EMPTY_PARTITION
;
2081 read_line(int pno
, struct part_desc
*ep
, char *dev
, int interactive
,
2082 struct disk_desc
*z
) {
2085 int fno
, pct
= pno
% 4;
2086 struct part_desc p
, *orig
;
2087 unsigned long long ff
, ff1
, ul
, ml
, ml1
, def
;
2088 int format
, lpno
, is_extd
;
2093 lpno
= index_to_linux(pno
, z
);
2096 if (pct
== 0 && (show_extended
|| pno
== 0))
2098 my_warn("%s:", partname(dev
, lpno
, 10));
2101 /* read input line - skip blank lines when reading from a file */
2103 fno
= read_stdin(fields
, line
, ARRAY_SIZE(fields
), ARRAY_SIZE(line
));
2104 } while (fno
== RD_CMD
|| (fno
== 0 && !interactive
));
2105 if (fno
== RD_EOF
) {
2107 } else if (fno
> 10 && *(fields
[10]) != 0) {
2108 warnx(_("too many input fields\n"));
2112 if (fno
== 1 && !strcmp(fields
[0], ".")) {
2117 /* use specified format, but round to cylinders if F_MEGABYTE specified */
2119 if (B
.cylindersize
&& specified_format
== F_MEGABYTE
&& !Linux
)
2120 format
= F_CYLINDER
;
2122 orig
= (one_only
? &(oldp
.partitions
[pno
]) : 0);
2127 /* first read the type - we need to know whether it is extended */
2128 /* stop reading when input blank (defaults) and all is full */
2130 if (fno
== 0) { /* empty line */
2131 if (orig
&& is_extended(orig
->p
.sys_type
))
2133 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
2134 ml
= max_length(pno
, is_extd
, ep
, format
, ff
, z
);
2135 if (ml
== 0 && is_extd
== 0) {
2137 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
2138 ml
= max_length(pno
, is_extd
, ep
, format
, ff
, z
);
2140 if (ml
== 0 && pno
>= 4) {
2141 /* no free blocks left - don't read any further */
2142 my_warn(_("No room for more\n"));
2146 if (fno
< 3 || !*(fields
[2]))
2147 ul
= orig
? orig
->p
.sys_type
:
2148 (is_extd
|| (pno
> 3 && pct
== 1 && show_extended
))
2149 ? EXTENDED_PARTITION
: LINUX_NATIVE
;
2150 else if (!strcmp(fields
[2], "L"))
2152 else if (!strcmp(fields
[2], "S"))
2154 else if (!strcmp(fields
[2], "E"))
2155 ul
= EXTENDED_PARTITION
;
2156 else if (!strcmp(fields
[2], "X"))
2157 ul
= LINUX_EXTENDED
;
2158 else if (get_ull(fields
[2], &ul
, LINUX_NATIVE
, 16))
2161 my_warn(_("Illegal type\n"));
2165 is_extd
= is_extended(ul
);
2168 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
2169 ff1
= ff
* unitsize(format
);
2170 def
= orig
? orig
->start
: (pno
> 4 && pct
> 1) ? 0 : ff1
;
2171 if (fno
< 1 || !*(fields
[0]))
2174 if (get_ull(fields
[0], &ul
, def
/ unitsize(0), 0))
2176 p
.start
= ul
* unitsize(0);
2177 p
.start
-= (p
.start
% unitsize(format
));
2181 ml
= max_length(pno
, is_extd
, ep
, format
, p
.start
/ unitsize(format
), z
);
2182 ml1
= ml
* unitsize(format
);
2183 def
= orig
? orig
->size
: (pno
> 4 && pct
> 1) ? 0 : ml1
;
2184 if (fno
< 2 || !*(fields
[1]))
2186 else if (!strcmp(fields
[1], "+"))
2189 if (get_ull(fields
[1], &ul
, def
/ unitsize(0), 0))
2191 p
.size
= ul
* unitsize(0) + unitsize(format
) - 1;
2192 p
.size
-= (p
.size
% unitsize(format
));
2195 my_warn(_("Warning: given size (%lu) exceeds max allowable size (%lu)\n"),
2196 (p
.size
+ unitsize(0) - 1) / unitsize(0), ml1
/ unitsize(0));
2200 if (p
.size
== 0 && pno
>= 4 && (fno
< 2 || !*(fields
[1]))) {
2201 my_warn(_("Warning: empty partition\n"));
2205 p
.p
.nr_sects
= p
.size
;
2207 if (p
.size
== 0 && !orig
) {
2208 if (fno
< 1 || !*(fields
[0]))
2210 if (fno
< 3 || !*(fields
[2]))
2211 p
.p
.sys_type
= EMPTY_PARTITION
;
2214 if (p
.start
< ff1
&& p
.size
> 0) {
2215 my_warn(_("Warning: bad partition start (earliest %lu)\n"),
2216 (ff1
+ unitsize(0) - 1) / unitsize(0));
2221 if (fno
< 4 || !*(fields
[3]))
2222 ul
= (orig
? orig
->p
.bootable
: 0);
2223 else if (!strcmp(fields
[3], "-"))
2225 else if (!strcmp(fields
[3], "*") || !strcmp(fields
[3], "+"))
2228 my_warn(_("unrecognized bootable flag - choose - or *\n"));
2233 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
) {
2234 if (!build_surrounding_extended(&p
, ep
, z
))
2236 } else if (!compute_start_sect(&p
, ep
))
2240 longchs aa
= chs_to_longchs(p
.p
.begin_chs
), bb
;
2244 } else if (fno
< 7) {
2245 my_warn(_("partial c,h,s specification?\n"));
2247 } else if (get_ul(fields
[4], &bb
.c
, aa
.c
, 0) ||
2248 get_ul(fields
[5], &bb
.h
, aa
.h
, 0) ||
2249 get_ul(fields
[6], &bb
.s
, aa
.s
, 0))
2251 p
.p
.begin_chs
= longchs_to_chs(bb
, B
);
2254 longchs aa
= chs_to_longchs(p
.p
.end_chs
), bb
;
2258 } else if (fno
< 10) {
2259 my_warn(_("partial c,h,s specification?\n"));
2261 } else if (get_ul(fields
[7], &bb
.c
, aa
.c
, 0) ||
2262 get_ul(fields
[8], &bb
.h
, aa
.h
, 0) ||
2263 get_ul(fields
[9], &bb
.s
, aa
.s
, 0))
2265 p
.p
.end_chs
= longchs_to_chs(bb
, B
);
2268 if (pno
> 3 && p
.size
&& show_extended
&& p
.p
.sys_type
!= EMPTY_PARTITION
2269 && (is_extended(p
.p
.sys_type
) != (pct
== 1))) {
2270 my_warn(_("Extended partition not where expected\n"));
2275 z
->partitions
[pno
] = p
;
2276 if (pno
>= z
->partno
)
2277 z
->partno
+= 4; /* reqd for out_partition() */
2280 out_partition(dev
, 0, &(z
->partitions
[pno
]), z
, B
);
2285 /* ep either points to the extended partition to contain this one,
2286 or to the empty partition that may become extended or is 0 */
2288 read_partition(char *dev
, int interactive
, int pno
, struct part_desc
*ep
,
2289 struct disk_desc
*z
) {
2290 struct part_desc
*p
= &(z
->partitions
[pno
]);
2294 *p
= oldp
.partitions
[pno
];
2295 if (one_only_pno
!= pno
)
2297 } else if (!show_extended
&& pno
> 4 && pno
% 4)
2300 while (!(i
= read_line(pno
, ep
, dev
, interactive
, z
)))
2302 errx(EXIT_FAILURE
, _("bad input"));
2310 if (pno
>= z
->partno
)
2316 read_partition_chain(char *dev
, int interactive
, struct part_desc
*ep
,
2317 struct disk_desc
*z
) {
2324 if (base
+ 4 > ARRAY_SIZE(z
->partitions
)) {
2325 warnx(_("too many partitions\n"));
2328 for (i
= 0; i
< 4; i
++)
2329 if (!read_partition(dev
, interactive
, base
+ i
, ep
, z
))
2331 for (i
= 0; i
< 4; i
++) {
2332 ep
= &(z
->partitions
[base
+ i
]);
2333 if (is_extended(ep
->p
.sys_type
) && ep
->size
)
2337 /* nothing found - maybe an empty partition is going
2339 if (one_only
|| show_extended
)
2341 ep
= &(z
->partitions
[base
+ 1]);
2342 if (ep
->size
|| ep
->p
.sys_type
!= EMPTY_PARTITION
)
2349 read_input(char *dev
, int interactive
, struct disk_desc
*z
) {
2351 struct part_desc
*partitions
= &(z
->partitions
[0]), *ep
;
2353 for (i
= 0; i
< ARRAY_SIZE(z
->partitions
); i
++)
2354 partitions
[i
] = zero_part_desc
;
2358 my_warn(_("Input in the following format; absent fields get a default value.\n"
2359 "<start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s>\n"
2360 "Usually you only need to specify <start> and <size> (and perhaps <type>).\n"));
2363 for (i
= 0; i
< 4; i
++)
2364 read_partition(dev
, interactive
, i
, 0, z
);
2365 for (i
= 0; i
< 4; i
++) {
2366 ep
= partitions
+ i
;
2367 if (is_extended(ep
->p
.sys_type
) && ep
->size
)
2368 read_partition_chain(dev
, interactive
, ep
, z
);
2370 add_sector_and_offset(z
);
2374 * G. The command line
2376 static void usage(FILE * out
)
2378 fputs(_("\nUsage:\n"), out
);
2380 _(" %s [options] <device> [...]\n"), program_invocation_short_name
);
2382 fputs(_("\nOptions:\n"), out
);
2383 fputs(_(" -s, --show-size list size of a partition\n"
2384 " -c, --id change or print partition Id\n"
2385 " --change-id change Id\n"
2386 " --print-id print Id\n"), out
);
2387 fputs(_(" -l, --list list partitions of each device\n"
2388 " -d, --dump idem, but in a format suitable for later input\n"
2389 " -i, --increment number cylinders etc. from 1 instead of from 0\n"
2390 " -u, --unit <letter> units to be used; <letter> can be one of\n"
2391 " S (sectors), C (cylinders), B (blocks), or M (MB)\n"), out
);
2392 fputs(_(" -1, --one-only reserved option that does nothing currently\n"
2393 " -T, --list-types list the known partition types\n"
2394 " -D, --DOS for DOS-compatibility: waste a little space\n"
2395 " -E, --DOS-extended DOS extended partition compatibility\n"
2396 " -R, --re-read make the kernel reread the partition table\n"), out
);
2397 fputs(_(" -N <number> change only the partition with this <number>\n"
2398 " -n do not actually write to disk\n"
2399 " -O <file> save the sectors that will be overwritten to <file>\n"
2400 " -I <file> restore sectors from <file>\n"), out
);
2401 fputs(_(" -V, --verify check that the listed partitions are reasonable\n"
2402 " -v, --version display version information and exit\n"
2403 " -h, --help display this help text and exit\n"), out
);
2405 fputs(_("\nDangerous options:\n"), out
);
2406 fputs(_(" -f, --force disable all consistency checking\n"
2407 " --no-reread do not check whether the partition is in use\n"
2408 " -q, --quiet suppress warning messages\n"
2409 " -L, --Linux do not complain about things irrelevant for Linux\n"), out
);
2410 fputs(_(" -g, --show-geometry print the kernel's idea of the geometry\n"
2411 " -G, --show-pt-geometry print geometry guessed from the partition table\n"), out
);
2412 fputs(_(" -A, --activate[=<device>] activate bootable flag\n"
2413 " -U, --unhide[=<dev>] set partition unhidden\n"
2414 " -x, --show-extended also list extended partitions in the output,\n"
2415 " or expect descriptors for them in the input\n"), out
);
2416 fputs(_(" --leave-last do not allocate the last cylinder\n"
2417 " --IBM same as --leave-last\n"), out
);
2418 fputs(_(" --in-order partitions are in order\n"
2419 " --not-in-order partitions are not in order\n"
2420 " --inside-outer all logicals inside outermost extended\n"
2421 " --not-inside-outer not all logicals inside outermost extended\n"), out
);
2422 fputs(_(" --nested every partition is disjoint from all others\n"
2423 " --chained like nested, but extended partitions may lie outside\n"
2424 " --onesector partitions are mutually disjoint\n"), out
);
2426 fputs(_("\nOverride the detected geometry using:\n"
2427 " -C, --cylinders <number> set the number of cylinders to use\n"
2428 " -H, --heads <number> set the number of heads to use\n"
2429 " -S, --sectors <number> set the number of sectors to use\n\n"), out
);
2431 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
2435 activate_usage(char *progn
) {
2437 printf(_("%s device list active partitions on device\n"), progn
);
2438 printf(_("%s device n1 n2 ... activate partitions n1 ..., inactivate the rest\n"),
2440 printf(_("%s -An device activate partition n, inactivate the other ones\n"),
2446 unhide_usage(char *progn
__attribute__ ((__unused__
))) {
2450 static const char short_opts
[] = "cdfghilnqsu:vx1A::C:DGH:I:LN:O:RS:TU::V";
2452 #define PRINT_ID 0400
2453 #define CHANGE_ID 01000
2456 OPT_NO_REREAD
= CHAR_MAX
+ 1,
2461 OPT_NOT_INSIDE_OUTER
,
2467 static const struct option long_opts
[] = {
2468 { "change-id", no_argument
, NULL
, 'c' + CHANGE_ID
},
2469 { "print-id", no_argument
, NULL
, 'c' + PRINT_ID
},
2470 { "id", no_argument
, NULL
, 'c' },
2471 { "dump", no_argument
, NULL
, 'd' },
2472 { "force", no_argument
, NULL
, 'f' },
2473 { "show-geometry", no_argument
, NULL
, 'g' },
2474 { "help", no_argument
, NULL
, 'h' },
2475 { "increment", no_argument
, NULL
, 'i' },
2476 { "list", no_argument
, NULL
, 'l' },
2477 { "quiet", no_argument
, NULL
, 'q' },
2478 { "show-size", no_argument
, NULL
, 's' },
2479 { "unit", required_argument
, NULL
, 'u' },
2480 { "version", no_argument
, NULL
, 'v' },
2481 { "show-extended", no_argument
, NULL
, 'x' },
2482 { "one-only", no_argument
, NULL
, '1' },
2483 { "cylinders", required_argument
, NULL
, 'C' },
2484 { "heads", required_argument
, NULL
, 'H' },
2485 { "sectors", required_argument
, NULL
, 'S' },
2486 { "show-pt-geometry", no_argument
, NULL
, 'G' },
2487 { "activate", optional_argument
, NULL
, 'A' },
2488 { "DOS", no_argument
, NULL
, 'D' },
2489 { "DOS-extended", no_argument
, NULL
, 'E' },
2490 { "Linux", no_argument
, NULL
, 'L' },
2491 { "re-read", no_argument
, NULL
, 'R' },
2492 { "list-types", no_argument
, NULL
, 'T' },
2493 { "unhide", optional_argument
, NULL
, 'U' },
2494 { "no-reread", no_argument
, NULL
, OPT_NO_REREAD
},
2495 { "IBM", no_argument
, NULL
, OPT_LEAVE_LAST
},
2496 { "leave-last", no_argument
, NULL
, OPT_LEAVE_LAST
},
2497 /* dangerous flags - not all completely implemented */
2498 { "in-order", no_argument
, NULL
, OPT_IN_ORDER
},
2499 { "not-in-order", no_argument
, NULL
, OPT_NOT_IN_ORDER
},
2500 { "inside-outer", no_argument
, NULL
, OPT_INSIDE_OUTER
},
2501 { "not-inside-outer", no_argument
, NULL
, OPT_NOT_INSIDE_OUTER
},
2502 { "nested", no_argument
, NULL
, OPT_NESTED
},
2503 { "chained", no_argument
, NULL
, OPT_CHAINED
},
2504 { "onesector", no_argument
, NULL
, OPT_ONESECTOR
},
2505 { NULL
, 0, NULL
, 0 }
2508 static int is_ide_cdrom_or_tape(char *device
)
2512 if ((fd
= open(device
, O_RDONLY
)) < 0)
2514 ret
= blkdev_is_cdrom(fd
);
2521 nextproc(FILE * procf
) {
2522 static char devname
[256];
2523 char line
[1024], ptname
[128 + 1];
2525 unsigned long long sz
;
2529 while (fgets(line
, sizeof(line
), procf
) != NULL
) {
2530 if (sscanf(line
, " %d %d %llu %128[^\n ]", &ma
, &mi
, &sz
, ptname
) != 4)
2532 snprintf(devname
, sizeof(devname
), "/dev/%s", ptname
);
2533 if (!is_whole_disk(devname
))
2535 return canonicalize_path(devname
);
2541 static void do_list(char *dev
, int silent
);
2542 static void do_size(char *dev
, int silent
);
2543 static void do_geom(char *dev
, int silent
);
2544 static void do_pt_geom(char *dev
, int silent
);
2545 static void do_fdisk(char *dev
);
2546 static void do_reread(char *dev
);
2547 static void do_change_id(char *dev
, char *part
, char *id
);
2548 static void do_unhide(char **av
, int ac
, char *arg
);
2549 static void do_activate(char **av
, int ac
, char *arg
);
2551 unsigned long long total_size
;
2554 main(int argc
, char **argv
) {
2559 int opt_out_geom
= 0;
2560 int opt_out_pt_geom
= 0;
2566 char *activatearg
= 0;
2567 char *unhidearg
= 0;
2569 setlocale(LC_ALL
, "");
2570 bindtextdomain(PACKAGE
, LOCALEDIR
);
2571 textdomain(PACKAGE
);
2572 atexit(close_stdout
);
2575 errx(EXIT_FAILURE
, _("no command?"));
2576 if ((progn
= strrchr(argv
[0], '/')) == NULL
)
2580 if (!strcmp(progn
, "activate"))
2581 activate
= 1; /* equivalent to `sfdisk -A' */
2582 #if 0 /* not important enough to deserve a name */
2583 else if (!strcmp(progn
, "unhide"))
2584 unhide
= 1; /* equivalent to `sfdisk -U' */
2589 while ((c
= getopt_long(argc
, argv
, short_opts
, long_opts
, NULL
)) != -1) {
2593 break; /* does not imply quiet */
2598 opt_out_pt_geom
= 1;
2604 case 'c' + PRINT_ID
:
2605 case 'c' + CHANGE_ID
:
2609 dump
= 1; /* fall through */
2623 set_format(*optarg
);
2626 printf(UTIL_LINUX_VERSION
);
2627 return EXIT_SUCCESS
;
2630 return EXIT_SUCCESS
;
2635 activatearg
= optarg
;
2639 U
.cylinders
= strtoul_or_err(optarg
, _("invalid cylinders argument"));
2648 U
.heads
= strtoul_or_err(optarg
, _("invalid heads argument"));
2654 one_only
= strtol_or_err(optarg
, _("invalid number of partitions argument"));
2657 restore_sector_file
= optarg
;
2660 save_sector_file
= optarg
;
2666 U
.sectors
= strtoul_or_err(optarg
, _("invalid sectors argument"));
2682 /* dangerous flags */
2684 partitions_in_order
= 1;
2686 case OPT_NOT_IN_ORDER
:
2687 partitions_in_order
= 0;
2689 case OPT_INSIDE_OUTER
:
2690 all_logicals_inside_outermost_extended
= 1;
2692 case OPT_NOT_INSIDE_OUTER
:
2693 all_logicals_inside_outermost_extended
= 0;
2709 case OPT_LEAVE_LAST
:
2715 if (optind
== argc
&&
2716 (opt_list
|| opt_out_geom
|| opt_out_pt_geom
|| opt_size
|| verify
)) {
2719 /* try all known devices */
2722 procf
= fopen(_PATH_PROC_PARTITIONS
, "r");
2724 fprintf(stderr
, _("cannot open %s\n"), _PATH_PROC_PARTITIONS
);
2726 while ((dev
= nextproc(procf
)) != NULL
) {
2727 if (!is_ide_cdrom_or_tape(dev
)) {
2730 if (opt_out_pt_geom
)
2734 if (opt_list
|| verify
)
2743 printf(_("total: %llu blocks\n"), total_size
);
2748 if (optind
== argc
) {
2750 activate_usage(fdisk
? "sfdisk -A" : progn
);
2752 unhide_usage(fdisk
? "sfdisk -U" : progn
);
2757 if (opt_list
|| opt_out_geom
|| opt_out_pt_geom
|| opt_size
|| verify
) {
2758 while (optind
< argc
) {
2760 do_geom(argv
[optind
], 0);
2761 if (opt_out_pt_geom
)
2762 do_pt_geom(argv
[optind
], 0);
2764 do_size(argv
[optind
], 0);
2765 if (opt_list
|| verify
)
2766 do_list(argv
[optind
], 0);
2773 do_activate(argv
+ optind
, argc
- optind
, activatearg
);
2777 do_unhide(argv
+ optind
, argc
- optind
, unhidearg
);
2781 if ((do_id
& PRINT_ID
) != 0 && optind
!= argc
- 2)
2782 errx(EXIT_FAILURE
, _("usage: sfdisk --print-id device partition-number"));
2783 else if ((do_id
& CHANGE_ID
) != 0 && optind
!= argc
- 3)
2784 errx(EXIT_FAILURE
, _("usage: sfdisk --change-id device partition-number Id"));
2785 else if (optind
!= argc
- 3 && optind
!= argc
- 2)
2786 errx(EXIT_FAILURE
, _("usage: sfdisk --id device partition-number [Id]"));
2787 do_change_id(argv
[optind
], argv
[optind
+ 1],
2788 (optind
== argc
- 2) ? 0 : argv
[optind
+ 2]);
2792 if (optind
!= argc
- 1)
2793 errx(EXIT_FAILURE
, _("can specify only one device (except with -l or -s)"));
2798 else if (restore_sector_file
)
2799 restore_sectors(dev
);
2807 * H. Listing the current situation
2811 my_open(char *dev
, int rw
, int silent
) {
2814 mode
= (rw
? O_RDWR
: O_RDONLY
);
2815 fd
= open(dev
, mode
);
2816 if (fd
< 0 && !silent
) {
2819 errx(EXIT_FAILURE
, _("cannot open %s read-write"), dev
);
2821 errx(EXIT_FAILURE
, _("cannot open %s for reading"), dev
);
2827 do_list(char *dev
, int silent
) {
2829 struct disk_desc
*z
;
2831 fd
= my_open(dev
, 0, silent
);
2838 get_cylindersize(dev
, fd
, dump
? 1 : opt_list
? 0 : 1);
2839 get_partitions(dev
, fd
, z
);
2842 out_partitions(dev
, z
);
2845 if (partitions_ok(fd
, z
))
2846 my_warn(_("%s: OK\n"), dev
);
2855 do_geom(char *dev
, int silent
) {
2859 fd
= my_open(dev
, 0, silent
);
2863 R
= get_geometry(dev
, fd
, silent
);
2865 printf(_("%s: %ld cylinders, %ld heads, %ld sectors/track\n"),
2866 dev
, R
.cylinders
, R
.heads
, R
.sectors
);
2872 do_pt_geom(char *dev
, int silent
) {
2874 struct disk_desc
*z
;
2877 fd
= my_open(dev
, 0, silent
);
2884 get_cylindersize(dev
, fd
, 1);
2885 get_partitions(dev
, fd
, z
);
2889 if (z
->partno
!= 0 && get_fdisk_geometry(z
)) {
2891 R
.sectors
= F
.sectors
;
2892 R
.cylindersize
= R
.heads
* R
.sectors
;
2893 R
.cylinders
= (R
.cylindersize
== 0) ? 0 : R
.total_size
/ R
.cylindersize
;
2897 printf(_("%s: %ld cylinders, %ld heads, %ld sectors/track\n"),
2898 dev
, R
.cylinders
, R
.heads
, R
.sectors
);
2903 /* for compatibility with earlier fdisk: provide option -s */
2905 do_size(char *dev
, int silent
) {
2907 unsigned long long size
;
2909 fd
= my_open(dev
, 0, silent
);
2913 if (blkdev_get_sectors(fd
, &size
) == -1) {
2916 errx(EXIT_FAILURE
, _("Cannot get size of %s"), dev
);
2921 size
/= 2; /* convert sectors to blocks */
2923 /* a CDROM drive without mounted CD yields MAXINT */
2924 if (silent
&& size
== ((1 << 30) - 1))
2928 printf("%s: %9llu\n", dev
, size
);
2930 printf("%llu\n", size
);
2939 * Activate: usually one wants to have a single primary partition
2940 * to be active. OS/2 fdisk makes non-bootable logical partitions
2941 * active - I don't know what that means to OS/2 Boot Manager.
2943 * Call: activate /dev/hda 2 5 7 make these partitions active
2944 * and the remaining ones inactive
2945 * Or: sfdisk -A /dev/hda 2 5 7
2947 * If only a single partition must be active, one may also use the form
2948 * sfdisk -A2 /dev/hda
2950 * With "activate /dev/hda" or "sfdisk -A /dev/hda" the active partitions
2951 * are listed but not changed. To get zero active partitions, use
2952 * "activate /dev/hda none" or "sfdisk -A /dev/hda none".
2953 * Use something like `echo ",,,*" | sfdisk -N2 /dev/hda' to only make
2954 * /dev/hda2 active, without changing other partitions.
2956 * A warning will be given if after the change not precisely one primary
2957 * partition is active.
2959 * The present syntax was chosen to be (somewhat) compatible with the
2960 * activate from the LILO package.
2963 set_active(struct disk_desc
*z
, char *pnam
) {
2966 pno
= asc_to_index(pnam
, z
);
2967 if (z
->partitions
[pno
].ptype
== DOS_TYPE
)
2968 z
->partitions
[pno
].p
.bootable
= 0x80;
2972 do_activate(char **av
, int ac
, char *arg
) {
2975 int rw
, i
, pno
, lpno
;
2976 struct disk_desc
*z
;
2980 rw
= (!no_write
&& (arg
|| ac
> 1));
2981 fd
= my_open(dev
, rw
, 0);
2984 get_cylindersize(dev
, fd
, 1);
2985 get_partitions(dev
, fd
, z
);
2987 if (!arg
&& ac
== 1) {
2988 /* list active partitions */
2989 for (pno
= 0; pno
< z
->partno
; pno
++) {
2990 if (z
->partitions
[pno
].p
.bootable
) {
2991 lpno
= index_to_linux(pno
, z
);
2992 if (pno
== linux_to_index(lpno
, z
))
2993 printf("%s\n", partname(dev
, lpno
, 0));
2995 printf("%s#%d\n", dev
, pno
);
2996 if (z
->partitions
[pno
].p
.bootable
!= 0x80)
2997 my_warn(_("bad active byte: 0x%x instead of 0x80\n"),
2998 z
->partitions
[pno
].p
.bootable
);
3002 /* clear `active byte' everywhere */
3003 for (pno
= 0; pno
< z
->partno
; pno
++)
3004 if (z
->partitions
[pno
].ptype
== DOS_TYPE
)
3005 z
->partitions
[pno
].p
.bootable
= 0;
3007 /* then set where desired */
3011 for (i
= 1; i
< ac
; i
++)
3012 set_active(z
, av
[i
]);
3014 /* then write to disk */
3015 if (write_partitions(dev
, fd
, z
))
3016 my_warn(_("Done\n\n"));
3021 for (pno
= 0; pno
< z
->partno
&& pno
< 4; pno
++)
3022 if (z
->partitions
[pno
].p
.bootable
)
3025 my_warn(_("You have %d active primary partitions. This does not matter for LILO,\n"
3026 "but the DOS MBR will only boot a disk with 1 active partition.\n"),
3033 set_unhidden(struct disk_desc
*z
, char *pnam
) {
3037 pno
= asc_to_index(pnam
, z
);
3038 id
= z
->partitions
[pno
].p
.sys_type
;
3039 if (id
== 0x11 || id
== 0x14 || id
== 0x16 || id
== 0x17)
3042 errx(EXIT_FAILURE
, _("partition %s has id %x and is not hidden"), pnam
, id
);
3043 z
->partitions
[pno
].p
.sys_type
= id
;
3047 * maybe remove and make part of --change-id
3050 do_unhide(char **av
, int ac
, char *arg
) {
3053 struct disk_desc
*z
;
3058 fd
= my_open(dev
, rw
, 0);
3061 get_cylindersize(dev
, fd
, 1);
3062 get_partitions(dev
, fd
, z
);
3064 /* unhide where desired */
3066 set_unhidden(z
, arg
);
3068 for (i
= 1; i
< ac
; i
++)
3069 set_unhidden(z
, av
[i
]);
3071 /* then write to disk */
3072 if (write_partitions(dev
, fd
, z
))
3073 my_warn(_("Done\n\n"));
3081 do_change_id(char *dev
, char *pnam
, char *id
) {
3083 struct disk_desc
*z
;
3089 fd
= my_open(dev
, rw
, 0);
3092 get_cylindersize(dev
, fd
, 1);
3093 get_partitions(dev
, fd
, z
);
3095 pno
= asc_to_index(pnam
, z
);
3097 printf("%x\n", z
->partitions
[pno
].p
.sys_type
);
3100 i
= strtoul(id
, NULL
, 16);
3102 errx(EXIT_FAILURE
, _("Bad Id %lx"), i
);
3103 z
->partitions
[pno
].p
.sys_type
= i
;
3105 if (write_partitions(dev
, fd
, z
))
3106 my_warn(_("Done\n\n"));
3115 do_reread(char *dev
) {
3118 fd
= my_open(dev
, 0, 0);
3119 if (reread_ioctl(fd
)) {
3120 warnx(_("This disk is currently in use.\n"));
3128 * I. Writing the new situation
3132 do_fdisk(char *dev
) {
3135 struct stat statbuf
;
3136 int interactive
= isatty(0);
3137 struct disk_desc
*z
;
3139 if (stat(dev
, &statbuf
) < 0) {
3141 errx(EXIT_FAILURE
, _("Fatal error: cannot find %s"), dev
);
3143 if (!S_ISBLK(statbuf
.st_mode
)) {
3144 warnx(_("Warning: %s is not a block device\n"), dev
);
3147 fd
= my_open(dev
, !no_write
, 0);
3149 if (!no_write
&& !no_reread
) {
3150 my_warn(_("Checking that no-one is using this disk right now ...\n"));
3151 if (reread_ioctl(fd
)) {
3152 warnx(_("\nThis disk is currently in use - repartitioning is probably a bad idea.\n"
3153 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
3154 "Use the --no-reread flag to suppress this check.\n"));
3156 warnx(_("Use the --force flag to overrule all checks.\n"));
3166 get_cylindersize(dev
, fd
, 0);
3167 get_partitions(dev
, fd
, z
);
3169 printf(_("Old situation:\n"));
3170 out_partitions(dev
, z
);
3172 if (one_only
&& (one_only_pno
= linux_to_index(one_only
, z
)) < 0)
3173 errx(EXIT_FAILURE
, _("Partition %d does not exist, cannot change it"), one_only
);
3179 read_input(dev
, interactive
, z
);
3181 printf(_("New situation:\n"));
3182 out_partitions(dev
, z
);
3184 if (!partitions_ok(fd
, z
) && !force
) {
3186 errx(EXIT_FAILURE
, _("I don't like these partitions - nothing changed.\n"
3187 "(If you really want this, use the --force option.)"));
3189 warnx(_("I don't like this - probably you should answer No\n"));
3194 /* TRANSLATORS: sfdisk uses rpmatch which means the answers y and n
3195 * should be translated, but that is not the case with q answer. */
3196 printf(_("Are you satisfied with this? [ynq] "));
3198 printf(_("Do you want to write this to disk? [ynq] "));
3199 ignore_result( fgets(answer
, sizeof(answer
), stdin
) );
3200 if (answer
[0] == 'q' || answer
[0] == 'Q') {
3201 errx(EXIT_FAILURE
, _("Quitting - nothing changed"));
3202 } else if (rpmatch(answer
) == 1) {
3204 } else if (rpmatch(answer
) == 0) {
3207 printf(_("Please answer one of y,n,q\n"));
3214 if (write_partitions(dev
, fd
, z
))
3215 printf(_("Successfully wrote the new partition table\n\n"));
3219 if (!reread_disk_partition(dev
, fd
)) { /* close fd on success */
3223 my_warn(_("If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)\n"
3224 "to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1\n"
3225 "(See fdisk(8).)\n"));
3227 sync(); /* superstition */