1 /* vi: set sw=4 ts=4: */
3 * sfdisk version 3.0 - aeb - 950813
5 * Copyright (C) 1995 Andries E. Brouwer (aeb@cwi.nl)
7 * This program is free software. You can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation: either Version 1
10 * or (at your option) any later version.
12 * A.V. Le Blanc (LeBlanc@mcc.ac.uk) wrote Linux fdisk 1992-1994,
13 * patched by various people (faith@cs.unc.edu, martin@cs.unc.edu,
14 * leisner@sdsp.mc.xerox.com, esr@snark.thyrsus.com, aeb@cwi.nl)
15 * 1993-1995, with version numbers (as far as I have seen) 0.93 - 2.0e.
16 * This program had (head,sector,cylinder) as basic unit, and was
17 * (therefore) broken in several ways for the use on larger disks -
18 * for example, my last patch (from 2.0d to 2.0e) was required
19 * to allow a partition to cross cylinder 8064, and to write an
20 * extended partition past the 4GB mark.
22 * The current program is a rewrite from scratch, and I started a
23 * version numbering at 3.0.
24 * Andries Brouwer, aeb@cwi.nl, 950813
26 * Well, a good user interface is still lacking. On the other hand,
27 * many configurations cannot be handled by any other fdisk.
28 * I changed the name to sfdisk to prevent confusion. - aeb, 970501
31 * 19990319 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> - i18n
33 * busyboxed by Erik Andersen <andersee@debian.org> -- I stipped out
34 * all the NLS, pulled in some includes, and made stuff smaller...
37 #define PROGNAME "sfdisk"
38 #define VERSION "3.07"
43 #include <stdlib.h> /* atoi, free */
44 #include <stdarg.h> /* varargs */
45 #include <unistd.h> /* read, write */
46 #include <fcntl.h> /* O_RDWR */
47 #include <errno.h> /* ERANGE */
48 #include <string.h> /* index() */
51 #include <sys/ioctl.h>
53 #include <linux/unistd.h> /* _syscall */
54 #include <linux/hdreg.h> /* HDIO_GETGEO */
55 #include <linux/fs.h> /* BLKGETSIZE */
58 static const char sfdisk_usage[] =
59 "sfdisk [options] device ...\n"
60 "device: something like /dev/hda or /dev/sda\n"
62 " -s [or --show-size]: list size of a partition\n"
63 " -c [or --id]: print or change partition Id\n"
64 " -l [or --list]: list partitions of each device\n"
65 " -d [or --dump]: idem, but in a format suitable for later input\n"
66 " -i [or --increment]: number cylinders etc. from 1 instead of from 0\n"
67 " -uS, -uB, -uC, -uM: accept/report in units of sectors/blocks/cylinders/MB\n"
68 " -T [or --list-types]:list the known partition types\n"
69 " -D [or --DOS]: for DOS-compatibility: waste a little space\n"
70 " -R [or --re-read]: make kernel reread partition table\n"
71 " -N# : change only the partition with number #\n"
72 " -n : do not actually write to disk\n"
73 " -O file : save the sectors that will be overwritten to file\n"
74 " -I file : restore these sectors again\n"
75 " -v [or --version]: print version\n"
76 " -? [or --help]: print this message\n"
77 "dangerous options:\n"
78 " -g [or --show-geometry]: print the kernel's idea of the geometry\n"
79 " -x [or --show-extended]: also list extended partitions on output\n\n"
80 " or expect descriptors for them on input\n"
81 " -L [or --Linux]: do not complain about things irrelevant for Linux\n"
82 " -q [or --quiet]: suppress warning messages\n"
83 " You can override the detected geometry using:\n"
84 " -C# [or --cylinders #]:set the number of cylinders to use\n"
85 " -H# [or --heads #]: set the number of heads to use\n"
86 " -S# [or --sectors #]: set the number of sectors to use\n"
88 "You can disable all consistency checking with:\n"
89 " -f [or --force]: do what I say, even if it is stupid\n";
93 /* common stuff for fdisk, cfdisk, sfdisk */
99 static struct systypes i386_sys_types[] = {
102 {0x02, "XENIX root"},
104 {0x04, "FAT16 <32M"},
105 {0x05, "Extended"}, /* DOS 3.3+ extended partition */
106 {0x06, "FAT16"}, /* DOS 16-bit >=32M */
107 {0x07, "HPFS/NTFS"}, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
108 {0x08, "AIX"}, /* AIX boot (AIX -- PS/2 port or SplitDrive) */
109 {0x09, "AIX bootable"}, /* AIX data or Coherent */
110 {0x0a, "OS/2 Boot Manager"}, /* OS/2 Boot Manager */
111 {0x0b, "Win95 FAT32"},
112 {0x0c, "Win95 FAT32 (LBA)"}, /* LBA really is `Extended Int 13h' */
113 {0x0e, "Win95 FAT16 (LBA)"},
114 {0x0f, "Win95 Ext'd (LBA)"},
116 {0x11, "Hidden FAT12"},
117 {0x12, "Compaq diagnostics"},
118 {0x14, "Hidden FAT16 <32M"},
119 {0x16, "Hidden FAT16"},
120 {0x17, "Hidden HPFS/NTFS"},
121 {0x18, "AST Windows swapfile"},
122 {0x1b, "Hidden Win95 FAT32"},
123 {0x1c, "Hidden Win95 FAT32 (LBA)"},
124 {0x1e, "Hidden Win95 FAT16 (LBA)"},
126 {0x3c, "PartitionMagic recovery"},
127 {0x40, "Venix 80286"},
128 {0x41, "PPC PReP Boot"},
131 {0x4e, "QNX4.x 2nd part"},
132 {0x4f, "QNX4.x 3rd part"},
133 {0x50, "OnTrack DM"},
134 {0x51, "OnTrack DM6 Aux1"}, /* (or Novell) */
135 {0x52, "CP/M"}, /* CP/M or Microport SysV/AT */
136 {0x53, "OnTrack DM6 Aux3"},
137 {0x54, "OnTrackDM6"},
139 {0x56, "Golden Bow"},
140 {0x5c, "Priam Edisk"},
142 {0x63, "GNU HURD or SysV"}, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
143 {0x64, "Novell Netware 286"},
144 {0x65, "Novell Netware 386"},
145 {0x70, "DiskSecure Multi-Boot"},
147 {0x80, "Old Minix"}, /* Minix 1.4a and earlier */
148 {0x81, "Minix / old Linux"}, /* Minix 1.4b and later */
149 {0x82, "Linux swap"}, /* also Solaris */
151 {0x84, "OS/2 hidden C: drive"},
152 {0x85, "Linux extended"},
153 {0x86, "NTFS volume set"},
154 {0x87, "NTFS volume set"},
156 {0x94, "Amoeba BBT"}, /* (bad block table) */
157 {0xa0, "IBM Thinkpad hibernation"},
163 {0xc1, "DRDOS/sec (FAT-12)"},
164 {0xc4, "DRDOS/sec (FAT-16 < 32M)"},
165 {0xc6, "DRDOS/sec (FAT-16)"},
167 {0xdb, "CP/M / CTOS / ..."}, /* CP/M or Concurrent CP/M or Concurrent DOS or CTOS */
168 {0xe1, "DOS access"}, /* DOS access or SpeedStor 12-bit FAT extended partition */
169 {0xe3, "DOS R/O"}, /* DOS R/O or SpeedStor */
170 {0xe4, "SpeedStor"}, /* SpeedStor 16-bit FAT extended partition < 1024 cyl. */
173 {0xf4, "SpeedStor"}, /* SpeedStor large partition */
174 {0xf2, "DOS secondary"}, /* DOS 3.3+ secondary */
175 {0xfd, "Linux raid autodetect"}, /* New (2.2.x) raid partition with autodetect
176 using persistent superblock */
177 {0xfe, "LANstep"}, /* SpeedStor >1024 cyl. or LANstep */
178 {0xff, "BBT"}, /* Xenix Bad Block Table */
182 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
188 * C. About heads, sectors and cylinders
189 * D. About system Ids
190 * E. About partitions
191 * F. The standard input
192 * G. The command line
193 * H. Listing the current situation
194 * I. Writing the new situation
196 static int exit_status = 0;
198 static int force = 0; /* 1: do what I say, even if it is stupid ... */
199 static int quiet = 0; /* 1: suppress all warnings */
200 static int Linux = 0; /* 1: suppress warnings irrelevant for Linux */
201 static int DOS = 0; /* 1: shift extended partitions by #sectors, not 1 */
202 static int dump = 0; /* 1: list in a format suitable for later input */
203 static int verify = 0; /* 1: check that listed partition is reasonable */
204 static int no_write = 0; /* 1: do not actually write to disk */
205 static int no_reread = 0; /* 1: skip the BLKRRPART ioctl test at startup */
206 static int leave_last = 0; /* 1: don't allocate the last cylinder */
207 static int opt_list = 0;
208 static char *save_sector_file = NULL;
209 static char *restore_sector_file = NULL;
211 static void warn(char *s, ...)
218 vfprintf(stderr, s, p);
227 * sseek: seek to specified sector - return 0 on failure
229 * For >4GB disks lseek needs a > 32bit arg, and we have to use llseek.
230 * On the other hand, a 32 bit sector number is OK until 2TB.
231 * The routines _llseek and sseek below are the only ones that
232 * know about the loff_t type.
236 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
237 loff_t *, res, uint, wh);
240 static int sseek(char *dev, unsigned int fd, unsigned long s)
244 in = ((loff_t) s << 9);
248 if (_llseek(fd, in >> 32, in & 0xffffffff, &out, SEEK_SET) != 0) {
250 if ((out = lseek(fd, in, SEEK_SET)) != in) {
253 errorMsg("seek error on %s - cannot seek to %lu\n", dev, s, FALSE);
258 errorMsg("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n",
259 (uint) (in >> 32), (uint) (in & 0xffffffff),
260 (uint) (out >> 32), (uint) (out & 0xffffffff));
271 * We preserve all sectors read in a chain - some of these will
272 * have to be modified and written back.
276 unsigned long sectornumber;
281 static void free_sectors(void)
287 sectorhead = s->next;
292 static struct sector *get_sector(char *dev, int fd, unsigned long sno)
296 for (s = sectorhead; s; s = s->next)
297 if (s->sectornumber == sno)
300 if (!sseek(dev, fd, sno))
303 s = (struct sector *) xmalloc(sizeof(struct sector));
305 if (read(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
307 errorMsg("read error on %s - cannot read sector %lu\n", dev, sno);
312 s->next = sectorhead;
314 s->sectornumber = sno;
315 s->to_be_written = 0;
320 static int msdos_signature(struct sector *s)
322 if (*(unsigned short *) (s->data + 0x1fe) != 0xaa55) {
323 errorMsg("ERROR: sector %lu does not have an msdos signature\n",
330 static int write_sectors(char *dev, int fd)
334 for (s = sectorhead; s; s = s->next)
335 if (s->to_be_written) {
336 if (!sseek(dev, fd, s->sectornumber))
338 if (write(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
340 errorMsg("write error on %s - cannot write sector %lu\n",
341 dev, s->sectornumber);
344 s->to_be_written = 0;
349 static void ulong_to_chars(unsigned long u, char *uu)
353 for (i = 0; i < 4; i++) {
359 static unsigned long chars_to_ulong(unsigned char *uu)
364 for (i = 3; i >= 0; i--)
365 u = (u << 8) | uu[i];
369 static int save_sectors(char *dev, int fdin)
375 fdout = open(save_sector_file, O_WRONLY | O_CREAT, 0444);
377 perror(save_sector_file);
378 errorMsg("cannot open partition sector save file (%s)\n",
383 for (s = sectorhead; s; s = s->next)
384 if (s->to_be_written) {
385 ulong_to_chars(s->sectornumber, ss);
386 if (!sseek(dev, fdin, s->sectornumber))
388 if (read(fdin, ss + 4, 512) != 512) {
390 errorMsg("read error on %s - cannot read sector %lu\n",
391 dev, s->sectornumber);
394 if (write(fdout, ss, sizeof(ss)) != sizeof(ss)) {
396 errorMsg("write error on %s\n"), save_sector_file;
403 static void reread_disk_partition(char *dev, int fd);
405 static int restore_sectors(char *dev)
412 if (stat(restore_sector_file, &statbuf) < 0) {
413 perror(restore_sector_file);
414 errorMsg("cannot stat partition restore file (%s)\n",
415 restore_sector_file);
418 if (statbuf.st_size % 516) {
419 errorMsg("partition restore file has wrong size - not restoring\n");
422 if (!(ss = (char *) malloc(statbuf.st_size))) {
423 errorMsg("out of memory?\n");
426 fdin = open(restore_sector_file, O_RDONLY);
428 perror(restore_sector_file);
429 errorMsg("cannot open partition restore file (%s)\n",
430 restore_sector_file);
433 if (read(fdin, ss, statbuf.st_size) != statbuf.st_size) {
435 errorMsg("error reading %s\n"), restore_sector_file;
439 fdout = open(dev, O_WRONLY);
442 errorMsg("cannot open device %s for writing\n"), dev;
447 ct = statbuf.st_size / 516;
449 sno = chars_to_ulong(ss);
450 if (!sseek(dev, fdout, sno))
452 if (write(fdout, ss + 4, 512) != 512) {
454 errorMsg("error writing sector %lu on %s\n", sno, dev);
461 reread_disk_partition(dev, fdout);
467 * C. About heads, sectors and cylinders
471 * <linux/hdreg.h> defines HDIO_GETGEO and
472 * struct hd_geometry {
473 * unsigned char heads;
474 * unsigned char sectors;
475 * unsigned short cylinders;
476 * unsigned long start;
481 * We consider several geometries for a disk:
482 * B - the BIOS geometry, gotten from the kernel via HDIO_GETGEO
483 * F - the fdisk geometry
484 * U - the user-specified geometry
486 * 0 means unspecified / unknown
489 unsigned long cylindersize;
490 unsigned long heads, sectors, cylinders;
493 static void get_cylindersize(char *dev, int fd, int silent)
495 struct hd_geometry g;
498 B.heads = B.sectors = B.cylinders = 0;
500 if (!ioctl(fd, HDIO_GETGEO, &g)) {
504 B.sectors = g.sectors;
505 B.cylinders = g.cylinders;
511 B.sectors = U.sectors;
513 B.cylinders = U.cylinders;
515 B.cylindersize = B.heads * B.sectors;
518 if (g.start && !force) {
520 ("Warning: start=%d - this looks like a partition rather than\n"
521 "the entire disk. Using fdisk on it is probably meaningless.\n"
522 "[Use the --force option if you really want this]\n",
526 if (B.heads != g.heads)
527 warn("Warning: HDIO_GETGEO says that there are %d heads\n",
529 if (B.sectors != g.sectors)
530 warn("Warning: HDIO_GETGEO says that there are %d sectors\n",
532 if (B.cylinders != g.cylinders)
533 warn("Warning: HDIO_GETGEO says that there are %d cylinders\n",
536 if (!B.heads || !B.sectors || !B.cylinders)
537 printf("Disk %s: cannot get geometry\n", dev);
540 ("Warning: unlikely number of sectors (%d - usually at most 63\n"
541 "This will give problems with all software that uses C/H/S addressing.\n",
544 printf("\nDisk %s: %lu cylinders, %lu heads, %lu sectors/track\n",
545 dev, B.cylinders, B.heads, B.sectors);
549 unsigned char h, s, c;
550 } chs; /* has some c bits in s */
551 static chs zero_chs = { 0, 0, 0 };
554 unsigned long h, s, c;
556 static longchs zero_longchs;
558 static chs longchs_to_chs(longchs aa, struct geometry G)
562 if (aa.h < 256 && aa.s < 64 && aa.c < 1024) {
564 a.s = aa.s | ((aa.c >> 2) & 0xc0);
566 } else if (G.heads && G.sectors) {
568 a.s = G.sectors | 0xc0;
575 static longchs chs_to_longchs(chs a)
582 aa.c = (aa.c << 2) + a.c;
586 static longchs ulong_to_longchs(unsigned long sno, struct geometry G)
590 if (G.heads && G.sectors && G.cylindersize) {
591 aa.s = 1 + sno % G.sectors;
592 aa.h = (sno / G.sectors) % G.heads;
593 aa.c = sno / G.cylindersize;
600 //static unsigned long
601 //longchs_to_ulong (longchs aa, struct geometry G) {
602 // return (aa.c*G.cylindersize + aa.h*G.sectors + aa.s - 1);
605 static chs ulong_to_chs(unsigned long sno, struct geometry G)
607 return longchs_to_chs(ulong_to_longchs(sno, G), G);
610 //static unsigned long
611 //chs_to_ulong (chs a, struct geometry G) {
612 // return longchs_to_ulong(chs_to_longchs(a), G);
615 static int is_equal_chs(chs a, chs b)
617 return (a.h == b.h && a.s == b.s && a.c == b.c);
620 static int chs_ok(chs a, char *v, char *w)
622 longchs aa = chs_to_longchs(a);
625 if (is_equal_chs(a, zero_chs))
627 if (B.heads && aa.h >= B.heads) {
628 warn("%s of partition %s has impossible value for head: "
629 "%d (should be in 0-%d)\n", w, v, aa.h, B.heads - 1);
632 if (B.sectors && (aa.s == 0 || aa.s > B.sectors)) {
633 warn("%s of partition %s has impossible value for sector: "
634 "%d (should be in 1-%d)\n", w, v, aa.s, B.sectors);
637 if (B.cylinders && aa.c >= B.cylinders) {
638 warn("%s of partition %s has impossible value for cylinders: "
639 "%d (should be in 0-%d)\n", w, v, aa.c, B.cylinders - 1);
646 * D. About system Ids
649 #define EMPTY_PARTITION 0
650 #define EXTENDED_PARTITION 5
651 #define WIN98_EXTENDED 0x0f
652 #define DM6_AUX1PARTITION 0x51
653 #define DM6_AUX3PARTITION 0x53
654 #define DM6_PARTITION 0x54
655 #define EZD_PARTITION 0x55
656 #define LINUX_SWAP 0x82
657 #define LINUX_NATIVE 0x83
658 #define LINUX_EXTENDED 0x85
659 #define BSD_PARTITION 0xa5
661 /* List of partition types now in i386_sys_types.c */
663 static const char *sysname(unsigned char type)
667 for (s = i386_sys_types; s->name; s++)
673 static void list_types(void)
677 printf("Id Name\n\n");
678 for (s = i386_sys_types; s->name; s++)
679 printf("%2x %s\n", s->type, s->name);
682 static int is_extended(unsigned char type)
684 return (type == EXTENDED_PARTITION
685 || type == LINUX_EXTENDED || type == WIN98_EXTENDED);
688 static int is_bsd(unsigned char type)
690 return (type == BSD_PARTITION);
694 * E. About partitions
697 /* MS/DOS partition */
700 unsigned char bootable; /* 0 or 0x80 */
702 unsigned char sys_type;
704 unsigned int start_sect; /* starting sector counting from 0 */
705 unsigned int nr_sects; /* nr of sectors in partition */
708 /* Unfortunately, partitions are not aligned, and non-Intel machines
709 are unhappy with non-aligned integers. So, we need a copy by hand. */
710 static int copy_to_int(unsigned char *cp)
721 static void copy_from_int(int m, char *cp)
732 static void copy_to_part(char *cp, struct partition *p)
735 p->begin_chs.h = *cp++;
736 p->begin_chs.s = *cp++;
737 p->begin_chs.c = *cp++;
739 p->end_chs.h = *cp++;
740 p->end_chs.s = *cp++;
741 p->end_chs.c = *cp++;
742 p->start_sect = copy_to_int(cp);
743 p->nr_sects = copy_to_int(cp + 4);
746 static void copy_from_part(struct partition *p, char *cp)
749 *cp++ = p->begin_chs.h;
750 *cp++ = p->begin_chs.s;
751 *cp++ = p->begin_chs.c;
753 *cp++ = p->end_chs.h;
754 *cp++ = p->end_chs.s;
755 *cp++ = p->end_chs.c;
756 copy_from_int(p->start_sect, cp);
757 copy_from_int(p->nr_sects, cp + 4);
760 /* Roughly speaking, Linux doesn't use any of the above fields except
761 for partition type, start sector and number of sectors. (However,
762 see also linux/drivers/scsi/fdomain.c.)
763 The only way partition type is used (in the kernel) is the comparison
764 for equality with EXTENDED_PARTITION (and these Disk Manager types). */
769 unsigned long sector, offset; /* disk location of this info */
771 struct part_desc *ep; /* extended partition containing this one */
777 struct part_desc *outer_extended_partition(struct part_desc *p)
784 static int is_parent(struct part_desc *pp, struct part_desc *p)
795 struct part_desc partitions[128];
799 /* determine where on the disk this information goes */
800 static void add_sector_and_offset(struct disk_desc *z)
805 for (pno = 0; pno < z->partno; pno++) {
806 p = &(z->partitions[pno]);
807 p->offset = 0x1be + (pno % 4) * sizeof(struct partition);
809 p->sector = (p->ep ? p->ep->start : 0);
813 /* tell the kernel to reread the partition tables */
814 static int reread_ioctl(int fd)
816 if (ioctl(fd, BLKRRPART)) {
823 static int is_blockdev(int fd)
827 return (fstat(fd, &statbuf) == 0 && S_ISBLK(statbuf.st_mode));
830 /* reread after writing */
831 static void reread_disk_partition(char *dev, int fd)
833 printf("Re-reading the partition table ...\n");
836 sleep(3); /* superfluous since 1.3.20 */
838 if (reread_ioctl(fd) && is_blockdev(fd))
839 printf("The command to re-read the partition table failed\n"
840 "Reboot your system now, before using mkfs\n");
844 printf("Error closing %s\n", dev);
849 /* find Linux name of this partition, assuming that it will have a name */
850 static int index_to_linux(int pno, struct disk_desc *z)
853 struct part_desc *p = &(z->partitions[0]);
855 for (i = 0; i < pno; i++, p++)
856 if (i < 4 || (p->size > 0 && !is_extended(p->p.sys_type)))
861 static int linux_to_index(int lpno, struct disk_desc *z)
864 struct part_desc *p = &(z->partitions[0]);
866 for (i = 0; i < z->partno && ct < lpno; i++, p++)
867 if ((i < 4 || (p->size > 0 && !is_extended(p->p.sys_type)))
873 static int asc_to_index(char *pnam, struct disk_desc *z)
878 pno = atoi(pnam + 1);
881 pno = linux_to_index(pnum, z);
883 if (!(pno >= 0 && pno < z->partno))
884 fatalError("%s: no such partition\n"), pnam;
889 * List partitions - in terms of sectors, blocks or cylinders
896 static int default_format = F_MEGABYTE;
897 static int specified_format = 0;
898 static int show_extended = 0;
899 static int one_only = 0;
900 static int one_only_pno;
901 static int increment = 0;
903 static void set_format(char c)
907 printf("unrecognized format - using sectors\n");
909 specified_format = F_SECTOR;
912 specified_format = F_BLOCK;
915 specified_format = F_CYLINDER;
918 specified_format = F_MEGABYTE;
923 static unsigned long unitsize(int format)
925 default_format = (B.cylindersize ? F_CYLINDER : F_MEGABYTE);
926 if (!format && !(format = specified_format))
927 format = default_format;
933 return B.cylindersize;
943 static unsigned long get_disksize(int format)
945 unsigned long cs = B.cylinders;
947 if (cs && leave_last)
949 return (cs * B.cylindersize) / unitsize(format);
952 static void out_partition_header(char *dev, int format, struct geometry G)
955 printf("# partition table of %s\n", dev);
956 printf("unit: sectors\n\n");
960 default_format = (G.cylindersize ? F_CYLINDER : F_MEGABYTE);
961 if (!format && !(format = specified_format))
962 format = default_format;
966 printf("unimplemented format - using %s\n",
967 G.cylindersize ? "cylinders" : "sectors");
969 if (G.cylindersize) {
970 printf("Units = cylinders of %lu bytes, blocks of 1024 bytes"
971 ", counting from %d\n\n",
972 G.cylindersize << 9, increment);
974 (" Device Boot Start End #cyls #blocks Id System\n");
979 printf("Units = sectors of 512 bytes, counting from %d\n\n",
982 (" Device Boot Start End #sectors Id System\n");
985 printf("Units = blocks of 1024 bytes, counting from %d\n\n",
988 (" Device Boot Start End #blocks Id System\n");
991 printf("Units = megabytes of 1048576 bytes, blocks of 1024 bytes"
992 ", counting from %d\n\n", increment);
994 (" Device Boot Start End MB #blocks Id System\n");
1000 out_rounddown(int width, unsigned long n, unsigned long unit, int inc)
1002 printf("%*lu", width, inc + n / unit);
1004 putchar((n % unit) ? '+' : ' ');
1009 out_roundup(int width, unsigned long n, unsigned long unit, int inc)
1011 if (n == (unsigned long) (-1))
1012 printf("%*s", width, "-");
1014 printf("%*lu", width, inc + n / unit);
1016 putchar(((n + 1) % unit) ? '-' : ' ');
1021 out_roundup_size(int width, unsigned long n, unsigned long unit)
1023 printf("%*lu", width, (n + unit - 1) / unit);
1025 putchar((n % unit) ? '-' : ' ');
1029 static int get_fdisk_geometry(struct part_desc *p)
1031 chs b = p->p.end_chs;
1032 longchs bb = chs_to_longchs(b);
1036 F.cylindersize = F.heads * F.sectors;
1037 return (F.sectors != B.sectors || F.heads != B.heads);
1041 out_partition(char *dev, int format, struct part_desc *p,
1042 struct disk_desc *z, struct geometry G)
1044 unsigned long start, end, size;
1047 if (!format && !(format = specified_format))
1048 format = default_format;
1050 pno = p - &(z->partitions[0]); /* our index */
1051 lpno = index_to_linux(pno, z); /* name of next one that has a name */
1052 if (pno == linux_to_index(lpno, z)) /* was that us? */
1053 printf("%8s%-2u", dev, lpno); /* yes */
1054 else if (show_extended)
1058 putchar(dump ? ':' : ' ');
1061 end = p->start + p->size - 1;
1065 printf(" start=%9lu", start);
1066 printf(", size=%8lu", size);
1067 if (p->ptype == DOS_TYPE) {
1068 printf(", Id=%2x", p->p.sys_type);
1069 if (p->p.bootable == 0x80)
1070 printf(", bootable");
1076 if (p->ptype != DOS_TYPE || p->p.bootable == 0)
1078 else if (p->p.bootable == 0x80)
1081 printf(" ? "); /* garbage */
1085 if (G.cylindersize) {
1086 out_rounddown(6, start, G.cylindersize, increment);
1087 out_roundup(6, end, G.cylindersize, increment);
1088 out_roundup_size(6, size, G.cylindersize);
1089 out_rounddown(8, size, 2, 0);
1095 out_rounddown(9, start, 1, increment);
1096 out_roundup(9, end, 1, increment);
1097 out_rounddown(9, size, 1, 0);
1101 printf("%8lu,%3lu ",
1102 p->sector / 2, ((p->sector & 1) ? 512 : 0) + p->offset);
1104 out_rounddown(8, start, 2, increment);
1105 out_roundup(8, end, 2, increment);
1106 out_rounddown(8, size, 2, 0);
1109 out_rounddown(5, start, 2048, increment);
1110 out_roundup(5, end, 2048, increment);
1111 out_roundup_size(5, size, 2048);
1112 out_rounddown(8, size, 2, 0);
1115 if (p->ptype == DOS_TYPE) {
1116 printf(" %2x %s\n", p->p.sys_type, sysname(p->p.sys_type));
1121 /* Is chs as we expect? */
1122 if (!quiet && p->ptype == DOS_TYPE) {
1126 a = (size ? ulong_to_chs(start, G) : zero_chs);
1128 aa = chs_to_longchs(a);
1129 bb = chs_to_longchs(b);
1130 if (a.s && !is_equal_chs(a, b))
1132 ("\t\tstart: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n",
1133 aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1134 a = (size ? ulong_to_chs(end, G) : zero_chs);
1136 aa = chs_to_longchs(a);
1137 bb = chs_to_longchs(b);
1138 if (a.s && !is_equal_chs(a, b))
1140 ("\t\tend: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n",
1141 aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1142 if (G.cylinders && G.cylinders < 1024 && bb.c > G.cylinders)
1144 ("partition ends on cylinder %ld, beyond the end of the disk\n",
1149 static void out_partitions(char *dev, struct disk_desc *z)
1151 struct part_desc *p;
1152 int pno, format = 0;
1155 printf("No partitions found\n");
1157 for (pno = 0; pno < z->partno; pno++) {
1158 p = &(z->partitions[pno]);
1159 if (p->size != 0 && p->p.sys_type != 0) {
1160 if (get_fdisk_geometry(p))
1162 ("Warning: The first partition looks like it was made\n"
1163 " for C/H/S=*/%ld/%ld (instead of %ld/%ld/%ld).\n"
1164 "For this listing I'll assume that geometry.\n",
1165 F.heads, F.sectors, B.cylinders, B.heads,
1170 out_partition_header(dev, format, F);
1171 for (pno = 0; pno < z->partno; pno++) {
1172 out_partition(dev, format, &(z->partitions[pno]), z, F);
1173 if (show_extended && pno % 4 == 3)
1179 static int disj(struct part_desc *p, struct part_desc *q)
1181 return ((p->start + p->size <= q->start)
1182 || (is_extended(p->p.sys_type)
1183 && q->start + q->size <= p->start + p->size));
1186 static char *pnumber(struct part_desc *p, struct disk_desc *z)
1188 static char buf[20];
1190 struct part_desc *p0 = &(z->partitions[0]);
1192 this = index_to_linux(p - p0, z);
1193 next = index_to_linux(p - p0 + 1, z);
1196 sprintf(buf, "%d", this);
1198 sprintf(buf, "[%d]", this);
1202 static int partitions_ok(struct disk_desc *z)
1204 struct part_desc *partitions = &(z->partitions[0]), *p, *q;
1205 int partno = z->partno;
1207 #define PNO(p) pnumber(p, z)
1209 /* Have at least 4 partitions been defined? */
1212 fatalError("no partition table present.\n");
1214 fatalError("strange, only %d partitions defined.\n"), partno;
1218 /* Are the partitions of size 0 marked empty?
1219 And do they have start = 0? And bootable = 0? */
1220 for (p = partitions; p - partitions < partno; p++)
1222 if (p->p.sys_type != EMPTY_PARTITION)
1224 ("Warning: partition %s has size 0 but is not marked Empty\n",
1226 else if (p->p.bootable != 0)
1227 warn("Warning: partition %s has size 0 and is bootable\n",
1229 else if (p->p.start_sect != 0)
1231 ("Warning: partition %s has size 0 and nonzero start\n",
1233 /* all this is probably harmless, no error return */
1236 /* Are the logical partitions contained in their extended partitions? */
1237 for (p = partitions + 4; p < partitions + partno; p++)
1238 if (p->ptype == DOS_TYPE)
1239 if (p->size && !is_extended(p->p.sys_type)) {
1241 if (p->start < q->start
1242 || p->start + p->size > q->start + q->size) {
1243 warn("Warning: partition %s "), PNO(p);
1244 warn("is not contained in partition %s\n"), PNO(q);
1249 /* Are the data partitions mutually disjoint? */
1250 for (p = partitions; p < partitions + partno; p++)
1251 if (p->size && !is_extended(p->p.sys_type))
1252 for (q = p + 1; q < partitions + partno; q++)
1253 if (q->size && !is_extended(q->p.sys_type))
1254 if (!((p->start > q->start) ? disj(q, p) : disj(p, q))) {
1255 warn("Warning: partitions %s "), PNO(p);
1256 warn("and %s overlap\n"), PNO(q);
1260 /* Are the data partitions and the extended partition
1261 table sectors disjoint? */
1262 for (p = partitions; p < partitions + partno; p++)
1263 if (p->size && !is_extended(p->p.sys_type))
1264 for (q = partitions; q < partitions + partno; q++)
1265 if (is_extended(q->p.sys_type))
1266 if (p->start <= q->start
1267 && p->start + p->size > q->start) {
1268 warn("Warning: partition %s contains part of ",
1270 warn("the partition table (sector %lu),\n",
1272 warn("and will destroy it when filled\n");
1276 /* Do they start past zero and end before end-of-disk? */
1278 unsigned long ds = get_disksize(F_SECTOR);
1280 for (p = partitions; p < partitions + partno; p++)
1282 if (p->start == 0) {
1283 warn("Warning: partition %s starts at sector 0\n",
1287 if (p->size && p->start + p->size > ds) {
1289 ("Warning: partition %s extends past end of disk\n",
1296 /* At most one chain of DOS extended partitions ? */
1297 /* It seems that the OS/2 fdisk has the additional requirement
1298 that the extended partition must be the fourth one */
1302 for (p = partitions; p < partitions + 4; p++)
1303 if (p->p.sys_type == EXTENDED_PARTITION)
1305 if (ect > 1 && !Linux) {
1307 ("Among the primary partitions, at most one can be extended\n");
1308 warn(" (although this is not a problem under Linux)\n");
1314 * Do all partitions start at a cylinder boundary ?
1315 * (this is not required for Linux)
1316 * The first partition starts after MBR.
1317 * Logical partitions start slightly after the containing extended partn.
1319 if (B.cylindersize) {
1320 for (p = partitions; p < partitions + partno; p++)
1322 if (p->start % B.cylindersize != 0
1324 || p->start / B.cylindersize !=
1325 p->ep->start / B.cylindersize)
1326 && (p->p.start_sect >= B.cylindersize)) {
1327 warn("Warning: partition %s does not start "
1328 "at a cylinder boundary\n", PNO(p));
1332 if ((p->start + p->size) % B.cylindersize) {
1333 warn("Warning: partition %s does not end "
1334 "at a cylinder boundary\n", PNO(p));
1341 /* Usually, one can boot only from primary partitions. */
1342 /* In fact, from a unique one only. */
1343 /* do not warn about bootable extended partitions -
1344 often LILO is there */
1348 for (p = partitions; p < partitions + partno; p++)
1349 if (p->p.bootable) {
1351 pno = p - partitions;
1352 else if (p - partitions < 4) {
1354 ("Warning: more than one primary partition is marked "
1355 "bootable (active)\n"
1356 "This does not matter for LILO, but the DOS MBR will "
1357 "not boot this disk.\n");
1360 if (p - partitions >= 4) {
1362 ("Warning: usually one can boot from primary partitions "
1363 "only\nLILO disregards the `bootable' flag.\n");
1367 if (pno == -1 || pno >= 4)
1369 ("Warning: no primary partition is marked bootable (active)\n"
1370 "This does not matter for LILO, but the DOS MBR will "
1371 "not boot this disk.\n");
1374 /* Is chs as we expect? */
1375 for (p = partitions; p < partitions + partno; p++)
1376 if (p->ptype == DOS_TYPE) {
1380 a = p->size ? ulong_to_chs(p->start, B) : zero_chs;
1382 aa = chs_to_longchs(a);
1383 bb = chs_to_longchs(b);
1384 if (!chs_ok(b, PNO(p), "start"))
1386 if (a.s && !is_equal_chs(a, b))
1388 ("partition %s: start: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n",
1389 PNO(p), aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1391 p->size ? ulong_to_chs(p->start + p->size - 1,
1394 aa = chs_to_longchs(a);
1395 bb = chs_to_longchs(b);
1396 if (!chs_ok(b, PNO(p), "end"))
1398 if (a.s && !is_equal_chs(a, b))
1400 ("partition %s: end: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n",
1401 PNO(p), aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1402 if (B.cylinders && B.cylinders < 1024 && bb.c > B.cylinders)
1404 ("partition %s ends on cylinder %ld, beyond the end of the disk\n",
1414 extended_partition(char *dev, int fd, struct part_desc *ep,
1415 struct disk_desc *z)
1419 unsigned long start, here, next;
1420 int i, moretodo = 1;
1422 struct part_desc *partitions = &(z->partitions[0]);
1423 int pno = z->partno;
1425 here = start = ep->start;
1430 if (!(s = get_sector(dev, fd, here)))
1433 if (!msdos_signature(s))
1436 cp = s->data + 0x1be;
1438 if (pno + 4 >= SIZE(z->partitions)) {
1439 printf("too many partitions - ignoring those past nr (%d)\n",
1446 for (i = 0; i < 4; i++, cp += sizeof(struct partition)) {
1447 partitions[pno].sector = here;
1448 partitions[pno].offset = cp - s->data;
1449 partitions[pno].ep = ep;
1450 copy_to_part(cp, &p);
1451 if (is_extended(p.sys_type)) {
1452 partitions[pno].start = start + p.start_sect;
1454 printf("tree of partitions?\n");
1456 next = partitions[pno].start; /* follow `upper' branch */
1459 partitions[pno].start = here + p.start_sect;
1461 partitions[pno].size = p.nr_sects;
1462 partitions[pno].ptype = DOS_TYPE;
1463 partitions[pno].p = p;
1472 #define BSD_DISKMAGIC (0x82564557UL)
1473 #define BSD_MAXPARTITIONS 8
1474 #define BSD_FS_UNUSED 0
1475 typedef unsigned char u8;
1476 typedef unsigned short u16;
1477 typedef unsigned int u32;
1478 struct bsd_disklabel {
1481 char d_typename[16];
1482 char d_packname[16];
1486 u16 d_npartitions; /* number of partitions in following */
1488 struct bsd_partition { /* the partition table */
1489 u32 p_size; /* number of sectors in partition */
1490 u32 p_offset; /* starting sector */
1491 u32 p_fsize; /* filesystem basic fragment size */
1492 u8 p_fstype; /* filesystem type, see below */
1493 u8 p_frag; /* filesystem fragments per block */
1494 u16 p_cpg; /* filesystem cylinders per group */
1495 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
1499 bsd_partition(char *dev, int fd, struct part_desc *ep, struct disk_desc *z)
1501 struct bsd_disklabel *l;
1502 struct bsd_partition *bp, *bp0;
1503 unsigned long start = ep->start;
1505 struct part_desc *partitions = &(z->partitions[0]);
1506 int pno = z->partno;
1508 if (!(s = get_sector(dev, fd, start + 1)))
1510 l = (struct bsd_disklabel *) (s->data);
1511 if (l->d_magic != BSD_DISKMAGIC)
1514 bp = bp0 = &l->d_partitions[0];
1515 while (bp - bp0 <= BSD_MAXPARTITIONS) {
1516 if (pno + 1 >= SIZE(z->partitions)) {
1517 printf("too many partitions - ignoring those "
1518 "past nr (%d)\n", pno - 1);
1521 if (bp->p_fstype != BSD_FS_UNUSED) {
1522 partitions[pno].start = bp->p_offset;
1523 partitions[pno].size = bp->p_size;
1524 partitions[pno].sector = start + 1;
1525 partitions[pno].offset = (char *) bp - (char *) bp0;
1526 partitions[pno].ep = 0;
1527 partitions[pno].ptype = BSD_TYPE;
1536 msdos_partition(char *dev, int fd, unsigned long start,
1537 struct disk_desc *z)
1541 struct partition pt;
1543 struct part_desc *partitions = &(z->partitions[0]);
1544 int pno = z->partno;
1546 if (!(s = get_sector(dev, fd, start)))
1549 if (!msdos_signature(s))
1552 cp = s->data + 0x1be;
1553 copy_to_part(cp, &pt);
1555 /* If I am not mistaken, recent kernels will hide this from us,
1556 so we will never actually see traces of a Disk Manager */
1557 if (pt.sys_type == DM6_PARTITION
1558 || pt.sys_type == EZD_PARTITION
1559 || pt.sys_type == DM6_AUX1PARTITION
1560 || pt.sys_type == DM6_AUX3PARTITION) {
1561 printf("detected Disk Manager - unable to handle that\n");
1565 unsigned int sig = *(unsigned short *) (s->data + 2);
1568 && *(unsigned short *) (s->data + sig) == 0x55aa
1569 && (1 & *(unsigned char *) (s->data + sig + 2))) {
1570 printf("DM6 signature found - giving up\n");
1575 for (pno = 0; pno < 4; pno++, cp += sizeof(struct partition)) {
1576 partitions[pno].sector = start;
1577 partitions[pno].offset = cp - s->data;
1578 copy_to_part(cp, &pt);
1579 partitions[pno].start = start + pt.start_sect;
1580 partitions[pno].size = pt.nr_sects;
1581 partitions[pno].ep = 0;
1582 partitions[pno].p = pt;
1587 for (i = 0; i < 4; i++) {
1588 if (is_extended(partitions[i].p.sys_type)) {
1589 if (!partitions[i].size) {
1590 printf("strange..., an extended partition of size 0?\n");
1593 extended_partition(dev, fd, &partitions[i], z);
1595 if (is_bsd(partitions[i].p.sys_type)) {
1596 if (!partitions[i].size) {
1597 printf("strange..., a BSD partition of size 0?\n");
1600 bsd_partition(dev, fd, &partitions[i], z);
1607 osf_partition(char *dev, int fd, unsigned long start, struct disk_desc *z)
1613 sun_partition(char *dev, int fd, unsigned long start, struct disk_desc *z)
1619 amiga_partition(char *dev, int fd, unsigned long start,
1620 struct disk_desc *z)
1625 static void get_partitions(char *dev, int fd, struct disk_desc *z)
1629 if (!msdos_partition(dev, fd, 0, z)
1630 && !osf_partition(dev, fd, 0, z)
1631 && !sun_partition(dev, fd, 0, z)
1632 && !amiga_partition(dev, fd, 0, z)) {
1633 printf(" %s: unrecognized partition\n", dev);
1638 static int write_partitions(char *dev, int fd, struct disk_desc *z)
1641 struct part_desc *partitions = &(z->partitions[0]), *p;
1642 int pno = z->partno;
1645 printf("-n flag was given: Nothing changed\n");
1649 for (p = partitions; p < partitions + pno; p++) {
1650 s = get_sector(dev, fd, p->sector);
1653 s->to_be_written = 1;
1654 copy_from_part(&(p->p), s->data + p->offset);
1655 *(unsigned short *) (&(s->data[0x1fe])) = 0xaa55;
1657 if (save_sector_file) {
1658 if (!save_sectors(dev, fd)) {
1659 fatalError("Failed saving the old sectors - aborting\n");
1663 if (!write_sectors(dev, fd)) {
1664 errorMsg("Failed writing the partition on %s\n"), dev;
1671 * F. The standard input
1676 * <start> <size> <type> <bootable> <c,h,s> <c,h,s>
1677 * Fields are separated by whitespace or comma or semicolon possibly
1678 * followed by whitespace; initial and trailing whitespace is ignored.
1679 * Numbers can be octal, decimal or hexadecimal, decimal is default
1680 * The <c,h,s> parts can (and probably should) be omitted.
1681 * Bootable is specified as [*|-], with as default not-bootable.
1682 * Type is given in hex, without the 0x prefix, or is [E|S|L|X], where
1683 * L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), and E
1684 * is EXTENDED_PARTITION (5), X is LINUX_EXTENDED (85).
1685 * The default value of start is the first nonassigned sector/cylinder/...
1686 * The default value of size is as much as possible (until next
1687 * partition or end-of-disk).
1688 * .: end of chain of extended partitions.
1690 * On interactive input an empty line means: all defaults.
1691 * Otherwise empty lines are ignored.
1694 static int eof, eob;
1705 3, "bootable", 1}, {
1715 * Read a line, split it into fields
1717 * (some primitive handwork, but a more elaborate parser seems
1724 read_stdin(unsigned char **fields, unsigned char *line, int fieldssize,
1727 unsigned char *lp, *ip;
1730 /* boolean true and empty string at start */
1733 for (fno = 0; fno < fieldssize; fno++)
1734 fields[fno] = line + 1;
1737 /* read a line from stdin */
1738 lp = fgets(line + 2, linesize, stdin);
1743 if (!(lp = index(lp, '\n')))
1744 fatalError("long or incomplete input line - quitting\n");
1747 /* remove comments, if any */
1748 if ((lp = index(line + 2, '#')) != 0)
1751 /* recognize a few commands - to be expanded */
1752 if (!strcmp(line + 2, "unit: sectors")) {
1753 specified_format = F_SECTOR;
1757 /* dump style? - then bad input is fatal */
1758 if ((ip = index(line + 2, ':')) != 0) {
1763 while (isspace(*ip))
1767 for (d = dumpflds; d - dumpflds < SIZE(dumpflds); d++) {
1768 if (!strncmp(ip, d->fldname, strlen(d->fldname))) {
1769 ip += strlen(d->fldname);
1770 while (isspace(*ip))
1773 fields[d->fldno] = line;
1774 else if (*ip == '=') {
1775 while (isspace(*++ip));
1776 fields[d->fldno] = ip;
1777 while (isalnum(*ip)) /* 0x07FF */
1780 fatalError("input error: `=' expected after %s field\n",
1782 if (fno <= d->fldno)
1786 if (*ip != ',' && *ip != ';')
1788 ("input error: unexpected character %c after %s field\n",
1794 fatalError("unrecognized input: %s\n"), ip;
1797 /* split line into fields */
1800 while ((c = *ip++) != 0) {
1801 if (!lp[-1] && (c == '\t' || c == ' '));
1802 else if (c == '\t' || c == ' ' || c == ',' || c == ';') {
1804 if (fno < fieldssize)
1811 if (lp == fields[fno - 1])
1816 /* read a number, use default if absent */
1817 static int get_ul(char *u, unsigned long *up, unsigned long def, int base)
1823 *up = strtoul(u, &nu, base);
1824 if (errno == ERANGE) {
1825 printf("number too big\n");
1829 printf("trailing junk after number\n");
1837 /* There are two common ways to structure extended partitions:
1838 as nested boxes, and as a chain. Sometimes the partitions
1839 must be given in order. Sometimes all logical partitions
1840 must lie inside the outermost extended partition.
1841 NESTED: every partition is contained in the surrounding partitions
1842 and is disjoint from all others.
1843 CHAINED: every data partition is contained in the surrounding partitions
1844 and disjoint from all others, but extended partitions may lie outside
1845 (insofar as allowed by all_logicals_inside_outermost_extended).
1846 ONESECTOR: all data partitions are mutually disjoint; extended partitions
1847 each use one sector only (except perhaps for the outermost one).
1849 static int partitions_in_order = 0;
1850 static int all_logicals_inside_outermost_extended = 1;
1851 static enum { NESTED, CHAINED, ONESECTOR } boxes = NESTED;
1853 /* find the default value for <start> - assuming entire units */
1854 static unsigned long
1855 first_free(int pno, int is_extended, struct part_desc *ep, int format,
1856 unsigned long mid, struct disk_desc *z)
1858 unsigned long ff, fff;
1859 unsigned long unit = unitsize(format);
1860 struct part_desc *partitions = &(z->partitions[0]), *pp = 0;
1862 /* if containing ep undefined, look at its container */
1863 if (ep && ep->p.sys_type == EMPTY_PARTITION)
1867 if (boxes == NESTED || (boxes == CHAINED && !is_extended))
1869 else if (all_logicals_inside_outermost_extended)
1870 pp = outer_extended_partition(ep);
1873 ff = pp ? (pp->start + unit - 1) / unit : 0;
1875 /* rounding up wastes almost an entire cylinder - round down
1876 and leave it to compute_start_sect() to fix the difference */
1877 ff = pp ? pp->start / unit : 0;
1879 /* MBR and 1st sector of an extended partition are never free */
1884 for (pp = partitions; pp < partitions + pno; pp++) {
1885 if (!is_parent(pp, ep) && pp->size > 0) {
1886 if ((partitions_in_order || pp->start / unit <= ff
1887 || (mid && pp->start / unit <= mid))
1888 && (fff = (pp->start + pp->size + unit - 1) / unit) > ff) {
1898 /* find the default value for <size> - assuming entire units */
1899 static unsigned long
1900 max_length(int pno, int is_extended, struct part_desc *ep, int format,
1901 unsigned long start, struct disk_desc *z)
1904 unsigned long unit = unitsize(format);
1905 struct part_desc *partitions = &(z->partitions[0]), *pp = 0;
1907 /* if containing ep undefined, look at its container */
1908 if (ep && ep->p.sys_type == EMPTY_PARTITION)
1912 if (boxes == NESTED || (boxes == CHAINED && !is_extended))
1914 else if (all_logicals_inside_outermost_extended)
1915 pp = outer_extended_partition(ep);
1917 fu = pp ? (pp->start + pp->size) / unit : get_disksize(format);
1919 for (pp = partitions; pp < partitions + pno; pp++)
1920 if (!is_parent(pp, ep) && pp->size > 0
1921 && pp->start / unit >= start && pp->start / unit < fu)
1922 fu = pp->start / unit;
1924 return (fu > start) ? fu - start : 0;
1927 /* compute starting sector of a partition inside an extended one */
1928 /* ep is 0 or points to surrounding extended partition */
1929 static int compute_start_sect(struct part_desc *p, struct part_desc *ep)
1932 int inc = (DOS && B.sectors) ? B.sectors : 1;
1935 if (ep && p->start + p->size >= ep->start + 1)
1936 delta = p->start - ep->start - inc;
1937 else if (p->start == 0 && p->size > 0)
1944 if (is_extended(p->p.sys_type) && boxes == ONESECTOR)
1946 else if ((int) (p->size) <= 0) {
1947 warn("no room for partition descriptor\n");
1952 : (is_extended(p->p.sys_type) ?
1953 outer_extended_partition(ep) : ep)->start);
1955 if (p->p.sys_type == EMPTY_PARTITION && p->size == 0) {
1956 p->p.start_sect = 0;
1957 p->p.begin_chs = zero_chs;
1958 p->p.end_chs = zero_chs;
1960 p->p.start_sect = p->start - base;
1961 p->p.begin_chs = ulong_to_chs(p->start, B);
1962 p->p.end_chs = ulong_to_chs(p->start + p->size - 1, B);
1964 p->p.nr_sects = p->size;
1968 /* build the extended partition surrounding a given logical partition */
1970 build_surrounding_extended(struct part_desc *p, struct part_desc *ep,
1971 struct disk_desc *z)
1973 int inc = (DOS && B.sectors) ? B.sectors : 1;
1974 int format = F_SECTOR;
1975 struct part_desc *p0 = &(z->partitions[0]), *eep = ep->ep;
1977 if (boxes == NESTED) {
1978 ep->start = first_free(ep - p0, 1, eep, format, p->start, z);
1979 ep->size = max_length(ep - p0, 1, eep, format, ep->start, z);
1980 if (ep->start > p->start
1981 || ep->start + ep->size < p->start + p->size) {
1982 warn("cannot build surrounding extended partition\n");
1986 ep->start = p->start;
1987 if (boxes == CHAINED)
1993 ep->p.nr_sects = ep->size;
1995 ep->p.sys_type = EXTENDED_PARTITION;
1996 if (!compute_start_sect(ep, eep) || !compute_start_sect(p, ep)) {
1997 ep->p.sys_type = EMPTY_PARTITION;
2006 read_line(int pno, struct part_desc *ep, char *dev, int interactive,
2007 struct disk_desc *z)
2009 unsigned char line[1000];
2010 unsigned char *fields[11];
2011 int fno, pct = pno % 4;
2012 struct part_desc p, *orig;
2013 unsigned long ff, ff1, ul, ml, ml1, def;
2014 int format, lpno, is_extd;
2019 lpno = index_to_linux(pno, z);
2022 if (pct == 0 && (show_extended || pno == 0))
2024 warn("%8s%d: ", dev, lpno);
2027 /* read input line - skip blank lines when reading from a file */
2029 fno = read_stdin(fields, line, SIZE(fields), SIZE(line));
2030 } while (fno == RD_CMD || (fno == 0 && !interactive));
2031 if (fno == RD_EOF) {
2033 } else if (fno > 10 && *(fields[10]) != 0) {
2034 printf("too many input fields\n");
2038 if (fno == 1 && !strcmp(fields[0], ".")) {
2043 /* use specified format, but round to cylinders if F_MEGABYTE specified */
2045 if (B.cylindersize && specified_format == F_MEGABYTE)
2046 format = F_CYLINDER;
2048 orig = (one_only ? &(oldp.partitions[pno]) : 0);
2053 /* first read the type - we need to know whether it is extended */
2054 /* stop reading when input blank (defaults) and all is full */
2056 if (fno == 0) { /* empty line */
2057 if (orig && is_extended(orig->p.sys_type))
2059 ff = first_free(pno, is_extd, ep, format, 0, z);
2060 ml = max_length(pno, is_extd, ep, format, ff, z);
2061 if (ml == 0 && is_extd == 0) {
2063 ff = first_free(pno, is_extd, ep, format, 0, z);
2064 ml = max_length(pno, is_extd, ep, format, ff, z);
2066 if (ml == 0 && pno >= 4) {
2067 /* no free blocks left - don't read any further */
2068 warn("No room for more\n");
2072 if (fno < 3 || !*(fields[2]))
2073 ul = orig ? orig->p.sys_type :
2074 (is_extd || (pno > 3 && pct == 1 && show_extended))
2075 ? EXTENDED_PARTITION : LINUX_NATIVE;
2076 else if (!strcmp(fields[2], "L"))
2078 else if (!strcmp(fields[2], "S"))
2080 else if (!strcmp(fields[2], "E"))
2081 ul = EXTENDED_PARTITION;
2082 else if (!strcmp(fields[2], "X"))
2083 ul = LINUX_EXTENDED;
2084 else if (get_ul(fields[2], &ul, LINUX_NATIVE, 16))
2087 warn("Illegal type\n");
2091 is_extd = is_extended(ul);
2094 ff = first_free(pno, is_extd, ep, format, 0, z);
2095 ff1 = ff * unitsize(format);
2096 def = orig ? orig->start : (pno > 4 && pct > 1) ? 0 : ff1;
2097 if (fno < 1 || !*(fields[0]))
2100 if (get_ul(fields[0], &ul, def / unitsize(0), 0))
2102 p.start = ul * unitsize(0);
2103 p.start -= (p.start % unitsize(format));
2108 max_length(pno, is_extd, ep, format, p.start / unitsize(format),
2110 ml1 = ml * unitsize(format);
2111 def = orig ? orig->size : (pno > 4 && pct > 1) ? 0 : ml1;
2112 if (fno < 2 || !*(fields[1]))
2115 if (get_ul(fields[1], &ul, def / unitsize(0), 0))
2117 p.size = ul * unitsize(0) + unitsize(format) - 1;
2118 p.size -= (p.size % unitsize(format));
2121 warn("Warning: exceeds max allowable size (%lu)\n",
2126 if (p.size == 0 && pno >= 4 && (fno < 2 || !*(fields[1]))) {
2127 warn("Warning: empty partition\n");
2131 p.p.nr_sects = p.size;
2133 if (p.size == 0 && !orig) {
2134 if (fno < 1 || !*(fields[0]))
2136 if (fno < 3 || !*(fields[2]))
2137 p.p.sys_type = EMPTY_PARTITION;
2140 if (p.start < ff1 && p.size > 0) {
2141 warn("Warning: bad partition start (earliest %lu)\n",
2142 (ff1 + unitsize(0) - 1) / unitsize(0));
2147 if (fno < 4 || !*(fields[3]))
2148 ul = (orig ? orig->p.bootable : 0);
2149 else if (!strcmp(fields[3], "-"))
2151 else if (!strcmp(fields[3], "*") || !strcmp(fields[3], "+"))
2154 warn("unrecognized bootable flag - choose - or *\n");
2159 if (ep && ep->p.sys_type == EMPTY_PARTITION) {
2160 if (!build_surrounding_extended(&p, ep, z))
2162 } else if (!compute_start_sect(&p, ep))
2166 longchs aa = chs_to_longchs(p.p.begin_chs), bb;
2170 } else if (fno < 7) {
2171 warn("partial c,h,s specification?\n");
2173 } else if (get_ul(fields[4], &bb.c, aa.c, 0) ||
2174 get_ul(fields[5], &bb.h, aa.h, 0) ||
2175 get_ul(fields[6], &bb.s, aa.s, 0))
2177 p.p.begin_chs = longchs_to_chs(bb, B);
2180 longchs aa = chs_to_longchs(p.p.end_chs), bb;
2184 } else if (fno < 10) {
2185 warn("partial c,h,s specification?\n");
2187 } else if (get_ul(fields[7], &bb.c, aa.c, 0) ||
2188 get_ul(fields[8], &bb.h, aa.h, 0) ||
2189 get_ul(fields[9], &bb.s, aa.s, 0))
2191 p.p.end_chs = longchs_to_chs(bb, B);
2194 if (pno > 3 && p.size && show_extended
2195 && p.p.sys_type != EMPTY_PARTITION
2196 && (is_extended(p.p.sys_type) != (pct == 1))) {
2197 warn("Extended partition not where expected\n");
2202 z->partitions[pno] = p;
2203 if (pno >= z->partno)
2204 z->partno += 4; /* reqd for out_partition() */
2207 out_partition(dev, 0, &(z->partitions[pno]), z, B);
2212 /* ep either points to the extended partition to contain this one,
2213 or to the empty partition that may become extended or is 0 */
2215 read_partition(char *dev, int interactive, int pno, struct part_desc *ep,
2216 struct disk_desc *z)
2218 struct part_desc *p = &(z->partitions[pno]);
2222 *p = oldp.partitions[pno];
2223 if (one_only_pno != pno)
2225 } else if (!show_extended && pno > 4 && pno % 4)
2228 while (!(i = read_line(pno, ep, dev, interactive, z)))
2230 fatalError("bad input\n");
2238 if (pno >= z->partno)
2244 read_partition_chain(char *dev, int interactive, struct part_desc *ep,
2245 struct disk_desc *z)
2252 if (base + 4 > SIZE(z->partitions)) {
2253 printf("too many partitions\n");
2256 for (i = 0; i < 4; i++)
2257 if (!read_partition(dev, interactive, base + i, ep, z))
2259 for (i = 0; i < 4; i++) {
2260 ep = &(z->partitions[base + i]);
2261 if (is_extended(ep->p.sys_type) && ep->size)
2265 /* nothing found - maybe an empty partition is going
2267 if (one_only || show_extended)
2269 ep = &(z->partitions[base + 1]);
2270 if (ep->size || ep->p.sys_type != EMPTY_PARTITION)
2276 static void read_input(char *dev, int interactive, struct disk_desc *z)
2279 struct part_desc *partitions = &(z->partitions[0]), *ep;
2281 for (i = 0; i < SIZE(z->partitions); i++)
2282 partitions[i] = zero_part_desc;
2287 ("Input in the following format; absent fields get a default value.\n"
2288 "<start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s>\n"
2289 "Usually you only need to specify <start> and <size> (and perhaps <type>).\n");
2292 for (i = 0; i < 4; i++)
2293 read_partition(dev, interactive, i, 0, z);
2294 for (i = 0; i < 4; i++) {
2295 ep = partitions + i;
2296 if (is_extended(ep->p.sys_type) && ep->size)
2297 read_partition_chain(dev, interactive, ep, z);
2299 add_sector_and_offset(z);
2303 * G. The command line
2306 static void version(void)
2308 printf("%s %s %s (aeb@cwi.nl, %s)\n", PROGNAME, "version", VERSION,
2312 static char short_opts[] = "cdfgilnqsu:vx?1A::C:DH:I:LN:O:RS:TU::V";
2314 #define PRINT_ID 0400
2315 #define CHANGE_ID 01000
2317 static const struct option long_opts[] = {
2318 {"change-id", no_argument, NULL, 'c' + CHANGE_ID},
2319 {"print-id", no_argument, NULL, 'c' + PRINT_ID},
2320 {"id", no_argument, NULL, 'c'},
2321 {"dump", no_argument, NULL, 'd'},
2322 {"force", no_argument, NULL, 'f'},
2323 {"show-geometry", no_argument, NULL, 'g'},
2324 {"increment", no_argument, NULL, 'i'},
2325 {"list", no_argument, NULL, 'l'},
2326 {"quiet", no_argument, NULL, 'q'},
2327 {"show-size", no_argument, NULL, 's'},
2328 {"unit", required_argument, NULL, 'u'},
2329 {"version", no_argument, NULL, 'v'},
2330 {"show-extended", no_argument, NULL, 'x'},
2331 {"help", no_argument, NULL, '?'},
2332 {"one-only", no_argument, NULL, '1'},
2333 {"cylinders", required_argument, NULL, 'C'},
2334 {"heads", required_argument, NULL, 'H'},
2335 {"sectors", required_argument, NULL, 'S'},
2336 {"activate", optional_argument, NULL, 'A'},
2337 {"DOS", no_argument, NULL, 'D'},
2338 {"Linux", no_argument, NULL, 'L'},
2339 {"re-read", no_argument, NULL, 'R'},
2340 {"list-types", no_argument, NULL, 'T'},
2341 {"unhide", optional_argument, NULL, 'U'},
2342 {"no-reread", no_argument, NULL, 160},
2343 {"IBM", no_argument, NULL, 161},
2344 {"leave-last", no_argument, NULL, 161},
2345 /* undocumented flags - not all completely implemented */
2346 {"in-order", no_argument, NULL, 128},
2347 {"not-in-order", no_argument, NULL, 129},
2348 {"inside-outer", no_argument, NULL, 130},
2349 {"not-inside-outer", no_argument, NULL, 131},
2350 {"nested", no_argument, NULL, 132},
2351 {"chained", no_argument, NULL, 133},
2352 {"onesector", no_argument, NULL, 134},
2356 /* default devices to list */
2357 static struct devd {
2358 char *pref, *letters;
2361 "hd", "abcdefgh"}, {
2367 static int is_ide_cdrom(char *device)
2369 /* No device was given explicitly, and we are trying some
2370 likely things. But opening /dev/hdc may produce errors like
2371 "hdc: tray open or drive not ready"
2372 if it happens to be a CD-ROM drive. So try to be careful.
2373 This only works since 2.1.73. */
2377 struct stat statbuf;
2379 sprintf(buf, "/proc/ide/%s/media", device + 5);
2380 procf = fopen(buf, "r");
2381 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2382 return !strncmp(buf, "cdrom", 5);
2384 /* Now when this proc file does not exist, skip the
2385 device when it is read-only. */
2386 if (stat(device, &statbuf) == 0)
2387 return (statbuf.st_mode & 0222) == 0;
2392 static void do_list(char *dev, int silent);
2393 static void do_size(char *dev, int silent);
2394 static void do_geom(char *dev, int silent);
2395 static void do_fdisk(char *dev);
2396 static void do_reread(char *dev);
2397 static void do_change_id(char *dev, char *part, char *id);
2398 static void do_unhide(char **av, int ac, char *arg);
2399 static void do_activate(char **av, int ac, char *arg);
2401 static int total_size;
2403 extern int sfdisk_main(int argc, char **argv)
2408 int opt_out_geom = 0;
2413 char *activatearg = 0;
2414 char *unhidearg = 0;
2417 usage(sfdisk_usage);
2419 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) !=
2424 break; /* does not imply quiet */
2432 case 'c' + PRINT_ID:
2433 case 'c' + CHANGE_ID:
2437 dump = 1; /* fall through */
2451 set_format(*optarg);
2460 activatearg = optarg;
2464 U.cylinders = atoi(optarg);
2470 U.heads = atoi(optarg);
2476 one_only = atoi(optarg);
2479 restore_sector_file = optarg;
2482 save_sector_file = optarg;
2488 U.sectors = atoi(optarg);
2502 usage(sfdisk_usage);
2504 /* undocumented flags */
2506 partitions_in_order = 1;
2509 partitions_in_order = 0;
2512 all_logicals_inside_outermost_extended = 1;
2515 all_logicals_inside_outermost_extended = 0;
2537 if (optind == argc && (opt_list || opt_out_geom || opt_size || verify)) {
2544 for (dp = defdevs; dp - defdevs < SIZE(defdevs); dp++) {
2547 sprintf(device, "/dev/%s%c", dp->pref, *lp++);
2548 if (!strcmp(dp->pref, "hd") && is_ide_cdrom(device))
2554 if (opt_list || verify)
2560 printf("total: %d blocks\n", total_size);
2566 usage(sfdisk_usage);
2568 if (opt_list || opt_out_geom || opt_size || verify) {
2569 while (optind < argc) {
2571 do_geom(argv[optind], 0);
2573 do_size(argv[optind], 0);
2574 if (opt_list || verify)
2575 do_list(argv[optind], 0);
2582 do_activate(argv + optind, argc - optind, activatearg);
2586 do_unhide(argv + optind, argc - optind, unhidearg);
2590 if ((do_id & PRINT_ID) != 0 && optind != argc - 2)
2591 fatalError("usage: sfdisk --print-id device partition-number\n");
2592 else if ((do_id & CHANGE_ID) != 0 && optind != argc - 3)
2594 ("usage: sfdisk --change-id device partition-number Id\n");
2595 else if (optind != argc - 3 && optind != argc - 2)
2596 fatalError("usage: sfdisk --id device partition-number [Id]\n");
2597 do_change_id(argv[optind], argv[optind + 1],
2598 (optind == argc - 2) ? 0 : argv[optind + 2]);
2602 if (optind != argc - 1)
2603 fatalError("can specify only one device (except with -l or -s)\n");
2608 else if (restore_sector_file)
2609 restore_sectors(dev);
2617 * H. Listing the current situation
2620 static int my_open(char *dev, int rw, int silent)
2624 mode = (rw ? O_RDWR : O_RDONLY);
2625 fd = open(dev, mode);
2626 if (fd < 0 && !silent) {
2628 fatalError("cannot open %s %s\n", dev,
2629 rw ? "read-write" : "for reading");
2634 static void do_list(char *dev, int silent)
2637 struct disk_desc *z;
2639 fd = my_open(dev, 0, silent);
2646 get_cylindersize(dev, fd, dump ? 1 : opt_list ? 0 : 1);
2647 get_partitions(dev, fd, z);
2650 out_partitions(dev, z);
2653 if (partitions_ok(z))
2654 warn("%s: OK\n"), dev;
2660 static void do_geom(char *dev, int silent)
2663 struct hd_geometry g;
2665 fd = my_open(dev, 0, silent);
2669 /* get_cylindersize(dev, fd, silent); */
2670 if (!ioctl(fd, HDIO_GETGEO, &g))
2671 printf("%s: %d cylinders, %d heads, %d sectors/track\n",
2672 dev, g.cylinders, g.heads, g.sectors);
2674 printf("%s: unknown geometry\n", dev);
2677 /* for compatibility with earlier fdisk: provide option -s */
2678 static void do_size(char *dev, int silent)
2683 fd = my_open(dev, 0, silent);
2687 if (ioctl(fd, BLKGETSIZE, &size)) {
2690 fatalError("BLKGETSIZE ioctl failed for %s\n"), dev;
2695 size /= 2; /* convert sectors to blocks */
2697 /* a CDROM drive without mounted CD yields MAXINT */
2698 if (silent && size == ((1 << 30) - 1))
2702 printf("%s: %9ld\n", dev, size);
2704 printf("%ld\n", size);
2710 * Activate: usually one wants to have a single primary partition
2711 * to be active. OS/2 fdisk makes non-bootable logical partitions
2712 * active - I don't know what that means to OS/2 Boot Manager.
2714 * Call: activate /dev/hda 2 5 7 make these partitions active
2715 * and the remaining ones inactive
2716 * Or: sfdisk -A /dev/hda 2 5 7
2718 * If only a single partition must be active, one may also use the form
2719 * sfdisk -A2 /dev/hda
2721 * With "activate /dev/hda" or "sfdisk -A /dev/hda" the active partitions
2722 * are listed but not changed. To get zero active partitions, use
2723 * "activate /dev/hda none" or "sfdisk -A /dev/hda none".
2724 * Use something like `echo ",,,*" | sfdisk -N2 /dev/hda' to only make
2725 * /dev/hda2 active, without changing other partitions.
2727 * A warning will be given if after the change not precisely one primary
2728 * partition is active.
2730 * The present syntax was chosen to be (somewhat) compatible with the
2731 * activate from the LILO package.
2733 static void set_active(struct disk_desc *z, char *pnam)
2737 pno = asc_to_index(pnam, z);
2738 z->partitions[pno].p.bootable = 0x80;
2741 static void do_activate(char **av, int ac, char *arg)
2745 int rw, i, pno, lpno;
2746 struct disk_desc *z;
2750 rw = (!no_write && (arg || ac > 1));
2751 fd = my_open(dev, rw, 0);
2754 get_cylindersize(dev, fd, 1);
2755 get_partitions(dev, fd, z);
2757 if (!arg && ac == 1) {
2758 /* list active partitions */
2759 for (pno = 0; pno < z->partno; pno++) {
2760 if (z->partitions[pno].p.bootable) {
2761 lpno = index_to_linux(pno, z);
2762 if (pno == linux_to_index(lpno, z))
2763 printf("%s%d\n", dev, lpno);
2765 printf("%s#%d\n", dev, pno);
2766 if (z->partitions[pno].p.bootable != 0x80)
2767 warn("bad active byte: 0x%x instead of 0x80\n",
2768 z->partitions[pno].p.bootable);
2772 /* clear `active byte' everywhere */
2773 for (pno = 0; pno < z->partno; pno++)
2774 z->partitions[pno].p.bootable = 0;
2776 /* then set where desired */
2780 for (i = 1; i < ac; i++)
2781 set_active(z, av[i]);
2783 /* then write to disk */
2784 if (write_partitions(dev, fd, z))
2790 for (pno = 0; pno < z->partno && pno < 4; pno++)
2791 if (z->partitions[pno].p.bootable)
2795 ("You have %d active primary partitions. This does not matter for LILO,\n"
2796 "but the DOS MBR will only boot a disk with 1 active partition.\n",
2800 static void set_unhidden(struct disk_desc *z, char *pnam)
2805 pno = asc_to_index(pnam, z);
2806 id = z->partitions[pno].p.sys_type;
2807 if (id == 0x11 || id == 0x14 || id == 0x16 || id == 0x17)
2810 fatalError("partition %s has id %x and is not hidden\n", pnam, id);
2811 z->partitions[pno].p.sys_type = id;
2815 * maybe remove and make part of --change-id
2817 static void do_unhide(char **av, int ac, char *arg)
2821 struct disk_desc *z;
2826 fd = my_open(dev, rw, 0);
2829 get_cylindersize(dev, fd, 1);
2830 get_partitions(dev, fd, z);
2832 /* unhide where desired */
2834 set_unhidden(z, arg);
2836 for (i = 1; i < ac; i++)
2837 set_unhidden(z, av[i]);
2839 /* then write to disk */
2840 if (write_partitions(dev, fd, z))
2846 static void do_change_id(char *dev, char *pnam, char *id)
2849 struct disk_desc *z;
2855 fd = my_open(dev, rw, 0);
2858 get_cylindersize(dev, fd, 1);
2859 get_partitions(dev, fd, z);
2861 pno = asc_to_index(pnam, z);
2863 printf("%x\n", z->partitions[pno].p.sys_type);
2866 i = strtoul(id, NULL, 16);
2868 fatalError("Bad Id %x\n"), i;
2869 z->partitions[pno].p.sys_type = i;
2871 if (write_partitions(dev, fd, z))
2877 static void do_reread(char *dev)
2881 fd = my_open(dev, 0, 0);
2882 if (reread_ioctl(fd))
2883 printf("This disk is currently in use.\n");
2887 * I. Writing the new situation
2890 static void do_fdisk(char *dev)
2894 struct stat statbuf;
2895 int interactive = isatty(0);
2896 struct disk_desc *z;
2898 if (stat(dev, &statbuf) < 0) {
2900 fatalError("Fatal error: cannot find %s\n"), dev;
2902 if (!S_ISBLK(statbuf.st_mode)) {
2903 warn("Warning: %s is not a block device\n"), dev;
2906 fd = my_open(dev, !no_write, 0);
2908 if (!no_write && !no_reread) {
2909 warn("Checking that no-one is using this disk right now ...\n");
2910 if (reread_ioctl(fd)) {
2912 ("\nThis disk is currently in use - repartitioning is probably a bad idea."
2913 "Umount all file systems, and swapoff all swap partitions on this disk."
2914 "Use the --no-reread flag to suppress this check.\n");
2916 printf("Use the --force flag to overrule all checks.\n");
2926 get_cylindersize(dev, fd, 0);
2927 get_partitions(dev, fd, z);
2929 printf("Old situation:\n");
2930 out_partitions(dev, z);
2932 if (one_only && (one_only_pno = linux_to_index(one_only, z)) < 0)
2933 fatalError("Partition %d does not exist, cannot change it\n"), one_only;
2939 read_input(dev, interactive, z);
2941 printf("New situation:\n");
2942 out_partitions(dev, z);
2944 if (!partitions_ok(z) && !force) {
2946 fatalError("I don't like these partitions - nothing changed.\n"
2947 "(If you really want this, use the --force option.)\n");
2950 ("I don't like this - probably you should answer No\n");
2955 printf("Are you satisfied with this? [ynq] ");
2957 printf("Do you want to write this to disk? [ynq] ");
2958 answer = c = getchar();
2959 while (c != '\n' && c != EOF)
2962 printf("\nsfdisk: premature end of input\n");
2963 if (c == EOF || answer == 'q' || answer == 'Q') {
2964 fatalError("Quitting - nothing changed\n");
2965 } else if (answer == 'n' || answer == 'N') {
2967 } else if (answer == 'y' || answer == 'Y') {
2970 printf("Please answer one of y,n,q\n");
2977 if (write_partitions(dev, fd, z))
2978 printf("Successfully wrote the new partition table\n\n");
2982 reread_disk_partition(dev, fd);
2985 ("If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)\n"
2986 "to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1\n"
2987 "(See fdisk(8).)\n");
2989 sync(); /* superstition */