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"
36 #define VERSION "3.08"
40 #include <stdlib.h> /* atoi, free */
41 #include <stdarg.h> /* varargs */
42 #include <unistd.h> /* read, write */
43 #include <fcntl.h> /* O_RDWR */
44 #include <errno.h> /* ERANGE */
45 #include <string.h> /* index() */
48 #include <sys/ioctl.h>
50 #include <sys/utsname.h>
51 #include <linux/unistd.h> /* _syscall */
55 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
61 * C. About heads, sectors and cylinders
64 * F. The standard input
66 * H. Listing the current situation
67 * I. Writing the new situation
71 int force = 0; /* 1: do what I say, even if it is stupid ... */
72 int quiet = 0; /* 1: suppress all warnings */
73 /* IA-64 gcc spec file currently does -DLinux... */
75 int Linux = 0; /* 1: suppress warnings irrelevant for Linux */
76 int DOS = 0; /* 1: shift extended partitions by #sectors, not 1 */
77 int DOS_extended = 0; /* 1: use starting cylinder boundary of extd partn */
78 int dump = 0; /* 1: list in a format suitable for later input */
79 int verify = 0; /* 1: check that listed partition is reasonable */
80 int no_write = 0; /* 1: do not actually write to disk */
81 int no_reread = 0; /* 1: skip the BLKRRPART ioctl test at startup */
82 int leave_last = 0; /* 1: don't allocate the last cylinder */
84 char *save_sector_file = NULL;
85 char *restore_sector_file = NULL;
88 do_warn(char *s, ...) {
93 vfprintf(stderr, s, p);
105 vfprintf(stderr, s, p);
112 error(char *s, ...) {
117 fprintf(stderr, "\n" PROGNAME ": ");
118 vfprintf(stderr, s, p);
124 fatal(char *s, ...) {
129 fprintf(stderr, "\n" PROGNAME ": ");
130 vfprintf(stderr, s, p);
137 * GCC nonsense - needed for GCC 3.4.x with -O2
139 * Maybe just test with #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 4) ?
141 #ifndef __GNUC_PREREQ
142 #define __GNUC_PREREQ(x,y) 0
144 #if __GNUC_PREREQ(3,4)
145 #define __attribute__used __attribute__ ((used))
147 #define __attribute__used
151 * arm needs PACKED - use it everywhere?
153 #if defined(__GNUC__) && (defined(__arm__) || defined(__alpha__))
154 # define PACKED __attribute__ ((packed))
165 * sseek: seek to specified sector - return 0 on failure
167 * For >4GB disks lseek needs a > 32bit arg, and we have to use llseek.
168 * On the other hand, a 32 bit sector number is OK until 2TB.
169 * The routines _llseek and sseek below are the only ones that
170 * know about the loff_t type.
172 * Note: we use 512-byte sectors here, irrespective of the hardware ss.
175 #if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) || defined (__s390x__)
180 static __attribute__used
181 int _llseek (unsigned int fd, ulong hi, ulong lo,
182 loff_t *res, unsigned int wh) {
183 return syscall (__NR__llseek, fd, hi, lo, res, wh);
188 sseek(char *dev, unsigned int fd, unsigned long s) {
190 in = ((loff_t) s << 9);
194 if (_llseek (fd, in>>32, in & 0xffffffff, &out, SEEK_SET) != 0) {
196 if ((out = lseek(fd, in, SEEK_SET)) != in) {
199 error(_("seek error on %s - cannot seek to %lu\n"), dev, s);
204 error(_("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n"),
205 (unsigned int)(in>>32), (unsigned int)(in & 0xffffffff),
206 (unsigned int)(out>>32), (unsigned int)(out & 0xffffffff));
217 * We preserve all sectors read in a chain - some of these will
218 * have to be modified and written back.
222 unsigned long sectornumber;
233 sectorhead = s->next;
238 static struct sector *
239 get_sector(char *dev, int fd, unsigned long sno) {
242 for(s = sectorhead; s; s = s->next)
243 if (s->sectornumber == sno)
246 if (!sseek(dev, fd, sno))
249 if (!(s = (struct sector *) malloc(sizeof(struct sector))))
250 fatal(_("out of memory - giving up\n"));
252 if (read(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
253 if (errno) /* 0 in case we read past end-of-disk */
255 error(_("read error on %s - cannot read sector %lu\n"), dev, sno);
260 s->next = sectorhead;
262 s->sectornumber = sno;
263 s->to_be_written = 0;
269 msdos_signature (struct sector *s) {
270 unsigned char *data = s->data;
271 if (data[510] == 0x55 && data[511] == 0xaa)
273 error(_("ERROR: sector %lu does not have an msdos signature\n"),
279 write_sectors(char *dev, int fd) {
282 for (s = sectorhead; s; s = s->next)
283 if (s->to_be_written) {
284 if (!sseek(dev, fd, s->sectornumber))
286 if (write(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
288 error(_("write error on %s - cannot write sector %lu\n"),
289 dev, s->sectornumber);
292 s->to_be_written = 0;
298 ulong_to_chars(unsigned long u, char *uu) {
308 chars_to_ulong(unsigned char *uu) {
313 u = (u << 8) | uu[i];
318 save_sectors(char *dev, int fdin) {
323 fdout = open(save_sector_file, O_WRONLY | O_CREAT, 0444);
325 perror(save_sector_file);
326 error(_("cannot open partition sector save file (%s)\n"),
331 for (s = sectorhead; s; s = s->next)
332 if (s->to_be_written) {
333 ulong_to_chars(s->sectornumber, ss);
334 if (!sseek(dev, fdin, s->sectornumber))
336 if (read(fdin, ss+4, 512) != 512) {
338 error(_("read error on %s - cannot read sector %lu\n"),
339 dev, s->sectornumber);
342 if (write(fdout, ss, sizeof(ss)) != sizeof(ss)) {
344 error(_("write error on %s\n"), save_sector_file);
351 static void reread_disk_partition(char *dev, int fd);
354 restore_sectors(char *dev) {
360 if (stat(restore_sector_file, &statbuf) < 0) {
361 perror(restore_sector_file);
362 error(_("cannot stat partition restore file (%s)\n"),
363 restore_sector_file);
366 if (statbuf.st_size % 516) {
367 error(_("partition restore file has wrong size - not restoring\n"));
370 if (!(ss = (char *) malloc(statbuf.st_size))) {
371 error(_("out of memory?\n"));
374 fdin = open(restore_sector_file, O_RDONLY);
376 perror(restore_sector_file);
377 error(_("cannot open partition restore file (%s)\n"),
378 restore_sector_file);
381 if (read(fdin, ss, statbuf.st_size) != statbuf.st_size) {
383 error(_("error reading %s\n"), restore_sector_file);
387 fdout = open(dev, O_WRONLY);
390 error(_("cannot open device %s for writing\n"), dev);
395 ct = statbuf.st_size/516;
397 sno = chars_to_ulong(ss);
398 if (!sseek(dev, fdout, sno))
400 if (write(fdout, ss+4, 512) != 512) {
402 error(_("error writing sector %lu on %s\n"), sno, dev);
409 reread_disk_partition(dev, fdout);
415 * C. About heads, sectors and cylinders
419 * <linux/hdreg.h> defines HDIO_GETGEO and
420 * struct hd_geometry {
421 * unsigned char heads;
422 * unsigned char sectors;
423 * unsigned short cylinders;
424 * unsigned long start;
427 * For large disks g.cylinders is truncated, so we use BLKGETSIZE.
431 * We consider several geometries for a disk:
432 * B - the BIOS geometry, gotten from the kernel via HDIO_GETGEO
433 * F - the fdisk geometry
434 * U - the user-specified geometry
436 * 0 means unspecified / unknown
439 unsigned long long total_size; /* in sectors */
440 unsigned long cylindersize; /* in sectors */
441 unsigned long heads, sectors, cylinders;
445 static struct geometry
446 get_geometry(char *dev, int fd, int silent) {
447 struct hd_geometry g;
449 unsigned long long sectors;
452 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 (disksize(fd, §ors)) {
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);
494 B.sectors = (U.sectors ? U.sectors : R.sectors);
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(cp);
724 p->nr_sects = copy_to_int(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) {
796 if (ioctl(fd, BLKRRPART)) {
799 /* 2.6.8 returns EIO for a zero table */
807 is_blockdev(int fd) {
810 return(fstat(fd, &statbuf) == 0 && S_ISBLK(statbuf.st_mode));
813 /* reread after writing */
815 reread_disk_partition(char *dev, int fd) {
816 printf(_("Re-reading the partition table ...\n"));
819 sleep(3); /* superfluous since 1.3.20 */
821 if (reread_ioctl(fd) && is_blockdev(fd))
822 do_warn(_("The command to re-read the partition table failed\n"
823 "Reboot your system now, before using mkfs\n"));
827 do_warn(_("Error closing %s\n"), dev);
832 /* find Linux name of this partition, assuming that it will have a name */
834 index_to_linux(int pno, struct disk_desc *z) {
836 struct part_desc *p = &(z->partitions[0]);
837 for (i=0; i<pno; i++,p++)
838 if (i < 4 || (p->size > 0 && !is_extended(p->p.sys_type)))
844 linux_to_index(int lpno, struct disk_desc *z) {
846 struct part_desc *p = &(z->partitions[0]);
847 for (i=0; i<z->partno && ct < lpno; i++,p++)
848 if ((i < 4 || (p->size > 0 && !is_extended(p->p.sys_type)))
855 asc_to_index(char *pnam, struct disk_desc *z) {
862 pno = linux_to_index(pnum, z);
864 if (!(pno >= 0 && pno < z->partno))
865 fatal(_("%s: no such partition\n"), pnam);
870 * List partitions - in terms of sectors, blocks or cylinders
877 int default_format = F_MEGABYTE;
878 int specified_format = 0;
879 int show_extended = 0;
888 do_warn(_("unrecognized format - using sectors\n"));
889 case 'S': specified_format = F_SECTOR; break;
890 case 'B': specified_format = F_BLOCK; break;
891 case 'C': specified_format = F_CYLINDER; break;
892 case 'M': specified_format = F_MEGABYTE; break;
897 unitsize(int format) {
898 default_format = (B.cylindersize ? F_CYLINDER : F_MEGABYTE);
899 if (!format && !(format = specified_format))
900 format = default_format;
906 return B.cylindersize;
917 get_disksize(int format) {
918 unsigned long cs = B.cylinders;
919 if (cs && leave_last)
921 return (cs * B.cylindersize) / unitsize(format);
925 out_partition_header(char *dev, int format, struct geometry G) {
927 printf(_("# partition table of %s\n"), dev);
928 printf("unit: sectors\n\n");
932 default_format = (G.cylindersize ? F_CYLINDER : F_MEGABYTE);
933 if (!format && !(format = specified_format))
934 format = default_format;
938 do_warn(_("unimplemented format - using %s\n"),
939 G.cylindersize ? _("cylinders") : _("sectors"));
941 if (G.cylindersize) {
942 printf(_("Units = cylinders of %lu bytes, blocks of 1024 bytes"
943 ", counting from %d\n\n"),
944 G.cylindersize<<9, increment);
945 printf(_(" Device Boot Start End #cyls #blocks Id System\n"));
950 printf(_("Units = sectors of 512 bytes, counting from %d\n\n"),
952 printf(_(" Device Boot Start End #sectors Id System\n"));
955 printf(_("Units = blocks of 1024 bytes, counting from %d\n\n"),
957 printf(_(" Device Boot Start End #blocks Id System\n"));
960 printf(_("Units = mebibytes of 1048576 bytes, blocks of 1024 bytes"
961 ", counting from %d\n\n"), increment);
962 printf(_(" Device Boot Start End MiB #blocks Id System\n"));
968 out_rounddown(int width, unsigned long n, unsigned long unit, int inc) {
969 printf("%*lu", width, inc + n/unit);
971 putchar((n % unit) ? '+' : ' ');
976 out_roundup(int width, unsigned long n, unsigned long unit, int inc) {
977 if (n == (unsigned long)(-1))
978 printf("%*s", width, "-");
980 printf("%*lu", width, inc + n/unit);
982 putchar(((n+1) % unit) ? '-' : ' ');
987 out_roundup_size(int width, unsigned long n, unsigned long unit) {
988 printf("%*lu", width, (n+unit-1)/unit);
990 putchar((n % unit) ? '-' : ' ');
994 static struct geometry
995 get_fdisk_geometry_one(struct part_desc *p) {
998 chs b = p->p.end_chs;
999 longchs bb = chs_to_longchs(b);
1002 G.cylindersize = G.heads*G.sectors;
1003 G.cylinders = G.start = 0;
1008 get_fdisk_geometry(struct disk_desc *z) {
1009 struct part_desc *p;
1011 struct geometry G0, G;
1014 G0.heads = G0.sectors = 0;
1015 for (pno=0; pno < z->partno; pno++) {
1016 p = &(z->partitions[pno]);
1017 if (p->size != 0 && p->p.sys_type != 0) {
1018 G = get_fdisk_geometry_one(p);
1022 } else if (G.heads != G0.heads || G.sectors != G0.sectors) {
1028 F = (agree ? G0 : B);
1029 return (F.sectors != B.sectors || F.heads != B.heads);
1033 out_partition(char *dev, int format, struct part_desc *p,
1034 struct disk_desc *z, struct geometry G) {
1035 unsigned long start, end, size;
1038 if (!format && !(format = specified_format))
1039 format = default_format;
1041 pno = p - &(z->partitions[0]); /* our index */
1042 lpno = index_to_linux(pno, z); /* name of next one that has a name */
1043 if (pno == linux_to_index(lpno, z)) /* was that us? */
1044 printf("%s", partname(dev, lpno, 10)); /* yes */
1045 else if (show_extended)
1049 putchar(dump ? ':' : ' ');
1052 end = p->start + p->size - 1;
1056 printf(" start=%9lu", start);
1057 printf(", size=%9lu", size);
1058 if (p->ptype == DOS_TYPE) {
1059 printf(", Id=%2x", p->p.sys_type);
1060 if (p->p.bootable == 0x80)
1061 printf(", bootable");
1067 if (p->ptype != DOS_TYPE || p->p.bootable == 0)
1069 else if (p->p.bootable == 0x80)
1072 printf(" ? "); /* garbage */
1076 if (G.cylindersize) {
1077 out_rounddown(6, start, G.cylindersize, increment);
1078 out_roundup(6, end, G.cylindersize, increment);
1079 out_roundup_size(6, size, G.cylindersize);
1080 out_rounddown(9, size, 2, 0);
1086 out_rounddown(9, start, 1, increment);
1087 out_roundup(9, end, 1, increment);
1088 out_rounddown(10, size, 1, 0);
1092 printf("%8lu,%3lu ",
1093 p->sector/2, ((p->sector & 1) ? 512 : 0) + p->offset);
1095 out_rounddown(8, start, 2, increment);
1096 out_roundup(8, end, 2, increment);
1097 out_rounddown(9, size, 2, 0);
1100 out_rounddown(5, start, 2048, increment);
1101 out_roundup(5, end, 2048, increment);
1102 out_roundup_size(5, size, 2048);
1103 out_rounddown(9, size, 2, 0);
1106 if (p->ptype == DOS_TYPE) {
1108 p->p.sys_type, sysname(p->p.sys_type));
1113 /* Is chs as we expect? */
1114 if (!quiet && p->ptype == DOS_TYPE) {
1117 a = (size ? ulong_to_chs(start,G) : zero_chs);
1119 aa = chs_to_longchs(a);
1120 bb = chs_to_longchs(b);
1121 if (a.s && !is_equal_chs(a, b))
1122 do_warn(_("\t\tstart: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1123 aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1124 a = (size ? ulong_to_chs(end,G) : zero_chs);
1126 aa = chs_to_longchs(a);
1127 bb = chs_to_longchs(b);
1128 if (a.s && !is_equal_chs(a, b))
1129 do_warn(_("\t\tend: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1130 aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1131 if (G.cylinders && G.cylinders < 1024 && bb.c > G.cylinders)
1132 do_warn(_("partition ends on cylinder %ld, beyond the end of the disk\n"),
1138 out_partitions(char *dev, struct disk_desc *z) {
1139 int pno, format = 0;
1142 do_warn(_("No partitions found\n"));
1144 if (get_fdisk_geometry(z) && !dump) {
1146 _("Warning: The partition table looks like it was made\n"
1147 " for C/H/S=*/%ld/%ld (instead of %ld/%ld/%ld).\n"
1148 "For this listing I'll assume that geometry.\n"),
1149 F.heads, F.sectors, B.cylinders, B.heads, B.sectors);
1152 out_partition_header(dev, format, F);
1153 for(pno=0; pno < z->partno; pno++) {
1154 out_partition(dev, format, &(z->partitions[pno]), z, F);
1155 if (show_extended && pno%4==3)
1162 disj(struct part_desc *p, struct part_desc *q) {
1164 ((p->start + p->size <= q->start)
1165 || (is_extended(p->p.sys_type)
1166 && q->start + q->size <= p->start + p->size));
1170 pnumber(struct part_desc *p, struct disk_desc *z) {
1171 static char buf[20];
1173 struct part_desc *p0 = &(z->partitions[0]);
1175 this = index_to_linux(p-p0, z);
1176 next = index_to_linux(p-p0+1, z);
1179 sprintf(buf, "%d", this);
1181 sprintf(buf, "[%d]", this);
1186 partitions_ok(struct disk_desc *z) {
1187 struct part_desc *partitions = &(z->partitions[0]), *p, *q;
1188 int partno = z->partno;
1190 #define PNO(p) pnumber(p, z)
1192 /* Have at least 4 partitions been defined? */
1195 fatal(_("no partition table present.\n"));
1197 fatal(_("strange, only %d partitions defined.\n"), partno);
1201 /* Are the partitions of size 0 marked empty?
1202 And do they have start = 0? And bootable = 0? */
1203 for (p = partitions; p - partitions < partno; p++)
1205 if (p->p.sys_type != EMPTY_PARTITION)
1206 warn(_("Warning: partition %s has size 0 but is not marked Empty\n"),
1208 else if (p->p.bootable != 0)
1209 warn(_("Warning: partition %s has size 0 and is bootable\n"),
1211 else if (p->p.start_sect != 0)
1212 warn(_("Warning: partition %s has size 0 and nonzero start\n"),
1214 /* all this is probably harmless, no error return */
1217 /* Are the logical partitions contained in their extended partitions? */
1218 for (p = partitions+4; p < partitions+partno; p++)
1219 if (p->ptype == DOS_TYPE)
1220 if (p->size && !is_extended(p->p.sys_type)) {
1222 if (p->start < q->start || p->start + p->size > q->start + q->size) {
1223 warn(_("Warning: partition %s "), PNO(p));
1224 warn(_("is not contained in partition %s\n"), PNO(q));
1229 /* Are the data partitions mutually disjoint? */
1230 for (p = partitions; p < partitions+partno; p++)
1231 if (p->size && !is_extended(p->p.sys_type))
1232 for (q = p+1; q < partitions+partno; q++)
1233 if (q->size && !is_extended(q->p.sys_type))
1234 if (!((p->start > q-> start) ? disj(q,p) : disj(p,q))) {
1235 warn(_("Warning: partitions %s "), PNO(p));
1236 warn(_("and %s overlap\n"), PNO(q));
1240 /* Are the data partitions and the extended partition
1241 table sectors disjoint? */
1242 for (p = partitions; p < partitions+partno; p++)
1243 if (p->size && !is_extended(p->p.sys_type))
1244 for (q = partitions; q < partitions+partno; q++)
1245 if (is_extended(q->p.sys_type))
1246 if (p->start <= q->start && p->start + p->size > q->start) {
1247 warn(_("Warning: partition %s contains part of "
1248 "the partition table (sector %lu),\n"
1249 "and will destroy it when filled\n"),
1254 /* Do they start past zero and end before end-of-disk? */
1255 { unsigned long ds = get_disksize(F_SECTOR);
1256 for (p = partitions; p < partitions+partno; p++)
1258 if (p->start == 0) {
1259 warn(_("Warning: partition %s starts at sector 0\n"), PNO(p));
1262 if (p->size && p->start + p->size > ds) {
1263 warn(_("Warning: partition %s extends past end of disk\n"),
1270 /* At most one chain of DOS extended partitions ? */
1271 /* It seems that the OS/2 fdisk has the additional requirement
1272 that the extended partition must be the fourth one */
1274 for (p = partitions; p < partitions+4; p++)
1275 if (p->p.sys_type == EXTENDED_PARTITION)
1277 if (ect > 1 && !Linux) {
1278 warn(_("Among the primary partitions, at most one can be extended\n"
1279 " (although this is not a problem under Linux)\n"));
1285 * Do all partitions start at a cylinder boundary ?
1286 * (this is not required for Linux)
1287 * The first partition starts after MBR.
1288 * Logical partitions start slightly after the containing extended partn.
1290 if (B.cylindersize) {
1291 for(p = partitions; p < partitions+partno; p++)
1293 if (p->start % B.cylindersize != 0
1294 && (!p->ep || p->start / B.cylindersize != p->ep->start / B.cylindersize)
1295 && (p->p.start_sect >= B.cylindersize)) {
1296 warn(_("Warning: partition %s does not start "
1297 "at a cylinder boundary\n"), PNO(p));
1301 if ((p->start + p->size) % B.cylindersize) {
1302 warn(_("Warning: partition %s does not end "
1303 "at a cylinder boundary\n"), PNO(p));
1310 /* Usually, one can boot only from primary partitions. */
1311 /* In fact, from a unique one only. */
1312 /* do not warn about bootable extended partitions -
1313 often LILO is there */
1315 for(p = partitions; p < partitions+partno; p++)
1316 if (p->p.bootable) {
1318 pno = p - partitions;
1319 else if (p - partitions < 4) {
1320 warn(_("Warning: more than one primary partition is marked "
1321 "bootable (active)\n"
1322 "This does not matter for LILO, but the DOS MBR will "
1323 "not boot this disk.\n"));
1326 if (p - partitions >= 4) {
1327 warn(_("Warning: usually one can boot from primary partitions "
1328 "only\nLILO disregards the `bootable' flag.\n"));
1332 if (pno == -1 || pno >= 4)
1333 warn(_("Warning: no primary partition is marked bootable (active)\n"
1334 "This does not matter for LILO, but the DOS MBR will "
1335 "not boot this disk.\n"));
1338 /* Is chs as we expect? */
1339 for(p = partitions; p < partitions+partno; p++)
1340 if (p->ptype == DOS_TYPE) {
1343 a = p->size ? ulong_to_chs(p->start,B) : zero_chs;
1345 aa = chs_to_longchs(a);
1346 bb = chs_to_longchs(b);
1347 if (!chs_ok(b, PNO(p), _("start")))
1349 if (a.s && !is_equal_chs(a, b))
1350 warn(_("partition %s: start: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1351 PNO(p), aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1352 a = p->size ? ulong_to_chs(p->start + p->size - 1, B) : zero_chs;
1354 aa = chs_to_longchs(a);
1355 bb = chs_to_longchs(b);
1356 if (!chs_ok(b, PNO(p), _("end")))
1358 if (a.s && !is_equal_chs(a, b))
1359 warn(_("partition %s: end: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1360 PNO(p), aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1361 if (B.cylinders && B.cylinders < 1024 && bb.c > B.cylinders)
1362 warn(_("partition %s ends on cylinder %ld, beyond the end of the disk\n"),
1372 extended_partition(char *dev, int fd, struct part_desc *ep, struct disk_desc *z) {
1375 unsigned long start, here, next;
1376 int i, moretodo = 1;
1378 struct part_desc *partitions = &(z->partitions[0]);
1379 int pno = z->partno;
1381 here = start = ep->start;
1383 if (B.cylindersize && start % B.cylindersize) {
1386 here = start -= (start % B.cylindersize);
1387 do_warn(_("Warning: shifted start of the extd partition "
1389 "(For listing purposes only. "
1390 "Do not change its contents.)\n"),
1393 do_warn(_("Warning: extended partition does not start at a "
1394 "cylinder boundary.\n"
1395 "DOS and Linux will interpret the contents differently.\n"));
1402 if (!(s = get_sector(dev, fd, here)))
1405 if (!msdos_signature(s))
1408 cp = s->data + 0x1be;
1410 if (pno+4 >= SIZE(z->partitions)) {
1411 do_warn(_("too many partitions - ignoring those past nr (%d)\n"),
1418 for (i=0; i<4; i++,cp += sizeof(struct partition)) {
1419 partitions[pno].sector = here;
1420 partitions[pno].offset = cp - s->data;
1421 partitions[pno].ep = ep;
1422 copy_to_part(cp,&p);
1423 if (is_extended(p.sys_type)) {
1424 partitions[pno].start = start + p.start_sect;
1426 do_warn(_("tree of partitions?\n"));
1428 next = partitions[pno].start; /* follow `upper' branch */
1431 partitions[pno].start = here + p.start_sect;
1433 partitions[pno].size = p.nr_sects;
1434 partitions[pno].ptype = DOS_TYPE;
1435 partitions[pno].p = p;
1444 #define BSD_DISKMAGIC (0x82564557UL)
1445 #define BSD_MAXPARTITIONS 16
1446 #define BSD_FS_UNUSED 0
1447 typedef unsigned char u8;
1448 typedef unsigned short u16;
1449 typedef unsigned int u32;
1450 struct bsd_disklabel {
1453 char d_typename[16];
1454 char d_packname[16];
1458 u16 d_npartitions; /* number of partitions in following */
1460 struct bsd_partition { /* the partition table */
1461 u32 p_size; /* number of sectors in partition */
1462 u32 p_offset; /* starting sector */
1463 u32 p_fsize; /* filesystem basic fragment size */
1464 u8 p_fstype; /* filesystem type, see below */
1465 u8 p_frag; /* filesystem fragments per block */
1466 u16 p_cpg; /* filesystem cylinders per group */
1467 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
1471 bsd_partition(char *dev, int fd, struct part_desc *ep, struct disk_desc *z) {
1472 struct bsd_disklabel *l;
1473 struct bsd_partition *bp, *bp0;
1474 unsigned long start = ep->start;
1476 struct part_desc *partitions = &(z->partitions[0]);
1477 int pno = z->partno;
1479 if (!(s = get_sector(dev,fd,start+1)))
1481 l = (struct bsd_disklabel *) (s->data);
1482 if (l->d_magic != BSD_DISKMAGIC || l->d_magic2 != BSD_DISKMAGIC)
1485 bp = bp0 = &l->d_partitions[0];
1486 while (bp - bp0 < BSD_MAXPARTITIONS && bp - bp0 < l->d_npartitions) {
1487 if (pno+1 >= SIZE(z->partitions)) {
1488 do_warn(_("too many partitions - ignoring those "
1489 "past nr (%d)\n"), pno-1);
1492 if (bp->p_fstype != BSD_FS_UNUSED) {
1493 partitions[pno].start = bp->p_offset;
1494 partitions[pno].size = bp->p_size;
1495 partitions[pno].sector = start+1;
1496 partitions[pno].offset = (char *)bp - (char *)bp0;
1497 partitions[pno].ep = 0;
1498 partitions[pno].ptype = BSD_TYPE;
1506 #define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r))
1509 linux_version_code(void) {
1510 struct utsname my_utsname;
1513 if (uname(&my_utsname) == 0) {
1514 p = atoi(strtok(my_utsname.release, "."));
1515 q = atoi(strtok(NULL, "."));
1516 r = atoi(strtok(NULL, "."));
1517 return MAKE_VERSION(p,q,r);
1523 msdos_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1526 struct partition pt;
1528 struct part_desc *partitions = &(z->partitions[0]);
1529 int pno = z->partno;
1530 int bsd_later = (linux_version_code() >= MAKE_VERSION(2,3,40));
1532 if (!(s = get_sector(dev, fd, start)))
1535 if (!msdos_signature(s))
1538 cp = s->data + 0x1be;
1539 copy_to_part(cp,&pt);
1541 /* If I am not mistaken, recent kernels will hide this from us,
1542 so we will never actually see traces of a Disk Manager */
1543 if (pt.sys_type == DM6_PARTITION
1544 || pt.sys_type == EZD_PARTITION
1545 || pt.sys_type == DM6_AUX1PARTITION
1546 || pt.sys_type == DM6_AUX3PARTITION) {
1547 do_warn(_("detected Disk Manager - unable to handle that\n"));
1550 { unsigned int sig = *(unsigned short *)(s->data + 2);
1552 && *(unsigned short *)(s->data + sig) == 0x55aa
1553 && (1 & *(unsigned char *)(s->data + sig + 2))) {
1554 do_warn(_("DM6 signature found - giving up\n"));
1559 for (pno=0; pno<4; pno++,cp += sizeof(struct partition)) {
1560 partitions[pno].sector = start;
1561 partitions[pno].offset = cp - s->data;
1562 copy_to_part(cp,&pt);
1563 partitions[pno].start = start + pt.start_sect;
1564 partitions[pno].size = pt.nr_sects;
1565 partitions[pno].ep = 0;
1566 partitions[pno].p = pt;
1571 for (i=0; i<4; i++) {
1572 if (is_extended(partitions[i].p.sys_type)) {
1573 if (!partitions[i].size) {
1574 do_warn(_("strange..., an extended partition of size 0?\n"));
1577 extended_partition(dev, fd, &partitions[i], z);
1579 if (!bsd_later && is_bsd(partitions[i].p.sys_type)) {
1580 if (!partitions[i].size) {
1581 do_warn(_("strange..., a BSD partition of size 0?\n"));
1584 bsd_partition(dev, fd, &partitions[i], z);
1589 for (i=0; i<4; i++) {
1590 if (is_bsd(partitions[i].p.sys_type)) {
1591 if (!partitions[i].size) {
1592 do_warn(_("strange..., a BSD partition of size 0?\n"));
1595 bsd_partition(dev, fd, &partitions[i], z);
1604 osf_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1609 sun_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1614 amiga_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1619 get_partitions(char *dev, int fd, struct disk_desc *z) {
1622 if (!msdos_partition(dev, fd, 0, z)
1623 && !osf_partition(dev, fd, 0, z)
1624 && !sun_partition(dev, fd, 0, z)
1625 && !amiga_partition(dev, fd, 0, z)) {
1626 do_warn(_(" %s: unrecognized partition table type\n"), dev);
1632 write_partitions(char *dev, int fd, struct disk_desc *z) {
1634 struct part_desc *partitions = &(z->partitions[0]), *p;
1635 int pno = z->partno;
1638 do_warn(_("-n flag was given: Nothing changed\n"));
1642 for (p = partitions; p < partitions+pno; p++) {
1643 s = get_sector(dev, fd, p->sector);
1645 s->to_be_written = 1;
1646 if (p->ptype == DOS_TYPE) {
1647 copy_from_part(&(p->p), s->data + p->offset);
1648 s->data[510] = 0x55;
1649 s->data[511] = 0xaa;
1652 if (save_sector_file) {
1653 if (!save_sectors(dev, fd)) {
1654 fatal(_("Failed saving the old sectors - aborting\n"));
1658 if (!write_sectors(dev, fd)) {
1659 error(_("Failed writing the partition on %s\n"), dev);
1666 * F. The standard input
1671 * <start> <size> <type> <bootable> <c,h,s> <c,h,s>
1672 * Fields are separated by whitespace or comma or semicolon possibly
1673 * followed by whitespace; initial and trailing whitespace is ignored.
1674 * Numbers can be octal, decimal or hexadecimal, decimal is default
1675 * The <c,h,s> parts can (and probably should) be omitted.
1676 * Bootable is specified as [*|-], with as default not-bootable.
1677 * Type is given in hex, without the 0x prefix, or is [E|S|L|X], where
1678 * L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), and E
1679 * is EXTENDED_PARTITION (5), X is LINUX_EXTENDED (85).
1680 * The default value of start is the first nonassigned sector/cylinder/...
1681 * The default value of size is as much as possible (until next
1682 * partition or end-of-disk).
1683 * .: end of chain of extended partitions.
1685 * On interactive input an empty line means: all defaults.
1686 * Otherwise empty lines are ignored.
1699 { 3, "bootable", 1 },
1709 * Read a line, split it into fields
1711 * (some primitive handwork, but a more elaborate parser seems
1718 read_stdin(unsigned char **fields, unsigned char *line, int fieldssize, int linesize) {
1719 unsigned char *lp, *ip;
1722 /* boolean true and empty string at start */
1725 for (fno=0; fno < fieldssize; fno++)
1726 fields[fno] = line + 1;
1729 /* read a line from stdin */
1730 lp = fgets(line+2, linesize, stdin);
1735 if (!(lp = index(lp, '\n')))
1736 fatal(_("long or incomplete input line - quitting\n"));
1739 /* remove comments, if any */
1740 if ((lp = index(line+2, '#')) != 0)
1743 /* recognize a few commands - to be expanded */
1744 if (!strcmp(line+2, "unit: sectors")) {
1745 specified_format = F_SECTOR;
1749 /* dump style? - then bad input is fatal */
1750 if ((ip = index(line+2, ':')) != 0) {
1759 for(d = dumpflds; d-dumpflds < SIZE(dumpflds); d++) {
1760 if (!strncmp(ip, d->fldname, strlen(d->fldname))) {
1761 ip += strlen(d->fldname);
1765 fields[d->fldno] = line;
1766 else if (*ip == '=') {
1767 while(isspace(*++ip)) ;
1768 fields[d->fldno] = ip;
1769 while(isalnum(*ip)) /* 0x07FF */
1772 fatal(_("input error: `=' expected after %s field\n"),
1774 if (fno <= d->fldno)
1778 if (*ip != ',' && *ip != ';')
1779 fatal(_("input error: unexpected character %c after %s field\n"),
1785 fatal(_("unrecognized input: %s\n"), ip);
1788 /* split line into fields */
1791 while((c = *ip++) != 0) {
1792 if (!lp[-1] && (c == '\t' || c == ' '))
1794 else if (c == '\t' || c == ' ' || c == ',' || c == ';') {
1796 if (fno < fieldssize)
1803 if (lp == fields[fno-1])
1808 /* read a number, use default if absent */
1809 /* a sign gives an offset from the default */
1811 get_ul(char *u, unsigned long *up, unsigned long def, int base) {
1819 } else if (*u == '-') {
1825 val = strtoul(u, &nu, base);
1826 if (errno == ERANGE) {
1827 do_warn(_("number too big\n"));
1831 do_warn(_("trailing junk after number\n"));
1836 else if (sign == -1)
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 int partitions_in_order = 0;
1857 int all_logicals_inside_outermost_extended = 1;
1858 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) {
1864 unsigned long ff, fff;
1865 unsigned long unit = unitsize(format);
1866 struct part_desc *partitions = &(z->partitions[0]), *pp = 0;
1868 /* if containing ep undefined, look at its container */
1869 if (ep && ep->p.sys_type == EMPTY_PARTITION)
1873 if (boxes == NESTED || (boxes == CHAINED && !is_extended))
1875 else if (all_logicals_inside_outermost_extended)
1876 pp = outer_extended_partition(ep);
1879 ff = pp ? (pp->start + unit - 1) / unit : 0;
1881 /* rounding up wastes almost an entire cylinder - round down
1882 and leave it to compute_start_sect() to fix the difference */
1883 ff = pp ? pp->start / unit : 0;
1885 /* MBR and 1st sector of an extended partition are never free */
1890 for(pp = partitions; pp < partitions+pno; pp++) {
1891 if (!is_parent(pp, ep) && pp->size > 0) {
1892 if ((partitions_in_order || pp->start / unit <= ff
1893 || (mid && pp->start / unit <= mid))
1894 && (fff = (pp->start + pp->size + unit - 1) / unit) > ff) {
1904 /* find the default value for <size> - assuming entire units */
1905 static unsigned long
1906 max_length(int pno, int is_extended, struct part_desc *ep, int format,
1907 unsigned long start, struct disk_desc *z) {
1909 unsigned long unit = unitsize(format);
1910 struct part_desc *partitions = &(z->partitions[0]), *pp = 0;
1912 /* if containing ep undefined, look at its container */
1913 if (ep && ep->p.sys_type == EMPTY_PARTITION)
1917 if (boxes == NESTED || (boxes == CHAINED && !is_extended))
1919 else if (all_logicals_inside_outermost_extended)
1920 pp = outer_extended_partition(ep);
1922 fu = pp ? (pp->start + pp->size) / unit : get_disksize(format);
1924 for(pp = partitions; pp < partitions+pno; pp++)
1925 if (!is_parent(pp, ep) && pp->size > 0
1926 && pp->start / unit >= start && pp->start / unit < fu)
1927 fu = pp->start / unit;
1929 return (fu > start) ? fu - start : 0;
1932 /* compute starting sector of a partition inside an extended one */
1933 /* return 0 on failure */
1934 /* ep is 0 or points to surrounding extended partition */
1936 compute_start_sect(struct part_desc *p, struct part_desc *ep) {
1938 int inc = (DOS && B.sectors) ? B.sectors : 1;
1941 if (ep && p->start + p->size >= ep->start + 1)
1942 delta = p->start - ep->start - inc;
1943 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) {
1979 int inc = (DOS && B.sectors) ? B.sectors : 1;
1980 int format = F_SECTOR;
1981 struct part_desc *p0 = &(z->partitions[0]), *eep = ep->ep;
1983 if (boxes == NESTED) {
1984 ep->start = first_free(ep-p0, 1, eep, format, p->start, z);
1985 ep->size = max_length(ep-p0, 1, eep, format, ep->start, z);
1986 if (ep->start > p->start || ep->start + ep->size < p->start + p->size) {
1987 warn(_("cannot build surrounding extended partition\n"));
1991 ep->start = p->start;
1992 if (boxes == CHAINED)
1998 ep->p.nr_sects = ep->size;
2000 ep->p.sys_type = EXTENDED_PARTITION;
2001 if (!compute_start_sect(ep, eep) || !compute_start_sect(p, ep)) {
2002 ep->p.sys_type = EMPTY_PARTITION;
2011 read_line(int pno, struct part_desc *ep, char *dev, int interactive,
2012 struct disk_desc *z) {
2013 unsigned char line[1000];
2014 unsigned char *fields[11];
2015 int fno, pct = pno%4;
2016 struct part_desc p, *orig;
2017 unsigned long ff, ff1, ul, ml, ml1, def;
2018 int format, lpno, is_extd;
2023 lpno = index_to_linux(pno, z);
2026 if (pct == 0 && (show_extended || pno == 0))
2028 warn("%s:", partname(dev, lpno, 10));
2031 /* read input line - skip blank lines when reading from a file */
2033 fno = read_stdin(fields, line, SIZE(fields), SIZE(line));
2034 } while(fno == RD_CMD || (fno == 0 && !interactive));
2035 if (fno == RD_EOF) {
2037 } else if (fno > 10 && *(fields[10]) != 0) {
2038 do_warn(_("too many input fields\n"));
2042 if (fno == 1 && !strcmp(fields[0], ".")) {
2047 /* use specified format, but round to cylinders if F_MEGABYTE specified */
2049 if (B.cylindersize && specified_format == F_MEGABYTE)
2050 format = F_CYLINDER;
2052 orig = (one_only ? &(oldp.partitions[pno]) : 0);
2057 /* first read the type - we need to know whether it is extended */
2058 /* stop reading when input blank (defaults) and all is full */
2060 if (fno == 0) { /* empty line */
2061 if (orig && is_extended(orig->p.sys_type))
2063 ff = first_free(pno, is_extd, ep, format, 0, z);
2064 ml = max_length(pno, is_extd, ep, format, ff, z);
2065 if (ml == 0 && is_extd == 0) {
2067 ff = first_free(pno, is_extd, ep, format, 0, z);
2068 ml = max_length(pno, is_extd, ep, format, ff, z);
2070 if (ml == 0 && pno >= 4) {
2071 /* no free blocks left - don't read any further */
2072 warn(_("No room for more\n"));
2076 if (fno < 3 || !*(fields[2]))
2077 ul = orig ? orig->p.sys_type :
2078 (is_extd || (pno > 3 && pct == 1 && show_extended))
2079 ? EXTENDED_PARTITION : LINUX_NATIVE;
2080 else if (!strcmp(fields[2], "L"))
2082 else if (!strcmp(fields[2], "S"))
2084 else if (!strcmp(fields[2], "E"))
2085 ul = EXTENDED_PARTITION;
2086 else if (!strcmp(fields[2], "X"))
2087 ul = LINUX_EXTENDED;
2088 else if (get_ul(fields[2], &ul, LINUX_NATIVE, 16))
2091 warn(_("Illegal type\n"));
2095 is_extd = is_extended(ul);
2098 ff = first_free(pno, is_extd, ep, format, 0, z);
2099 ff1 = ff * unitsize(format);
2100 def = orig ? orig->start : (pno > 4 && pct > 1) ? 0 : ff1;
2101 if (fno < 1 || !*(fields[0]))
2104 if (get_ul(fields[0], &ul, def / unitsize(0), 0))
2106 p.start = ul * unitsize(0);
2107 p.start -= (p.start % unitsize(format));
2111 ml = max_length(pno, is_extd, ep, format, p.start / unitsize(format), z);
2112 ml1 = ml * unitsize(format);
2113 def = orig ? orig->size : (pno > 4 && pct > 1) ? 0 : ml1;
2114 if (fno < 2 || !*(fields[1]))
2117 if (get_ul(fields[1], &ul, def / unitsize(0), 0))
2119 p.size = ul * unitsize(0) + unitsize(format) - 1;
2120 p.size -= (p.size % unitsize(format));
2123 warn(_("Warning: given size (%lu) exceeds max allowable size (%lu)\n"),
2124 (p.size + unitsize(0) - 1) / unitsize(0), ml1 / unitsize(0));
2128 if (p.size == 0 && pno >= 4 && (fno < 2 || !*(fields[1]))) {
2129 warn(_("Warning: empty partition\n"));
2133 p.p.nr_sects = p.size;
2135 if (p.size == 0 && !orig) {
2136 if (fno < 1 || !*(fields[0]))
2138 if (fno < 3 || !*(fields[2]))
2139 p.p.sys_type = EMPTY_PARTITION;
2142 if (p.start < ff1 && p.size > 0) {
2143 warn(_("Warning: bad partition start (earliest %lu)\n"),
2144 (ff1 + unitsize(0) - 1) / unitsize(0));
2149 if (fno < 4 || !*(fields[3]))
2150 ul = (orig ? orig->p.bootable : 0);
2151 else if (!strcmp(fields[3], "-"))
2153 else if (!strcmp(fields[3], "*") || !strcmp(fields[3], "+"))
2156 warn(_("unrecognized bootable flag - choose - or *\n"));
2161 if (ep && ep->p.sys_type == EMPTY_PARTITION) {
2162 if (!build_surrounding_extended(&p, ep, z))
2165 if (!compute_start_sect(&p, ep))
2168 { longchs aa = chs_to_longchs(p.p.begin_chs), bb;
2172 } else if (fno < 7) {
2173 warn(_("partial c,h,s specification?\n"));
2175 } else if (get_ul(fields[4], &bb.c, aa.c, 0) ||
2176 get_ul(fields[5], &bb.h, aa.h, 0) ||
2177 get_ul(fields[6], &bb.s, aa.s, 0))
2179 p.p.begin_chs = longchs_to_chs(bb,B);
2181 { longchs aa = chs_to_longchs(p.p.end_chs), bb;
2185 } else if (fno < 10) {
2186 warn(_("partial c,h,s specification?\n"));
2188 } else if (get_ul(fields[7], &bb.c, aa.c, 0) ||
2189 get_ul(fields[8], &bb.h, aa.h, 0) ||
2190 get_ul(fields[9], &bb.s, aa.s, 0))
2192 p.p.end_chs = longchs_to_chs(bb, B);
2195 if (pno > 3 && p.size && show_extended && p.p.sys_type != EMPTY_PARTITION
2196 && (is_extended(p.p.sys_type) != (pct == 1))) {
2197 warn(_("Extended partition not where expected\n"));
2202 z->partitions[pno] = p;
2203 if (pno >= z->partno)
2204 z->partno += 4; /* reqd for out_partition() */
2207 out_partition(dev, 0, &(z->partitions[pno]), z, B);
2212 /* ep either points to the extended partition to contain this one,
2213 or to the empty partition that may become extended or is 0 */
2215 read_partition(char *dev, int interactive, int pno, struct part_desc *ep,
2216 struct disk_desc *z) {
2217 struct part_desc *p = &(z->partitions[pno]);
2221 *p = oldp.partitions[pno];
2222 if (one_only_pno != pno)
2224 } else if (!show_extended && pno > 4 && pno%4)
2227 while (!(i = read_line(pno, ep, dev, interactive, z)))
2229 fatal(_("bad input\n"));
2237 if (pno >= z->partno)
2243 read_partition_chain(char *dev, int interactive, struct part_desc *ep,
2244 struct disk_desc *z) {
2250 if (base+4 > SIZE(z->partitions)) {
2251 do_warn(_("too many partitions\n"));
2255 if (!read_partition(dev, interactive, base+i, ep, z))
2257 for (i=0; i<4; i++) {
2258 ep = &(z->partitions[base+i]);
2259 if (is_extended(ep->p.sys_type) && ep->size)
2263 /* nothing found - maybe an empty partition is going
2265 if (one_only || show_extended)
2267 ep = &(z->partitions[base+1]);
2268 if (ep->size || ep->p.sys_type != EMPTY_PARTITION)
2275 read_input(char *dev, int interactive, struct disk_desc *z) {
2277 struct part_desc *partitions = &(z->partitions[0]), *ep;
2279 for (i=0; i < SIZE(z->partitions); i++)
2280 partitions[i] = zero_part_desc;
2284 warn(_("Input in the following format; absent fields get a default value.\n"
2285 "<start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s>\n"
2286 "Usually you only need to specify <start> and <size> (and perhaps <type>).\n"));
2290 read_partition(dev, interactive, i, 0, z);
2291 for (i=0; i<4; i++) {
2293 if (is_extended(ep->p.sys_type) && ep->size)
2294 read_partition_chain(dev, interactive, ep, z);
2296 add_sector_and_offset(z);
2300 * G. The command line
2303 static void version(void) {
2304 printf("%s %s %s (aeb@cwi.nl, %s) from util-linux-"
2305 UTIL_LINUX_VERSION "\n",
2306 PROGNAME, _("version"), VERSION, DATE);
2312 printf(_("Usage: %s [options] device ...\n"), PROGNAME);
2313 puts (_("device: something like /dev/hda or /dev/sda"));
2314 puts (_("useful options:"));
2315 puts (_(" -s [or --show-size]: list size of a partition"));
2316 puts (_(" -c [or --id]: print or change partition Id"));
2317 puts (_(" -l [or --list]: list partitions of each device"));
2318 puts (_(" -d [or --dump]: idem, but in a format suitable for later input"));
2319 puts (_(" -i [or --increment]: number cylinders etc. from 1 instead of from 0"));
2320 puts (_(" -uS, -uB, -uC, -uM: accept/report in units of sectors/blocks/cylinders/MB"));
2321 puts (_(" -T [or --list-types]:list the known partition types"));
2322 puts (_(" -D [or --DOS]: for DOS-compatibility: waste a little space"));
2323 puts (_(" -R [or --re-read]: make kernel reread partition table"));
2324 puts (_(" -N# : change only the partition with number #"));
2325 puts (_(" -n : do not actually write to disk"));
2326 puts (_(" -O file : save the sectors that will be overwritten to file"));
2327 puts (_(" -I file : restore these sectors again"));
2328 puts (_(" -v [or --version]: print version"));
2329 puts (_(" -? [or --help]: print this message"));
2330 puts (_("dangerous options:"));
2331 puts (_(" -g [or --show-geometry]: print the kernel's idea of the geometry"));
2332 puts (_(" -G [or --show-pt-geometry]: print geometry guessed from the partition table"));
2333 puts (_(" -x [or --show-extended]: also list extended partitions on output\n"
2334 " or expect descriptors for them on input"));
2335 puts (_(" -L [or --Linux]: do not complain about things irrelevant for Linux"));
2336 puts (_(" -q [or --quiet]: suppress warning messages"));
2337 puts (_(" You can override the detected geometry using:"));
2338 puts (_(" -C# [or --cylinders #]:set the number of cylinders to use"));
2339 puts (_(" -H# [or --heads #]: set the number of heads to use"));
2340 puts (_(" -S# [or --sectors #]: set the number of sectors to use"));
2341 puts (_("You can disable all consistency checking with:"));
2342 puts (_(" -f [or --force]: do what I say, even if it is stupid"));
2347 activate_usage(char *progn) {
2349 printf(_("%s device list active partitions on device\n"), progn);
2350 printf(_("%s device n1 n2 ... activate partitions n1 ..., inactivate the rest\n"), progn);
2351 printf(_("%s -An device activate partition n, inactivate the other ones\n"), PROGNAME);
2356 unhide_usage(char *progn) {
2360 static char short_opts[] = "cdfgilnqsu:vx?1A::C:DGH:I:LN:O:RS:TU::V";
2362 #define PRINT_ID 0400
2363 #define CHANGE_ID 01000
2365 static const struct option long_opts[] = {
2366 { "change-id", no_argument, NULL, 'c' + CHANGE_ID },
2367 { "print-id", no_argument, NULL, 'c' + PRINT_ID },
2368 { "id", no_argument, NULL, 'c' },
2369 { "dump", no_argument, NULL, 'd' },
2370 { "force", no_argument, NULL, 'f' },
2371 { "show-geometry", no_argument, NULL, 'g' },
2372 { "increment", no_argument, NULL, 'i' },
2373 { "list", no_argument, NULL, 'l' },
2374 { "quiet", no_argument, NULL, 'q' },
2375 { "show-size", no_argument, NULL, 's' },
2376 { "unit", required_argument, NULL, 'u' },
2377 { "version", no_argument, NULL, 'v' },
2378 { "show-extended", no_argument, NULL, 'x' },
2379 { "help", no_argument, NULL, '?' },
2380 { "one-only", no_argument, NULL, '1' },
2381 { "cylinders", required_argument, NULL, 'C' },
2382 { "heads", required_argument, NULL, 'H' },
2383 { "sectors", required_argument, NULL, 'S' },
2384 { "show-pt-geometry", no_argument, NULL, 'G' },
2385 { "activate", optional_argument, NULL, 'A' },
2386 { "DOS", no_argument, NULL, 'D' },
2387 { "DOS-extended", no_argument, NULL, 'E' },
2388 { "Linux", no_argument, NULL, 'L' },
2389 { "re-read", no_argument, NULL, 'R' },
2390 { "list-types", no_argument, NULL, 'T' },
2391 { "unhide", optional_argument, NULL, 'U' },
2392 { "no-reread", no_argument, NULL, 160 },
2393 { "IBM", no_argument, NULL, 161 },
2394 { "leave-last", no_argument, NULL, 161 },
2395 /* undocumented flags - not all completely implemented */
2396 { "in-order", no_argument, NULL, 128 },
2397 { "not-in-order", no_argument, NULL, 129 },
2398 { "inside-outer", no_argument, NULL, 130 },
2399 { "not-inside-outer", no_argument, NULL, 131 },
2400 { "nested", no_argument, NULL, 132 },
2401 { "chained", no_argument, NULL, 133 },
2402 { "onesector", no_argument, NULL, 134 },
2403 { NULL, 0, NULL, 0 }
2407 is_ide_cdrom_or_tape(char *device) {
2410 struct stat statbuf;
2413 /* No device was given explicitly, and we are trying some
2414 likely things. But opening /dev/hdc may produce errors like
2415 "hdc: tray open or drive not ready"
2416 if it happens to be a CD-ROM drive. It even happens that
2417 the process hangs on the attempt to read a music CD.
2418 So try to be careful. This only works since 2.1.73. */
2420 if (strncmp("/dev/hd", device, 7))
2423 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2424 procf = fopen(buf, "r");
2425 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2426 is_ide = (!strncmp(buf, "cdrom", 5) ||
2427 !strncmp(buf, "tape", 4));
2429 /* Now when this proc file does not exist, skip the
2430 device when it is read-only. */
2431 if (stat(device, &statbuf) == 0)
2432 is_ide = ((statbuf.st_mode & 0222) == 0);
2440 is_probably_full_disk(char *name) {
2441 struct hd_geometry geometry;
2444 fd = open(name, O_RDONLY);
2446 i = ioctl(fd, HDIO_GETGEO, &geometry);
2449 return (fd >= 0 && i == 0 && geometry.start == 0);
2452 #define PROC_PARTITIONS "/proc/partitions"
2453 static FILE *procf = NULL;
2457 procf = fopen(PROC_PARTITIONS, "r");
2459 fprintf(stderr, _("cannot open %s\n"), PROC_PARTITIONS);
2464 static char devname[120];
2465 char line[100], ptname[100];
2470 while (fgets(line, sizeof(line), procf) != NULL) {
2471 if (sscanf (line, " %d %d %d %[^\n ]",
2472 &ma, &mi, &sz, ptname) != 4)
2474 snprintf(devname, sizeof(devname), "/dev/%s", ptname);
2475 if (!is_probably_full_disk(devname))
2485 static void do_list(char *dev, int silent);
2486 static void do_size(char *dev, int silent);
2487 static void do_geom(char *dev, int silent);
2488 static void do_pt_geom(char *dev, int silent);
2489 static void do_fdisk(char *dev);
2490 static void do_reread(char *dev);
2491 static void do_change_id(char *dev, char *part, char *id);
2492 static void do_unhide(char **av, int ac, char *arg);
2493 static void do_activate(char **av, int ac, char *arg);
2495 unsigned long long total_size;
2498 main(int argc, char **argv) {
2503 int opt_out_geom = 0;
2504 int opt_out_pt_geom = 0;
2510 char *activatearg = 0;
2511 char *unhidearg = 0;
2514 setlocale(LC_ALL, "");
2515 bindtextdomain(PACKAGE, LOCALEDIR);
2516 textdomain(PACKAGE);
2519 fatal(_("no command?\n"));
2520 if ((progn = rindex(argv[0], '/')) == NULL)
2524 if (!strcmp(progn, "activate"))
2525 activate = 1; /* equivalent to `sfdisk -A' */
2526 #if 0 /* not important enough to deserve a name */
2527 else if (!strcmp(progn, "unhide"))
2528 unhide = 1; /* equivalent to `sfdisk -U' */
2533 while ((c = getopt_long (argc, argv, short_opts, long_opts, NULL)) != -1) {
2536 force = 1; break; /* does not imply quiet */
2538 opt_out_geom = 1; break;
2540 opt_out_pt_geom = 1; break;
2542 increment = 1; break;
2544 case 'c' + PRINT_ID:
2545 case 'c' + CHANGE_ID:
2548 dump = 1; /* fall through */
2550 opt_list = 1; break;
2552 no_write = 1; break;
2556 opt_size = 1; break;
2558 set_format(*optarg); break;
2563 show_extended = 1; break;
2565 activatearg = optarg;
2566 activate = 1; break;
2568 U.cylinders = atoi(optarg); break;
2572 DOS_extended = 1; break;
2574 U.heads = atoi(optarg); break;
2578 one_only = atoi(optarg); break;
2580 restore_sector_file = optarg; break;
2582 save_sector_file = optarg; break;
2584 opt_reread = 1; break;
2586 U.sectors = atoi(optarg); break;
2599 /* undocumented flags */
2601 partitions_in_order = 1; break;
2603 partitions_in_order = 0; break;
2605 all_logicals_inside_outermost_extended = 1; break;
2607 all_logicals_inside_outermost_extended = 0; break;
2609 boxes = NESTED; break;
2611 boxes = CHAINED; break;
2613 boxes = ONESECTOR; break;
2617 no_reread = 1; break;
2619 leave_last = 1; break;
2623 if (optind == argc &&
2624 (opt_list || opt_out_geom || opt_out_pt_geom || opt_size || verify)) {
2625 /* try all known devices */
2628 while ((dev = nextproc()) != NULL) {
2629 if (is_ide_cdrom_or_tape(dev))
2633 if (opt_out_pt_geom)
2637 if (opt_list || verify)
2642 printf(_("total: %llu blocks\n"), total_size);
2647 if (optind == argc) {
2649 activate_usage(fdisk ? "sfdisk -A" : progn);
2651 unhide_usage(fdisk ? "sfdisk -U" : progn);
2656 if (opt_list || opt_out_geom || opt_out_pt_geom || opt_size || verify) {
2657 while (optind < argc) {
2659 do_geom(argv[optind], 0);
2660 if (opt_out_pt_geom)
2661 do_pt_geom(argv[optind], 0);
2663 do_size(argv[optind], 0);
2664 if (opt_list || verify)
2665 do_list(argv[optind], 0);
2672 do_activate(argv+optind, argc-optind, activatearg);
2676 do_unhide(argv+optind, argc-optind, unhidearg);
2680 if ((do_id & PRINT_ID) != 0 && optind != argc-2)
2681 fatal(_("usage: sfdisk --print-id device partition-number\n"));
2682 else if ((do_id & CHANGE_ID) != 0 && optind != argc-3)
2683 fatal(_("usage: sfdisk --change-id device partition-number Id\n"));
2684 else if (optind != argc-3 && optind != argc-2)
2685 fatal(_("usage: sfdisk --id device partition-number [Id]\n"));
2686 do_change_id(argv[optind], argv[optind+1],
2687 (optind == argc-2) ? 0 : argv[optind+2]);
2691 if (optind != argc-1)
2692 fatal(_("can specify only one device (except with -l or -s)\n"));
2697 else if (restore_sector_file)
2698 restore_sectors(dev);
2706 * H. Listing the current situation
2710 my_open (char *dev, int rw, int silent) {
2713 mode = (rw ? O_RDWR : O_RDONLY);
2714 fd = open(dev, mode);
2715 if (fd < 0 && !silent) {
2718 fatal(_("cannot open %s read-write\n"), dev);
2720 fatal(_("cannot open %s for reading\n"), dev);
2726 do_list (char *dev, int silent) {
2728 struct disk_desc *z;
2730 fd = my_open(dev, 0, silent);
2737 get_cylindersize(dev, fd, dump ? 1 : opt_list ? 0 : 1);
2738 get_partitions(dev, fd, z);
2741 out_partitions(dev, z);
2744 if (partitions_ok(z))
2745 warn(_("%s: OK\n"), dev);
2752 do_geom (char *dev, int silent) {
2756 fd = my_open(dev, 0, silent);
2760 R = get_geometry(dev, fd, silent);
2762 printf(_("%s: %ld cylinders, %ld heads, %ld sectors/track\n"),
2763 dev, R.cylinders, R.heads, R.sectors);
2767 do_pt_geom (char *dev, int silent) {
2769 struct disk_desc *z;
2772 fd = my_open(dev, 0, silent);
2779 get_cylindersize(dev, fd, 1);
2780 get_partitions(dev, fd, z);
2784 if (z->partno != 0 && get_fdisk_geometry(z)) {
2786 R.sectors = F.sectors;
2787 R.cylindersize = R.heads * R.sectors;
2788 R.cylinders = (R.cylindersize == 0) ? 0 :
2789 R.total_size / R.cylindersize;
2793 printf(_("%s: %ld cylinders, %ld heads, %ld sectors/track\n"),
2794 dev, R.cylinders, R.heads, R.sectors);
2797 /* for compatibility with earlier fdisk: provide option -s */
2799 do_size (char *dev, int silent) {
2801 unsigned long long size;
2803 fd = my_open(dev, 0, silent);
2807 if (disksize(fd, &size)) {
2810 fatal(_("Cannot get size of %s\n"), dev);
2815 size /= 2; /* convert sectors to blocks */
2817 /* a CDROM drive without mounted CD yields MAXINT */
2818 if (silent && size == ((1<<30)-1))
2822 printf("%s: %9llu\n", dev, size);
2824 printf("%llu\n", size);
2830 * Activate: usually one wants to have a single primary partition
2831 * to be active. OS/2 fdisk makes non-bootable logical partitions
2832 * active - I don't know what that means to OS/2 Boot Manager.
2834 * Call: activate /dev/hda 2 5 7 make these partitions active
2835 * and the remaining ones inactive
2836 * Or: sfdisk -A /dev/hda 2 5 7
2838 * If only a single partition must be active, one may also use the form
2839 * sfdisk -A2 /dev/hda
2841 * With "activate /dev/hda" or "sfdisk -A /dev/hda" the active partitions
2842 * are listed but not changed. To get zero active partitions, use
2843 * "activate /dev/hda none" or "sfdisk -A /dev/hda none".
2844 * Use something like `echo ",,,*" | sfdisk -N2 /dev/hda' to only make
2845 * /dev/hda2 active, without changing other partitions.
2847 * A warning will be given if after the change not precisely one primary
2848 * partition is active.
2850 * The present syntax was chosen to be (somewhat) compatible with the
2851 * activate from the LILO package.
2854 set_active (struct disk_desc *z, char *pnam) {
2857 pno = asc_to_index(pnam, z);
2858 if (z->partitions[pno].ptype == DOS_TYPE)
2859 z->partitions[pno].p.bootable = 0x80;
2863 do_activate (char **av, int ac, char *arg) {
2866 int rw, i, pno, lpno;
2867 struct disk_desc *z;
2871 rw = (!no_write && (arg || ac > 1));
2872 fd = my_open(dev, rw, 0);
2875 get_cylindersize(dev, fd, 1);
2876 get_partitions(dev, fd, z);
2878 if (!arg && ac == 1) {
2879 /* list active partitions */
2880 for (pno=0; pno < z->partno; pno++) {
2881 if (z->partitions[pno].p.bootable) {
2882 lpno = index_to_linux(pno, z);
2883 if (pno == linux_to_index(lpno, z))
2884 printf("%s\n", partname(dev, lpno, 0));
2886 printf("%s#%d\n", dev, pno);
2887 if (z->partitions[pno].p.bootable != 0x80)
2888 warn(_("bad active byte: 0x%x instead of 0x80\n"),
2889 z->partitions[pno].p.bootable);
2893 /* clear `active byte' everywhere */
2894 for (pno=0; pno < z->partno; pno++)
2895 if (z->partitions[pno].ptype == DOS_TYPE)
2896 z->partitions[pno].p.bootable = 0;
2898 /* then set where desired */
2901 else for(i=1; i<ac; i++)
2902 set_active(z, av[i]);
2904 /* then write to disk */
2905 if (write_partitions(dev, fd, z))
2906 warn(_("Done\n\n"));
2911 for (pno=0; pno < z->partno && pno < 4; pno++)
2912 if (z->partitions[pno].p.bootable)
2915 warn(_("You have %d active primary partitions. This does not matter for LILO,\n"
2916 "but the DOS MBR will only boot a disk with 1 active partition.\n"), i);
2920 set_unhidden (struct disk_desc *z, char *pnam) {
2924 pno = asc_to_index(pnam, z);
2925 id = z->partitions[pno].p.sys_type;
2926 if (id == 0x11 || id == 0x14 || id == 0x16 || id == 0x17)
2929 fatal(_("partition %s has id %x and is not hidden\n"), pnam, id);
2930 z->partitions[pno].p.sys_type = id;
2934 * maybe remove and make part of --change-id
2937 do_unhide (char **av, int ac, char *arg) {
2940 struct disk_desc *z;
2945 fd = my_open(dev, rw, 0);
2948 get_cylindersize(dev, fd, 1);
2949 get_partitions(dev, fd, z);
2951 /* unhide where desired */
2953 set_unhidden(z, arg);
2954 else for(i=1; i<ac; i++)
2955 set_unhidden(z, av[i]);
2957 /* then write to disk */
2958 if (write_partitions(dev, fd, z))
2959 warn(_("Done\n\n"));
2965 do_change_id(char *dev, char *pnam, char *id) {
2967 struct disk_desc *z;
2973 fd = my_open(dev, rw, 0);
2976 get_cylindersize(dev, fd, 1);
2977 get_partitions(dev, fd, z);
2979 pno = asc_to_index(pnam, z);
2981 printf("%x\n", z->partitions[pno].p.sys_type);
2984 i = strtoul(id, NULL, 16);
2986 fatal(_("Bad Id %lx\n"), i);
2987 z->partitions[pno].p.sys_type = i;
2989 if (write_partitions(dev, fd, z))
2990 warn(_("Done\n\n"));
2996 do_reread(char *dev) {
2999 fd = my_open(dev, 0, 0);
3000 if (reread_ioctl(fd))
3001 do_warn(_("This disk is currently in use.\n"));
3005 * I. Writing the new situation
3009 do_fdisk(char *dev){
3012 struct stat statbuf;
3013 int interactive = isatty(0);
3014 struct disk_desc *z;
3016 if (stat(dev, &statbuf) < 0) {
3018 fatal(_("Fatal error: cannot find %s\n"), dev);
3020 if (!S_ISBLK(statbuf.st_mode)) {
3021 do_warn(_("Warning: %s is not a block device\n"), dev);
3024 fd = my_open(dev, !no_write, 0);
3026 if (!no_write && !no_reread) {
3027 warn(_("Checking that no-one is using this disk right now ...\n"));
3028 if (reread_ioctl(fd)) {
3029 do_warn(_("\nThis disk is currently in use - repartitioning is probably a bad idea.\n"
3030 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
3031 "Use the --no-reread flag to suppress this check.\n"));
3033 do_warn(_("Use the --force flag to overrule all checks.\n"));
3043 get_cylindersize(dev, fd, 0);
3044 get_partitions(dev, fd, z);
3046 printf(_("Old situation:\n"));
3047 out_partitions(dev, z);
3049 if (one_only && (one_only_pno = linux_to_index(one_only, z)) < 0)
3050 fatal(_("Partition %d does not exist, cannot change it\n"), one_only);
3056 read_input(dev, interactive, z);
3058 printf(_("New situation:\n"));
3059 out_partitions(dev, z);
3061 if (!partitions_ok(z) && !force) {
3063 fatal(_("I don't like these partitions - nothing changed.\n"
3064 "(If you really want this, use the --force option.)\n"));
3066 do_warn(_("I don't like this - probably you should answer No\n"));
3071 printf(_("Are you satisfied with this? [ynq] "));
3073 printf(_("Do you want to write this to disk? [ynq] "));
3074 answer = c = getchar();
3075 while (c != '\n' && c != EOF)
3078 printf(_("\nsfdisk: premature end of input\n"));
3079 if (c == EOF || answer == 'q' || answer == 'Q') {
3080 fatal(_("Quitting - nothing changed\n"));
3081 } else if (answer == 'n' || answer == 'N') {
3083 } else if (answer == 'y' || answer == 'Y') {
3086 printf(_("Please answer one of y,n,q\n"));
3093 if (write_partitions(dev, fd, z))
3094 printf(_("Successfully wrote the new partition table\n\n"));
3098 reread_disk_partition(dev, fd);
3100 warn(_("If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)\n"
3101 "to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1\n"
3102 "(See fdisk(8).)\n"));
3104 sync(); /* superstition */