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 */
57 #define BB_DECLARE_EXTERN
58 #define bb_need_memory_exhausted
61 static const char sfdisk_usage[] =
62 "sfdisk [options] device ...\n"
63 #ifndef BB_FEATURE_TRIVIAL_HELP
64 "\ndevice: something like /dev/hda or /dev/sda\n"
66 " -s [or --show-size]: list size of a partition\n"
67 " -c [or --id]: print or change partition Id\n"
68 " -l [or --list]: list partitions of each device\n"
69 " -d [or --dump]: idem, but in a format suitable for later input\n"
70 " -i [or --increment]: number cylinders etc. from 1 instead of from 0\n"
71 " -uS, -uB, -uC, -uM: accept/report in units of sectors/blocks/cylinders/MB\n"
72 " -T [or --list-types]:list the known partition types\n"
73 " -D [or --DOS]: for DOS-compatibility: waste a little space\n"
74 " -R [or --re-read]: make kernel reread partition table\n"
75 " -N# : change only the partition with number #\n"
76 " -n : do not actually write to disk\n"
77 " -O file : save the sectors that will be overwritten to file\n"
78 " -I file : restore these sectors again\n"
79 " -v [or --version]: print version\n"
80 " -? [or --help]: print this message\n"
81 "dangerous options:\n"
82 " -g [or --show-geometry]: print the kernel's idea of the geometry\n"
83 " -x [or --show-extended]: also list extended partitions on output\n\n"
84 " or expect descriptors for them on input\n"
85 " -L [or --Linux]: do not complain about things irrelevant for Linux\n"
86 " -q [or --quiet]: suppress warning messages\n"
87 " You can override the detected geometry using:\n"
88 " -C# [or --cylinders #]:set the number of cylinders to use\n"
89 " -H# [or --heads #]: set the number of heads to use\n"
90 " -S# [or --sectors #]: set the number of sectors to use\n"
92 "You can disable all consistency checking with:\n"
93 " -f [or --force]: do what I say, even if it is stupid\n"
99 /* common stuff for fdisk, cfdisk, sfdisk */
105 static struct systypes i386_sys_types[] = {
108 {0x02, "XENIX root"},
110 {0x04, "FAT16 <32M"},
111 {0x05, "Extended"}, /* DOS 3.3+ extended partition */
112 {0x06, "FAT16"}, /* DOS 16-bit >=32M */
113 {0x07, "HPFS/NTFS"}, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
114 {0x08, "AIX"}, /* AIX boot (AIX -- PS/2 port or SplitDrive) */
115 {0x09, "AIX bootable"}, /* AIX data or Coherent */
116 {0x0a, "OS/2 Boot Manager"}, /* OS/2 Boot Manager */
117 {0x0b, "Win95 FAT32"},
118 {0x0c, "Win95 FAT32 (LBA)"}, /* LBA really is `Extended Int 13h' */
119 {0x0e, "Win95 FAT16 (LBA)"},
120 {0x0f, "Win95 Ext'd (LBA)"},
122 {0x11, "Hidden FAT12"},
123 {0x12, "Compaq diagnostics"},
124 {0x14, "Hidden FAT16 <32M"},
125 {0x16, "Hidden FAT16"},
126 {0x17, "Hidden HPFS/NTFS"},
127 {0x18, "AST Windows swapfile"},
128 {0x1b, "Hidden Win95 FAT32"},
129 {0x1c, "Hidden Win95 FAT32 (LBA)"},
130 {0x1e, "Hidden Win95 FAT16 (LBA)"},
132 {0x3c, "PartitionMagic recovery"},
133 {0x40, "Venix 80286"},
134 {0x41, "PPC PReP Boot"},
137 {0x4e, "QNX4.x 2nd part"},
138 {0x4f, "QNX4.x 3rd part"},
139 {0x50, "OnTrack DM"},
140 {0x51, "OnTrack DM6 Aux1"}, /* (or Novell) */
141 {0x52, "CP/M"}, /* CP/M or Microport SysV/AT */
142 {0x53, "OnTrack DM6 Aux3"},
143 {0x54, "OnTrackDM6"},
145 {0x56, "Golden Bow"},
146 {0x5c, "Priam Edisk"},
148 {0x63, "GNU HURD or SysV"}, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
149 {0x64, "Novell Netware 286"},
150 {0x65, "Novell Netware 386"},
151 {0x70, "DiskSecure Multi-Boot"},
153 {0x80, "Old Minix"}, /* Minix 1.4a and earlier */
154 {0x81, "Minix / old Linux"}, /* Minix 1.4b and later */
155 {0x82, "Linux swap"}, /* also Solaris */
157 {0x84, "OS/2 hidden C: drive"},
158 {0x85, "Linux extended"},
159 {0x86, "NTFS volume set"},
160 {0x87, "NTFS volume set"},
162 {0x94, "Amoeba BBT"}, /* (bad block table) */
163 {0xa0, "IBM Thinkpad hibernation"},
169 {0xc1, "DRDOS/sec (FAT-12)"},
170 {0xc4, "DRDOS/sec (FAT-16 < 32M)"},
171 {0xc6, "DRDOS/sec (FAT-16)"},
173 {0xdb, "CP/M / CTOS / ..."}, /* CP/M or Concurrent CP/M or Concurrent DOS or CTOS */
174 {0xe1, "DOS access"}, /* DOS access or SpeedStor 12-bit FAT extended partition */
175 {0xe3, "DOS R/O"}, /* DOS R/O or SpeedStor */
176 {0xe4, "SpeedStor"}, /* SpeedStor 16-bit FAT extended partition < 1024 cyl. */
179 {0xf4, "SpeedStor"}, /* SpeedStor large partition */
180 {0xf2, "DOS secondary"}, /* DOS 3.3+ secondary */
181 {0xfd, "Linux raid autodetect"}, /* New (2.2.x) raid partition with autodetect
182 using persistent superblock */
183 {0xfe, "LANstep"}, /* SpeedStor >1024 cyl. or LANstep */
184 {0xff, "BBT"}, /* Xenix Bad Block Table */
188 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
194 * C. About heads, sectors and cylinders
195 * D. About system Ids
196 * E. About partitions
197 * F. The standard input
198 * G. The command line
199 * H. Listing the current situation
200 * I. Writing the new situation
202 static int exit_status = 0;
204 static int force = 0; /* 1: do what I say, even if it is stupid ... */
205 static int quiet = 0; /* 1: suppress all warnings */
206 static int Linux = 0; /* 1: suppress warnings irrelevant for Linux */
207 static int DOS = 0; /* 1: shift extended partitions by #sectors, not 1 */
208 static int dump = 0; /* 1: list in a format suitable for later input */
209 static int verify = 0; /* 1: check that listed partition is reasonable */
210 static int no_write = 0; /* 1: do not actually write to disk */
211 static int no_reread = 0; /* 1: skip the BLKRRPART ioctl test at startup */
212 static int leave_last = 0; /* 1: don't allocate the last cylinder */
213 static int opt_list = 0;
214 static char *save_sector_file = NULL;
215 static char *restore_sector_file = NULL;
217 static void warn(char *s, ...) __attribute__ ((format (printf, 1, 2)));
218 static void warn(char *s, ...)
225 vfprintf(stderr, s, p);
234 * sseek: seek to specified sector - return 0 on failure
236 * For >4GB disks lseek needs a > 32bit arg, and we have to use llseek.
237 * On the other hand, a 32 bit sector number is OK until 2TB.
238 * The routines _llseek and sseek below are the only ones that
239 * know about the loff_t type.
243 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
244 loff_t *, res, uint, wh);
247 static int sseek(char *dev, unsigned int fd, unsigned long s)
251 in = ((loff_t) s << 9);
255 if (_llseek(fd, in >> 32, in & 0xffffffff, &out, SEEK_SET) != 0) {
257 if ((out = lseek(fd, in, SEEK_SET)) != in) {
260 errorMsg("seek error on %s - cannot seek to %lu\n", dev, s);
265 errorMsg("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n",
266 (uint) (in >> 32), (uint) (in & 0xffffffff),
267 (uint) (out >> 32), (uint) (out & 0xffffffff));
278 * We preserve all sectors read in a chain - some of these will
279 * have to be modified and written back.
283 unsigned long sectornumber;
288 static void free_sectors(void)
294 sectorhead = s->next;
299 static struct sector *get_sector(char *dev, int fd, unsigned long sno)
303 for (s = sectorhead; s; s = s->next)
304 if (s->sectornumber == sno)
307 if (!sseek(dev, fd, sno))
310 s = (struct sector *) xmalloc(sizeof(struct sector));
312 if (read(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
314 errorMsg("read error on %s - cannot read sector %lu\n", dev, sno);
319 s->next = sectorhead;
321 s->sectornumber = sno;
322 s->to_be_written = 0;
327 static int msdos_signature(struct sector *s)
329 if (*(unsigned short *) (s->data + 0x1fe) != 0xaa55) {
330 errorMsg("ERROR: sector %lu does not have an msdos signature\n",
337 static int write_sectors(char *dev, int fd)
341 for (s = sectorhead; s; s = s->next)
342 if (s->to_be_written) {
343 if (!sseek(dev, fd, s->sectornumber))
345 if (write(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
347 errorMsg("write error on %s - cannot write sector %lu\n",
348 dev, s->sectornumber);
351 s->to_be_written = 0;
356 static void ulong_to_chars(unsigned long u, char *uu)
360 for (i = 0; i < 4; i++) {
366 static unsigned long chars_to_ulong(unsigned char *uu)
371 for (i = 3; i >= 0; i--)
372 u = (u << 8) | uu[i];
376 static int save_sectors(char *dev, int fdin)
382 fdout = open(save_sector_file, O_WRONLY | O_CREAT, 0444);
384 perror(save_sector_file);
385 errorMsg("cannot open partition sector save file (%s)\n",
390 for (s = sectorhead; s; s = s->next)
391 if (s->to_be_written) {
392 ulong_to_chars(s->sectornumber, ss);
393 if (!sseek(dev, fdin, s->sectornumber))
395 if (read(fdin, ss + 4, 512) != 512) {
397 errorMsg("read error on %s - cannot read sector %lu\n",
398 dev, s->sectornumber);
401 if (write(fdout, ss, sizeof(ss)) != sizeof(ss)) {
403 errorMsg("write error on %s\n", save_sector_file);
410 static void reread_disk_partition(char *dev, int fd);
412 static int restore_sectors(char *dev)
419 if (stat(restore_sector_file, &statbuf) < 0) {
420 perror(restore_sector_file);
421 errorMsg("cannot stat partition restore file (%s)\n",
422 restore_sector_file);
425 if (statbuf.st_size % 516) {
426 errorMsg("partition restore file has wrong size - not restoring\n");
429 if (!(ss = (char *) malloc(statbuf.st_size))) {
430 errorMsg(memory_exhausted, "sfdisk");
433 fdin = open(restore_sector_file, O_RDONLY);
435 perror(restore_sector_file);
436 errorMsg("cannot open partition restore file (%s)\n",
437 restore_sector_file);
440 if (read(fdin, ss, statbuf.st_size) != statbuf.st_size) {
442 errorMsg("error reading %s\n", restore_sector_file);
446 fdout = open(dev, O_WRONLY);
449 errorMsg("cannot open device %s for writing\n", dev);
454 ct = statbuf.st_size / 516;
456 sno = chars_to_ulong(ss);
457 if (!sseek(dev, fdout, sno))
459 if (write(fdout, ss + 4, 512) != 512) {
461 errorMsg("error writing sector %lu on %s\n", sno, dev);
468 reread_disk_partition(dev, fdout);
474 * C. About heads, sectors and cylinders
478 * <linux/hdreg.h> defines HDIO_GETGEO and
479 * struct hd_geometry {
480 * unsigned char heads;
481 * unsigned char sectors;
482 * unsigned short cylinders;
483 * unsigned long start;
488 * We consider several geometries for a disk:
489 * B - the BIOS geometry, gotten from the kernel via HDIO_GETGEO
490 * F - the fdisk geometry
491 * U - the user-specified geometry
493 * 0 means unspecified / unknown
496 unsigned long cylindersize;
497 unsigned long heads, sectors, cylinders;
500 static void get_cylindersize(char *dev, int fd, int silent)
502 struct hd_geometry g;
505 B.heads = B.sectors = B.cylinders = 0;
507 if (!ioctl(fd, HDIO_GETGEO, &g)) {
511 B.sectors = g.sectors;
512 B.cylinders = g.cylinders;
518 B.sectors = U.sectors;
520 B.cylinders = U.cylinders;
522 B.cylindersize = B.heads * B.sectors;
525 if (g.start && !force) {
527 ("Warning: start=%ld - this looks like a partition rather than\n"
528 "the entire disk. Using fdisk on it is probably meaningless.\n"
529 "[Use the --force option if you really want this]\n",
533 if (B.heads != g.heads)
534 warn("Warning: HDIO_GETGEO says that there are %d heads\n",
536 if (B.sectors != g.sectors)
537 warn("Warning: HDIO_GETGEO says that there are %d sectors\n",
539 if (B.cylinders != g.cylinders)
540 warn("Warning: HDIO_GETGEO says that there are %d cylinders\n",
543 if (!B.heads || !B.sectors || !B.cylinders)
544 printf("Disk %s: cannot get geometry\n", dev);
547 ("Warning: unlikely number of sectors (%ld - usually at most 63\n"
548 "This will give problems with all software that uses C/H/S addressing.\n",
551 printf("\nDisk %s: %lu cylinders, %lu heads, %lu sectors/track\n",
552 dev, B.cylinders, B.heads, B.sectors);
556 unsigned char h, s, c;
557 } chs; /* has some c bits in s */
558 static chs zero_chs = { 0, 0, 0 };
561 unsigned long h, s, c;
563 static longchs zero_longchs;
565 static chs longchs_to_chs(longchs aa, struct geometry G)
569 if (aa.h < 256 && aa.s < 64 && aa.c < 1024) {
571 a.s = aa.s | ((aa.c >> 2) & 0xc0);
573 } else if (G.heads && G.sectors) {
575 a.s = G.sectors | 0xc0;
582 static longchs chs_to_longchs(chs a)
589 aa.c = (aa.c << 2) + a.c;
593 static longchs ulong_to_longchs(unsigned long sno, struct geometry G)
597 if (G.heads && G.sectors && G.cylindersize) {
598 aa.s = 1 + sno % G.sectors;
599 aa.h = (sno / G.sectors) % G.heads;
600 aa.c = sno / G.cylindersize;
607 //static unsigned long
608 //longchs_to_ulong (longchs aa, struct geometry G) {
609 // return (aa.c*G.cylindersize + aa.h*G.sectors + aa.s - 1);
612 static chs ulong_to_chs(unsigned long sno, struct geometry G)
614 return longchs_to_chs(ulong_to_longchs(sno, G), G);
617 //static unsigned long
618 //chs_to_ulong (chs a, struct geometry G) {
619 // return longchs_to_ulong(chs_to_longchs(a), G);
622 static int is_equal_chs(chs a, chs b)
624 return (a.h == b.h && a.s == b.s && a.c == b.c);
627 static int chs_ok(chs a, char *v, char *w)
629 longchs aa = chs_to_longchs(a);
632 if (is_equal_chs(a, zero_chs))
634 if (B.heads && aa.h >= B.heads) {
635 warn("%s of partition %s has impossible value for head: "
636 "%ld (should be in 0-%ld)\n", w, v, aa.h, B.heads - 1);
639 if (B.sectors && (aa.s == 0 || aa.s > B.sectors)) {
640 warn("%s of partition %s has impossible value for sector: "
641 "%ld (should be in 1-%ld)\n", w, v, aa.s, B.sectors);
644 if (B.cylinders && aa.c >= B.cylinders) {
645 warn("%s of partition %s has impossible value for cylinders: "
646 "%ld (should be in 0-%ld)\n", w, v, aa.c, B.cylinders - 1);
653 * D. About system Ids
656 #define EMPTY_PARTITION 0
657 #define EXTENDED_PARTITION 5
658 #define WIN98_EXTENDED 0x0f
659 #define DM6_AUX1PARTITION 0x51
660 #define DM6_AUX3PARTITION 0x53
661 #define DM6_PARTITION 0x54
662 #define EZD_PARTITION 0x55
663 #define LINUX_SWAP 0x82
664 #define LINUX_NATIVE 0x83
665 #define LINUX_EXTENDED 0x85
666 #define BSD_PARTITION 0xa5
668 /* List of partition types now in i386_sys_types.c */
670 static const char *sysname(unsigned char type)
674 for (s = i386_sys_types; s->name; s++)
680 static void list_types(void)
684 printf("Id Name\n\n");
685 for (s = i386_sys_types; s->name; s++)
686 printf("%2x %s\n", s->type, s->name);
689 static int is_extended(unsigned char type)
691 return (type == EXTENDED_PARTITION
692 || type == LINUX_EXTENDED || type == WIN98_EXTENDED);
695 static int is_bsd(unsigned char type)
697 return (type == BSD_PARTITION);
701 * E. About partitions
704 /* MS/DOS partition */
707 unsigned char bootable; /* 0 or 0x80 */
709 unsigned char sys_type;
711 unsigned int start_sect; /* starting sector counting from 0 */
712 unsigned int nr_sects; /* nr of sectors in partition */
715 /* Unfortunately, partitions are not aligned, and non-Intel machines
716 are unhappy with non-aligned integers. So, we need a copy by hand. */
717 static int copy_to_int(unsigned char *cp)
728 static void copy_from_int(int m, char *cp)
739 static void copy_to_part(char *cp, struct partition *p)
742 p->begin_chs.h = *cp++;
743 p->begin_chs.s = *cp++;
744 p->begin_chs.c = *cp++;
746 p->end_chs.h = *cp++;
747 p->end_chs.s = *cp++;
748 p->end_chs.c = *cp++;
749 p->start_sect = copy_to_int(cp);
750 p->nr_sects = copy_to_int(cp + 4);
753 static void copy_from_part(struct partition *p, char *cp)
756 *cp++ = p->begin_chs.h;
757 *cp++ = p->begin_chs.s;
758 *cp++ = p->begin_chs.c;
760 *cp++ = p->end_chs.h;
761 *cp++ = p->end_chs.s;
762 *cp++ = p->end_chs.c;
763 copy_from_int(p->start_sect, cp);
764 copy_from_int(p->nr_sects, cp + 4);
767 /* Roughly speaking, Linux doesn't use any of the above fields except
768 for partition type, start sector and number of sectors. (However,
769 see also linux/drivers/scsi/fdomain.c.)
770 The only way partition type is used (in the kernel) is the comparison
771 for equality with EXTENDED_PARTITION (and these Disk Manager types). */
776 unsigned long sector, offset; /* disk location of this info */
778 struct part_desc *ep; /* extended partition containing this one */
784 struct part_desc *outer_extended_partition(struct part_desc *p)
791 static int is_parent(struct part_desc *pp, struct part_desc *p)
802 struct part_desc partitions[128];
806 /* determine where on the disk this information goes */
807 static void add_sector_and_offset(struct disk_desc *z)
812 for (pno = 0; pno < z->partno; pno++) {
813 p = &(z->partitions[pno]);
814 p->offset = 0x1be + (pno % 4) * sizeof(struct partition);
816 p->sector = (p->ep ? p->ep->start : 0);
820 /* tell the kernel to reread the partition tables */
821 static int reread_ioctl(int fd)
823 if (ioctl(fd, BLKRRPART)) {
830 static int is_blockdev(int fd)
834 return (fstat(fd, &statbuf) == 0 && S_ISBLK(statbuf.st_mode));
837 /* reread after writing */
838 static void reread_disk_partition(char *dev, int fd)
840 printf("Re-reading the partition table ...\n");
843 sleep(3); /* superfluous since 1.3.20 */
845 if (reread_ioctl(fd) && is_blockdev(fd))
846 printf("The command to re-read the partition table failed\n"
847 "Reboot your system now, before using mkfs\n");
851 printf("Error closing %s\n", dev);
856 /* find Linux name of this partition, assuming that it will have a name */
857 static int index_to_linux(int pno, struct disk_desc *z)
860 struct part_desc *p = &(z->partitions[0]);
862 for (i = 0; i < pno; i++, p++)
863 if (i < 4 || (p->size > 0 && !is_extended(p->p.sys_type)))
868 static int linux_to_index(int lpno, struct disk_desc *z)
871 struct part_desc *p = &(z->partitions[0]);
873 for (i = 0; i < z->partno && ct < lpno; i++, p++)
874 if ((i < 4 || (p->size > 0 && !is_extended(p->p.sys_type)))
880 static int asc_to_index(char *pnam, struct disk_desc *z)
885 pno = atoi(pnam + 1);
888 pno = linux_to_index(pnum, z);
890 if (!(pno >= 0 && pno < z->partno))
891 fatalError("%s: no such partition\n", pnam);
896 * List partitions - in terms of sectors, blocks or cylinders
903 static int default_format = F_MEGABYTE;
904 static int specified_format = 0;
905 static int show_extended = 0;
906 static int one_only = 0;
907 static int one_only_pno;
908 static int increment = 0;
910 static void set_format(char c)
914 printf("unrecognized format - using sectors\n");
916 specified_format = F_SECTOR;
919 specified_format = F_BLOCK;
922 specified_format = F_CYLINDER;
925 specified_format = F_MEGABYTE;
930 static unsigned long unitsize(int format)
932 default_format = (B.cylindersize ? F_CYLINDER : F_MEGABYTE);
933 if (!format && !(format = specified_format))
934 format = default_format;
940 return B.cylindersize;
950 static unsigned long get_disksize(int format)
952 unsigned long cs = B.cylinders;
954 if (cs && leave_last)
956 return (cs * B.cylindersize) / unitsize(format);
959 static void out_partition_header(char *dev, int format, struct geometry G)
962 printf("# partition table of %s\n", dev);
963 printf("unit: sectors\n\n");
967 default_format = (G.cylindersize ? F_CYLINDER : F_MEGABYTE);
968 if (!format && !(format = specified_format))
969 format = default_format;
973 printf("unimplemented format - using %s\n",
974 G.cylindersize ? "cylinders" : "sectors");
976 if (G.cylindersize) {
977 printf("Units = cylinders of %lu bytes, blocks of 1024 bytes"
978 ", counting from %d\n\n",
979 G.cylindersize << 9, increment);
981 (" Device Boot Start End #cyls #blocks Id System\n");
986 printf("Units = sectors of 512 bytes, counting from %d\n\n",
989 (" Device Boot Start End #sectors Id System\n");
992 printf("Units = blocks of 1024 bytes, counting from %d\n\n",
995 (" Device Boot Start End #blocks Id System\n");
998 printf("Units = megabytes of 1048576 bytes, blocks of 1024 bytes"
999 ", counting from %d\n\n", increment);
1001 (" Device Boot Start End MB #blocks Id System\n");
1007 out_rounddown(int width, unsigned long n, unsigned long unit, int inc)
1009 printf("%*lu", width, inc + n / unit);
1011 putchar((n % unit) ? '+' : ' ');
1016 out_roundup(int width, unsigned long n, unsigned long unit, int inc)
1018 if (n == (unsigned long) (-1))
1019 printf("%*s", width, "-");
1021 printf("%*lu", width, inc + n / unit);
1023 putchar(((n + 1) % unit) ? '-' : ' ');
1028 out_roundup_size(int width, unsigned long n, unsigned long unit)
1030 printf("%*lu", width, (n + unit - 1) / unit);
1032 putchar((n % unit) ? '-' : ' ');
1036 static int get_fdisk_geometry(struct part_desc *p)
1038 chs b = p->p.end_chs;
1039 longchs bb = chs_to_longchs(b);
1043 F.cylindersize = F.heads * F.sectors;
1044 return (F.sectors != B.sectors || F.heads != B.heads);
1048 out_partition(char *dev, int format, struct part_desc *p,
1049 struct disk_desc *z, struct geometry G)
1051 unsigned long start, end, size;
1054 if (!format && !(format = specified_format))
1055 format = default_format;
1057 pno = p - &(z->partitions[0]); /* our index */
1058 lpno = index_to_linux(pno, z); /* name of next one that has a name */
1059 if (pno == linux_to_index(lpno, z)) /* was that us? */
1060 printf("%8s%-2u", dev, lpno); /* yes */
1061 else if (show_extended)
1065 putchar(dump ? ':' : ' ');
1068 end = p->start + p->size - 1;
1072 printf(" start=%9lu", start);
1073 printf(", size=%8lu", size);
1074 if (p->ptype == DOS_TYPE) {
1075 printf(", Id=%2x", p->p.sys_type);
1076 if (p->p.bootable == 0x80)
1077 printf(", bootable");
1083 if (p->ptype != DOS_TYPE || p->p.bootable == 0)
1085 else if (p->p.bootable == 0x80)
1088 printf(" ? "); /* garbage */
1092 if (G.cylindersize) {
1093 out_rounddown(6, start, G.cylindersize, increment);
1094 out_roundup(6, end, G.cylindersize, increment);
1095 out_roundup_size(6, size, G.cylindersize);
1096 out_rounddown(8, size, 2, 0);
1102 out_rounddown(9, start, 1, increment);
1103 out_roundup(9, end, 1, increment);
1104 out_rounddown(9, size, 1, 0);
1108 printf("%8lu,%3lu ",
1109 p->sector / 2, ((p->sector & 1) ? 512 : 0) + p->offset);
1111 out_rounddown(8, start, 2, increment);
1112 out_roundup(8, end, 2, increment);
1113 out_rounddown(8, size, 2, 0);
1116 out_rounddown(5, start, 2048, increment);
1117 out_roundup(5, end, 2048, increment);
1118 out_roundup_size(5, size, 2048);
1119 out_rounddown(8, size, 2, 0);
1122 if (p->ptype == DOS_TYPE) {
1123 printf(" %2x %s\n", p->p.sys_type, sysname(p->p.sys_type));
1128 /* Is chs as we expect? */
1129 if (!quiet && p->ptype == DOS_TYPE) {
1133 a = (size ? ulong_to_chs(start, G) : zero_chs);
1135 aa = chs_to_longchs(a);
1136 bb = chs_to_longchs(b);
1137 if (a.s && !is_equal_chs(a, b))
1139 ("\t\tstart: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n",
1140 aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1141 a = (size ? ulong_to_chs(end, G) : zero_chs);
1143 aa = chs_to_longchs(a);
1144 bb = chs_to_longchs(b);
1145 if (a.s && !is_equal_chs(a, b))
1147 ("\t\tend: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n",
1148 aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1149 if (G.cylinders && G.cylinders < 1024 && bb.c > G.cylinders)
1151 ("partition ends on cylinder %ld, beyond the end of the disk\n",
1156 static void out_partitions(char *dev, struct disk_desc *z)
1158 struct part_desc *p;
1159 int pno, format = 0;
1162 printf("No partitions found\n");
1164 for (pno = 0; pno < z->partno; pno++) {
1165 p = &(z->partitions[pno]);
1166 if (p->size != 0 && p->p.sys_type != 0) {
1167 if (get_fdisk_geometry(p))
1169 ("Warning: The first partition looks like it was made\n"
1170 " for C/H/S=*/%ld/%ld (instead of %ld/%ld/%ld).\n"
1171 "For this listing I'll assume that geometry.\n",
1172 F.heads, F.sectors, B.cylinders, B.heads,
1177 out_partition_header(dev, format, F);
1178 for (pno = 0; pno < z->partno; pno++) {
1179 out_partition(dev, format, &(z->partitions[pno]), z, F);
1180 if (show_extended && pno % 4 == 3)
1186 static int disj(struct part_desc *p, struct part_desc *q)
1188 return ((p->start + p->size <= q->start)
1189 || (is_extended(p->p.sys_type)
1190 && q->start + q->size <= p->start + p->size));
1193 static char *pnumber(struct part_desc *p, struct disk_desc *z)
1195 static char buf[20];
1197 struct part_desc *p0 = &(z->partitions[0]);
1199 this = index_to_linux(p - p0, z);
1200 next = index_to_linux(p - p0 + 1, z);
1203 sprintf(buf, "%d", this);
1205 sprintf(buf, "[%d]", this);
1209 static int partitions_ok(struct disk_desc *z)
1211 struct part_desc *partitions = &(z->partitions[0]), *p, *q;
1212 int partno = z->partno;
1214 #define PNO(p) pnumber(p, z)
1216 /* Have at least 4 partitions been defined? */
1219 fatalError("no partition table present.\n");
1221 fatalError("strange, only %d partitions defined.\n", partno);
1225 /* Are the partitions of size 0 marked empty?
1226 And do they have start = 0? And bootable = 0? */
1227 for (p = partitions; p - partitions < partno; p++)
1229 if (p->p.sys_type != EMPTY_PARTITION)
1231 ("Warning: partition %s has size 0 but is not marked Empty\n",
1233 else if (p->p.bootable != 0)
1234 warn("Warning: partition %s has size 0 and is bootable\n",
1236 else if (p->p.start_sect != 0)
1238 ("Warning: partition %s has size 0 and nonzero start\n",
1240 /* all this is probably harmless, no error return */
1243 /* Are the logical partitions contained in their extended partitions? */
1244 for (p = partitions + 4; p < partitions + partno; p++)
1245 if (p->ptype == DOS_TYPE)
1246 if (p->size && !is_extended(p->p.sys_type)) {
1248 if (p->start < q->start
1249 || p->start + p->size > q->start + q->size) {
1250 warn("Warning: partition %s ", PNO(p));
1251 warn("is not contained in partition %s\n", PNO(q));
1256 /* Are the data partitions mutually disjoint? */
1257 for (p = partitions; p < partitions + partno; p++)
1258 if (p->size && !is_extended(p->p.sys_type))
1259 for (q = p + 1; q < partitions + partno; q++)
1260 if (q->size && !is_extended(q->p.sys_type))
1261 if (!((p->start > q->start) ? disj(q, p) : disj(p, q))) {
1262 warn("Warning: partitions %s ", PNO(p));
1263 warn("and %s overlap\n", PNO(q));
1267 /* Are the data partitions and the extended partition
1268 table sectors disjoint? */
1269 for (p = partitions; p < partitions + partno; p++)
1270 if (p->size && !is_extended(p->p.sys_type))
1271 for (q = partitions; q < partitions + partno; q++)
1272 if (is_extended(q->p.sys_type))
1273 if (p->start <= q->start
1274 && p->start + p->size > q->start) {
1275 warn("Warning: partition %s contains part of ",
1277 warn("the partition table (sector %lu),\n",
1279 warn("and will destroy it when filled\n");
1283 /* Do they start past zero and end before end-of-disk? */
1285 unsigned long ds = get_disksize(F_SECTOR);
1287 for (p = partitions; p < partitions + partno; p++)
1289 if (p->start == 0) {
1290 warn("Warning: partition %s starts at sector 0\n",
1294 if (p->size && p->start + p->size > ds) {
1296 ("Warning: partition %s extends past end of disk\n",
1303 /* At most one chain of DOS extended partitions ? */
1304 /* It seems that the OS/2 fdisk has the additional requirement
1305 that the extended partition must be the fourth one */
1309 for (p = partitions; p < partitions + 4; p++)
1310 if (p->p.sys_type == EXTENDED_PARTITION)
1312 if (ect > 1 && !Linux) {
1314 ("Among the primary partitions, at most one can be extended\n");
1315 warn(" (although this is not a problem under Linux)\n");
1321 * Do all partitions start at a cylinder boundary ?
1322 * (this is not required for Linux)
1323 * The first partition starts after MBR.
1324 * Logical partitions start slightly after the containing extended partn.
1326 if (B.cylindersize) {
1327 for (p = partitions; p < partitions + partno; p++)
1329 if (p->start % B.cylindersize != 0
1331 || p->start / B.cylindersize !=
1332 p->ep->start / B.cylindersize)
1333 && (p->p.start_sect >= B.cylindersize)) {
1334 warn("Warning: partition %s does not start "
1335 "at a cylinder boundary\n", PNO(p));
1339 if ((p->start + p->size) % B.cylindersize) {
1340 warn("Warning: partition %s does not end "
1341 "at a cylinder boundary\n", PNO(p));
1348 /* Usually, one can boot only from primary partitions. */
1349 /* In fact, from a unique one only. */
1350 /* do not warn about bootable extended partitions -
1351 often LILO is there */
1355 for (p = partitions; p < partitions + partno; p++)
1356 if (p->p.bootable) {
1358 pno = p - partitions;
1359 else if (p - partitions < 4) {
1361 ("Warning: more than one primary partition is marked "
1362 "bootable (active)\n"
1363 "This does not matter for LILO, but the DOS MBR will "
1364 "not boot this disk.\n");
1367 if (p - partitions >= 4) {
1369 ("Warning: usually one can boot from primary partitions "
1370 "only\nLILO disregards the `bootable' flag.\n");
1374 if (pno == -1 || pno >= 4)
1376 ("Warning: no primary partition is marked bootable (active)\n"
1377 "This does not matter for LILO, but the DOS MBR will "
1378 "not boot this disk.\n");
1381 /* Is chs as we expect? */
1382 for (p = partitions; p < partitions + partno; p++)
1383 if (p->ptype == DOS_TYPE) {
1387 a = p->size ? ulong_to_chs(p->start, B) : zero_chs;
1389 aa = chs_to_longchs(a);
1390 bb = chs_to_longchs(b);
1391 if (!chs_ok(b, PNO(p), "start"))
1393 if (a.s && !is_equal_chs(a, b))
1395 ("partition %s: start: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n",
1396 PNO(p), aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1398 p->size ? ulong_to_chs(p->start + p->size - 1,
1401 aa = chs_to_longchs(a);
1402 bb = chs_to_longchs(b);
1403 if (!chs_ok(b, PNO(p), "end"))
1405 if (a.s && !is_equal_chs(a, b))
1407 ("partition %s: end: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n",
1408 PNO(p), aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1409 if (B.cylinders && B.cylinders < 1024 && bb.c > B.cylinders)
1411 ("partition %s ends on cylinder %ld, beyond the end of the disk\n",
1421 extended_partition(char *dev, int fd, struct part_desc *ep,
1422 struct disk_desc *z)
1426 unsigned long start, here, next;
1427 int i, moretodo = 1;
1429 struct part_desc *partitions = &(z->partitions[0]);
1430 int pno = z->partno;
1432 here = start = ep->start;
1437 if (!(s = get_sector(dev, fd, here)))
1440 if (!msdos_signature(s))
1443 cp = s->data + 0x1be;
1445 if (pno + 4 >= SIZE(z->partitions)) {
1446 printf("too many partitions - ignoring those past nr (%d)\n",
1453 for (i = 0; i < 4; i++, cp += sizeof(struct partition)) {
1454 partitions[pno].sector = here;
1455 partitions[pno].offset = cp - s->data;
1456 partitions[pno].ep = ep;
1457 copy_to_part(cp, &p);
1458 if (is_extended(p.sys_type)) {
1459 partitions[pno].start = start + p.start_sect;
1461 printf("tree of partitions?\n");
1463 next = partitions[pno].start; /* follow `upper' branch */
1466 partitions[pno].start = here + p.start_sect;
1468 partitions[pno].size = p.nr_sects;
1469 partitions[pno].ptype = DOS_TYPE;
1470 partitions[pno].p = p;
1479 #define BSD_DISKMAGIC (0x82564557UL)
1480 #define BSD_MAXPARTITIONS 8
1481 #define BSD_FS_UNUSED 0
1482 typedef unsigned char u8;
1483 typedef unsigned short u16;
1484 typedef unsigned int u32;
1485 struct bsd_disklabel {
1488 char d_typename[16];
1489 char d_packname[16];
1493 u16 d_npartitions; /* number of partitions in following */
1495 struct bsd_partition { /* the partition table */
1496 u32 p_size; /* number of sectors in partition */
1497 u32 p_offset; /* starting sector */
1498 u32 p_fsize; /* filesystem basic fragment size */
1499 u8 p_fstype; /* filesystem type, see below */
1500 u8 p_frag; /* filesystem fragments per block */
1501 u16 p_cpg; /* filesystem cylinders per group */
1502 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
1506 bsd_partition(char *dev, int fd, struct part_desc *ep, struct disk_desc *z)
1508 struct bsd_disklabel *l;
1509 struct bsd_partition *bp, *bp0;
1510 unsigned long start = ep->start;
1512 struct part_desc *partitions = &(z->partitions[0]);
1513 int pno = z->partno;
1515 if (!(s = get_sector(dev, fd, start + 1)))
1517 l = (struct bsd_disklabel *) (s->data);
1518 if (l->d_magic != BSD_DISKMAGIC)
1521 bp = bp0 = &l->d_partitions[0];
1522 while (bp - bp0 <= BSD_MAXPARTITIONS) {
1523 if (pno + 1 >= SIZE(z->partitions)) {
1524 printf("too many partitions - ignoring those "
1525 "past nr (%d)\n", pno - 1);
1528 if (bp->p_fstype != BSD_FS_UNUSED) {
1529 partitions[pno].start = bp->p_offset;
1530 partitions[pno].size = bp->p_size;
1531 partitions[pno].sector = start + 1;
1532 partitions[pno].offset = (char *) bp - (char *) bp0;
1533 partitions[pno].ep = 0;
1534 partitions[pno].ptype = BSD_TYPE;
1543 msdos_partition(char *dev, int fd, unsigned long start,
1544 struct disk_desc *z)
1548 struct partition pt;
1550 struct part_desc *partitions = &(z->partitions[0]);
1551 int pno = z->partno;
1553 if (!(s = get_sector(dev, fd, start)))
1556 if (!msdos_signature(s))
1559 cp = s->data + 0x1be;
1560 copy_to_part(cp, &pt);
1562 /* If I am not mistaken, recent kernels will hide this from us,
1563 so we will never actually see traces of a Disk Manager */
1564 if (pt.sys_type == DM6_PARTITION
1565 || pt.sys_type == EZD_PARTITION
1566 || pt.sys_type == DM6_AUX1PARTITION
1567 || pt.sys_type == DM6_AUX3PARTITION) {
1568 printf("detected Disk Manager - unable to handle that\n");
1572 unsigned int sig = *(unsigned short *) (s->data + 2);
1575 && *(unsigned short *) (s->data + sig) == 0x55aa
1576 && (1 & *(unsigned char *) (s->data + sig + 2))) {
1577 printf("DM6 signature found - giving up\n");
1582 for (pno = 0; pno < 4; pno++, cp += sizeof(struct partition)) {
1583 partitions[pno].sector = start;
1584 partitions[pno].offset = cp - s->data;
1585 copy_to_part(cp, &pt);
1586 partitions[pno].start = start + pt.start_sect;
1587 partitions[pno].size = pt.nr_sects;
1588 partitions[pno].ep = 0;
1589 partitions[pno].p = pt;
1594 for (i = 0; i < 4; i++) {
1595 if (is_extended(partitions[i].p.sys_type)) {
1596 if (!partitions[i].size) {
1597 printf("strange..., an extended partition of size 0?\n");
1600 extended_partition(dev, fd, &partitions[i], z);
1602 if (is_bsd(partitions[i].p.sys_type)) {
1603 if (!partitions[i].size) {
1604 printf("strange..., a BSD partition of size 0?\n");
1607 bsd_partition(dev, fd, &partitions[i], z);
1614 osf_partition(char *dev, int fd, unsigned long start, struct disk_desc *z)
1620 sun_partition(char *dev, int fd, unsigned long start, struct disk_desc *z)
1626 amiga_partition(char *dev, int fd, unsigned long start,
1627 struct disk_desc *z)
1632 static void get_partitions(char *dev, int fd, struct disk_desc *z)
1636 if (!msdos_partition(dev, fd, 0, z)
1637 && !osf_partition(dev, fd, 0, z)
1638 && !sun_partition(dev, fd, 0, z)
1639 && !amiga_partition(dev, fd, 0, z)) {
1640 printf(" %s: unrecognized partition\n", dev);
1645 static int write_partitions(char *dev, int fd, struct disk_desc *z)
1648 struct part_desc *partitions = &(z->partitions[0]), *p;
1649 int pno = z->partno;
1652 printf("-n flag was given: Nothing changed\n");
1656 for (p = partitions; p < partitions + pno; p++) {
1657 s = get_sector(dev, fd, p->sector);
1660 s->to_be_written = 1;
1661 copy_from_part(&(p->p), s->data + p->offset);
1662 *(unsigned short *) (&(s->data[0x1fe])) = 0xaa55;
1664 if (save_sector_file) {
1665 if (!save_sectors(dev, fd)) {
1666 fatalError("Failed saving the old sectors - aborting\n");
1670 if (!write_sectors(dev, fd)) {
1671 errorMsg("Failed writing the partition on %s\n", dev);
1678 * F. The standard input
1683 * <start> <size> <type> <bootable> <c,h,s> <c,h,s>
1684 * Fields are separated by whitespace or comma or semicolon possibly
1685 * followed by whitespace; initial and trailing whitespace is ignored.
1686 * Numbers can be octal, decimal or hexadecimal, decimal is default
1687 * The <c,h,s> parts can (and probably should) be omitted.
1688 * Bootable is specified as [*|-], with as default not-bootable.
1689 * Type is given in hex, without the 0x prefix, or is [E|S|L|X], where
1690 * L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), and E
1691 * is EXTENDED_PARTITION (5), X is LINUX_EXTENDED (85).
1692 * The default value of start is the first nonassigned sector/cylinder/...
1693 * The default value of size is as much as possible (until next
1694 * partition or end-of-disk).
1695 * .: end of chain of extended partitions.
1697 * On interactive input an empty line means: all defaults.
1698 * Otherwise empty lines are ignored.
1701 static int eof, eob;
1712 3, "bootable", 1}, {
1722 * Read a line, split it into fields
1724 * (some primitive handwork, but a more elaborate parser seems
1731 read_stdin(unsigned char **fields, unsigned char *line, int fieldssize,
1734 unsigned char *lp, *ip;
1737 /* boolean true and empty string at start */
1740 for (fno = 0; fno < fieldssize; fno++)
1741 fields[fno] = line + 1;
1744 /* read a line from stdin */
1745 lp = fgets(line + 2, linesize, stdin);
1750 if (!(lp = index(lp, '\n')))
1751 fatalError("long or incomplete input line - quitting\n");
1754 /* remove comments, if any */
1755 if ((lp = index(line + 2, '#')) != 0)
1758 /* recognize a few commands - to be expanded */
1759 if (!strcmp(line + 2, "unit: sectors")) {
1760 specified_format = F_SECTOR;
1764 /* dump style? - then bad input is fatal */
1765 if ((ip = index(line + 2, ':')) != 0) {
1770 while (isspace(*ip))
1774 for (d = dumpflds; d - dumpflds < SIZE(dumpflds); d++) {
1775 if (!strncmp(ip, d->fldname, strlen(d->fldname))) {
1776 ip += strlen(d->fldname);
1777 while (isspace(*ip))
1780 fields[d->fldno] = line;
1781 else if (*ip == '=') {
1782 while (isspace(*++ip));
1783 fields[d->fldno] = ip;
1784 while (isalnum(*ip)) /* 0x07FF */
1787 fatalError("input error: `=' expected after %s field\n",
1789 if (fno <= d->fldno)
1793 if (*ip != ',' && *ip != ';')
1795 ("input error: unexpected character %c after %s field\n",
1801 fatalError("unrecognized input: %s\n", ip);
1804 /* split line into fields */
1807 while ((c = *ip++) != 0) {
1808 if (!lp[-1] && (c == '\t' || c == ' '));
1809 else if (c == '\t' || c == ' ' || c == ',' || c == ';') {
1811 if (fno < fieldssize)
1818 if (lp == fields[fno - 1])
1823 /* read a number, use default if absent */
1824 static int get_ul(char *u, unsigned long *up, unsigned long def, int base)
1830 *up = strtoul(u, &nu, base);
1831 if (errno == ERANGE) {
1832 printf("number too big\n");
1836 printf("trailing junk after number\n");
1844 /* There are two common ways to structure extended partitions:
1845 as nested boxes, and as a chain. Sometimes the partitions
1846 must be given in order. Sometimes all logical partitions
1847 must lie inside the outermost extended partition.
1848 NESTED: every partition is contained in the surrounding partitions
1849 and is disjoint from all others.
1850 CHAINED: every data partition is contained in the surrounding partitions
1851 and disjoint from all others, but extended partitions may lie outside
1852 (insofar as allowed by all_logicals_inside_outermost_extended).
1853 ONESECTOR: all data partitions are mutually disjoint; extended partitions
1854 each use one sector only (except perhaps for the outermost one).
1856 static int partitions_in_order = 0;
1857 static int all_logicals_inside_outermost_extended = 1;
1858 static enum { NESTED, CHAINED, ONESECTOR } boxes = NESTED;
1860 /* find the default value for <start> - assuming entire units */
1861 static unsigned long
1862 first_free(int pno, int is_extended, struct part_desc *ep, int format,
1863 unsigned long mid, struct disk_desc *z)
1865 unsigned long ff, fff;
1866 unsigned long unit = unitsize(format);
1867 struct part_desc *partitions = &(z->partitions[0]), *pp = 0;
1869 /* if containing ep undefined, look at its container */
1870 if (ep && ep->p.sys_type == EMPTY_PARTITION)
1874 if (boxes == NESTED || (boxes == CHAINED && !is_extended))
1876 else if (all_logicals_inside_outermost_extended)
1877 pp = outer_extended_partition(ep);
1880 ff = pp ? (pp->start + unit - 1) / unit : 0;
1882 /* rounding up wastes almost an entire cylinder - round down
1883 and leave it to compute_start_sect() to fix the difference */
1884 ff = pp ? pp->start / unit : 0;
1886 /* MBR and 1st sector of an extended partition are never free */
1891 for (pp = partitions; pp < partitions + pno; pp++) {
1892 if (!is_parent(pp, ep) && pp->size > 0) {
1893 if ((partitions_in_order || pp->start / unit <= ff
1894 || (mid && pp->start / unit <= mid))
1895 && (fff = (pp->start + pp->size + unit - 1) / unit) > ff) {
1905 /* find the default value for <size> - assuming entire units */
1906 static unsigned long
1907 max_length(int pno, int is_extended, struct part_desc *ep, int format,
1908 unsigned long start, struct disk_desc *z)
1911 unsigned long unit = unitsize(format);
1912 struct part_desc *partitions = &(z->partitions[0]), *pp = 0;
1914 /* if containing ep undefined, look at its container */
1915 if (ep && ep->p.sys_type == EMPTY_PARTITION)
1919 if (boxes == NESTED || (boxes == CHAINED && !is_extended))
1921 else if (all_logicals_inside_outermost_extended)
1922 pp = outer_extended_partition(ep);
1924 fu = pp ? (pp->start + pp->size) / unit : get_disksize(format);
1926 for (pp = partitions; pp < partitions + pno; pp++)
1927 if (!is_parent(pp, ep) && pp->size > 0
1928 && pp->start / unit >= start && pp->start / unit < fu)
1929 fu = pp->start / unit;
1931 return (fu > start) ? fu - start : 0;
1934 /* compute starting sector of a partition inside an extended one */
1935 /* ep is 0 or points to surrounding extended partition */
1936 static int compute_start_sect(struct part_desc *p, struct part_desc *ep)
1939 int inc = (DOS && B.sectors) ? B.sectors : 1;
1942 if (ep && p->start + p->size >= ep->start + 1)
1943 delta = p->start - ep->start - inc;
1944 else if (p->start == 0 && p->size > 0)
1951 if (is_extended(p->p.sys_type) && boxes == ONESECTOR)
1953 else if ((int) (p->size) <= 0) {
1954 warn("no room for partition descriptor\n");
1959 : (is_extended(p->p.sys_type) ?
1960 outer_extended_partition(ep) : ep)->start);
1962 if (p->p.sys_type == EMPTY_PARTITION && p->size == 0) {
1963 p->p.start_sect = 0;
1964 p->p.begin_chs = zero_chs;
1965 p->p.end_chs = zero_chs;
1967 p->p.start_sect = p->start - base;
1968 p->p.begin_chs = ulong_to_chs(p->start, B);
1969 p->p.end_chs = ulong_to_chs(p->start + p->size - 1, B);
1971 p->p.nr_sects = p->size;
1975 /* build the extended partition surrounding a given logical partition */
1977 build_surrounding_extended(struct part_desc *p, struct part_desc *ep,
1978 struct disk_desc *z)
1980 int inc = (DOS && B.sectors) ? B.sectors : 1;
1981 int format = F_SECTOR;
1982 struct part_desc *p0 = &(z->partitions[0]), *eep = ep->ep;
1984 if (boxes == NESTED) {
1985 ep->start = first_free(ep - p0, 1, eep, format, p->start, z);
1986 ep->size = max_length(ep - p0, 1, eep, format, ep->start, z);
1987 if (ep->start > p->start
1988 || ep->start + ep->size < p->start + p->size) {
1989 warn("cannot build surrounding extended partition\n");
1993 ep->start = p->start;
1994 if (boxes == CHAINED)
2000 ep->p.nr_sects = ep->size;
2002 ep->p.sys_type = EXTENDED_PARTITION;
2003 if (!compute_start_sect(ep, eep) || !compute_start_sect(p, ep)) {
2004 ep->p.sys_type = EMPTY_PARTITION;
2013 read_line(int pno, struct part_desc *ep, char *dev, int interactive,
2014 struct disk_desc *z)
2016 unsigned char line[1000];
2017 unsigned char *fields[11];
2018 int fno, pct = pno % 4;
2019 struct part_desc p, *orig;
2020 unsigned long ff, ff1, ul, ml, ml1, def;
2021 int format, lpno, is_extd;
2026 lpno = index_to_linux(pno, z);
2029 if (pct == 0 && (show_extended || pno == 0))
2031 warn("%8s%d: ", dev, lpno);
2034 /* read input line - skip blank lines when reading from a file */
2036 fno = read_stdin(fields, line, SIZE(fields), SIZE(line));
2037 } while (fno == RD_CMD || (fno == 0 && !interactive));
2038 if (fno == RD_EOF) {
2040 } else if (fno > 10 && *(fields[10]) != 0) {
2041 printf("too many input fields\n");
2045 if (fno == 1 && !strcmp(fields[0], ".")) {
2050 /* use specified format, but round to cylinders if F_MEGABYTE specified */
2052 if (B.cylindersize && specified_format == F_MEGABYTE)
2053 format = F_CYLINDER;
2055 orig = (one_only ? &(oldp.partitions[pno]) : 0);
2060 /* first read the type - we need to know whether it is extended */
2061 /* stop reading when input blank (defaults) and all is full */
2063 if (fno == 0) { /* empty line */
2064 if (orig && is_extended(orig->p.sys_type))
2066 ff = first_free(pno, is_extd, ep, format, 0, z);
2067 ml = max_length(pno, is_extd, ep, format, ff, z);
2068 if (ml == 0 && is_extd == 0) {
2070 ff = first_free(pno, is_extd, ep, format, 0, z);
2071 ml = max_length(pno, is_extd, ep, format, ff, z);
2073 if (ml == 0 && pno >= 4) {
2074 /* no free blocks left - don't read any further */
2075 warn("No room for more\n");
2079 if (fno < 3 || !*(fields[2]))
2080 ul = orig ? orig->p.sys_type :
2081 (is_extd || (pno > 3 && pct == 1 && show_extended))
2082 ? EXTENDED_PARTITION : LINUX_NATIVE;
2083 else if (!strcmp(fields[2], "L"))
2085 else if (!strcmp(fields[2], "S"))
2087 else if (!strcmp(fields[2], "E"))
2088 ul = EXTENDED_PARTITION;
2089 else if (!strcmp(fields[2], "X"))
2090 ul = LINUX_EXTENDED;
2091 else if (get_ul(fields[2], &ul, LINUX_NATIVE, 16))
2094 warn("Illegal type\n");
2098 is_extd = is_extended(ul);
2101 ff = first_free(pno, is_extd, ep, format, 0, z);
2102 ff1 = ff * unitsize(format);
2103 def = orig ? orig->start : (pno > 4 && pct > 1) ? 0 : ff1;
2104 if (fno < 1 || !*(fields[0]))
2107 if (get_ul(fields[0], &ul, def / unitsize(0), 0))
2109 p.start = ul * unitsize(0);
2110 p.start -= (p.start % unitsize(format));
2115 max_length(pno, is_extd, ep, format, p.start / unitsize(format),
2117 ml1 = ml * unitsize(format);
2118 def = orig ? orig->size : (pno > 4 && pct > 1) ? 0 : ml1;
2119 if (fno < 2 || !*(fields[1]))
2122 if (get_ul(fields[1], &ul, def / unitsize(0), 0))
2124 p.size = ul * unitsize(0) + unitsize(format) - 1;
2125 p.size -= (p.size % unitsize(format));
2128 warn("Warning: exceeds max allowable size (%lu)\n",
2133 if (p.size == 0 && pno >= 4 && (fno < 2 || !*(fields[1]))) {
2134 warn("Warning: empty partition\n");
2138 p.p.nr_sects = p.size;
2140 if (p.size == 0 && !orig) {
2141 if (fno < 1 || !*(fields[0]))
2143 if (fno < 3 || !*(fields[2]))
2144 p.p.sys_type = EMPTY_PARTITION;
2147 if (p.start < ff1 && p.size > 0) {
2148 warn("Warning: bad partition start (earliest %lu)\n",
2149 (ff1 + unitsize(0) - 1) / unitsize(0));
2154 if (fno < 4 || !*(fields[3]))
2155 ul = (orig ? orig->p.bootable : 0);
2156 else if (!strcmp(fields[3], "-"))
2158 else if (!strcmp(fields[3], "*") || !strcmp(fields[3], "+"))
2161 warn("unrecognized bootable flag - choose - or *\n");
2166 if (ep && ep->p.sys_type == EMPTY_PARTITION) {
2167 if (!build_surrounding_extended(&p, ep, z))
2169 } else if (!compute_start_sect(&p, ep))
2173 longchs aa = chs_to_longchs(p.p.begin_chs), bb;
2177 } else if (fno < 7) {
2178 warn("partial c,h,s specification?\n");
2180 } else if (get_ul(fields[4], &bb.c, aa.c, 0) ||
2181 get_ul(fields[5], &bb.h, aa.h, 0) ||
2182 get_ul(fields[6], &bb.s, aa.s, 0))
2184 p.p.begin_chs = longchs_to_chs(bb, B);
2187 longchs aa = chs_to_longchs(p.p.end_chs), bb;
2191 } else if (fno < 10) {
2192 warn("partial c,h,s specification?\n");
2194 } else if (get_ul(fields[7], &bb.c, aa.c, 0) ||
2195 get_ul(fields[8], &bb.h, aa.h, 0) ||
2196 get_ul(fields[9], &bb.s, aa.s, 0))
2198 p.p.end_chs = longchs_to_chs(bb, B);
2201 if (pno > 3 && p.size && show_extended
2202 && p.p.sys_type != EMPTY_PARTITION
2203 && (is_extended(p.p.sys_type) != (pct == 1))) {
2204 warn("Extended partition not where expected\n");
2209 z->partitions[pno] = p;
2210 if (pno >= z->partno)
2211 z->partno += 4; /* reqd for out_partition() */
2214 out_partition(dev, 0, &(z->partitions[pno]), z, B);
2219 /* ep either points to the extended partition to contain this one,
2220 or to the empty partition that may become extended or is 0 */
2222 read_partition(char *dev, int interactive, int pno, struct part_desc *ep,
2223 struct disk_desc *z)
2225 struct part_desc *p = &(z->partitions[pno]);
2229 *p = oldp.partitions[pno];
2230 if (one_only_pno != pno)
2232 } else if (!show_extended && pno > 4 && pno % 4)
2235 while (!(i = read_line(pno, ep, dev, interactive, z)))
2237 fatalError("bad input\n");
2245 if (pno >= z->partno)
2251 read_partition_chain(char *dev, int interactive, struct part_desc *ep,
2252 struct disk_desc *z)
2259 if (base + 4 > SIZE(z->partitions)) {
2260 printf("too many partitions\n");
2263 for (i = 0; i < 4; i++)
2264 if (!read_partition(dev, interactive, base + i, ep, z))
2266 for (i = 0; i < 4; i++) {
2267 ep = &(z->partitions[base + i]);
2268 if (is_extended(ep->p.sys_type) && ep->size)
2272 /* nothing found - maybe an empty partition is going
2274 if (one_only || show_extended)
2276 ep = &(z->partitions[base + 1]);
2277 if (ep->size || ep->p.sys_type != EMPTY_PARTITION)
2283 static void read_input(char *dev, int interactive, struct disk_desc *z)
2286 struct part_desc *partitions = &(z->partitions[0]), *ep;
2288 for (i = 0; i < SIZE(z->partitions); i++)
2289 partitions[i] = zero_part_desc;
2294 ("Input in the following format; absent fields get a default value.\n"
2295 "<start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s>\n"
2296 "Usually you only need to specify <start> and <size> (and perhaps <type>).\n");
2299 for (i = 0; i < 4; i++)
2300 read_partition(dev, interactive, i, 0, z);
2301 for (i = 0; i < 4; i++) {
2302 ep = partitions + i;
2303 if (is_extended(ep->p.sys_type) && ep->size)
2304 read_partition_chain(dev, interactive, ep, z);
2306 add_sector_and_offset(z);
2310 * G. The command line
2313 static void version(void)
2315 printf("%s %s %s (aeb@cwi.nl, %s)\n", PROGNAME, "version", VERSION,
2319 static char short_opts[] = "cdfgilnqsu:vx?1A::C:DH:I:LN:O:RS:TU::V";
2321 #define PRINT_ID 0400
2322 #define CHANGE_ID 01000
2324 static const struct option long_opts[] = {
2325 {"change-id", no_argument, NULL, 'c' + CHANGE_ID},
2326 {"print-id", no_argument, NULL, 'c' + PRINT_ID},
2327 {"id", no_argument, NULL, 'c'},
2328 {"dump", no_argument, NULL, 'd'},
2329 {"force", no_argument, NULL, 'f'},
2330 {"show-geometry", no_argument, NULL, 'g'},
2331 {"increment", no_argument, NULL, 'i'},
2332 {"list", no_argument, NULL, 'l'},
2333 {"quiet", no_argument, NULL, 'q'},
2334 {"show-size", no_argument, NULL, 's'},
2335 {"unit", required_argument, NULL, 'u'},
2336 {"version", no_argument, NULL, 'v'},
2337 {"show-extended", no_argument, NULL, 'x'},
2338 {"help", no_argument, NULL, '?'},
2339 {"one-only", no_argument, NULL, '1'},
2340 {"cylinders", required_argument, NULL, 'C'},
2341 {"heads", required_argument, NULL, 'H'},
2342 {"sectors", required_argument, NULL, 'S'},
2343 {"activate", optional_argument, NULL, 'A'},
2344 {"DOS", no_argument, NULL, 'D'},
2345 {"Linux", no_argument, NULL, 'L'},
2346 {"re-read", no_argument, NULL, 'R'},
2347 {"list-types", no_argument, NULL, 'T'},
2348 {"unhide", optional_argument, NULL, 'U'},
2349 {"no-reread", no_argument, NULL, 160},
2350 {"IBM", no_argument, NULL, 161},
2351 {"leave-last", no_argument, NULL, 161},
2352 /* undocumented flags - not all completely implemented */
2353 {"in-order", no_argument, NULL, 128},
2354 {"not-in-order", no_argument, NULL, 129},
2355 {"inside-outer", no_argument, NULL, 130},
2356 {"not-inside-outer", no_argument, NULL, 131},
2357 {"nested", no_argument, NULL, 132},
2358 {"chained", no_argument, NULL, 133},
2359 {"onesector", no_argument, NULL, 134},
2363 /* default devices to list */
2364 static struct devd {
2365 char *pref, *letters;
2368 "hd", "abcdefgh"}, {
2374 static int is_ide_cdrom(char *device)
2376 /* No device was given explicitly, and we are trying some
2377 likely things. But opening /dev/hdc may produce errors like
2378 "hdc: tray open or drive not ready"
2379 if it happens to be a CD-ROM drive. So try to be careful.
2380 This only works since 2.1.73. */
2384 struct stat statbuf;
2386 sprintf(buf, "/proc/ide/%s/media", device + 5);
2387 procf = fopen(buf, "r");
2388 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2389 return !strncmp(buf, "cdrom", 5);
2391 /* Now when this proc file does not exist, skip the
2392 device when it is read-only. */
2393 if (stat(device, &statbuf) == 0)
2394 return (statbuf.st_mode & 0222) == 0;
2399 static void do_list(char *dev, int silent);
2400 static void do_size(char *dev, int silent);
2401 static void do_geom(char *dev, int silent);
2402 static void do_fdisk(char *dev);
2403 static void do_reread(char *dev);
2404 static void do_change_id(char *dev, char *part, char *id);
2405 static void do_unhide(char **av, int ac, char *arg);
2406 static void do_activate(char **av, int ac, char *arg);
2408 static int total_size;
2410 extern int sfdisk_main(int argc, char **argv)
2415 int opt_out_geom = 0;
2420 char *activatearg = 0;
2421 char *unhidearg = 0;
2424 usage(sfdisk_usage);
2426 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) !=
2431 break; /* does not imply quiet */
2439 case 'c' + PRINT_ID:
2440 case 'c' + CHANGE_ID:
2444 dump = 1; /* fall through */
2458 set_format(*optarg);
2467 activatearg = optarg;
2471 U.cylinders = atoi(optarg);
2477 U.heads = atoi(optarg);
2483 one_only = atoi(optarg);
2486 restore_sector_file = optarg;
2489 save_sector_file = optarg;
2495 U.sectors = atoi(optarg);
2509 usage(sfdisk_usage);
2511 /* undocumented flags */
2513 partitions_in_order = 1;
2516 partitions_in_order = 0;
2519 all_logicals_inside_outermost_extended = 1;
2522 all_logicals_inside_outermost_extended = 0;
2544 if (optind == argc && (opt_list || opt_out_geom || opt_size || verify)) {
2551 for (dp = defdevs; dp - defdevs < SIZE(defdevs); dp++) {
2554 sprintf(device, "/dev/%s%c", dp->pref, *lp++);
2555 if (!strcmp(dp->pref, "hd") && is_ide_cdrom(device))
2561 if (opt_list || verify)
2567 printf("total: %d blocks\n", total_size);
2573 usage(sfdisk_usage);
2575 if (opt_list || opt_out_geom || opt_size || verify) {
2576 while (optind < argc) {
2578 do_geom(argv[optind], 0);
2580 do_size(argv[optind], 0);
2581 if (opt_list || verify)
2582 do_list(argv[optind], 0);
2589 do_activate(argv + optind, argc - optind, activatearg);
2593 do_unhide(argv + optind, argc - optind, unhidearg);
2597 if ((do_id & PRINT_ID) != 0 && optind != argc - 2)
2598 fatalError("usage: sfdisk --print-id device partition-number\n");
2599 else if ((do_id & CHANGE_ID) != 0 && optind != argc - 3)
2601 ("usage: sfdisk --change-id device partition-number Id\n");
2602 else if (optind != argc - 3 && optind != argc - 2)
2603 fatalError("usage: sfdisk --id device partition-number [Id]\n");
2604 do_change_id(argv[optind], argv[optind + 1],
2605 (optind == argc - 2) ? 0 : argv[optind + 2]);
2609 if (optind != argc - 1)
2610 fatalError("can specify only one device (except with -l or -s)\n");
2615 else if (restore_sector_file)
2616 restore_sectors(dev);
2624 * H. Listing the current situation
2627 static int my_open(char *dev, int rw, int silent)
2631 mode = (rw ? O_RDWR : O_RDONLY);
2632 fd = open(dev, mode);
2633 if (fd < 0 && !silent) {
2635 fatalError("cannot open %s %s\n", dev,
2636 rw ? "read-write" : "for reading");
2641 static void do_list(char *dev, int silent)
2644 struct disk_desc *z;
2646 fd = my_open(dev, 0, silent);
2653 get_cylindersize(dev, fd, dump ? 1 : opt_list ? 0 : 1);
2654 get_partitions(dev, fd, z);
2657 out_partitions(dev, z);
2660 if (partitions_ok(z))
2661 warn("%s: OK\n", dev);
2667 static void do_geom(char *dev, int silent)
2670 struct hd_geometry g;
2672 fd = my_open(dev, 0, silent);
2676 /* get_cylindersize(dev, fd, silent); */
2677 if (!ioctl(fd, HDIO_GETGEO, &g))
2678 printf("%s: %d cylinders, %d heads, %d sectors/track\n",
2679 dev, g.cylinders, g.heads, g.sectors);
2681 printf("%s: unknown geometry\n", dev);
2684 /* for compatibility with earlier fdisk: provide option -s */
2685 static void do_size(char *dev, int silent)
2690 fd = my_open(dev, 0, silent);
2694 if (ioctl(fd, BLKGETSIZE, &size)) {
2697 fatalError("BLKGETSIZE ioctl failed for %s\n", dev);
2702 size /= 2; /* convert sectors to blocks */
2704 /* a CDROM drive without mounted CD yields MAXINT */
2705 if (silent && size == ((1 << 30) - 1))
2709 printf("%s: %9ld\n", dev, size);
2711 printf("%ld\n", size);
2717 * Activate: usually one wants to have a single primary partition
2718 * to be active. OS/2 fdisk makes non-bootable logical partitions
2719 * active - I don't know what that means to OS/2 Boot Manager.
2721 * Call: activate /dev/hda 2 5 7 make these partitions active
2722 * and the remaining ones inactive
2723 * Or: sfdisk -A /dev/hda 2 5 7
2725 * If only a single partition must be active, one may also use the form
2726 * sfdisk -A2 /dev/hda
2728 * With "activate /dev/hda" or "sfdisk -A /dev/hda" the active partitions
2729 * are listed but not changed. To get zero active partitions, use
2730 * "activate /dev/hda none" or "sfdisk -A /dev/hda none".
2731 * Use something like `echo ",,,*" | sfdisk -N2 /dev/hda' to only make
2732 * /dev/hda2 active, without changing other partitions.
2734 * A warning will be given if after the change not precisely one primary
2735 * partition is active.
2737 * The present syntax was chosen to be (somewhat) compatible with the
2738 * activate from the LILO package.
2740 static void set_active(struct disk_desc *z, char *pnam)
2744 pno = asc_to_index(pnam, z);
2745 z->partitions[pno].p.bootable = 0x80;
2748 static void do_activate(char **av, int ac, char *arg)
2752 int rw, i, pno, lpno;
2753 struct disk_desc *z;
2757 rw = (!no_write && (arg || ac > 1));
2758 fd = my_open(dev, rw, 0);
2761 get_cylindersize(dev, fd, 1);
2762 get_partitions(dev, fd, z);
2764 if (!arg && ac == 1) {
2765 /* list active partitions */
2766 for (pno = 0; pno < z->partno; pno++) {
2767 if (z->partitions[pno].p.bootable) {
2768 lpno = index_to_linux(pno, z);
2769 if (pno == linux_to_index(lpno, z))
2770 printf("%s%d\n", dev, lpno);
2772 printf("%s#%d\n", dev, pno);
2773 if (z->partitions[pno].p.bootable != 0x80)
2774 warn("bad active byte: 0x%x instead of 0x80\n",
2775 z->partitions[pno].p.bootable);
2779 /* clear `active byte' everywhere */
2780 for (pno = 0; pno < z->partno; pno++)
2781 z->partitions[pno].p.bootable = 0;
2783 /* then set where desired */
2787 for (i = 1; i < ac; i++)
2788 set_active(z, av[i]);
2790 /* then write to disk */
2791 if (write_partitions(dev, fd, z))
2797 for (pno = 0; pno < z->partno && pno < 4; pno++)
2798 if (z->partitions[pno].p.bootable)
2802 ("You have %d active primary partitions. This does not matter for LILO,\n"
2803 "but the DOS MBR will only boot a disk with 1 active partition.\n",
2807 static void set_unhidden(struct disk_desc *z, char *pnam)
2812 pno = asc_to_index(pnam, z);
2813 id = z->partitions[pno].p.sys_type;
2814 if (id == 0x11 || id == 0x14 || id == 0x16 || id == 0x17)
2817 fatalError("partition %s has id %x and is not hidden\n", pnam, id);
2818 z->partitions[pno].p.sys_type = id;
2822 * maybe remove and make part of --change-id
2824 static void do_unhide(char **av, int ac, char *arg)
2828 struct disk_desc *z;
2833 fd = my_open(dev, rw, 0);
2836 get_cylindersize(dev, fd, 1);
2837 get_partitions(dev, fd, z);
2839 /* unhide where desired */
2841 set_unhidden(z, arg);
2843 for (i = 1; i < ac; i++)
2844 set_unhidden(z, av[i]);
2846 /* then write to disk */
2847 if (write_partitions(dev, fd, z))
2853 static void do_change_id(char *dev, char *pnam, char *id)
2856 struct disk_desc *z;
2862 fd = my_open(dev, rw, 0);
2865 get_cylindersize(dev, fd, 1);
2866 get_partitions(dev, fd, z);
2868 pno = asc_to_index(pnam, z);
2870 printf("%x\n", z->partitions[pno].p.sys_type);
2873 i = strtoul(id, NULL, 16);
2875 fatalError("Bad Id %lx\n", i);
2876 z->partitions[pno].p.sys_type = i;
2878 if (write_partitions(dev, fd, z))
2884 static void do_reread(char *dev)
2888 fd = my_open(dev, 0, 0);
2889 if (reread_ioctl(fd))
2890 printf("This disk is currently in use.\n");
2894 * I. Writing the new situation
2897 static void do_fdisk(char *dev)
2901 struct stat statbuf;
2902 int interactive = isatty(0);
2903 struct disk_desc *z;
2905 if (stat(dev, &statbuf) < 0) {
2907 fatalError("Fatal error: cannot find %s\n", dev);
2909 if (!S_ISBLK(statbuf.st_mode)) {
2910 warn("Warning: %s is not a block device\n", dev);
2913 fd = my_open(dev, !no_write, 0);
2915 if (!no_write && !no_reread) {
2916 warn("Checking that no-one is using this disk right now ...\n");
2917 if (reread_ioctl(fd)) {
2919 ("\nThis disk is currently in use - repartitioning is probably a bad idea."
2920 "Umount all file systems, and swapoff all swap partitions on this disk."
2921 "Use the --no-reread flag to suppress this check.\n");
2923 printf("Use the --force flag to overrule all checks.\n");
2933 get_cylindersize(dev, fd, 0);
2934 get_partitions(dev, fd, z);
2936 printf("Old situation:\n");
2937 out_partitions(dev, z);
2939 if (one_only && (one_only_pno = linux_to_index(one_only, z)) < 0)
2940 fatalError("Partition %d does not exist, cannot change it\n", one_only);
2946 read_input(dev, interactive, z);
2948 printf("New situation:\n");
2949 out_partitions(dev, z);
2951 if (!partitions_ok(z) && !force) {
2953 fatalError("I don't like these partitions - nothing changed.\n"
2954 "(If you really want this, use the --force option.)\n");
2957 ("I don't like this - probably you should answer No\n");
2962 printf("Are you satisfied with this? [ynq] ");
2964 printf("Do you want to write this to disk? [ynq] ");
2965 answer = c = getchar();
2966 while (c != '\n' && c != EOF)
2969 printf("\nsfdisk: premature end of input\n");
2970 if (c == EOF || answer == 'q' || answer == 'Q') {
2971 fatalError("Quitting - nothing changed\n");
2972 } else if (answer == 'n' || answer == 'N') {
2974 } else if (answer == 'y' || answer == 'Y') {
2977 printf("Please answer one of y,n,q\n");
2984 if (write_partitions(dev, fd, z))
2985 printf("Successfully wrote the new partition table\n\n");
2989 reread_disk_partition(dev, fd);
2992 ("If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)\n"
2993 "to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1\n"
2994 "(See fdisk(8).)\n");
2996 sync(); /* superstition */