2 * sfdisk version 3.0 - aeb - 950813
4 * Copyright (C) 1995 Andries E. Brouwer (aeb@cwi.nl)
6 * This program is free software. You can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation: either Version 1
9 * or (at your option) any later version.
11 * A.V. Le Blanc (LeBlanc@mcc.ac.uk) wrote Linux fdisk 1992-1994,
12 * patched by various people (faith@cs.unc.edu, martin@cs.unc.edu,
13 * leisner@sdsp.mc.xerox.com, esr@snark.thyrsus.com, aeb@cwi.nl)
14 * 1993-1995, with version numbers (as far as I have seen) 0.93 - 2.0e.
15 * This program had (head,sector,cylinder) as basic unit, and was
16 * (therefore) broken in several ways for the use on larger disks -
17 * for example, my last patch (from 2.0d to 2.0e) was required
18 * to allow a partition to cross cylinder 8064, and to write an
19 * extended partition past the 4GB mark.
21 * The current program is a rewrite from scratch, and I started a
22 * version numbering at 3.0.
23 * Andries Brouwer, aeb@cwi.nl, 950813
25 * Well, a good user interface is still lacking. On the other hand,
26 * many configurations cannot be handled by any other fdisk.
27 * I changed the name to sfdisk to prevent confusion. - aeb, 970501
30 * 19990319 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> - i18n
31 * 20040428 - Jeroen Dobbelaere <jeroen.dobbelaere@acunia.com> - added PACKED
32 * 20040824 - David A. Wheeler <dwheeler@dwheeler.com> - warnings to stderr
35 #define PROGNAME "sfdisk"
38 #include <stdlib.h> /* atoi, free */
39 #include <stdarg.h> /* varargs */
40 #include <unistd.h> /* read, write */
41 #include <fcntl.h> /* O_RDWR */
42 #include <errno.h> /* ERANGE */
43 #include <string.h> /* strchr(), strrchr() */
46 #include <sys/ioctl.h>
48 #include <sys/utsname.h>
51 #include "linux_version.h"
53 #include "wholedisk.h"
56 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
62 * C. About heads, sectors and cylinders
65 * F. The standard input
67 * H. Listing the current situation
68 * I. Writing the new situation
72 int force = 0; /* 1: do what I say, even if it is stupid ... */
73 int quiet = 0; /* 1: suppress all warnings */
74 /* IA-64 gcc spec file currently does -DLinux... */
76 int Linux = 0; /* 1: suppress warnings irrelevant for Linux */
77 int DOS = 0; /* 1: shift extended partitions by #sectors, not 1 */
78 int DOS_extended = 0; /* 1: use starting cylinder boundary of extd partn */
79 int dump = 0; /* 1: list in a format suitable for later input */
80 int verify = 0; /* 1: check that listed partition is reasonable */
81 int no_write = 0; /* 1: do not actually write to disk */
82 int no_reread = 0; /* 1: skip the BLKRRPART ioctl test at startup */
83 int leave_last = 0; /* 1: don't allocate the last cylinder */
85 char *save_sector_file = NULL;
86 char *restore_sector_file = NULL;
89 do_warn(char *s, ...) {
94 vfprintf(stderr, s, p);
106 vfprintf(stderr, s, p);
113 error(char *s, ...) {
118 fprintf(stderr, "\n" PROGNAME ": ");
119 vfprintf(stderr, s, p);
125 fatal(char *s, ...) {
130 fprintf(stderr, "\n" PROGNAME ": ");
131 vfprintf(stderr, s, p);
138 * GCC nonsense - needed for GCC 3.4.x with -O2
140 * Maybe just test with #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 4) ?
142 #ifndef __GNUC_PREREQ
143 #define __GNUC_PREREQ(x,y) 0
145 #if __GNUC_PREREQ(3,4)
146 #define __attribute__used __attribute__ ((used))
148 #define __attribute__used
152 * arm needs PACKED - use it everywhere?
154 #if defined(__GNUC__) && (defined(__arm__) || defined(__alpha__))
155 # define PACKED __attribute__ ((packed))
166 * sseek: seek to specified sector - return 0 on failure
168 * Note: we use 512-byte sectors here, irrespective of the hardware ss.
172 sseek(char *dev, unsigned int fd, unsigned long s) {
174 in = ((off_t) s << 9);
177 if ((out = lseek(fd, in, SEEK_SET)) != in) {
179 error(_("seek error on %s - cannot seek to %lu\n"), dev, s);
184 error(_("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n"),
185 (unsigned int)(in>>32), (unsigned int)(in & 0xffffffff),
186 (unsigned int)(out>>32), (unsigned int)(out & 0xffffffff));
197 * We preserve all sectors read in a chain - some of these will
198 * have to be modified and written back.
202 unsigned long sectornumber;
213 sectorhead = s->next;
218 static struct sector *
219 get_sector(char *dev, int fd, unsigned long sno) {
222 for(s = sectorhead; s; s = s->next)
223 if (s->sectornumber == sno)
226 if (!sseek(dev, fd, sno))
229 if (!(s = (struct sector *) malloc(sizeof(struct sector))))
230 fatal(_("out of memory - giving up\n"));
232 if (read(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
233 if (errno) /* 0 in case we read past end-of-disk */
235 error(_("read error on %s - cannot read sector %lu\n"), dev, sno);
240 s->next = sectorhead;
242 s->sectornumber = sno;
243 s->to_be_written = 0;
249 msdos_signature (struct sector *s) {
250 unsigned char *data = (unsigned char *)s->data;
251 if (data[510] == 0x55 && data[511] == 0xaa)
253 error(_("ERROR: sector %lu does not have an msdos signature\n"),
259 write_sectors(char *dev, int fd) {
262 for (s = sectorhead; s; s = s->next)
263 if (s->to_be_written) {
264 if (!sseek(dev, fd, s->sectornumber))
266 if (write(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
268 error(_("write error on %s - cannot write sector %lu\n"),
269 dev, s->sectornumber);
272 s->to_be_written = 0;
278 ulong_to_chars(unsigned long u, char *uu) {
288 chars_to_ulong(unsigned char *uu) {
293 u = (u << 8) | uu[i];
298 save_sectors(char *dev, int fdin) {
303 fdout = open(save_sector_file, O_WRONLY | O_CREAT, 0444);
305 perror(save_sector_file);
306 error(_("cannot open partition sector save file (%s)\n"),
311 for (s = sectorhead; s; s = s->next)
312 if (s->to_be_written) {
313 ulong_to_chars(s->sectornumber, ss);
314 if (!sseek(dev, fdin, s->sectornumber))
316 if (read(fdin, ss+4, 512) != 512) {
318 error(_("read error on %s - cannot read sector %lu\n"),
319 dev, s->sectornumber);
322 if (write(fdout, ss, sizeof(ss)) != sizeof(ss)) {
324 error(_("write error on %s\n"), save_sector_file);
338 static void reread_disk_partition(char *dev, int fd);
341 restore_sectors(char *dev) {
342 int fdin = -1, fdout = -1;
348 if (stat(restore_sector_file, &statbuf) < 0) {
349 perror(restore_sector_file);
350 error(_("cannot stat partition restore file (%s)\n"),
351 restore_sector_file);
354 if (statbuf.st_size % 516) {
355 error(_("partition restore file has wrong size - not restoring\n"));
358 if (!(ss = (char *) malloc(statbuf.st_size))) {
359 error(_("out of memory?\n"));
362 fdin = open(restore_sector_file, O_RDONLY);
364 perror(restore_sector_file);
365 error(_("cannot open partition restore file (%s)\n"),
366 restore_sector_file);
369 if (read(fdin, ss, statbuf.st_size) != statbuf.st_size) {
371 error(_("error reading %s\n"), restore_sector_file);
375 fdout = open(dev, O_WRONLY);
378 error(_("cannot open device %s for writing\n"), dev);
383 ct = statbuf.st_size/516;
385 sno = chars_to_ulong((unsigned char *) ss);
386 if (!sseek(dev, fdout, sno))
388 if (write(fdout, ss+4, 512) != 512) {
390 error(_("error writing sector %lu on %s\n"), sno, dev);
397 reread_disk_partition(dev, fdout);
412 * C. About heads, sectors and cylinders
416 * <linux/hdreg.h> defines HDIO_GETGEO and
417 * struct hd_geometry {
418 * unsigned char heads;
419 * unsigned char sectors;
420 * unsigned short cylinders;
421 * unsigned long start;
424 * For large disks g.cylinders is truncated, so we use BLKGETSIZE.
428 * We consider several geometries for a disk:
429 * B - the BIOS geometry, gotten from the kernel via HDIO_GETGEO
430 * F - the fdisk geometry
431 * U - the user-specified geometry
433 * 0 means unspecified / unknown
436 unsigned long long total_size; /* in sectors */
437 unsigned long cylindersize; /* in sectors */
438 unsigned long heads, sectors, cylinders;
442 static struct geometry
443 get_geometry(char *dev, int fd, int silent) {
444 struct hd_geometry g;
446 unsigned long long sectors;
450 if (ioctl(fd, HDIO_GETGEO, &g))
453 g.heads = g.sectors = g.cylinders = g.start = 0;
455 do_warn(_("Disk %s: cannot get geometry\n"), dev);
460 R.sectors = g.sectors;
461 R.cylindersize = R.heads * R.sectors;
465 if (blkdev_get_sectors(fd, §ors) == -1) {
466 /* maybe an ordinary file */
469 if (fstat(fd, &s) == 0 && S_ISREG(s.st_mode))
470 R.total_size = (s.st_size >> 9);
472 do_warn(_("Disk %s: cannot get size\n"), dev);
474 R.total_size = sectors;
476 if (R.cylindersize && R.total_size) {
477 sectors /= R.cylindersize;
488 get_cylindersize(char *dev, int fd, int silent) {
491 R = get_geometry(dev, fd, silent);
493 B.heads = (U.heads ? U.heads : R.heads ? R.heads : 255);
494 B.sectors = (U.sectors ? U.sectors : R.sectors ? R.sectors : 63);
495 B.cylinders = (U.cylinders ? U.cylinders : R.cylinders);
497 B.cylindersize = B.heads * B.sectors;
498 B.total_size = R.total_size;
500 if (B.cylinders == 0 && B.cylindersize != 0)
501 B.cylinders = B.total_size / B.cylindersize;
503 if (R.start && !force) {
505 _("Warning: start=%lu - this looks like a partition rather than\n"
506 "the entire disk. Using fdisk on it is probably meaningless.\n"
507 "[Use the --force option if you really want this]\n"), R.start);
511 if (R.heads && B.heads != R.heads)
512 warn(_("Warning: HDIO_GETGEO says that there are %lu heads\n"),
514 if (R.sectors && B.sectors != R.sectors)
515 warn(_("Warning: HDIO_GETGEO says that there are %lu sectors\n"),
517 if (R.cylinders && B.cylinders != R.cylinders
518 && B.cylinders < 65536 && R.cylinders < 65536)
519 warn(_("Warning: BLKGETSIZE/HDIO_GETGEO says that there are %lu cylinders\n"),
524 warn(_("Warning: unlikely number of sectors (%lu) - usually at most 63\n"
525 "This will give problems with all software that uses C/H/S addressing.\n"),
528 printf(_("\nDisk %s: %lu cylinders, %lu heads, %lu sectors/track\n"),
529 dev, B.cylinders, B.heads, B.sectors);
532 typedef struct { unsigned char h,s,c; } PACKED chs; /* has some c bits in s */
533 chs zero_chs = { 0,0,0 };
535 typedef struct { unsigned long h,s,c; } longchs;
536 longchs zero_longchs;
539 longchs_to_chs (longchs aa, struct geometry G) {
542 if (aa.h < 256 && aa.s < 64 && aa.c < 1024) {
544 a.s = aa.s | ((aa.c >> 2) & 0xc0);
546 } else if (G.heads && G.sectors) {
548 a.s = G.sectors | 0xc0;
556 chs_to_longchs (chs a) {
562 aa.c = (aa.c << 2) + a.c;
567 ulong_to_longchs (unsigned long sno, struct geometry G) {
570 if (G.heads && G.sectors && G.cylindersize) {
571 aa.s = 1 + sno % G.sectors;
572 aa.h = (sno / G.sectors) % G.heads;
573 aa.c = sno / G.cylindersize;
581 ulong_to_chs (unsigned long sno, struct geometry G) {
582 return longchs_to_chs(ulong_to_longchs(sno, G), G);
587 longchs_to_ulong (longchs aa, struct geometry G) {
588 return (aa.c*G.cylindersize + aa.h*G.sectors + aa.s - 1);
592 chs_to_ulong (chs a, struct geometry G) {
593 return longchs_to_ulong(chs_to_longchs(a), G);
598 is_equal_chs (chs a, chs b) {
599 return (a.h == b.h && a.s == b.s && a.c == b.c);
603 chs_ok (chs a, char *v, char *w) {
604 longchs aa = chs_to_longchs(a);
607 if (is_equal_chs(a, zero_chs))
609 if (B.heads && aa.h >= B.heads) {
610 warn(_("%s of partition %s has impossible value for head: "
611 "%lu (should be in 0-%lu)\n"), w, v, aa.h, B.heads-1);
614 if (B.sectors && (aa.s == 0 || aa.s > B.sectors)) {
615 warn(_("%s of partition %s has impossible value for sector: "
616 "%lu (should be in 1-%lu)\n"), w, v, aa.s, B.sectors);
619 if (B.cylinders && aa.c >= B.cylinders) {
620 warn(_("%s of partition %s has impossible value for cylinders: "
621 "%lu (should be in 0-%lu)\n"), w, v, aa.c, B.cylinders-1);
628 * D. About system Ids
631 #define EMPTY_PARTITION 0
632 #define EXTENDED_PARTITION 5
633 #define WIN98_EXTENDED 0x0f
634 #define DM6_AUX1PARTITION 0x51
635 #define DM6_AUX3PARTITION 0x53
636 #define DM6_PARTITION 0x54
637 #define EZD_PARTITION 0x55
638 #define LINUX_SWAP 0x82
639 #define LINUX_NATIVE 0x83
640 #define LINUX_EXTENDED 0x85
641 #define BSD_PARTITION 0xa5
642 #define NETBSD_PARTITION 0xa9
644 /* List of partition types now in i386_sys_types.c */
647 sysname(unsigned char type) {
650 for (s = i386_sys_types; s->name; s++)
660 printf(_("Id Name\n\n"));
661 for (s = i386_sys_types; s->name; s++)
662 printf("%2x %s\n", s->type, _(s->name));
666 is_extended(unsigned char type) {
667 return (type == EXTENDED_PARTITION
668 || type == LINUX_EXTENDED
669 || type == WIN98_EXTENDED);
673 is_bsd(unsigned char type) {
674 return (type == BSD_PARTITION || type == NETBSD_PARTITION);
678 * E. About partitions
681 /* MS/DOS partition */
684 unsigned char bootable; /* 0 or 0x80 */
686 unsigned char sys_type;
688 unsigned int start_sect; /* starting sector counting from 0 */
689 unsigned int nr_sects; /* nr of sectors in partition */
692 /* Unfortunately, partitions are not aligned, and non-Intel machines
693 are unhappy with non-aligned integers. So, we need a copy by hand. */
695 copy_to_int(unsigned char *cp) {
706 copy_from_int(int m, char *cp) {
707 *cp++ = (m & 0xff); m >>= 8;
708 *cp++ = (m & 0xff); m >>= 8;
709 *cp++ = (m & 0xff); m >>= 8;
714 copy_to_part(char *cp, struct partition *p) {
716 p->begin_chs.h = *cp++;
717 p->begin_chs.s = *cp++;
718 p->begin_chs.c = *cp++;
720 p->end_chs.h = *cp++;
721 p->end_chs.s = *cp++;
722 p->end_chs.c = *cp++;
723 p->start_sect = copy_to_int((unsigned char *) cp);
724 p->nr_sects = copy_to_int((unsigned char *) cp+4);
728 copy_from_part(struct partition *p, char *cp) {
730 *cp++ = p->begin_chs.h;
731 *cp++ = p->begin_chs.s;
732 *cp++ = p->begin_chs.c;
734 *cp++ = p->end_chs.h;
735 *cp++ = p->end_chs.s;
736 *cp++ = p->end_chs.c;
737 copy_from_int(p->start_sect, cp);
738 copy_from_int(p->nr_sects, cp+4);
741 /* Roughly speaking, Linux doesn't use any of the above fields except
742 for partition type, start sector and number of sectors. (However,
743 see also linux/drivers/scsi/fdomain.c.)
744 The only way partition type is used (in the kernel) is the comparison
745 for equality with EXTENDED_PARTITION (and these Disk Manager types). */
750 unsigned long sector, offset; /* disk location of this info */
752 struct part_desc *ep; /* extended partition containing this one */
758 static struct part_desc *
759 outer_extended_partition(struct part_desc *p) {
766 is_parent(struct part_desc *pp, struct part_desc *p) {
776 struct part_desc partitions[512];
780 /* determine where on the disk this information goes */
782 add_sector_and_offset(struct disk_desc *z) {
786 for (pno = 0; pno < z->partno; pno++) {
787 p = &(z->partitions[pno]);
788 p->offset = 0x1be + (pno%4)*sizeof(struct partition);
789 p->sector = (p->ep ? p->ep->start : 0);
793 /* tell the kernel to reread the partition tables */
795 reread_ioctl(int fd) {
797 if (ioctl(fd, BLKRRPART))
804 /* 2.6.8 returns EIO for a zero table */
812 is_blockdev(int fd) {
815 return(fstat(fd, &statbuf) == 0 && S_ISBLK(statbuf.st_mode));
818 /* reread after writing */
820 reread_disk_partition(char *dev, int fd) {
821 printf(_("Re-reading the partition table ...\n"));
824 sleep(3); /* superfluous since 1.3.20 */
826 if (reread_ioctl(fd) && is_blockdev(fd))
827 do_warn(_("The command to re-read the partition table failed.\n"
828 "Run partprobe(8), kpartx(8) or reboot your system now,\n"
829 "before using mkfs\n"));
833 do_warn(_("Error closing %s\n"), dev);
838 /* find Linux name of this partition, assuming that it will have a name */
840 index_to_linux(int pno, struct disk_desc *z) {
842 struct part_desc *p = &(z->partitions[0]);
843 for (i=0; i<pno; i++,p++)
844 if (i < 4 || (p->size > 0 && !is_extended(p->p.sys_type)))
850 linux_to_index(int lpno, struct disk_desc *z) {
852 struct part_desc *p = &(z->partitions[0]);
853 for (i=0; i<z->partno && ct < lpno; i++,p++)
854 if ((i < 4 || (p->size > 0 && !is_extended(p->p.sys_type)))
861 asc_to_index(char *pnam, struct disk_desc *z) {
868 pno = linux_to_index(pnum, z);
870 if (!(pno >= 0 && pno < z->partno))
871 fatal(_("%s: no such partition\n"), pnam);
876 * List partitions - in terms of sectors, blocks or cylinders
883 int default_format = F_MEGABYTE;
884 int specified_format = 0;
885 int show_extended = 0;
894 do_warn(_("unrecognized format - using sectors\n"));
895 case 'S': specified_format = F_SECTOR; break;
896 case 'B': specified_format = F_BLOCK; break;
897 case 'C': specified_format = F_CYLINDER; break;
898 case 'M': specified_format = F_MEGABYTE; break;
903 unitsize(int format) {
904 default_format = (B.cylindersize ? F_CYLINDER : F_MEGABYTE);
905 if (!format && !(format = specified_format))
906 format = default_format;
912 return B.cylindersize;
923 get_disksize(int format) {
924 unsigned long cs = B.cylinders;
925 if (cs && leave_last)
927 return (cs * B.cylindersize) / unitsize(format);
931 out_partition_header(char *dev, int format, struct geometry G) {
933 printf("# partition table of %s\n", dev);
934 printf("unit: sectors\n\n");
938 default_format = (G.cylindersize ? F_CYLINDER : F_MEGABYTE);
939 if (!format && !(format = specified_format))
940 format = default_format;
944 do_warn(_("unimplemented format - using %s\n"),
945 G.cylindersize ? _("cylinders") : _("sectors"));
947 if (G.cylindersize) {
948 printf(_("Units = cylinders of %lu bytes, blocks of 1024 bytes"
949 ", counting from %d\n\n"),
950 G.cylindersize<<9, increment);
951 printf(_(" Device Boot Start End #cyls #blocks Id System\n"));
956 printf(_("Units = sectors of 512 bytes, counting from %d\n\n"),
958 printf(_(" Device Boot Start End #sectors Id System\n"));
961 printf(_("Units = blocks of 1024 bytes, counting from %d\n\n"),
963 printf(_(" Device Boot Start End #blocks Id System\n"));
966 printf(_("Units = mebibytes of 1048576 bytes, blocks of 1024 bytes"
967 ", counting from %d\n\n"), increment);
968 printf(_(" Device Boot Start End MiB #blocks Id System\n"));
974 out_rounddown(int width, unsigned long n, unsigned long unit, int inc) {
975 printf("%*lu", width, inc + n/unit);
977 putchar((n % unit) ? '+' : ' ');
982 out_roundup(int width, unsigned long n, unsigned long unit, int inc) {
983 if (n == (unsigned long)(-1))
984 printf("%*s", width, "-");
986 printf("%*lu", width, inc + n/unit);
988 putchar(((n+1) % unit) ? '-' : ' ');
993 out_roundup_size(int width, unsigned long n, unsigned long unit) {
994 printf("%*lu", width, (n+unit-1)/unit);
996 putchar((n % unit) ? '-' : ' ');
1000 static struct geometry
1001 get_fdisk_geometry_one(struct part_desc *p) {
1004 memset(&G, 0, sizeof(struct geometry));
1005 chs b = p->p.end_chs;
1006 longchs bb = chs_to_longchs(b);
1009 G.cylindersize = G.heads*G.sectors;
1014 get_fdisk_geometry(struct disk_desc *z) {
1015 struct part_desc *p;
1017 struct geometry G0, G;
1019 memset(&G0, 0, sizeof(struct geometry));
1021 for (pno=0; pno < z->partno; pno++) {
1022 p = &(z->partitions[pno]);
1023 if (p->size != 0 && p->p.sys_type != 0) {
1024 G = get_fdisk_geometry_one(p);
1028 } else if (G.heads != G0.heads || G.sectors != G0.sectors) {
1034 F = (agree ? G0 : B);
1035 return (F.sectors != B.sectors || F.heads != B.heads);
1039 out_partition(char *dev, int format, struct part_desc *p,
1040 struct disk_desc *z, struct geometry G) {
1041 unsigned long start, end, size;
1044 if (!format && !(format = specified_format))
1045 format = default_format;
1047 pno = p - &(z->partitions[0]); /* our index */
1048 lpno = index_to_linux(pno, z); /* name of next one that has a name */
1049 if (pno == linux_to_index(lpno, z)) /* was that us? */
1050 printf("%s", partname(dev, lpno, 10)); /* yes */
1051 else if (show_extended)
1055 putchar(dump ? ':' : ' ');
1058 end = p->start + p->size - 1;
1062 printf(" start=%9lu", start);
1063 printf(", size=%9lu", size);
1064 if (p->ptype == DOS_TYPE) {
1065 printf(", Id=%2x", p->p.sys_type);
1066 if (p->p.bootable == 0x80)
1067 printf(", bootable");
1073 if (p->ptype != DOS_TYPE || p->p.bootable == 0)
1075 else if (p->p.bootable == 0x80)
1078 printf(" ? "); /* garbage */
1082 if (G.cylindersize) {
1083 out_rounddown(6, start, G.cylindersize, increment);
1084 out_roundup(6, end, G.cylindersize, increment);
1085 out_roundup_size(6, size, G.cylindersize);
1086 out_rounddown(9, size, 2, 0);
1092 out_rounddown(9, start, 1, increment);
1093 out_roundup(9, end, 1, increment);
1094 out_rounddown(10, size, 1, 0);
1098 printf("%8lu,%3lu ",
1099 p->sector/2, ((p->sector & 1) ? 512 : 0) + p->offset);
1101 out_rounddown(8, start, 2, increment);
1102 out_roundup(8, end, 2, increment);
1103 out_rounddown(9, size, 2, 0);
1106 out_rounddown(5, start, 2048, increment);
1107 out_roundup(5, end, 2048, increment);
1108 out_roundup_size(5, size, 2048);
1109 out_rounddown(9, size, 2, 0);
1112 if (p->ptype == DOS_TYPE) {
1114 p->p.sys_type, sysname(p->p.sys_type));
1119 /* Is chs as we expect? */
1120 if (!quiet && p->ptype == DOS_TYPE) {
1123 a = (size ? ulong_to_chs(start,G) : zero_chs);
1125 aa = chs_to_longchs(a);
1126 bb = chs_to_longchs(b);
1127 if (a.s && !is_equal_chs(a, b))
1128 do_warn(_("\t\tstart: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1129 aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1130 a = (size ? ulong_to_chs(end,G) : zero_chs);
1132 aa = chs_to_longchs(a);
1133 bb = chs_to_longchs(b);
1134 if (a.s && !is_equal_chs(a, b))
1135 do_warn(_("\t\tend: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1136 aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1137 if (G.cylinders && G.cylinders < 1024 && bb.c > G.cylinders)
1138 do_warn(_("partition ends on cylinder %ld, beyond the end of the disk\n"),
1144 out_partitions(char *dev, struct disk_desc *z) {
1145 int pno, format = 0;
1148 do_warn(_("No partitions found\n"));
1150 if (get_fdisk_geometry(z) && !dump) {
1152 _("Warning: The partition table looks like it was made\n"
1153 " for C/H/S=*/%ld/%ld (instead of %ld/%ld/%ld).\n"
1154 "For this listing I'll assume that geometry.\n"),
1155 F.heads, F.sectors, B.cylinders, B.heads, B.sectors);
1158 out_partition_header(dev, format, F);
1159 for(pno=0; pno < z->partno; pno++) {
1160 out_partition(dev, format, &(z->partitions[pno]), z, F);
1161 if (show_extended && pno%4==3)
1168 disj(struct part_desc *p, struct part_desc *q) {
1170 ((p->start + p->size <= q->start)
1171 || (is_extended(p->p.sys_type)
1172 && q->start + q->size <= p->start + p->size));
1176 pnumber(struct part_desc *p, struct disk_desc *z) {
1177 static char buf[20];
1179 struct part_desc *p0 = &(z->partitions[0]);
1181 this = index_to_linux(p-p0, z);
1182 next = index_to_linux(p-p0+1, z);
1185 sprintf(buf, "%d", this);
1187 sprintf(buf, "[%d]", this);
1192 partitions_ok(struct disk_desc *z) {
1193 struct part_desc *partitions = &(z->partitions[0]), *p, *q;
1194 int partno = z->partno;
1196 #define PNO(p) pnumber(p, z)
1198 /* Have at least 4 partitions been defined? */
1201 fatal(_("no partition table present.\n"));
1203 fatal(_("strange, only %d partitions defined.\n"), partno);
1207 /* Are the partitions of size 0 marked empty?
1208 And do they have start = 0? And bootable = 0? */
1209 for (p = partitions; p - partitions < partno; p++)
1211 if (p->p.sys_type != EMPTY_PARTITION)
1212 warn(_("Warning: partition %s has size 0 but is not marked Empty\n"),
1214 else if (p->p.bootable != 0)
1215 warn(_("Warning: partition %s has size 0 and is bootable\n"),
1217 else if (p->p.start_sect != 0)
1218 warn(_("Warning: partition %s has size 0 and nonzero start\n"),
1220 /* all this is probably harmless, no error return */
1223 /* Are the logical partitions contained in their extended partitions? */
1224 for (p = partitions+4; p < partitions+partno; p++)
1225 if (p->ptype == DOS_TYPE)
1226 if (p->size && !is_extended(p->p.sys_type)) {
1228 if (p->start < q->start || p->start + p->size > q->start + q->size) {
1229 warn(_("Warning: partition %s "), PNO(p));
1230 warn(_("is not contained in partition %s\n"), PNO(q));
1235 /* Are the data partitions mutually disjoint? */
1236 for (p = partitions; p < partitions+partno; p++)
1237 if (p->size && !is_extended(p->p.sys_type))
1238 for (q = p+1; q < partitions+partno; q++)
1239 if (q->size && !is_extended(q->p.sys_type))
1240 if (!((p->start > q-> start) ? disj(q,p) : disj(p,q))) {
1241 warn(_("Warning: partitions %s "), PNO(p));
1242 warn(_("and %s overlap\n"), PNO(q));
1246 /* Are the data partitions and the extended partition
1247 table sectors disjoint? */
1248 for (p = partitions; p < partitions+partno; p++)
1249 if (p->size && !is_extended(p->p.sys_type))
1250 for (q = partitions; q < partitions+partno; q++)
1251 if (is_extended(q->p.sys_type))
1252 if (p->start <= q->start && p->start + p->size > q->start) {
1253 warn(_("Warning: partition %s contains part of "
1254 "the partition table (sector %lu),\n"
1255 "and will destroy it when filled\n"),
1260 /* Do they start past zero and end before end-of-disk? */
1261 { unsigned long ds = get_disksize(F_SECTOR);
1262 for (p = partitions; p < partitions+partno; p++)
1264 if (p->start == 0) {
1265 warn(_("Warning: partition %s starts at sector 0\n"), PNO(p));
1268 if (p->size && p->start + p->size > ds) {
1269 warn(_("Warning: partition %s extends past end of disk\n"),
1276 /* At most one chain of DOS extended partitions ? */
1277 /* It seems that the OS/2 fdisk has the additional requirement
1278 that the extended partition must be the fourth one */
1280 for (p = partitions; p < partitions+4; p++)
1281 if (p->p.sys_type == EXTENDED_PARTITION)
1283 if (ect > 1 && !Linux) {
1284 warn(_("Among the primary partitions, at most one can be extended\n"
1285 " (although this is not a problem under Linux)\n"));
1291 * Do all partitions start at a cylinder boundary ?
1292 * (this is not required for Linux)
1293 * The first partition starts after MBR.
1294 * Logical partitions start slightly after the containing extended partn.
1296 if (B.cylindersize) {
1297 for(p = partitions; p < partitions+partno; p++)
1299 if (p->start % B.cylindersize != 0
1300 && (!p->ep || p->start / B.cylindersize != p->ep->start / B.cylindersize)
1301 && (p->p.start_sect >= B.cylindersize)) {
1302 warn(_("Warning: partition %s does not start "
1303 "at a cylinder boundary\n"), PNO(p));
1307 if ((p->start + p->size) % B.cylindersize) {
1308 warn(_("Warning: partition %s does not end "
1309 "at a cylinder boundary\n"), PNO(p));
1316 /* Usually, one can boot only from primary partitions. */
1317 /* In fact, from a unique one only. */
1318 /* do not warn about bootable extended partitions -
1319 often LILO is there */
1321 for(p = partitions; p < partitions+partno; p++)
1322 if (p->p.bootable) {
1324 pno = p - partitions;
1325 else if (p - partitions < 4) {
1326 warn(_("Warning: more than one primary partition is marked "
1327 "bootable (active)\n"
1328 "This does not matter for LILO, but the DOS MBR will "
1329 "not boot this disk.\n"));
1332 if (p - partitions >= 4) {
1333 warn(_("Warning: usually one can boot from primary partitions "
1334 "only\nLILO disregards the `bootable' flag.\n"));
1338 if (pno == -1 || pno >= 4)
1339 warn(_("Warning: no primary partition is marked bootable (active)\n"
1340 "This does not matter for LILO, but the DOS MBR will "
1341 "not boot this disk.\n"));
1344 /* Is chs as we expect? */
1345 for(p = partitions; p < partitions+partno; p++)
1346 if (p->ptype == DOS_TYPE) {
1349 a = p->size ? ulong_to_chs(p->start,B) : zero_chs;
1351 aa = chs_to_longchs(a);
1352 bb = chs_to_longchs(b);
1353 if (!chs_ok(b, PNO(p), _("start")))
1355 if (a.s && !is_equal_chs(a, b))
1356 warn(_("partition %s: start: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1357 PNO(p), aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1358 a = p->size ? ulong_to_chs(p->start + p->size - 1, B) : zero_chs;
1360 aa = chs_to_longchs(a);
1361 bb = chs_to_longchs(b);
1362 if (!chs_ok(b, PNO(p), _("end")))
1364 if (a.s && !is_equal_chs(a, b))
1365 warn(_("partition %s: end: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1366 PNO(p), aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1367 if (B.cylinders && B.cylinders < 1024 && bb.c > B.cylinders)
1368 warn(_("partition %s ends on cylinder %ld, beyond the end of the disk\n"),
1378 extended_partition(char *dev, int fd, struct part_desc *ep, struct disk_desc *z) {
1381 unsigned long start, here, next;
1382 int i, moretodo = 1;
1384 struct part_desc *partitions = &(z->partitions[0]);
1385 int pno = z->partno;
1387 here = start = ep->start;
1389 if (B.cylindersize && start % B.cylindersize) {
1392 here = start -= (start % B.cylindersize);
1393 do_warn(_("Warning: shifted start of the extd partition "
1395 "(For listing purposes only. "
1396 "Do not change its contents.)\n"),
1399 do_warn(_("Warning: extended partition does not start at a "
1400 "cylinder boundary.\n"
1401 "DOS and Linux will interpret the contents differently.\n"));
1408 if (!(s = get_sector(dev, fd, here)))
1411 if (!msdos_signature(s))
1414 cp = s->data + 0x1be;
1416 if (pno+4 >= SIZE(z->partitions)) {
1417 do_warn(_("too many partitions - ignoring those past nr (%d)\n"),
1424 for (i=0; i<4; i++,cp += sizeof(struct partition)) {
1425 partitions[pno].sector = here;
1426 partitions[pno].offset = cp - s->data;
1427 partitions[pno].ep = ep;
1428 copy_to_part(cp,&p);
1429 if (is_extended(p.sys_type)) {
1430 partitions[pno].start = start + p.start_sect;
1432 do_warn(_("tree of partitions?\n"));
1434 next = partitions[pno].start; /* follow `upper' branch */
1437 partitions[pno].start = here + p.start_sect;
1439 partitions[pno].size = p.nr_sects;
1440 partitions[pno].ptype = DOS_TYPE;
1441 partitions[pno].p = p;
1450 #define BSD_DISKMAGIC (0x82564557UL)
1451 #define BSD_MAXPARTITIONS 16
1452 #define BSD_FS_UNUSED 0
1453 typedef unsigned char u8;
1454 typedef unsigned short u16;
1455 typedef unsigned int u32;
1456 struct bsd_disklabel {
1459 char d_typename[16];
1460 char d_packname[16];
1464 u16 d_npartitions; /* number of partitions in following */
1466 struct bsd_partition { /* the partition table */
1467 u32 p_size; /* number of sectors in partition */
1468 u32 p_offset; /* starting sector */
1469 u32 p_fsize; /* filesystem basic fragment size */
1470 u8 p_fstype; /* filesystem type, see below */
1471 u8 p_frag; /* filesystem fragments per block */
1472 u16 p_cpg; /* filesystem cylinders per group */
1473 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
1477 bsd_partition(char *dev, int fd, struct part_desc *ep, struct disk_desc *z) {
1478 struct bsd_disklabel *l;
1479 struct bsd_partition *bp, *bp0;
1480 unsigned long start = ep->start;
1482 struct part_desc *partitions = &(z->partitions[0]);
1483 int pno = z->partno;
1485 if (!(s = get_sector(dev,fd,start+1)))
1487 l = (struct bsd_disklabel *) (s->data);
1488 if (l->d_magic != BSD_DISKMAGIC || l->d_magic2 != BSD_DISKMAGIC)
1491 bp = bp0 = &l->d_partitions[0];
1492 while (bp - bp0 < BSD_MAXPARTITIONS && bp - bp0 < l->d_npartitions) {
1493 if (pno+1 >= SIZE(z->partitions)) {
1494 do_warn(_("too many partitions - ignoring those "
1495 "past nr (%d)\n"), pno-1);
1498 if (bp->p_fstype != BSD_FS_UNUSED) {
1499 partitions[pno].start = bp->p_offset;
1500 partitions[pno].size = bp->p_size;
1501 partitions[pno].sector = start+1;
1502 partitions[pno].offset = (char *)bp - (char *)bp0;
1503 partitions[pno].ep = 0;
1504 partitions[pno].ptype = BSD_TYPE;
1513 msdos_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1516 struct partition pt;
1518 struct part_desc *partitions = &(z->partitions[0]);
1519 int pno = z->partno;
1522 bsd_later = (get_linux_version() >= KERNEL_VERSION(2,3,40));
1525 if (!(s = get_sector(dev, fd, start)))
1528 if (!msdos_signature(s))
1531 cp = s->data + 0x1be;
1532 copy_to_part(cp,&pt);
1534 /* If I am not mistaken, recent kernels will hide this from us,
1535 so we will never actually see traces of a Disk Manager */
1536 if (pt.sys_type == DM6_PARTITION
1537 || pt.sys_type == EZD_PARTITION
1538 || pt.sys_type == DM6_AUX1PARTITION
1539 || pt.sys_type == DM6_AUX3PARTITION) {
1540 do_warn(_("detected Disk Manager - unable to handle that\n"));
1543 { unsigned int sig = *(unsigned short *)(s->data + 2);
1545 && *(unsigned short *)(s->data + sig) == 0x55aa
1546 && (1 & *(unsigned char *)(s->data + sig + 2))) {
1547 do_warn(_("DM6 signature found - giving up\n"));
1552 for (pno=0; pno<4; pno++,cp += sizeof(struct partition)) {
1553 partitions[pno].sector = start;
1554 partitions[pno].offset = cp - s->data;
1555 copy_to_part(cp,&pt);
1556 partitions[pno].start = start + pt.start_sect;
1557 partitions[pno].size = pt.nr_sects;
1558 partitions[pno].ep = 0;
1559 partitions[pno].p = pt;
1564 for (i=0; i<4; i++) {
1565 if (is_extended(partitions[i].p.sys_type)) {
1566 if (!partitions[i].size) {
1567 do_warn(_("strange..., an extended partition of size 0?\n"));
1570 extended_partition(dev, fd, &partitions[i], z);
1572 if (!bsd_later && is_bsd(partitions[i].p.sys_type)) {
1573 if (!partitions[i].size) {
1574 do_warn(_("strange..., a BSD partition of size 0?\n"));
1577 bsd_partition(dev, fd, &partitions[i], z);
1582 for (i=0; i<4; i++) {
1583 if (is_bsd(partitions[i].p.sys_type)) {
1584 if (!partitions[i].size) {
1585 do_warn(_("strange..., a BSD partition of size 0?\n"));
1588 bsd_partition(dev, fd, &partitions[i], z);
1597 osf_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1602 sun_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1607 amiga_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1612 get_partitions(char *dev, int fd, struct disk_desc *z) {
1615 if (!msdos_partition(dev, fd, 0, z)
1616 && !osf_partition(dev, fd, 0, z)
1617 && !sun_partition(dev, fd, 0, z)
1618 && !amiga_partition(dev, fd, 0, z)) {
1619 do_warn(_(" %s: unrecognized partition table type\n"), dev);
1625 write_partitions(char *dev, int fd, struct disk_desc *z) {
1627 struct part_desc *partitions = &(z->partitions[0]), *p;
1628 int pno = z->partno;
1631 do_warn(_("-n flag was given: Nothing changed\n"));
1635 for (p = partitions; p < partitions+pno; p++) {
1636 s = get_sector(dev, fd, p->sector);
1638 s->to_be_written = 1;
1639 if (p->ptype == DOS_TYPE) {
1640 copy_from_part(&(p->p), s->data + p->offset);
1641 s->data[510] = 0x55;
1642 s->data[511] = 0xaa;
1645 if (save_sector_file) {
1646 if (!save_sectors(dev, fd)) {
1647 fatal(_("Failed saving the old sectors - aborting\n"));
1651 if (!write_sectors(dev, fd)) {
1652 error(_("Failed writing the partition on %s\n"), dev);
1659 * F. The standard input
1664 * <start> <size> <type> <bootable> <c,h,s> <c,h,s>
1665 * Fields are separated by whitespace or comma or semicolon possibly
1666 * followed by whitespace; initial and trailing whitespace is ignored.
1667 * Numbers can be octal, decimal or hexadecimal, decimal is default
1668 * The <c,h,s> parts can (and probably should) be omitted.
1669 * Bootable is specified as [*|-], with as default not-bootable.
1670 * Type is given in hex, without the 0x prefix, or is [E|S|L|X], where
1671 * L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), and E
1672 * is EXTENDED_PARTITION (5), X is LINUX_EXTENDED (85).
1673 * The default value of start is the first nonassigned sector/cylinder/...
1674 * The default value of size is as much as possible (until next
1675 * partition or end-of-disk).
1676 * .: end of chain of extended partitions.
1678 * On interactive input an empty line means: all defaults.
1679 * Otherwise empty lines are ignored.
1692 { 3, "bootable", 1 },
1702 * Read a line, split it into fields
1704 * (some primitive handwork, but a more elaborate parser seems
1711 read_stdin(char **fields, char *line, int fieldssize, int linesize) {
1715 /* boolean true and empty string at start */
1718 for (fno=0; fno < fieldssize; fno++)
1719 fields[fno] = line + 1;
1722 /* read a line from stdin */
1723 lp = fgets(line+2, linesize-2, stdin);
1728 if (!(lp = strchr(lp, '\n')))
1729 fatal(_("long or incomplete input line - quitting\n"));
1732 /* remove comments, if any */
1733 if ((lp = strchr(line+2, '#')) != 0)
1736 /* recognize a few commands - to be expanded */
1737 if (!strcmp(line+2, "unit: sectors")) {
1738 specified_format = F_SECTOR;
1742 /* dump style? - then bad input is fatal */
1743 if ((ip = strchr(line+2, ':')) != 0) {
1752 for(d = dumpflds; d-dumpflds < SIZE(dumpflds); d++) {
1753 if (!strncmp(ip, d->fldname, strlen(d->fldname))) {
1754 ip += strlen(d->fldname);
1758 fields[d->fldno] = line;
1759 else if (*ip == '=') {
1760 while(isspace(*++ip)) ;
1761 fields[d->fldno] = ip;
1762 while(isalnum(*ip)) /* 0x07FF */
1765 fatal(_("input error: `=' expected after %s field\n"),
1767 if (fno <= d->fldno)
1771 if (*ip != ',' && *ip != ';')
1772 fatal(_("input error: unexpected character %c after %s field\n"),
1778 fatal(_("unrecognized input: %s\n"), ip);
1781 /* split line into fields */
1784 while((c = *ip++) != 0) {
1785 if (!lp[-1] && (c == '\t' || c == ' '))
1787 else if (c == '\t' || c == ' ' || c == ',' || c == ';') {
1789 if (fno < fieldssize)
1796 if (lp == fields[fno-1])
1801 /* read a number, use default if absent */
1802 /* a sign gives an offset from the default */
1804 get_ul(char *u, unsigned long *up, unsigned long def, int base) {
1812 } else if (*u == '-') {
1818 val = strtoul(u, &nu, base);
1819 if (errno == ERANGE) {
1820 do_warn(_("number too big\n"));
1824 do_warn(_("trailing junk after number\n"));
1829 else if (sign == -1)
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 int partitions_in_order = 0;
1850 int all_logicals_inside_outermost_extended = 1;
1851 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) {
1857 unsigned long ff, fff;
1858 unsigned long unit = unitsize(format);
1859 struct part_desc *partitions = &(z->partitions[0]), *pp = 0;
1861 /* if containing ep undefined, look at its container */
1862 if (ep && ep->p.sys_type == EMPTY_PARTITION)
1866 if (boxes == NESTED || (boxes == CHAINED && !is_extended))
1868 else if (all_logicals_inside_outermost_extended)
1869 pp = outer_extended_partition(ep);
1872 ff = pp ? (pp->start + unit - 1) / unit : 0;
1874 /* rounding up wastes almost an entire cylinder - round down
1875 and leave it to compute_start_sect() to fix the difference */
1876 ff = pp ? pp->start / unit : 0;
1878 /* MBR and 1st sector of an extended partition are never free */
1883 for(pp = partitions; pp < partitions+pno; pp++) {
1884 if (!is_parent(pp, ep) && pp->size > 0) {
1885 if ((partitions_in_order || pp->start / unit <= ff
1886 || (mid && pp->start / unit <= mid))
1887 && (fff = (pp->start + pp->size + unit - 1) / unit) > ff) {
1897 /* find the default value for <size> - assuming entire units */
1898 static unsigned long
1899 max_length(int pno, int is_extended, struct part_desc *ep, int format,
1900 unsigned long start, struct disk_desc *z) {
1902 unsigned long unit = unitsize(format);
1903 struct part_desc *partitions = &(z->partitions[0]), *pp = 0;
1905 /* if containing ep undefined, look at its container */
1906 if (ep && ep->p.sys_type == EMPTY_PARTITION)
1910 if (boxes == NESTED || (boxes == CHAINED && !is_extended))
1912 else if (all_logicals_inside_outermost_extended)
1913 pp = outer_extended_partition(ep);
1915 fu = pp ? (pp->start + pp->size) / unit : get_disksize(format);
1917 for(pp = partitions; pp < partitions+pno; pp++)
1918 if (!is_parent(pp, ep) && pp->size > 0
1919 && pp->start / unit >= start && pp->start / unit < fu)
1920 fu = pp->start / unit;
1922 return (fu > start) ? fu - start : 0;
1925 /* compute starting sector of a partition inside an extended one */
1926 /* return 0 on failure */
1927 /* ep is 0 or points to surrounding extended partition */
1929 compute_start_sect(struct part_desc *p, struct part_desc *ep) {
1931 int inc = (DOS && B.sectors) ? B.sectors : 1;
1934 if (ep && p->start + p->size >= ep->start + 1)
1935 delta = p->start - ep->start - inc;
1936 else if (p->start == 0 && p->size > 0)
1942 unsigned long old_size = p->size;
1945 if (is_extended(p->p.sys_type) && boxes == ONESECTOR)
1947 else if (old_size <= -delta) {
1948 warn(_("no room for partition descriptor\n"));
1953 : (is_extended(p->p.sys_type) ?
1954 outer_extended_partition(ep) : ep)->start);
1956 if (p->p.sys_type == EMPTY_PARTITION && p->size == 0) {
1957 p->p.start_sect = 0;
1958 p->p.begin_chs = zero_chs;
1959 p->p.end_chs = zero_chs;
1961 p->p.start_sect = p->start - base;
1962 p->p.begin_chs = ulong_to_chs(p->start,B);
1963 p->p.end_chs = ulong_to_chs(p->start + p->size - 1,B);
1965 p->p.nr_sects = p->size;
1969 /* build the extended partition surrounding a given logical partition */
1971 build_surrounding_extended(struct part_desc *p, struct part_desc *ep,
1972 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 || ep->start + ep->size < p->start + p->size) {
1981 warn(_("cannot build surrounding extended partition\n"));
1985 ep->start = p->start;
1986 if (boxes == CHAINED)
1992 ep->p.nr_sects = ep->size;
1994 ep->p.sys_type = EXTENDED_PARTITION;
1995 if (!compute_start_sect(ep, eep) || !compute_start_sect(p, ep)) {
1996 ep->p.sys_type = EMPTY_PARTITION;
2005 read_line(int pno, struct part_desc *ep, char *dev, int interactive,
2006 struct disk_desc *z) {
2009 int fno, pct = pno%4;
2010 struct part_desc p, *orig;
2011 unsigned long ff, ff1, ul, ml, ml1, def;
2012 int format, lpno, is_extd;
2017 lpno = index_to_linux(pno, z);
2020 if (pct == 0 && (show_extended || pno == 0))
2022 warn("%s:", partname(dev, lpno, 10));
2025 /* read input line - skip blank lines when reading from a file */
2027 fno = read_stdin(fields, line, SIZE(fields), SIZE(line));
2028 } while(fno == RD_CMD || (fno == 0 && !interactive));
2029 if (fno == RD_EOF) {
2031 } else if (fno > 10 && *(fields[10]) != 0) {
2032 do_warn(_("too many input fields\n"));
2036 if (fno == 1 && !strcmp(fields[0], ".")) {
2041 /* use specified format, but round to cylinders if F_MEGABYTE specified */
2043 if (B.cylindersize && specified_format == F_MEGABYTE)
2044 format = F_CYLINDER;
2046 orig = (one_only ? &(oldp.partitions[pno]) : 0);
2051 /* first read the type - we need to know whether it is extended */
2052 /* stop reading when input blank (defaults) and all is full */
2054 if (fno == 0) { /* empty line */
2055 if (orig && is_extended(orig->p.sys_type))
2057 ff = first_free(pno, is_extd, ep, format, 0, z);
2058 ml = max_length(pno, is_extd, ep, format, ff, z);
2059 if (ml == 0 && is_extd == 0) {
2061 ff = first_free(pno, is_extd, ep, format, 0, z);
2062 ml = max_length(pno, is_extd, ep, format, ff, z);
2064 if (ml == 0 && pno >= 4) {
2065 /* no free blocks left - don't read any further */
2066 warn(_("No room for more\n"));
2070 if (fno < 3 || !*(fields[2]))
2071 ul = orig ? orig->p.sys_type :
2072 (is_extd || (pno > 3 && pct == 1 && show_extended))
2073 ? EXTENDED_PARTITION : LINUX_NATIVE;
2074 else if (!strcmp(fields[2], "L"))
2076 else if (!strcmp(fields[2], "S"))
2078 else if (!strcmp(fields[2], "E"))
2079 ul = EXTENDED_PARTITION;
2080 else if (!strcmp(fields[2], "X"))
2081 ul = LINUX_EXTENDED;
2082 else if (get_ul(fields[2], &ul, LINUX_NATIVE, 16))
2085 warn(_("Illegal type\n"));
2089 is_extd = is_extended(ul);
2092 ff = first_free(pno, is_extd, ep, format, 0, z);
2093 ff1 = ff * unitsize(format);
2094 def = orig ? orig->start : (pno > 4 && pct > 1) ? 0 : ff1;
2095 if (fno < 1 || !*(fields[0]))
2098 if (get_ul(fields[0], &ul, def / unitsize(0), 0))
2100 p.start = ul * unitsize(0);
2101 p.start -= (p.start % unitsize(format));
2105 ml = max_length(pno, is_extd, ep, format, p.start / unitsize(format), z);
2106 ml1 = ml * unitsize(format);
2107 def = orig ? orig->size : (pno > 4 && pct > 1) ? 0 : ml1;
2108 if (fno < 2 || !*(fields[1]))
2111 if (get_ul(fields[1], &ul, def / unitsize(0), 0))
2113 p.size = ul * unitsize(0) + unitsize(format) - 1;
2114 p.size -= (p.size % unitsize(format));
2117 warn(_("Warning: given size (%lu) exceeds max allowable size (%lu)\n"),
2118 (p.size + unitsize(0) - 1) / unitsize(0), ml1 / unitsize(0));
2122 if (p.size == 0 && pno >= 4 && (fno < 2 || !*(fields[1]))) {
2123 warn(_("Warning: empty partition\n"));
2127 p.p.nr_sects = p.size;
2129 if (p.size == 0 && !orig) {
2130 if (fno < 1 || !*(fields[0]))
2132 if (fno < 3 || !*(fields[2]))
2133 p.p.sys_type = EMPTY_PARTITION;
2136 if (p.start < ff1 && p.size > 0) {
2137 warn(_("Warning: bad partition start (earliest %lu)\n"),
2138 (ff1 + unitsize(0) - 1) / unitsize(0));
2143 if (fno < 4 || !*(fields[3]))
2144 ul = (orig ? orig->p.bootable : 0);
2145 else if (!strcmp(fields[3], "-"))
2147 else if (!strcmp(fields[3], "*") || !strcmp(fields[3], "+"))
2150 warn(_("unrecognized bootable flag - choose - or *\n"));
2155 if (ep && ep->p.sys_type == EMPTY_PARTITION) {
2156 if (!build_surrounding_extended(&p, ep, z))
2159 if (!compute_start_sect(&p, ep))
2162 { longchs aa = chs_to_longchs(p.p.begin_chs), bb;
2166 } else if (fno < 7) {
2167 warn(_("partial c,h,s specification?\n"));
2169 } else if (get_ul(fields[4], &bb.c, aa.c, 0) ||
2170 get_ul(fields[5], &bb.h, aa.h, 0) ||
2171 get_ul(fields[6], &bb.s, aa.s, 0))
2173 p.p.begin_chs = longchs_to_chs(bb,B);
2175 { longchs aa = chs_to_longchs(p.p.end_chs), bb;
2179 } else if (fno < 10) {
2180 warn(_("partial c,h,s specification?\n"));
2182 } else if (get_ul(fields[7], &bb.c, aa.c, 0) ||
2183 get_ul(fields[8], &bb.h, aa.h, 0) ||
2184 get_ul(fields[9], &bb.s, aa.s, 0))
2186 p.p.end_chs = longchs_to_chs(bb, B);
2189 if (pno > 3 && p.size && show_extended && p.p.sys_type != EMPTY_PARTITION
2190 && (is_extended(p.p.sys_type) != (pct == 1))) {
2191 warn(_("Extended partition not where expected\n"));
2196 z->partitions[pno] = p;
2197 if (pno >= z->partno)
2198 z->partno += 4; /* reqd for out_partition() */
2201 out_partition(dev, 0, &(z->partitions[pno]), z, B);
2206 /* ep either points to the extended partition to contain this one,
2207 or to the empty partition that may become extended or is 0 */
2209 read_partition(char *dev, int interactive, int pno, struct part_desc *ep,
2210 struct disk_desc *z) {
2211 struct part_desc *p = &(z->partitions[pno]);
2215 *p = oldp.partitions[pno];
2216 if (one_only_pno != pno)
2218 } else if (!show_extended && pno > 4 && pno%4)
2221 while (!(i = read_line(pno, ep, dev, interactive, z)))
2223 fatal(_("bad input\n"));
2231 if (pno >= z->partno)
2237 read_partition_chain(char *dev, int interactive, struct part_desc *ep,
2238 struct disk_desc *z) {
2244 if (base+4 > SIZE(z->partitions)) {
2245 do_warn(_("too many partitions\n"));
2249 if (!read_partition(dev, interactive, base+i, ep, z))
2251 for (i=0; i<4; i++) {
2252 ep = &(z->partitions[base+i]);
2253 if (is_extended(ep->p.sys_type) && ep->size)
2257 /* nothing found - maybe an empty partition is going
2259 if (one_only || show_extended)
2261 ep = &(z->partitions[base+1]);
2262 if (ep->size || ep->p.sys_type != EMPTY_PARTITION)
2269 read_input(char *dev, int interactive, struct disk_desc *z) {
2271 struct part_desc *partitions = &(z->partitions[0]), *ep;
2273 for (i=0; i < SIZE(z->partitions); i++)
2274 partitions[i] = zero_part_desc;
2278 warn(_("Input in the following format; absent fields get a default value.\n"
2279 "<start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s>\n"
2280 "Usually you only need to specify <start> and <size> (and perhaps <type>).\n"));
2284 read_partition(dev, interactive, i, 0, z);
2285 for (i=0; i<4; i++) {
2287 if (is_extended(ep->p.sys_type) && ep->size)
2288 read_partition_chain(dev, interactive, ep, z);
2290 add_sector_and_offset(z);
2294 * G. The command line
2297 static void version(void) {
2298 printf("sfdisk (%s)\n", PACKAGE_STRING);
2304 printf(_("Usage: %s [options] device ...\n"), PROGNAME);
2305 puts (_("device: something like /dev/hda or /dev/sda"));
2306 puts (_("useful options:"));
2307 puts (_(" -s [or --show-size]: list size of a partition"));
2308 puts (_(" -c [or --id]: print or change partition Id"));
2309 puts (_(" -l [or --list]: list partitions of each device"));
2310 puts (_(" -d [or --dump]: idem, but in a format suitable for later input"));
2311 puts (_(" -i [or --increment]: number cylinders etc. from 1 instead of from 0"));
2312 puts (_(" -uS, -uB, -uC, -uM: accept/report in units of sectors/blocks/cylinders/MB"));
2313 puts (_(" -T [or --list-types]:list the known partition types"));
2314 puts (_(" -D [or --DOS]: for DOS-compatibility: waste a little space"));
2315 puts (_(" -R [or --re-read]: make kernel reread partition table"));
2316 puts (_(" -N# : change only the partition with number #"));
2317 puts (_(" -n : do not actually write to disk"));
2318 puts (_(" -O file : save the sectors that will be overwritten to file"));
2319 puts (_(" -I file : restore these sectors again"));
2320 puts (_(" -v [or --version]: print version"));
2321 puts (_(" -? [or --help]: print this message"));
2322 puts (_("dangerous options:"));
2323 puts (_(" -g [or --show-geometry]: print the kernel's idea of the geometry"));
2324 puts (_(" -G [or --show-pt-geometry]: print geometry guessed from the partition table"));
2325 puts (_(" -x [or --show-extended]: also list extended partitions on output\n"
2326 " or expect descriptors for them on input"));
2327 puts (_(" -L [or --Linux]: do not complain about things irrelevant for Linux"));
2328 puts (_(" -q [or --quiet]: suppress warning messages"));
2329 puts (_(" You can override the detected geometry using:"));
2330 puts (_(" -C# [or --cylinders #]:set the number of cylinders to use"));
2331 puts (_(" -H# [or --heads #]: set the number of heads to use"));
2332 puts (_(" -S# [or --sectors #]: set the number of sectors to use"));
2333 puts (_("You can disable all consistency checking with:"));
2334 puts (_(" -f [or --force]: do what I say, even if it is stupid"));
2339 activate_usage(char *progn) {
2341 printf(_("%s device list active partitions on device\n"), progn);
2342 printf(_("%s device n1 n2 ... activate partitions n1 ..., inactivate the rest\n"), progn);
2343 printf(_("%s -An device activate partition n, inactivate the other ones\n"), PROGNAME);
2348 unhide_usage(char *progn) {
2352 static char short_opts[] = "cdfgilnqsu:vx?1A::C:DGH:I:LN:O:RS:TU::V";
2354 #define PRINT_ID 0400
2355 #define CHANGE_ID 01000
2357 static const struct option long_opts[] = {
2358 { "change-id", no_argument, NULL, 'c' + CHANGE_ID },
2359 { "print-id", no_argument, NULL, 'c' + PRINT_ID },
2360 { "id", no_argument, NULL, 'c' },
2361 { "dump", no_argument, NULL, 'd' },
2362 { "force", no_argument, NULL, 'f' },
2363 { "show-geometry", no_argument, NULL, 'g' },
2364 { "increment", no_argument, NULL, 'i' },
2365 { "list", no_argument, NULL, 'l' },
2366 { "quiet", no_argument, NULL, 'q' },
2367 { "show-size", no_argument, NULL, 's' },
2368 { "unit", required_argument, NULL, 'u' },
2369 { "version", no_argument, NULL, 'v' },
2370 { "show-extended", no_argument, NULL, 'x' },
2371 { "help", no_argument, NULL, '?' },
2372 { "one-only", no_argument, NULL, '1' },
2373 { "cylinders", required_argument, NULL, 'C' },
2374 { "heads", required_argument, NULL, 'H' },
2375 { "sectors", required_argument, NULL, 'S' },
2376 { "show-pt-geometry", no_argument, NULL, 'G' },
2377 { "activate", optional_argument, NULL, 'A' },
2378 { "DOS", no_argument, NULL, 'D' },
2379 { "DOS-extended", no_argument, NULL, 'E' },
2380 { "Linux", no_argument, NULL, 'L' },
2381 { "re-read", no_argument, NULL, 'R' },
2382 { "list-types", no_argument, NULL, 'T' },
2383 { "unhide", optional_argument, NULL, 'U' },
2384 { "no-reread", no_argument, NULL, 160 },
2385 { "IBM", no_argument, NULL, 161 },
2386 { "leave-last", no_argument, NULL, 161 },
2387 /* undocumented flags - not all completely implemented */
2388 { "in-order", no_argument, NULL, 128 },
2389 { "not-in-order", no_argument, NULL, 129 },
2390 { "inside-outer", no_argument, NULL, 130 },
2391 { "not-inside-outer", no_argument, NULL, 131 },
2392 { "nested", no_argument, NULL, 132 },
2393 { "chained", no_argument, NULL, 133 },
2394 { "onesector", no_argument, NULL, 134 },
2395 { NULL, 0, NULL, 0 }
2399 is_ide_cdrom_or_tape(char *device) {
2402 struct stat statbuf;
2405 /* No device was given explicitly, and we are trying some
2406 likely things. But opening /dev/hdc may produce errors like
2407 "hdc: tray open or drive not ready"
2408 if it happens to be a CD-ROM drive. It even happens that
2409 the process hangs on the attempt to read a music CD.
2410 So try to be careful. This only works since 2.1.73. */
2412 if (strncmp("/dev/hd", device, 7))
2415 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2416 procf = fopen(buf, "r");
2417 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2418 is_ide = (!strncmp(buf, "cdrom", 5) ||
2419 !strncmp(buf, "tape", 4));
2421 /* Now when this proc file does not exist, skip the
2422 device when it is read-only. */
2423 if (stat(device, &statbuf) == 0)
2424 is_ide = ((statbuf.st_mode & 0222) == 0);
2431 #define PROC_PARTITIONS "/proc/partitions"
2434 nextproc(FILE *procf) {
2435 static char devname[256];
2436 char line[1024], ptname[128];
2438 unsigned long long sz;
2442 while (fgets(line, sizeof(line), procf) != NULL) {
2443 if (sscanf (line, " %d %d %llu %128[^\n ]",
2444 &ma, &mi, &sz, ptname) != 4)
2446 snprintf(devname, sizeof(devname), "/dev/%s", ptname);
2447 if (!is_whole_disk(devname))
2456 gpt_warning(char *dev, int warn_only)
2461 if (dev && gpt_probe_signature_devname(dev)) {
2463 fprintf(stderr, _("\nWARNING: GPT (GUID Partition Table) detected on '%s'! "
2464 "The util sfdisk doesn't support GPT. Use GNU Parted.\n\n"), dev);
2466 fprintf(stderr, _("Use the --force flag to overrule this check.\n"));
2472 static void do_list(char *dev, int silent);
2473 static void do_size(char *dev, int silent);
2474 static void do_geom(char *dev, int silent);
2475 static void do_pt_geom(char *dev, int silent);
2476 static void do_fdisk(char *dev);
2477 static void do_reread(char *dev);
2478 static void do_change_id(char *dev, char *part, char *id);
2479 static void do_unhide(char **av, int ac, char *arg);
2480 static void do_activate(char **av, int ac, char *arg);
2482 unsigned long long total_size;
2485 main(int argc, char **argv) {
2490 int opt_out_geom = 0;
2491 int opt_out_pt_geom = 0;
2497 char *activatearg = 0;
2498 char *unhidearg = 0;
2500 setlocale(LC_ALL, "");
2501 bindtextdomain(PACKAGE, LOCALEDIR);
2502 textdomain(PACKAGE);
2505 fatal(_("no command?\n"));
2506 if ((progn = strrchr(argv[0], '/')) == NULL)
2510 if (!strcmp(progn, "activate"))
2511 activate = 1; /* equivalent to `sfdisk -A' */
2512 #if 0 /* not important enough to deserve a name */
2513 else if (!strcmp(progn, "unhide"))
2514 unhide = 1; /* equivalent to `sfdisk -U' */
2519 while ((c = getopt_long (argc, argv, short_opts, long_opts, NULL)) != -1) {
2522 force = 1; break; /* does not imply quiet */
2524 opt_out_geom = 1; break;
2526 opt_out_pt_geom = 1; break;
2528 increment = 1; break;
2530 case 'c' + PRINT_ID:
2531 case 'c' + CHANGE_ID:
2534 dump = 1; /* fall through */
2536 opt_list = 1; break;
2538 no_write = 1; break;
2542 opt_size = 1; break;
2544 set_format(*optarg); break;
2549 show_extended = 1; break;
2551 activatearg = optarg;
2552 activate = 1; break;
2554 U.cylinders = atoi(optarg); break;
2558 DOS_extended = 1; break;
2560 U.heads = atoi(optarg); break;
2564 one_only = atoi(optarg); break;
2566 restore_sector_file = optarg; break;
2568 save_sector_file = optarg; break;
2570 opt_reread = 1; break;
2572 U.sectors = atoi(optarg); break;
2585 /* undocumented flags */
2587 partitions_in_order = 1; break;
2589 partitions_in_order = 0; break;
2591 all_logicals_inside_outermost_extended = 1; break;
2593 all_logicals_inside_outermost_extended = 0; break;
2595 boxes = NESTED; break;
2597 boxes = CHAINED; break;
2599 boxes = ONESECTOR; break;
2603 no_reread = 1; break;
2605 leave_last = 1; break;
2609 if (optind == argc &&
2610 (opt_list || opt_out_geom || opt_out_pt_geom || opt_size || verify)) {
2613 /* try all known devices */
2616 procf = fopen(PROC_PARTITIONS, "r");
2618 fprintf(stderr, _("cannot open %s\n"), PROC_PARTITIONS);
2620 while ((dev = nextproc(procf)) != NULL) {
2621 if (is_ide_cdrom_or_tape(dev))
2623 gpt_warning(dev, 1);
2626 if (opt_out_pt_geom)
2630 if (opt_list || verify)
2637 printf(_("total: %llu blocks\n"), total_size);
2642 if (optind == argc) {
2644 activate_usage(fdisk ? "sfdisk -A" : progn);
2646 unhide_usage(fdisk ? "sfdisk -U" : progn);
2651 if (opt_list || opt_out_geom || opt_out_pt_geom || opt_size || verify) {
2652 while (optind < argc) {
2653 gpt_warning(argv[optind], 1);
2655 do_geom(argv[optind], 0);
2656 if (opt_out_pt_geom)
2657 do_pt_geom(argv[optind], 0);
2659 do_size(argv[optind], 0);
2660 if (opt_list || verify)
2661 do_list(argv[optind], 0);
2667 if (optind != argc-1)
2668 gpt_warning(argv[optind], 0);
2671 do_activate(argv+optind, argc-optind, activatearg);
2675 do_unhide(argv+optind, argc-optind, unhidearg);
2679 if ((do_id & PRINT_ID) != 0 && optind != argc-2)
2680 fatal(_("usage: sfdisk --print-id device partition-number\n"));
2681 else if ((do_id & CHANGE_ID) != 0 && optind != argc-3)
2682 fatal(_("usage: sfdisk --change-id device partition-number Id\n"));
2683 else if (optind != argc-3 && optind != argc-2)
2684 fatal(_("usage: sfdisk --id device partition-number [Id]\n"));
2685 do_change_id(argv[optind], argv[optind+1],
2686 (optind == argc-2) ? 0 : argv[optind+2]);
2690 if (optind != argc-1)
2691 fatal(_("can specify only one device (except with -l or -s)\n"));
2696 else if (restore_sector_file)
2697 restore_sectors(dev);
2705 * H. Listing the current situation
2709 my_open (char *dev, int rw, int silent) {
2712 mode = (rw ? O_RDWR : O_RDONLY);
2713 fd = open(dev, mode);
2714 if (fd < 0 && !silent) {
2717 fatal(_("cannot open %s read-write\n"), dev);
2719 fatal(_("cannot open %s for reading\n"), dev);
2725 do_list (char *dev, int silent) {
2727 struct disk_desc *z;
2729 fd = my_open(dev, 0, silent);
2736 get_cylindersize(dev, fd, dump ? 1 : opt_list ? 0 : 1);
2737 get_partitions(dev, fd, z);
2740 out_partitions(dev, z);
2743 if (partitions_ok(z))
2744 warn(_("%s: OK\n"), dev);
2753 do_geom (char *dev, int silent) {
2757 fd = my_open(dev, 0, silent);
2761 R = get_geometry(dev, fd, silent);
2763 printf(_("%s: %ld cylinders, %ld heads, %ld sectors/track\n"),
2764 dev, R.cylinders, R.heads, R.sectors);
2770 do_pt_geom (char *dev, int silent) {
2772 struct disk_desc *z;
2775 fd = my_open(dev, 0, silent);
2782 get_cylindersize(dev, fd, 1);
2783 get_partitions(dev, fd, z);
2787 if (z->partno != 0 && get_fdisk_geometry(z)) {
2789 R.sectors = F.sectors;
2790 R.cylindersize = R.heads * R.sectors;
2791 R.cylinders = (R.cylindersize == 0) ? 0 :
2792 R.total_size / R.cylindersize;
2796 printf(_("%s: %ld cylinders, %ld heads, %ld sectors/track\n"),
2797 dev, R.cylinders, R.heads, R.sectors);
2802 /* for compatibility with earlier fdisk: provide option -s */
2804 do_size (char *dev, int silent) {
2806 unsigned long long size;
2808 fd = my_open(dev, 0, silent);
2812 if (blkdev_get_sectors(fd, &size) == -1) {
2815 fatal(_("Cannot get size of %s\n"), dev);
2820 size /= 2; /* convert sectors to blocks */
2822 /* a CDROM drive without mounted CD yields MAXINT */
2823 if (silent && size == ((1<<30)-1))
2827 printf("%s: %9llu\n", dev, size);
2829 printf("%llu\n", size);
2837 * Activate: usually one wants to have a single primary partition
2838 * to be active. OS/2 fdisk makes non-bootable logical partitions
2839 * active - I don't know what that means to OS/2 Boot Manager.
2841 * Call: activate /dev/hda 2 5 7 make these partitions active
2842 * and the remaining ones inactive
2843 * Or: sfdisk -A /dev/hda 2 5 7
2845 * If only a single partition must be active, one may also use the form
2846 * sfdisk -A2 /dev/hda
2848 * With "activate /dev/hda" or "sfdisk -A /dev/hda" the active partitions
2849 * are listed but not changed. To get zero active partitions, use
2850 * "activate /dev/hda none" or "sfdisk -A /dev/hda none".
2851 * Use something like `echo ",,,*" | sfdisk -N2 /dev/hda' to only make
2852 * /dev/hda2 active, without changing other partitions.
2854 * A warning will be given if after the change not precisely one primary
2855 * partition is active.
2857 * The present syntax was chosen to be (somewhat) compatible with the
2858 * activate from the LILO package.
2861 set_active (struct disk_desc *z, char *pnam) {
2864 pno = asc_to_index(pnam, z);
2865 if (z->partitions[pno].ptype == DOS_TYPE)
2866 z->partitions[pno].p.bootable = 0x80;
2870 do_activate (char **av, int ac, char *arg) {
2873 int rw, i, pno, lpno;
2874 struct disk_desc *z;
2878 rw = (!no_write && (arg || ac > 1));
2879 fd = my_open(dev, rw, 0);
2882 get_cylindersize(dev, fd, 1);
2883 get_partitions(dev, fd, z);
2885 if (!arg && ac == 1) {
2886 /* list active partitions */
2887 for (pno=0; pno < z->partno; pno++) {
2888 if (z->partitions[pno].p.bootable) {
2889 lpno = index_to_linux(pno, z);
2890 if (pno == linux_to_index(lpno, z))
2891 printf("%s\n", partname(dev, lpno, 0));
2893 printf("%s#%d\n", dev, pno);
2894 if (z->partitions[pno].p.bootable != 0x80)
2895 warn(_("bad active byte: 0x%x instead of 0x80\n"),
2896 z->partitions[pno].p.bootable);
2900 /* clear `active byte' everywhere */
2901 for (pno=0; pno < z->partno; pno++)
2902 if (z->partitions[pno].ptype == DOS_TYPE)
2903 z->partitions[pno].p.bootable = 0;
2905 /* then set where desired */
2908 else for(i=1; i<ac; i++)
2909 set_active(z, av[i]);
2911 /* then write to disk */
2912 if (write_partitions(dev, fd, z))
2913 warn(_("Done\n\n"));
2918 for (pno=0; pno < z->partno && pno < 4; pno++)
2919 if (z->partitions[pno].p.bootable)
2922 warn(_("You have %d active primary partitions. This does not matter for LILO,\n"
2923 "but the DOS MBR will only boot a disk with 1 active partition.\n"), i);
2929 set_unhidden (struct disk_desc *z, char *pnam) {
2933 pno = asc_to_index(pnam, z);
2934 id = z->partitions[pno].p.sys_type;
2935 if (id == 0x11 || id == 0x14 || id == 0x16 || id == 0x17)
2938 fatal(_("partition %s has id %x and is not hidden\n"), pnam, id);
2939 z->partitions[pno].p.sys_type = id;
2943 * maybe remove and make part of --change-id
2946 do_unhide (char **av, int ac, char *arg) {
2949 struct disk_desc *z;
2954 fd = my_open(dev, rw, 0);
2957 get_cylindersize(dev, fd, 1);
2958 get_partitions(dev, fd, z);
2960 /* unhide where desired */
2962 set_unhidden(z, arg);
2963 else for(i=1; i<ac; i++)
2964 set_unhidden(z, av[i]);
2966 /* then write to disk */
2967 if (write_partitions(dev, fd, z))
2968 warn(_("Done\n\n"));
2976 do_change_id(char *dev, char *pnam, char *id) {
2978 struct disk_desc *z;
2984 fd = my_open(dev, rw, 0);
2987 get_cylindersize(dev, fd, 1);
2988 get_partitions(dev, fd, z);
2990 pno = asc_to_index(pnam, z);
2992 printf("%x\n", z->partitions[pno].p.sys_type);
2995 i = strtoul(id, NULL, 16);
2997 fatal(_("Bad Id %lx\n"), i);
2998 z->partitions[pno].p.sys_type = i;
3000 if (write_partitions(dev, fd, z))
3001 warn(_("Done\n\n"));
3009 do_reread(char *dev) {
3012 fd = my_open(dev, 0, 0);
3013 if (reread_ioctl(fd))
3014 do_warn(_("This disk is currently in use.\n"));
3020 * I. Writing the new situation
3024 do_fdisk(char *dev){
3027 struct stat statbuf;
3028 int interactive = isatty(0);
3029 struct disk_desc *z;
3031 if (stat(dev, &statbuf) < 0) {
3033 fatal(_("Fatal error: cannot find %s\n"), dev);
3035 if (!S_ISBLK(statbuf.st_mode)) {
3036 do_warn(_("Warning: %s is not a block device\n"), dev);
3039 fd = my_open(dev, !no_write, 0);
3041 if (!no_write && !no_reread) {
3042 warn(_("Checking that no-one is using this disk right now ...\n"));
3043 if (reread_ioctl(fd)) {
3044 do_warn(_("\nThis disk is currently in use - repartitioning is probably a bad idea.\n"
3045 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
3046 "Use the --no-reread flag to suppress this check.\n"));
3048 do_warn(_("Use the --force flag to overrule all checks.\n"));
3058 get_cylindersize(dev, fd, 0);
3059 get_partitions(dev, fd, z);
3061 printf(_("Old situation:\n"));
3062 out_partitions(dev, z);
3064 if (one_only && (one_only_pno = linux_to_index(one_only, z)) < 0)
3065 fatal(_("Partition %d does not exist, cannot change it\n"), one_only);
3071 read_input(dev, interactive, z);
3073 printf(_("New situation:\n"));
3074 out_partitions(dev, z);
3076 if (!partitions_ok(z) && !force) {
3078 fatal(_("I don't like these partitions - nothing changed.\n"
3079 "(If you really want this, use the --force option.)\n"));
3081 do_warn(_("I don't like this - probably you should answer No\n"));
3086 printf(_("Are you satisfied with this? [ynq] "));
3088 printf(_("Do you want to write this to disk? [ynq] "));
3089 answer = c = getchar();
3090 while (c != '\n' && c != EOF)
3093 printf(_("\nsfdisk: premature end of input\n"));
3094 if (c == EOF || answer == 'q' || answer == 'Q') {
3095 fatal(_("Quitting - nothing changed\n"));
3096 } else if (answer == 'n' || answer == 'N') {
3098 } else if (answer == 'y' || answer == 'Y') {
3101 printf(_("Please answer one of y,n,q\n"));
3108 if (write_partitions(dev, fd, z))
3109 printf(_("Successfully wrote the new partition table\n\n"));
3113 reread_disk_partition(dev, fd);
3115 warn(_("If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)\n"
3116 "to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1\n"
3117 "(See fdisk(8).)\n"));
3119 sync(); /* superstition */