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 * 20040824 - David A. Wheeler <dwheeler@dwheeler.com> - warnings to stderr
34 #define PROGNAME "sfdisk"
35 #define VERSION "3.08"
39 #include <stdlib.h> /* atoi, free */
40 #include <stdarg.h> /* varargs */
41 #include <unistd.h> /* read, write */
42 #include <fcntl.h> /* O_RDWR */
43 #include <errno.h> /* ERANGE */
44 #include <string.h> /* index() */
47 #include <sys/ioctl.h>
49 #include <sys/utsname.h>
50 #include <linux/unistd.h> /* _syscall */
54 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
60 * C. About heads, sectors and cylinders
63 * F. The standard input
65 * H. Listing the current situation
66 * I. Writing the new situation
70 int force
= 0; /* 1: do what I say, even if it is stupid ... */
71 int quiet
= 0; /* 1: suppress all warnings */
72 /* IA-64 gcc spec file currently does -DLinux... */
74 int Linux
= 0; /* 1: suppress warnings irrelevant for Linux */
75 int DOS
= 0; /* 1: shift extended partitions by #sectors, not 1 */
76 int DOS_extended
= 0; /* 1: use starting cylinder boundary of extd partn */
77 int dump
= 0; /* 1: list in a format suitable for later input */
78 int verify
= 0; /* 1: check that listed partition is reasonable */
79 int no_write
= 0; /* 1: do not actually write to disk */
80 int no_reread
= 0; /* 1: skip the BLKRRPART ioctl test at startup */
81 int leave_last
= 0; /* 1: don't allocate the last cylinder */
83 char *save_sector_file
= NULL
;
84 char *restore_sector_file
= NULL
;
87 do_warn(char *s
, ...) {
92 vfprintf(stderr
, s
, p
);
108 error(char *s
, ...) {
113 fprintf(stderr
, "\n" PROGNAME
": ");
114 vfprintf(stderr
, s
, p
);
120 fatal(char *s
, ...) {
125 fprintf(stderr
, "\n" PROGNAME
": ");
126 vfprintf(stderr
, s
, p
);
133 * GCC nonsense - needed for GCC 3.4.x with -O2
135 #if defined(__GNUC__PREREQ) && __GNUC_PREREQ(3,4)
136 #define __attribute__used __attribute__ ((used))
138 #define __attribute__used
141 /* Or test with #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 4) */
149 * sseek: seek to specified sector - return 0 on failure
151 * For >4GB disks lseek needs a > 32bit arg, and we have to use llseek.
152 * On the other hand, a 32 bit sector number is OK until 2TB.
153 * The routines _llseek and sseek below are the only ones that
154 * know about the loff_t type.
156 * Note: we use 512-byte sectors here, irrespective of the hardware ss.
159 #if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) || defined (__s390x__)
164 static __attribute__used
165 _syscall5(int, _llseek
, unsigned int, fd
, ulong
, hi
, ulong
, lo
,
166 loff_t
*, res
, unsigned int, wh
);
170 sseek(char *dev
, unsigned int fd
, unsigned long s
) {
172 in
= ((loff_t
) s
<< 9);
176 if (_llseek (fd
, in
>>32, in
& 0xffffffff, &out
, SEEK_SET
) != 0) {
178 if ((out
= lseek(fd
, in
, SEEK_SET
)) != in
) {
181 error(_("seek error on %s - cannot seek to %lu\n"), dev
, s
);
186 error(_("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n"),
187 (unsigned int)(in
>>32), (unsigned int)(in
& 0xffffffff),
188 (unsigned int)(out
>>32), (unsigned int)(out
& 0xffffffff));
199 * We preserve all sectors read in a chain - some of these will
200 * have to be modified and written back.
204 unsigned long sectornumber
;
215 sectorhead
= s
->next
;
220 static struct sector
*
221 get_sector(char *dev
, int fd
, unsigned long sno
) {
224 for(s
= sectorhead
; s
; s
= s
->next
)
225 if (s
->sectornumber
== sno
)
228 if (!sseek(dev
, fd
, sno
))
231 if (!(s
= (struct sector
*) malloc(sizeof(struct sector
))))
232 fatal(_("out of memory - giving up\n"));
234 if (read(fd
, s
->data
, sizeof(s
->data
)) != sizeof(s
->data
)) {
235 if (errno
) /* 0 in case we read past end-of-disk */
237 error(_("read error on %s - cannot read sector %lu\n"), dev
, sno
);
242 s
->next
= sectorhead
;
244 s
->sectornumber
= sno
;
245 s
->to_be_written
= 0;
251 msdos_signature (struct sector
*s
) {
252 unsigned char *data
= s
->data
;
253 if (data
[510] == 0x55 && data
[511] == 0xaa)
255 error(_("ERROR: sector %lu does not have an msdos signature\n"),
261 write_sectors(char *dev
, int fd
) {
264 for (s
= sectorhead
; s
; s
= s
->next
)
265 if (s
->to_be_written
) {
266 if (!sseek(dev
, fd
, s
->sectornumber
))
268 if (write(fd
, s
->data
, sizeof(s
->data
)) != sizeof(s
->data
)) {
270 error(_("write error on %s - cannot write sector %lu\n"),
271 dev
, s
->sectornumber
);
274 s
->to_be_written
= 0;
280 ulong_to_chars(unsigned long u
, char *uu
) {
290 chars_to_ulong(unsigned char *uu
) {
295 u
= (u
<< 8) | uu
[i
];
300 save_sectors(char *dev
, int fdin
) {
305 fdout
= open(save_sector_file
, O_WRONLY
| O_CREAT
, 0444);
307 perror(save_sector_file
);
308 error(_("cannot open partition sector save file (%s)\n"),
313 for (s
= sectorhead
; s
; s
= s
->next
)
314 if (s
->to_be_written
) {
315 ulong_to_chars(s
->sectornumber
, ss
);
316 if (!sseek(dev
, fdin
, s
->sectornumber
))
318 if (read(fdin
, ss
+4, 512) != 512) {
320 error(_("read error on %s - cannot read sector %lu\n"),
321 dev
, s
->sectornumber
);
324 if (write(fdout
, ss
, sizeof(ss
)) != sizeof(ss
)) {
326 error(_("write error on %s\n"), save_sector_file
);
333 static void reread_disk_partition(char *dev
, int fd
);
336 restore_sectors(char *dev
) {
342 if (stat(restore_sector_file
, &statbuf
) < 0) {
343 perror(restore_sector_file
);
344 error(_("cannot stat partition restore file (%s)\n"),
345 restore_sector_file
);
348 if (statbuf
.st_size
% 516) {
349 error(_("partition restore file has wrong size - not restoring\n"));
352 if (!(ss
= (char *) malloc(statbuf
.st_size
))) {
353 error(_("out of memory?\n"));
356 fdin
= open(restore_sector_file
, O_RDONLY
);
358 perror(restore_sector_file
);
359 error(_("cannot open partition restore file (%s)\n"),
360 restore_sector_file
);
363 if (read(fdin
, ss
, statbuf
.st_size
) != statbuf
.st_size
) {
365 error(_("error reading %s\n"), restore_sector_file
);
369 fdout
= open(dev
, O_WRONLY
);
372 error(_("cannot open device %s for writing\n"), dev
);
377 ct
= statbuf
.st_size
/516;
379 sno
= chars_to_ulong(ss
);
380 if (!sseek(dev
, fdout
, sno
))
382 if (write(fdout
, ss
+4, 512) != 512) {
384 error(_("error writing sector %lu on %s\n"), sno
, dev
);
391 reread_disk_partition(dev
, fdout
);
397 * C. About heads, sectors and cylinders
401 * <linux/hdreg.h> defines HDIO_GETGEO and
402 * struct hd_geometry {
403 * unsigned char heads;
404 * unsigned char sectors;
405 * unsigned short cylinders;
406 * unsigned long start;
409 * For large disks g.cylinders is truncated, so we use BLKGETSIZE.
413 * We consider several geometries for a disk:
414 * B - the BIOS geometry, gotten from the kernel via HDIO_GETGEO
415 * F - the fdisk geometry
416 * U - the user-specified geometry
418 * 0 means unspecified / unknown
421 unsigned long cylindersize
;
422 unsigned long heads
, sectors
, cylinders
;
426 static struct geometry
427 get_geometry(char *dev
, int fd
, int silent
) {
428 struct hd_geometry g
;
430 unsigned long long sectors
;
433 if (ioctl(fd
, HDIO_GETGEO
, &g
)) {
434 g
.heads
= g
.sectors
= g
.cylinders
= g
.start
= 0;
436 do_warn(_("Disk %s: cannot get geometry\n"), dev
);
441 R
.sectors
= g
.sectors
;
442 R
.cylindersize
= R
.heads
* R
.sectors
;
445 if (disksize(fd
, §ors
)) {
447 do_warn(_("Disk %s: cannot get size\n"), dev
);
448 } else if (R
.cylindersize
) {
449 sectors
/= R
.cylindersize
;
459 get_cylindersize(char *dev
, int fd
, int silent
) {
462 R
= get_geometry(dev
, fd
, silent
);
464 B
.heads
= (U
.heads
? U
.heads
: R
.heads
);
465 B
.sectors
= (U
.sectors
? U
.sectors
: R
.sectors
);
466 B
.cylinders
= (U
.cylinders
? U
.cylinders
: R
.cylinders
);
468 B
.cylindersize
= B
.heads
* B
.sectors
;
470 if (R
.start
&& !force
) {
472 _("Warning: start=%lu - this looks like a partition rather than\n"
473 "the entire disk. Using fdisk on it is probably meaningless.\n"
474 "[Use the --force option if you really want this]\n"), R
.start
);
478 if (R
.heads
&& B
.heads
!= R
.heads
)
479 warn(_("Warning: HDIO_GETGEO says that there are %lu heads\n"),
481 if (R
.sectors
&& B
.sectors
!= R
.sectors
)
482 warn(_("Warning: HDIO_GETGEO says that there are %lu sectors\n"),
484 if (R
.cylinders
&& B
.cylinders
!= R
.cylinders
485 && B
.cylinders
< 65536 && R
.cylinders
< 65536)
486 warn(_("Warning: BLKGETSIZE/HDIO_GETGEO says that there are %lu cylinders\n"),
490 warn(_("Warning: unlikely number of sectors (%lu) - usually at most 63\n"
491 "This will give problems with all software that uses C/H/S addressing.\n"),
494 printf(_("\nDisk %s: %lu cylinders, %lu heads, %lu sectors/track\n"),
495 dev
, B
.cylinders
, B
.heads
, B
.sectors
);
498 typedef struct { unsigned char h
,s
,c
; } chs
; /* has some c bits in s */
499 chs zero_chs
= { 0,0,0 };
501 typedef struct { unsigned long h
,s
,c
; } longchs
;
502 longchs zero_longchs
;
505 longchs_to_chs (longchs aa
, struct geometry G
) {
508 if (aa
.h
< 256 && aa
.s
< 64 && aa
.c
< 1024) {
510 a
.s
= aa
.s
| ((aa
.c
>> 2) & 0xc0);
512 } else if (G
.heads
&& G
.sectors
) {
514 a
.s
= G
.sectors
| 0xc0;
522 chs_to_longchs (chs a
) {
528 aa
.c
= (aa
.c
<< 2) + a
.c
;
533 ulong_to_longchs (unsigned long sno
, struct geometry G
) {
536 if (G
.heads
&& G
.sectors
&& G
.cylindersize
) {
537 aa
.s
= 1 + sno
% G
.sectors
;
538 aa
.h
= (sno
/ G
.sectors
) % G
.heads
;
539 aa
.c
= sno
/ G
.cylindersize
;
547 ulong_to_chs (unsigned long sno
, struct geometry G
) {
548 return longchs_to_chs(ulong_to_longchs(sno
, G
), G
);
553 longchs_to_ulong (longchs aa
, struct geometry G
) {
554 return (aa
.c
*G
.cylindersize
+ aa
.h
*G
.sectors
+ aa
.s
- 1);
558 chs_to_ulong (chs a
, struct geometry G
) {
559 return longchs_to_ulong(chs_to_longchs(a
), G
);
564 is_equal_chs (chs a
, chs b
) {
565 return (a
.h
== b
.h
&& a
.s
== b
.s
&& a
.c
== b
.c
);
569 chs_ok (chs a
, char *v
, char *w
) {
570 longchs aa
= chs_to_longchs(a
);
573 if (is_equal_chs(a
, zero_chs
))
575 if (B
.heads
&& aa
.h
>= B
.heads
) {
576 warn(_("%s of partition %s has impossible value for head: "
577 "%lu (should be in 0-%lu)\n"), w
, v
, aa
.h
, B
.heads
-1);
580 if (B
.sectors
&& (aa
.s
== 0 || aa
.s
> B
.sectors
)) {
581 warn(_("%s of partition %s has impossible value for sector: "
582 "%lu (should be in 1-%lu)\n"), w
, v
, aa
.s
, B
.sectors
);
585 if (B
.cylinders
&& aa
.c
>= B
.cylinders
) {
586 warn(_("%s of partition %s has impossible value for cylinders: "
587 "%lu (should be in 0-%lu)\n"), w
, v
, aa
.c
, B
.cylinders
-1);
594 * D. About system Ids
597 #define EMPTY_PARTITION 0
598 #define EXTENDED_PARTITION 5
599 #define WIN98_EXTENDED 0x0f
600 #define DM6_AUX1PARTITION 0x51
601 #define DM6_AUX3PARTITION 0x53
602 #define DM6_PARTITION 0x54
603 #define EZD_PARTITION 0x55
604 #define LINUX_SWAP 0x82
605 #define LINUX_NATIVE 0x83
606 #define LINUX_EXTENDED 0x85
607 #define BSD_PARTITION 0xa5
608 #define NETBSD_PARTITION 0xa9
610 /* List of partition types now in i386_sys_types.c */
613 sysname(unsigned char type
) {
616 for (s
= i386_sys_types
; s
->name
; s
++)
626 printf(_("Id Name\n\n"));
627 for (s
= i386_sys_types
; s
->name
; s
++)
628 printf("%2x %s\n", s
->type
, _(s
->name
));
632 is_extended(unsigned char type
) {
633 return (type
== EXTENDED_PARTITION
634 || type
== LINUX_EXTENDED
635 || type
== WIN98_EXTENDED
);
639 is_bsd(unsigned char type
) {
640 return (type
== BSD_PARTITION
|| type
== NETBSD_PARTITION
);
644 * E. About partitions
647 /* MS/DOS partition */
650 unsigned char bootable
; /* 0 or 0x80 */
652 unsigned char sys_type
;
654 unsigned int start_sect
; /* starting sector counting from 0 */
655 unsigned int nr_sects
; /* nr of sectors in partition */
658 /* Unfortunately, partitions are not aligned, and non-Intel machines
659 are unhappy with non-aligned integers. So, we need a copy by hand. */
661 copy_to_int(unsigned char *cp
) {
672 copy_from_int(int m
, char *cp
) {
673 *cp
++ = (m
& 0xff); m
>>= 8;
674 *cp
++ = (m
& 0xff); m
>>= 8;
675 *cp
++ = (m
& 0xff); m
>>= 8;
680 copy_to_part(char *cp
, struct partition
*p
) {
682 p
->begin_chs
.h
= *cp
++;
683 p
->begin_chs
.s
= *cp
++;
684 p
->begin_chs
.c
= *cp
++;
686 p
->end_chs
.h
= *cp
++;
687 p
->end_chs
.s
= *cp
++;
688 p
->end_chs
.c
= *cp
++;
689 p
->start_sect
= copy_to_int(cp
);
690 p
->nr_sects
= copy_to_int(cp
+4);
694 copy_from_part(struct partition
*p
, char *cp
) {
696 *cp
++ = p
->begin_chs
.h
;
697 *cp
++ = p
->begin_chs
.s
;
698 *cp
++ = p
->begin_chs
.c
;
700 *cp
++ = p
->end_chs
.h
;
701 *cp
++ = p
->end_chs
.s
;
702 *cp
++ = p
->end_chs
.c
;
703 copy_from_int(p
->start_sect
, cp
);
704 copy_from_int(p
->nr_sects
, cp
+4);
707 /* Roughly speaking, Linux doesn't use any of the above fields except
708 for partition type, start sector and number of sectors. (However,
709 see also linux/drivers/scsi/fdomain.c.)
710 The only way partition type is used (in the kernel) is the comparison
711 for equality with EXTENDED_PARTITION (and these Disk Manager types). */
716 unsigned long sector
, offset
; /* disk location of this info */
718 struct part_desc
*ep
; /* extended partition containing this one */
724 static struct part_desc
*
725 outer_extended_partition(struct part_desc
*p
) {
732 is_parent(struct part_desc
*pp
, struct part_desc
*p
) {
742 struct part_desc partitions
[512];
746 /* determine where on the disk this information goes */
748 add_sector_and_offset(struct disk_desc
*z
) {
752 for (pno
= 0; pno
< z
->partno
; pno
++) {
753 p
= &(z
->partitions
[pno
]);
754 p
->offset
= 0x1be + (pno
%4)*sizeof(struct partition
);
755 p
->sector
= (p
->ep
? p
->ep
->start
: 0);
759 /* tell the kernel to reread the partition tables */
761 reread_ioctl(int fd
) {
762 if (ioctl(fd
, BLKRRPART
)) {
770 is_blockdev(int fd
) {
773 return(fstat(fd
, &statbuf
) == 0 && S_ISBLK(statbuf
.st_mode
));
776 /* reread after writing */
778 reread_disk_partition(char *dev
, int fd
) {
779 printf(_("Re-reading the partition table ...\n"));
782 sleep(3); /* superfluous since 1.3.20 */
784 if (reread_ioctl(fd
) && is_blockdev(fd
))
785 do_warn(_("The command to re-read the partition table failed\n"
786 "Reboot your system now, before using mkfs\n"));
790 do_warn(_("Error closing %s\n"), dev
);
795 /* find Linux name of this partition, assuming that it will have a name */
797 index_to_linux(int pno
, struct disk_desc
*z
) {
799 struct part_desc
*p
= &(z
->partitions
[0]);
800 for (i
=0; i
<pno
; i
++,p
++)
801 if (i
< 4 || (p
->size
> 0 && !is_extended(p
->p
.sys_type
)))
807 linux_to_index(int lpno
, struct disk_desc
*z
) {
809 struct part_desc
*p
= &(z
->partitions
[0]);
810 for (i
=0; i
<z
->partno
&& ct
< lpno
; i
++,p
++)
811 if ((i
< 4 || (p
->size
> 0 && !is_extended(p
->p
.sys_type
)))
818 asc_to_index(char *pnam
, struct disk_desc
*z
) {
825 pno
= linux_to_index(pnum
, z
);
827 if (!(pno
>= 0 && pno
< z
->partno
))
828 fatal(_("%s: no such partition\n"), pnam
);
833 * List partitions - in terms of sectors, blocks or cylinders
840 int default_format
= F_MEGABYTE
;
841 int specified_format
= 0;
842 int show_extended
= 0;
851 do_warn(_("unrecognized format - using sectors\n"));
852 case 'S': specified_format
= F_SECTOR
; break;
853 case 'B': specified_format
= F_BLOCK
; break;
854 case 'C': specified_format
= F_CYLINDER
; break;
855 case 'M': specified_format
= F_MEGABYTE
; break;
860 unitsize(int format
) {
861 default_format
= (B
.cylindersize
? F_CYLINDER
: F_MEGABYTE
);
862 if (!format
&& !(format
= specified_format
))
863 format
= default_format
;
869 return B
.cylindersize
;
880 get_disksize(int format
) {
881 unsigned long cs
= B
.cylinders
;
882 if (cs
&& leave_last
)
884 return (cs
* B
.cylindersize
) / unitsize(format
);
888 out_partition_header(char *dev
, int format
, struct geometry G
) {
890 printf(_("# partition table of %s\n"), dev
);
891 printf("unit: sectors\n\n");
895 default_format
= (G
.cylindersize
? F_CYLINDER
: F_MEGABYTE
);
896 if (!format
&& !(format
= specified_format
))
897 format
= default_format
;
901 do_warn(_("unimplemented format - using %s\n"),
902 G
.cylindersize
? _("cylinders") : _("sectors"));
904 if (G
.cylindersize
) {
905 printf(_("Units = cylinders of %lu bytes, blocks of 1024 bytes"
906 ", counting from %d\n\n"),
907 G
.cylindersize
<<9, increment
);
908 printf(_(" Device Boot Start End #cyls #blocks Id System\n"));
913 printf(_("Units = sectors of 512 bytes, counting from %d\n\n"),
915 printf(_(" Device Boot Start End #sectors Id System\n"));
918 printf(_("Units = blocks of 1024 bytes, counting from %d\n\n"),
920 printf(_(" Device Boot Start End #blocks Id System\n"));
923 printf(_("Units = mebibytes of 1048576 bytes, blocks of 1024 bytes"
924 ", counting from %d\n\n"), increment
);
925 printf(_(" Device Boot Start End MiB #blocks Id System\n"));
931 out_rounddown(int width
, unsigned long n
, unsigned long unit
, int inc
) {
932 printf("%*lu", width
, inc
+ n
/unit
);
934 putchar((n
% unit
) ? '+' : ' ');
939 out_roundup(int width
, unsigned long n
, unsigned long unit
, int inc
) {
940 if (n
== (unsigned long)(-1))
941 printf("%*s", width
, "-");
943 printf("%*lu", width
, inc
+ n
/unit
);
945 putchar(((n
+1) % unit
) ? '-' : ' ');
950 out_roundup_size(int width
, unsigned long n
, unsigned long unit
) {
951 printf("%*lu", width
, (n
+unit
-1)/unit
);
953 putchar((n
% unit
) ? '-' : ' ');
957 static struct geometry
958 get_fdisk_geometry_one(struct part_desc
*p
) {
961 chs b
= p
->p
.end_chs
;
962 longchs bb
= chs_to_longchs(b
);
965 G
.cylindersize
= G
.heads
*G
.sectors
;
966 G
.cylinders
= G
.start
= 0;
971 get_fdisk_geometry(struct disk_desc
*z
) {
974 struct geometry G0
, G
;
977 G0
.heads
= G0
.sectors
= 0;
978 for (pno
=0; pno
< z
->partno
; pno
++) {
979 p
= &(z
->partitions
[pno
]);
980 if (p
->size
!= 0 && p
->p
.sys_type
!= 0) {
981 G
= get_fdisk_geometry_one(p
);
985 } else if (G
.heads
!= G0
.heads
|| G
.sectors
!= G0
.sectors
) {
991 F
= (agree
? G0
: B
);
992 return (F
.sectors
!= B
.sectors
|| F
.heads
!= B
.heads
);
996 out_partition(char *dev
, int format
, struct part_desc
*p
,
997 struct disk_desc
*z
, struct geometry G
) {
998 unsigned long start
, end
, size
;
1001 if (!format
&& !(format
= specified_format
))
1002 format
= default_format
;
1004 pno
= p
- &(z
->partitions
[0]); /* our index */
1005 lpno
= index_to_linux(pno
, z
); /* name of next one that has a name */
1006 if (pno
== linux_to_index(lpno
, z
)) /* was that us? */
1007 printf("%s", partname(dev
, lpno
, 10)); /* yes */
1008 else if (show_extended
)
1012 putchar(dump
? ':' : ' ');
1015 end
= p
->start
+ p
->size
- 1;
1019 printf(" start=%9lu", start
);
1020 printf(", size=%9lu", size
);
1021 if (p
->ptype
== DOS_TYPE
) {
1022 printf(", Id=%2x", p
->p
.sys_type
);
1023 if (p
->p
.bootable
== 0x80)
1024 printf(", bootable");
1030 if (p
->ptype
!= DOS_TYPE
|| p
->p
.bootable
== 0)
1032 else if (p
->p
.bootable
== 0x80)
1035 printf(" ? "); /* garbage */
1039 if (G
.cylindersize
) {
1040 out_rounddown(6, start
, G
.cylindersize
, increment
);
1041 out_roundup(6, end
, G
.cylindersize
, increment
);
1042 out_roundup_size(6, size
, G
.cylindersize
);
1043 out_rounddown(9, size
, 2, 0);
1049 out_rounddown(9, start
, 1, increment
);
1050 out_roundup(9, end
, 1, increment
);
1051 out_rounddown(10, size
, 1, 0);
1055 printf("%8lu,%3lu ",
1056 p
->sector
/2, ((p
->sector
& 1) ? 512 : 0) + p
->offset
);
1058 out_rounddown(8, start
, 2, increment
);
1059 out_roundup(8, end
, 2, increment
);
1060 out_rounddown(9, size
, 2, 0);
1063 out_rounddown(5, start
, 2048, increment
);
1064 out_roundup(5, end
, 2048, increment
);
1065 out_roundup_size(5, size
, 2048);
1066 out_rounddown(9, size
, 2, 0);
1069 if (p
->ptype
== DOS_TYPE
) {
1071 p
->p
.sys_type
, sysname(p
->p
.sys_type
));
1076 /* Is chs as we expect? */
1077 if (!quiet
&& p
->ptype
== DOS_TYPE
) {
1080 a
= (size
? ulong_to_chs(start
,G
) : zero_chs
);
1082 aa
= chs_to_longchs(a
);
1083 bb
= chs_to_longchs(b
);
1084 if (a
.s
&& !is_equal_chs(a
, b
))
1085 do_warn(_("\t\tstart: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1086 aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1087 a
= (size
? ulong_to_chs(end
,G
) : zero_chs
);
1089 aa
= chs_to_longchs(a
);
1090 bb
= chs_to_longchs(b
);
1091 if (a
.s
&& !is_equal_chs(a
, b
))
1092 do_warn(_("\t\tend: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1093 aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1094 if (G
.cylinders
&& G
.cylinders
< 1024 && bb
.c
> G
.cylinders
)
1095 do_warn(_("partition ends on cylinder %ld, beyond the end of the disk\n"),
1101 out_partitions(char *dev
, struct disk_desc
*z
) {
1102 int pno
, format
= 0;
1105 do_warn(_("No partitions found\n"));
1107 if (get_fdisk_geometry(z
) && !dump
) {
1109 _("Warning: The partition table looks like it was made\n"
1110 " for C/H/S=*/%ld/%ld (instead of %ld/%ld/%ld).\n"
1111 "For this listing I'll assume that geometry.\n"),
1112 F
.heads
, F
.sectors
, B
.cylinders
, B
.heads
, B
.sectors
);
1115 out_partition_header(dev
, format
, F
);
1116 for(pno
=0; pno
< z
->partno
; pno
++) {
1117 out_partition(dev
, format
, &(z
->partitions
[pno
]), z
, F
);
1118 if (show_extended
&& pno
%4==3)
1125 disj(struct part_desc
*p
, struct part_desc
*q
) {
1127 ((p
->start
+ p
->size
<= q
->start
)
1128 || (is_extended(p
->p
.sys_type
)
1129 && q
->start
+ q
->size
<= p
->start
+ p
->size
));
1133 pnumber(struct part_desc
*p
, struct disk_desc
*z
) {
1134 static char buf
[20];
1136 struct part_desc
*p0
= &(z
->partitions
[0]);
1138 this = index_to_linux(p
-p0
, z
);
1139 next
= index_to_linux(p
-p0
+1, z
);
1142 sprintf(buf
, "%d", this);
1144 sprintf(buf
, "[%d]", this);
1149 partitions_ok(struct disk_desc
*z
) {
1150 struct part_desc
*partitions
= &(z
->partitions
[0]), *p
, *q
;
1151 int partno
= z
->partno
;
1153 #define PNO(p) pnumber(p, z)
1155 /* Have at least 4 partitions been defined? */
1158 fatal(_("no partition table present.\n"));
1160 fatal(_("strange, only %d partitions defined.\n"), partno
);
1164 /* Are the partitions of size 0 marked empty?
1165 And do they have start = 0? And bootable = 0? */
1166 for (p
= partitions
; p
- partitions
< partno
; p
++)
1168 if (p
->p
.sys_type
!= EMPTY_PARTITION
)
1169 warn(_("Warning: partition %s has size 0 but is not marked Empty\n"),
1171 else if (p
->p
.bootable
!= 0)
1172 warn(_("Warning: partition %s has size 0 and is bootable\n"),
1174 else if (p
->p
.start_sect
!= 0)
1175 warn(_("Warning: partition %s has size 0 and nonzero start\n"),
1177 /* all this is probably harmless, no error return */
1180 /* Are the logical partitions contained in their extended partitions? */
1181 for (p
= partitions
+4; p
< partitions
+partno
; p
++)
1182 if (p
->ptype
== DOS_TYPE
)
1183 if (p
->size
&& !is_extended(p
->p
.sys_type
)) {
1185 if (p
->start
< q
->start
|| p
->start
+ p
->size
> q
->start
+ q
->size
) {
1186 warn(_("Warning: partition %s "), PNO(p
));
1187 warn(_("is not contained in partition %s\n"), PNO(q
));
1192 /* Are the data partitions mutually disjoint? */
1193 for (p
= partitions
; p
< partitions
+partno
; p
++)
1194 if (p
->size
&& !is_extended(p
->p
.sys_type
))
1195 for (q
= p
+1; q
< partitions
+partno
; q
++)
1196 if (q
->size
&& !is_extended(q
->p
.sys_type
))
1197 if (!((p
->start
> q
-> start
) ? disj(q
,p
) : disj(p
,q
))) {
1198 warn(_("Warning: partitions %s "), PNO(p
));
1199 warn(_("and %s overlap\n"), PNO(q
));
1203 /* Are the data partitions and the extended partition
1204 table sectors disjoint? */
1205 for (p
= partitions
; p
< partitions
+partno
; p
++)
1206 if (p
->size
&& !is_extended(p
->p
.sys_type
))
1207 for (q
= partitions
; q
< partitions
+partno
; q
++)
1208 if (is_extended(q
->p
.sys_type
))
1209 if (p
->start
<= q
->start
&& p
->start
+ p
->size
> q
->start
) {
1210 warn(_("Warning: partition %s contains part of "
1211 "the partition table (sector %lu),\n"
1212 "and will destroy it when filled\n"),
1217 /* Do they start past zero and end before end-of-disk? */
1218 { unsigned long ds
= get_disksize(F_SECTOR
);
1219 for (p
= partitions
; p
< partitions
+partno
; p
++)
1221 if (p
->start
== 0) {
1222 warn(_("Warning: partition %s starts at sector 0\n"), PNO(p
));
1225 if (p
->size
&& p
->start
+ p
->size
> ds
) {
1226 warn(_("Warning: partition %s extends past end of disk\n"),
1233 /* At most one chain of DOS extended partitions ? */
1234 /* It seems that the OS/2 fdisk has the additional requirement
1235 that the extended partition must be the fourth one */
1237 for (p
= partitions
; p
< partitions
+4; p
++)
1238 if (p
->p
.sys_type
== EXTENDED_PARTITION
)
1240 if (ect
> 1 && !Linux
) {
1241 warn(_("Among the primary partitions, at most one can be extended\n"
1242 " (although this is not a problem under Linux)\n"));
1248 * Do all partitions start at a cylinder boundary ?
1249 * (this is not required for Linux)
1250 * The first partition starts after MBR.
1251 * Logical partitions start slightly after the containing extended partn.
1253 if (B
.cylindersize
) {
1254 for(p
= partitions
; p
< partitions
+partno
; p
++)
1256 if (p
->start
% B
.cylindersize
!= 0
1257 && (!p
->ep
|| p
->start
/ B
.cylindersize
!= p
->ep
->start
/ B
.cylindersize
)
1258 && (p
->p
.start_sect
>= B
.cylindersize
)) {
1259 warn(_("Warning: partition %s does not start "
1260 "at a cylinder boundary\n"), PNO(p
));
1264 if ((p
->start
+ p
->size
) % B
.cylindersize
) {
1265 warn(_("Warning: partition %s does not end "
1266 "at a cylinder boundary\n"), PNO(p
));
1273 /* Usually, one can boot only from primary partitions. */
1274 /* In fact, from a unique one only. */
1275 /* do not warn about bootable extended partitions -
1276 often LILO is there */
1278 for(p
= partitions
; p
< partitions
+partno
; p
++)
1279 if (p
->p
.bootable
) {
1281 pno
= p
- partitions
;
1282 else if (p
- partitions
< 4) {
1283 warn(_("Warning: more than one primary partition is marked "
1284 "bootable (active)\n"
1285 "This does not matter for LILO, but the DOS MBR will "
1286 "not boot this disk.\n"));
1289 if (p
- partitions
>= 4) {
1290 warn(_("Warning: usually one can boot from primary partitions "
1291 "only\nLILO disregards the `bootable' flag.\n"));
1295 if (pno
== -1 || pno
>= 4)
1296 warn(_("Warning: no primary partition is marked bootable (active)\n"
1297 "This does not matter for LILO, but the DOS MBR will "
1298 "not boot this disk.\n"));
1301 /* Is chs as we expect? */
1302 for(p
= partitions
; p
< partitions
+partno
; p
++)
1303 if (p
->ptype
== DOS_TYPE
) {
1306 a
= p
->size
? ulong_to_chs(p
->start
,B
) : zero_chs
;
1308 aa
= chs_to_longchs(a
);
1309 bb
= chs_to_longchs(b
);
1310 if (!chs_ok(b
, PNO(p
), _("start")))
1312 if (a
.s
&& !is_equal_chs(a
, b
))
1313 warn(_("partition %s: start: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1314 PNO(p
), aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1315 a
= p
->size
? ulong_to_chs(p
->start
+ p
->size
- 1, B
) : zero_chs
;
1317 aa
= chs_to_longchs(a
);
1318 bb
= chs_to_longchs(b
);
1319 if (!chs_ok(b
, PNO(p
), _("end")))
1321 if (a
.s
&& !is_equal_chs(a
, b
))
1322 warn(_("partition %s: end: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1323 PNO(p
), aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1324 if (B
.cylinders
&& B
.cylinders
< 1024 && bb
.c
> B
.cylinders
)
1325 warn(_("partition %s ends on cylinder %ld, beyond the end of the disk\n"),
1335 extended_partition(char *dev
, int fd
, struct part_desc
*ep
, struct disk_desc
*z
) {
1338 unsigned long start
, here
, next
;
1339 int i
, moretodo
= 1;
1341 struct part_desc
*partitions
= &(z
->partitions
[0]);
1342 int pno
= z
->partno
;
1344 here
= start
= ep
->start
;
1346 if (B
.cylindersize
&& start
% B
.cylindersize
) {
1349 here
= start
-= (start
% B
.cylindersize
);
1350 do_warn(_("Warning: shifted start of the extd partition "
1352 "(For listing purposes only. "
1353 "Do not change its contents.)\n"),
1356 do_warn(_("Warning: extended partition does not start at a "
1357 "cylinder boundary.\n"
1358 "DOS and Linux will interpret the contents differently.\n"));
1365 if (!(s
= get_sector(dev
, fd
, here
)))
1368 if (!msdos_signature(s
))
1371 cp
= s
->data
+ 0x1be;
1373 if (pno
+4 >= SIZE(z
->partitions
)) {
1374 do_warn(_("too many partitions - ignoring those past nr (%d)\n"),
1381 for (i
=0; i
<4; i
++,cp
+= sizeof(struct partition
)) {
1382 partitions
[pno
].sector
= here
;
1383 partitions
[pno
].offset
= cp
- s
->data
;
1384 partitions
[pno
].ep
= ep
;
1385 copy_to_part(cp
,&p
);
1386 if (is_extended(p
.sys_type
)) {
1387 partitions
[pno
].start
= start
+ p
.start_sect
;
1389 do_warn(_("tree of partitions?\n"));
1391 next
= partitions
[pno
].start
; /* follow `upper' branch */
1394 partitions
[pno
].start
= here
+ p
.start_sect
;
1396 partitions
[pno
].size
= p
.nr_sects
;
1397 partitions
[pno
].ptype
= DOS_TYPE
;
1398 partitions
[pno
].p
= p
;
1407 #define BSD_DISKMAGIC (0x82564557UL)
1408 #define BSD_MAXPARTITIONS 16
1409 #define BSD_FS_UNUSED 0
1410 typedef unsigned char u8
;
1411 typedef unsigned short u16
;
1412 typedef unsigned int u32
;
1413 struct bsd_disklabel
{
1416 char d_typename
[16];
1417 char d_packname
[16];
1421 u16 d_npartitions
; /* number of partitions in following */
1423 struct bsd_partition
{ /* the partition table */
1424 u32 p_size
; /* number of sectors in partition */
1425 u32 p_offset
; /* starting sector */
1426 u32 p_fsize
; /* filesystem basic fragment size */
1427 u8 p_fstype
; /* filesystem type, see below */
1428 u8 p_frag
; /* filesystem fragments per block */
1429 u16 p_cpg
; /* filesystem cylinders per group */
1430 } d_partitions
[BSD_MAXPARTITIONS
]; /* actually may be more */
1434 bsd_partition(char *dev
, int fd
, struct part_desc
*ep
, struct disk_desc
*z
) {
1435 struct bsd_disklabel
*l
;
1436 struct bsd_partition
*bp
, *bp0
;
1437 unsigned long start
= ep
->start
;
1439 struct part_desc
*partitions
= &(z
->partitions
[0]);
1440 int pno
= z
->partno
;
1442 if (!(s
= get_sector(dev
,fd
,start
+1)))
1444 l
= (struct bsd_disklabel
*) (s
->data
);
1445 if (l
->d_magic
!= BSD_DISKMAGIC
|| l
->d_magic2
!= BSD_DISKMAGIC
)
1448 bp
= bp0
= &l
->d_partitions
[0];
1449 while (bp
- bp0
< BSD_MAXPARTITIONS
&& bp
- bp0
< l
->d_npartitions
) {
1450 if (pno
+1 >= SIZE(z
->partitions
)) {
1451 do_warn(_("too many partitions - ignoring those "
1452 "past nr (%d)\n"), pno
-1);
1455 if (bp
->p_fstype
!= BSD_FS_UNUSED
) {
1456 partitions
[pno
].start
= bp
->p_offset
;
1457 partitions
[pno
].size
= bp
->p_size
;
1458 partitions
[pno
].sector
= start
+1;
1459 partitions
[pno
].offset
= (char *)bp
- (char *)bp0
;
1460 partitions
[pno
].ep
= 0;
1461 partitions
[pno
].ptype
= BSD_TYPE
;
1469 #define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r))
1472 linux_version_code(void) {
1473 struct utsname my_utsname
;
1476 if (uname(&my_utsname
) == 0) {
1477 p
= atoi(strtok(my_utsname
.release
, "."));
1478 q
= atoi(strtok(NULL
, "."));
1479 r
= atoi(strtok(NULL
, "."));
1480 return MAKE_VERSION(p
,q
,r
);
1486 msdos_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1489 struct partition pt
;
1491 struct part_desc
*partitions
= &(z
->partitions
[0]);
1492 int pno
= z
->partno
;
1493 int bsd_later
= (linux_version_code() >= MAKE_VERSION(2,3,40));
1495 if (!(s
= get_sector(dev
, fd
, start
)))
1498 if (!msdos_signature(s
))
1501 cp
= s
->data
+ 0x1be;
1502 copy_to_part(cp
,&pt
);
1504 /* If I am not mistaken, recent kernels will hide this from us,
1505 so we will never actually see traces of a Disk Manager */
1506 if (pt
.sys_type
== DM6_PARTITION
1507 || pt
.sys_type
== EZD_PARTITION
1508 || pt
.sys_type
== DM6_AUX1PARTITION
1509 || pt
.sys_type
== DM6_AUX3PARTITION
) {
1510 do_warn(_("detected Disk Manager - unable to handle that\n"));
1513 { unsigned int sig
= *(unsigned short *)(s
->data
+ 2);
1515 && *(unsigned short *)(s
->data
+ sig
) == 0x55aa
1516 && (1 & *(unsigned char *)(s
->data
+ sig
+ 2))) {
1517 do_warn(_("DM6 signature found - giving up\n"));
1522 for (pno
=0; pno
<4; pno
++,cp
+= sizeof(struct partition
)) {
1523 partitions
[pno
].sector
= start
;
1524 partitions
[pno
].offset
= cp
- s
->data
;
1525 copy_to_part(cp
,&pt
);
1526 partitions
[pno
].start
= start
+ pt
.start_sect
;
1527 partitions
[pno
].size
= pt
.nr_sects
;
1528 partitions
[pno
].ep
= 0;
1529 partitions
[pno
].p
= pt
;
1534 for (i
=0; i
<4; i
++) {
1535 if (is_extended(partitions
[i
].p
.sys_type
)) {
1536 if (!partitions
[i
].size
) {
1537 do_warn(_("strange..., an extended partition of size 0?\n"));
1540 extended_partition(dev
, fd
, &partitions
[i
], z
);
1542 if (!bsd_later
&& is_bsd(partitions
[i
].p
.sys_type
)) {
1543 if (!partitions
[i
].size
) {
1544 do_warn(_("strange..., a BSD partition of size 0?\n"));
1547 bsd_partition(dev
, fd
, &partitions
[i
], z
);
1552 for (i
=0; i
<4; i
++) {
1553 if (is_bsd(partitions
[i
].p
.sys_type
)) {
1554 if (!partitions
[i
].size
) {
1555 do_warn(_("strange..., a BSD partition of size 0?\n"));
1558 bsd_partition(dev
, fd
, &partitions
[i
], z
);
1567 osf_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1572 sun_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1577 amiga_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1582 get_partitions(char *dev
, int fd
, struct disk_desc
*z
) {
1585 if (!msdos_partition(dev
, fd
, 0, z
)
1586 && !osf_partition(dev
, fd
, 0, z
)
1587 && !sun_partition(dev
, fd
, 0, z
)
1588 && !amiga_partition(dev
, fd
, 0, z
)) {
1589 do_warn(_(" %s: unrecognized partition table type\n"), dev
);
1595 write_partitions(char *dev
, int fd
, struct disk_desc
*z
) {
1597 struct part_desc
*partitions
= &(z
->partitions
[0]), *p
;
1598 int pno
= z
->partno
;
1601 do_warn(_("-n flag was given: Nothing changed\n"));
1605 for (p
= partitions
; p
< partitions
+pno
; p
++) {
1606 s
= get_sector(dev
, fd
, p
->sector
);
1608 s
->to_be_written
= 1;
1609 if (p
->ptype
== DOS_TYPE
) {
1610 copy_from_part(&(p
->p
), s
->data
+ p
->offset
);
1611 s
->data
[510] = 0x55;
1612 s
->data
[511] = 0xaa;
1615 if (save_sector_file
) {
1616 if (!save_sectors(dev
, fd
)) {
1617 fatal(_("Failed saving the old sectors - aborting\n"));
1621 if (!write_sectors(dev
, fd
)) {
1622 error(_("Failed writing the partition on %s\n"), dev
);
1629 * F. The standard input
1634 * <start> <size> <type> <bootable> <c,h,s> <c,h,s>
1635 * Fields are separated by whitespace or comma or semicolon possibly
1636 * followed by whitespace; initial and trailing whitespace is ignored.
1637 * Numbers can be octal, decimal or hexadecimal, decimal is default
1638 * The <c,h,s> parts can (and probably should) be omitted.
1639 * Bootable is specified as [*|-], with as default not-bootable.
1640 * Type is given in hex, without the 0x prefix, or is [E|S|L|X], where
1641 * L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), and E
1642 * is EXTENDED_PARTITION (5), X is LINUX_EXTENDED (85).
1643 * The default value of start is the first nonassigned sector/cylinder/...
1644 * The default value of size is as much as possible (until next
1645 * partition or end-of-disk).
1646 * .: end of chain of extended partitions.
1648 * On interactive input an empty line means: all defaults.
1649 * Otherwise empty lines are ignored.
1662 { 3, "bootable", 1 },
1672 * Read a line, split it into fields
1674 * (some primitive handwork, but a more elaborate parser seems
1681 read_stdin(unsigned char **fields
, unsigned char *line
, int fieldssize
, int linesize
) {
1682 unsigned char *lp
, *ip
;
1685 /* boolean true and empty string at start */
1688 for (fno
=0; fno
< fieldssize
; fno
++)
1689 fields
[fno
] = line
+ 1;
1692 /* read a line from stdin */
1693 lp
= fgets(line
+2, linesize
, stdin
);
1698 if (!(lp
= index(lp
, '\n')))
1699 fatal(_("long or incomplete input line - quitting\n"));
1702 /* remove comments, if any */
1703 if ((lp
= index(line
+2, '#')) != 0)
1706 /* recognize a few commands - to be expanded */
1707 if (!strcmp(line
+2, "unit: sectors")) {
1708 specified_format
= F_SECTOR
;
1712 /* dump style? - then bad input is fatal */
1713 if ((ip
= index(line
+2, ':')) != 0) {
1722 for(d
= dumpflds
; d
-dumpflds
< SIZE(dumpflds
); d
++) {
1723 if (!strncmp(ip
, d
->fldname
, strlen(d
->fldname
))) {
1724 ip
+= strlen(d
->fldname
);
1728 fields
[d
->fldno
] = line
;
1729 else if (*ip
== '=') {
1730 while(isspace(*++ip
)) ;
1731 fields
[d
->fldno
] = ip
;
1732 while(isalnum(*ip
)) /* 0x07FF */
1735 fatal(_("input error: `=' expected after %s field\n"),
1737 if (fno
<= d
->fldno
)
1741 if (*ip
!= ',' && *ip
!= ';')
1742 fatal(_("input error: unexpected character %c after %s field\n"),
1748 fatal(_("unrecognized input: %s\n"), ip
);
1751 /* split line into fields */
1754 while((c
= *ip
++) != 0) {
1755 if (!lp
[-1] && (c
== '\t' || c
== ' '))
1757 else if (c
== '\t' || c
== ' ' || c
== ',' || c
== ';') {
1759 if (fno
< fieldssize
)
1766 if (lp
== fields
[fno
-1])
1771 /* read a number, use default if absent */
1772 /* a sign gives an offset from the default */
1774 get_ul(char *u
, unsigned long *up
, unsigned long def
, int base
) {
1782 } else if (*u
== '-') {
1788 val
= strtoul(u
, &nu
, base
);
1789 if (errno
== ERANGE
) {
1790 do_warn(_("number too big\n"));
1794 do_warn(_("trailing junk after number\n"));
1799 else if (sign
== -1)
1807 /* There are two common ways to structure extended partitions:
1808 as nested boxes, and as a chain. Sometimes the partitions
1809 must be given in order. Sometimes all logical partitions
1810 must lie inside the outermost extended partition.
1811 NESTED: every partition is contained in the surrounding partitions
1812 and is disjoint from all others.
1813 CHAINED: every data partition is contained in the surrounding partitions
1814 and disjoint from all others, but extended partitions may lie outside
1815 (insofar as allowed by all_logicals_inside_outermost_extended).
1816 ONESECTOR: all data partitions are mutually disjoint; extended partitions
1817 each use one sector only (except perhaps for the outermost one).
1819 int partitions_in_order
= 0;
1820 int all_logicals_inside_outermost_extended
= 1;
1821 enum { NESTED
, CHAINED
, ONESECTOR
} boxes
= NESTED
;
1823 /* find the default value for <start> - assuming entire units */
1824 static unsigned long
1825 first_free(int pno
, int is_extended
, struct part_desc
*ep
, int format
,
1826 unsigned long mid
, struct disk_desc
*z
) {
1827 unsigned long ff
, fff
;
1828 unsigned long unit
= unitsize(format
);
1829 struct part_desc
*partitions
= &(z
->partitions
[0]), *pp
= 0;
1831 /* if containing ep undefined, look at its container */
1832 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
)
1836 if (boxes
== NESTED
|| (boxes
== CHAINED
&& !is_extended
))
1838 else if (all_logicals_inside_outermost_extended
)
1839 pp
= outer_extended_partition(ep
);
1842 ff
= pp
? (pp
->start
+ unit
- 1) / unit
: 0;
1844 /* rounding up wastes almost an entire cylinder - round down
1845 and leave it to compute_start_sect() to fix the difference */
1846 ff
= pp
? pp
->start
/ unit
: 0;
1848 /* MBR and 1st sector of an extended partition are never free */
1853 for(pp
= partitions
; pp
< partitions
+pno
; pp
++) {
1854 if (!is_parent(pp
, ep
) && pp
->size
> 0) {
1855 if ((partitions_in_order
|| pp
->start
/ unit
<= ff
1856 || (mid
&& pp
->start
/ unit
<= mid
))
1857 && (fff
= (pp
->start
+ pp
->size
+ unit
- 1) / unit
) > ff
) {
1867 /* find the default value for <size> - assuming entire units */
1868 static unsigned long
1869 max_length(int pno
, int is_extended
, struct part_desc
*ep
, int format
,
1870 unsigned long start
, struct disk_desc
*z
) {
1872 unsigned long unit
= unitsize(format
);
1873 struct part_desc
*partitions
= &(z
->partitions
[0]), *pp
= 0;
1875 /* if containing ep undefined, look at its container */
1876 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
)
1880 if (boxes
== NESTED
|| (boxes
== CHAINED
&& !is_extended
))
1882 else if (all_logicals_inside_outermost_extended
)
1883 pp
= outer_extended_partition(ep
);
1885 fu
= pp
? (pp
->start
+ pp
->size
) / unit
: get_disksize(format
);
1887 for(pp
= partitions
; pp
< partitions
+pno
; pp
++)
1888 if (!is_parent(pp
, ep
) && pp
->size
> 0
1889 && pp
->start
/ unit
>= start
&& pp
->start
/ unit
< fu
)
1890 fu
= pp
->start
/ unit
;
1892 return (fu
> start
) ? fu
- start
: 0;
1895 /* compute starting sector of a partition inside an extended one */
1896 /* return 0 on failure */
1897 /* ep is 0 or points to surrounding extended partition */
1899 compute_start_sect(struct part_desc
*p
, struct part_desc
*ep
) {
1901 int inc
= (DOS
&& B
.sectors
) ? B
.sectors
: 1;
1904 if (ep
&& p
->start
+ p
->size
>= ep
->start
+ 1)
1905 delta
= p
->start
- ep
->start
- inc
;
1906 else if (p
->start
== 0 && p
->size
> 0)
1914 if (is_extended(p
->p
.sys_type
) && boxes
== ONESECTOR
)
1916 else if ((int)(p
->size
) <= 0) {
1917 warn(_("no room for partition descriptor\n"));
1922 : (is_extended(p
->p
.sys_type
) ?
1923 outer_extended_partition(ep
) : ep
)->start
);
1925 if (p
->p
.sys_type
== EMPTY_PARTITION
&& p
->size
== 0) {
1926 p
->p
.start_sect
= 0;
1927 p
->p
.begin_chs
= zero_chs
;
1928 p
->p
.end_chs
= zero_chs
;
1930 p
->p
.start_sect
= p
->start
- base
;
1931 p
->p
.begin_chs
= ulong_to_chs(p
->start
,B
);
1932 p
->p
.end_chs
= ulong_to_chs(p
->start
+ p
->size
- 1,B
);
1934 p
->p
.nr_sects
= p
->size
;
1938 /* build the extended partition surrounding a given logical partition */
1940 build_surrounding_extended(struct part_desc
*p
, struct part_desc
*ep
,
1941 struct disk_desc
*z
) {
1942 int inc
= (DOS
&& B
.sectors
) ? B
.sectors
: 1;
1943 int format
= F_SECTOR
;
1944 struct part_desc
*p0
= &(z
->partitions
[0]), *eep
= ep
->ep
;
1946 if (boxes
== NESTED
) {
1947 ep
->start
= first_free(ep
-p0
, 1, eep
, format
, p
->start
, z
);
1948 ep
->size
= max_length(ep
-p0
, 1, eep
, format
, ep
->start
, z
);
1949 if (ep
->start
> p
->start
|| ep
->start
+ ep
->size
< p
->start
+ p
->size
) {
1950 warn(_("cannot build surrounding extended partition\n"));
1954 ep
->start
= p
->start
;
1955 if (boxes
== CHAINED
)
1961 ep
->p
.nr_sects
= ep
->size
;
1963 ep
->p
.sys_type
= EXTENDED_PARTITION
;
1964 if (!compute_start_sect(ep
, eep
) || !compute_start_sect(p
, ep
)) {
1965 ep
->p
.sys_type
= EMPTY_PARTITION
;
1974 read_line(int pno
, struct part_desc
*ep
, char *dev
, int interactive
,
1975 struct disk_desc
*z
) {
1976 unsigned char line
[1000];
1977 unsigned char *fields
[11];
1978 int fno
, pct
= pno
%4;
1979 struct part_desc p
, *orig
;
1980 unsigned long ff
, ff1
, ul
, ml
, ml1
, def
;
1981 int format
, lpno
, is_extd
;
1986 lpno
= index_to_linux(pno
, z
);
1989 if (pct
== 0 && (show_extended
|| pno
== 0))
1991 warn("%s:", partname(dev
, lpno
, 10));
1994 /* read input line - skip blank lines when reading from a file */
1996 fno
= read_stdin(fields
, line
, SIZE(fields
), SIZE(line
));
1997 } while(fno
== RD_CMD
|| (fno
== 0 && !interactive
));
1998 if (fno
== RD_EOF
) {
2000 } else if (fno
> 10 && *(fields
[10]) != 0) {
2001 do_warn(_("too many input fields\n"));
2005 if (fno
== 1 && !strcmp(fields
[0], ".")) {
2010 /* use specified format, but round to cylinders if F_MEGABYTE specified */
2012 if (B
.cylindersize
&& specified_format
== F_MEGABYTE
)
2013 format
= F_CYLINDER
;
2015 orig
= (one_only
? &(oldp
.partitions
[pno
]) : 0);
2020 /* first read the type - we need to know whether it is extended */
2021 /* stop reading when input blank (defaults) and all is full */
2023 if (fno
== 0) { /* empty line */
2024 if (orig
&& is_extended(orig
->p
.sys_type
))
2026 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
2027 ml
= max_length(pno
, is_extd
, ep
, format
, ff
, z
);
2028 if (ml
== 0 && is_extd
== 0) {
2030 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
2031 ml
= max_length(pno
, is_extd
, ep
, format
, ff
, z
);
2033 if (ml
== 0 && pno
>= 4) {
2034 /* no free blocks left - don't read any further */
2035 warn(_("No room for more\n"));
2039 if (fno
< 3 || !*(fields
[2]))
2040 ul
= orig
? orig
->p
.sys_type
:
2041 (is_extd
|| (pno
> 3 && pct
== 1 && show_extended
))
2042 ? EXTENDED_PARTITION
: LINUX_NATIVE
;
2043 else if (!strcmp(fields
[2], "L"))
2045 else if (!strcmp(fields
[2], "S"))
2047 else if (!strcmp(fields
[2], "E"))
2048 ul
= EXTENDED_PARTITION
;
2049 else if (!strcmp(fields
[2], "X"))
2050 ul
= LINUX_EXTENDED
;
2051 else if (get_ul(fields
[2], &ul
, LINUX_NATIVE
, 16))
2054 warn(_("Illegal type\n"));
2058 is_extd
= is_extended(ul
);
2061 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
2062 ff1
= ff
* unitsize(format
);
2063 def
= orig
? orig
->start
: (pno
> 4 && pct
> 1) ? 0 : ff1
;
2064 if (fno
< 1 || !*(fields
[0]))
2067 if (get_ul(fields
[0], &ul
, def
/ unitsize(0), 0))
2069 p
.start
= ul
* unitsize(0);
2070 p
.start
-= (p
.start
% unitsize(format
));
2074 ml
= max_length(pno
, is_extd
, ep
, format
, p
.start
/ unitsize(format
), z
);
2075 ml1
= ml
* unitsize(format
);
2076 def
= orig
? orig
->size
: (pno
> 4 && pct
> 1) ? 0 : ml1
;
2077 if (fno
< 2 || !*(fields
[1]))
2080 if (get_ul(fields
[1], &ul
, def
/ unitsize(0), 0))
2082 p
.size
= ul
* unitsize(0) + unitsize(format
) - 1;
2083 p
.size
-= (p
.size
% unitsize(format
));
2086 warn(_("Warning: given size (%lu) exceeds max allowable size (%lu)\n"),
2087 (p
.size
+ unitsize(0) - 1) / unitsize(0), ml1
/ unitsize(0));
2091 if (p
.size
== 0 && pno
>= 4 && (fno
< 2 || !*(fields
[1]))) {
2092 warn(_("Warning: empty partition\n"));
2096 p
.p
.nr_sects
= p
.size
;
2098 if (p
.size
== 0 && !orig
) {
2099 if (fno
< 1 || !*(fields
[0]))
2101 if (fno
< 3 || !*(fields
[2]))
2102 p
.p
.sys_type
= EMPTY_PARTITION
;
2105 if (p
.start
< ff1
&& p
.size
> 0) {
2106 warn(_("Warning: bad partition start (earliest %lu)\n"),
2107 (ff1
+ unitsize(0) - 1) / unitsize(0));
2112 if (fno
< 4 || !*(fields
[3]))
2113 ul
= (orig
? orig
->p
.bootable
: 0);
2114 else if (!strcmp(fields
[3], "-"))
2116 else if (!strcmp(fields
[3], "*") || !strcmp(fields
[3], "+"))
2119 warn(_("unrecognized bootable flag - choose - or *\n"));
2124 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
) {
2125 if (!build_surrounding_extended(&p
, ep
, z
))
2128 if (!compute_start_sect(&p
, ep
))
2131 { longchs aa
= chs_to_longchs(p
.p
.begin_chs
), bb
;
2135 } else if (fno
< 7) {
2136 warn(_("partial c,h,s specification?\n"));
2138 } else if (get_ul(fields
[4], &bb
.c
, aa
.c
, 0) ||
2139 get_ul(fields
[5], &bb
.h
, aa
.h
, 0) ||
2140 get_ul(fields
[6], &bb
.s
, aa
.s
, 0))
2142 p
.p
.begin_chs
= longchs_to_chs(bb
,B
);
2144 { longchs aa
= chs_to_longchs(p
.p
.end_chs
), bb
;
2148 } else if (fno
< 10) {
2149 warn(_("partial c,h,s specification?\n"));
2151 } else if (get_ul(fields
[7], &bb
.c
, aa
.c
, 0) ||
2152 get_ul(fields
[8], &bb
.h
, aa
.h
, 0) ||
2153 get_ul(fields
[9], &bb
.s
, aa
.s
, 0))
2155 p
.p
.end_chs
= longchs_to_chs(bb
, B
);
2158 if (pno
> 3 && p
.size
&& show_extended
&& p
.p
.sys_type
!= EMPTY_PARTITION
2159 && (is_extended(p
.p
.sys_type
) != (pct
== 1))) {
2160 warn(_("Extended partition not where expected\n"));
2165 z
->partitions
[pno
] = p
;
2166 if (pno
>= z
->partno
)
2167 z
->partno
+= 4; /* reqd for out_partition() */
2170 out_partition(dev
, 0, &(z
->partitions
[pno
]), z
, B
);
2175 /* ep either points to the extended partition to contain this one,
2176 or to the empty partition that may become extended or is 0 */
2178 read_partition(char *dev
, int interactive
, int pno
, struct part_desc
*ep
,
2179 struct disk_desc
*z
) {
2180 struct part_desc
*p
= &(z
->partitions
[pno
]);
2184 *p
= oldp
.partitions
[pno
];
2185 if (one_only_pno
!= pno
)
2187 } else if (!show_extended
&& pno
> 4 && pno
%4)
2190 while (!(i
= read_line(pno
, ep
, dev
, interactive
, z
)))
2192 fatal(_("bad input\n"));
2200 if (pno
>= z
->partno
)
2206 read_partition_chain(char *dev
, int interactive
, struct part_desc
*ep
,
2207 struct disk_desc
*z
) {
2213 if (base
+4 > SIZE(z
->partitions
)) {
2214 do_warn(_("too many partitions\n"));
2218 if (!read_partition(dev
, interactive
, base
+i
, ep
, z
))
2220 for (i
=0; i
<4; i
++) {
2221 ep
= &(z
->partitions
[base
+i
]);
2222 if (is_extended(ep
->p
.sys_type
) && ep
->size
)
2226 /* nothing found - maybe an empty partition is going
2228 if (one_only
|| show_extended
)
2230 ep
= &(z
->partitions
[base
+1]);
2231 if (ep
->size
|| ep
->p
.sys_type
!= EMPTY_PARTITION
)
2238 read_input(char *dev
, int interactive
, struct disk_desc
*z
) {
2240 struct part_desc
*partitions
= &(z
->partitions
[0]), *ep
;
2242 for (i
=0; i
< SIZE(z
->partitions
); i
++)
2243 partitions
[i
] = zero_part_desc
;
2247 warn(_("Input in the following format; absent fields get a default value.\n"
2248 "<start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s>\n"
2249 "Usually you only need to specify <start> and <size> (and perhaps <type>).\n"));
2253 read_partition(dev
, interactive
, i
, 0, z
);
2254 for (i
=0; i
<4; i
++) {
2256 if (is_extended(ep
->p
.sys_type
) && ep
->size
)
2257 read_partition_chain(dev
, interactive
, ep
, z
);
2259 add_sector_and_offset(z
);
2263 * G. The command line
2266 static void version(void) {
2267 printf("%s %s %s (aeb@cwi.nl, %s)\n", PROGNAME
, _("version"), VERSION
, DATE
);
2273 printf(_("Usage: %s [options] device ...\n"), PROGNAME
);
2274 puts (_("device: something like /dev/hda or /dev/sda"));
2275 puts (_("useful options:"));
2276 puts (_(" -s [or --show-size]: list size of a partition"));
2277 puts (_(" -c [or --id]: print or change partition Id"));
2278 puts (_(" -l [or --list]: list partitions of each device"));
2279 puts (_(" -d [or --dump]: idem, but in a format suitable for later input"));
2280 puts (_(" -i [or --increment]: number cylinders etc. from 1 instead of from 0"));
2281 puts (_(" -uS, -uB, -uC, -uM: accept/report in units of sectors/blocks/cylinders/MB"));
2282 puts (_(" -T [or --list-types]:list the known partition types"));
2283 puts (_(" -D [or --DOS]: for DOS-compatibility: waste a little space"));
2284 puts (_(" -R [or --re-read]: make kernel reread partition table"));
2285 puts (_(" -N# : change only the partition with number #"));
2286 puts (_(" -n : do not actually write to disk"));
2287 puts (_(" -O file : save the sectors that will be overwritten to file"));
2288 puts (_(" -I file : restore these sectors again"));
2289 puts (_(" -v [or --version]: print version"));
2290 puts (_(" -? [or --help]: print this message"));
2291 puts (_("dangerous options:"));
2292 puts (_(" -g [or --show-geometry]: print the kernel's idea of the geometry"));
2293 puts (_(" -x [or --show-extended]: also list extended partitions on output\n"
2294 " or expect descriptors for them on input"));
2295 puts (_(" -L [or --Linux]: do not complain about things irrelevant for Linux"));
2296 puts (_(" -q [or --quiet]: suppress warning messages"));
2297 puts (_(" You can override the detected geometry using:"));
2298 puts (_(" -C# [or --cylinders #]:set the number of cylinders to use"));
2299 puts (_(" -H# [or --heads #]: set the number of heads to use"));
2300 puts (_(" -S# [or --sectors #]: set the number of sectors to use"));
2301 puts (_("You can disable all consistency checking with:"));
2302 puts (_(" -f [or --force]: do what I say, even if it is stupid"));
2307 activate_usage(char *progn
) {
2309 printf(_("%s device list active partitions on device\n"), progn
);
2310 printf(_("%s device n1 n2 ... activate partitions n1 ..., inactivate the rest\n"), progn
);
2311 printf(_("%s -An device activate partition n, inactivate the other ones\n"), PROGNAME
);
2316 unhide_usage(char *progn
) {
2320 static char short_opts
[] = "cdfgilnqsu:vx?1A::C:DH:I:LN:O:RS:TU::V";
2322 #define PRINT_ID 0400
2323 #define CHANGE_ID 01000
2325 static const struct option long_opts
[] = {
2326 { "change-id", no_argument
, NULL
, 'c' + CHANGE_ID
},
2327 { "print-id", no_argument
, NULL
, 'c' + PRINT_ID
},
2328 { "id", no_argument
, NULL
, 'c' },
2329 { "dump", no_argument
, NULL
, 'd' },
2330 { "force", no_argument
, NULL
, 'f' },
2331 { "show-geometry", no_argument
, NULL
, 'g' },
2332 { "increment", no_argument
, NULL
, 'i' },
2333 { "list", no_argument
, NULL
, 'l' },
2334 { "quiet", no_argument
, NULL
, 'q' },
2335 { "show-size", no_argument
, NULL
, 's' },
2336 { "unit", required_argument
, NULL
, 'u' },
2337 { "version", no_argument
, NULL
, 'v' },
2338 { "show-extended", no_argument
, NULL
, 'x' },
2339 { "help", no_argument
, NULL
, '?' },
2340 { "one-only", no_argument
, NULL
, '1' },
2341 { "cylinders", required_argument
, NULL
, 'C' },
2342 { "heads", required_argument
, NULL
, 'H' },
2343 { "sectors", required_argument
, NULL
, 'S' },
2344 { "activate", optional_argument
, NULL
, 'A' },
2345 { "DOS", no_argument
, NULL
, 'D' },
2346 { "DOS-extended", no_argument
, NULL
, 'E' },
2347 { "Linux", no_argument
, NULL
, 'L' },
2348 { "re-read", no_argument
, NULL
, 'R' },
2349 { "list-types", no_argument
, NULL
, 'T' },
2350 { "unhide", optional_argument
, NULL
, 'U' },
2351 { "no-reread", no_argument
, NULL
, 160 },
2352 { "IBM", no_argument
, NULL
, 161 },
2353 { "leave-last", no_argument
, NULL
, 161 },
2354 /* undocumented flags - not all completely implemented */
2355 { "in-order", no_argument
, NULL
, 128 },
2356 { "not-in-order", no_argument
, NULL
, 129 },
2357 { "inside-outer", no_argument
, NULL
, 130 },
2358 { "not-inside-outer", no_argument
, NULL
, 131 },
2359 { "nested", no_argument
, NULL
, 132 },
2360 { "chained", no_argument
, NULL
, 133 },
2361 { "onesector", no_argument
, NULL
, 134 },
2362 { NULL
, 0, NULL
, 0 }
2366 is_ide_cdrom_or_tape(char *device
) {
2369 struct stat statbuf
;
2372 /* No device was given explicitly, and we are trying some
2373 likely things. But opening /dev/hdc may produce errors like
2374 "hdc: tray open or drive not ready"
2375 if it happens to be a CD-ROM drive. It even happens that
2376 the process hangs on the attempt to read a music CD.
2377 So try to be careful. This only works since 2.1.73. */
2379 if (strncmp("/dev/hd", device
, 7))
2382 snprintf(buf
, sizeof(buf
), "/proc/ide/%s/media", device
+5);
2383 procf
= fopen(buf
, "r");
2384 if (procf
!= NULL
&& fgets(buf
, sizeof(buf
), procf
))
2385 is_ide
= (!strncmp(buf
, "cdrom", 5) ||
2386 !strncmp(buf
, "tape", 4));
2388 /* Now when this proc file does not exist, skip the
2389 device when it is read-only. */
2390 if (stat(device
, &statbuf
) == 0)
2391 is_ide
= ((statbuf
.st_mode
& 0222) == 0);
2399 is_probably_full_disk(char *name
) {
2400 struct hd_geometry geometry
;
2403 fd
= open(name
, O_RDONLY
);
2405 i
= ioctl(fd
, HDIO_GETGEO
, &geometry
);
2408 return (fd
>= 0 && i
== 0 && geometry
.start
== 0);
2411 #define PROC_PARTITIONS "/proc/partitions"
2412 static FILE *procf
= NULL
;
2416 procf
= fopen(PROC_PARTITIONS
, "r");
2418 fprintf(stderr
, _("cannot open %s\n"), PROC_PARTITIONS
);
2423 static char devname
[120];
2424 char line
[100], ptname
[100];
2429 while (fgets(line
, sizeof(line
), procf
) != NULL
) {
2430 if (sscanf (line
, " %d %d %d %[^\n ]",
2431 &ma
, &mi
, &sz
, ptname
) != 4)
2433 snprintf(devname
, sizeof(devname
), "/dev/%s", ptname
);
2434 if (!is_probably_full_disk(devname
))
2444 static void do_list(char *dev
, int silent
);
2445 static void do_size(char *dev
, int silent
);
2446 static void do_geom(char *dev
, int silent
);
2447 static void do_fdisk(char *dev
);
2448 static void do_reread(char *dev
);
2449 static void do_change_id(char *dev
, char *part
, char *id
);
2450 static void do_unhide(char **av
, int ac
, char *arg
);
2451 static void do_activate(char **av
, int ac
, char *arg
);
2453 unsigned long long total_size
;
2456 main(int argc
, char **argv
) {
2461 int opt_out_geom
= 0;
2467 char *activatearg
= 0;
2468 char *unhidearg
= 0;
2470 setlocale(LC_ALL
, "");
2471 bindtextdomain(PACKAGE
, LOCALEDIR
);
2472 textdomain(PACKAGE
);
2475 fatal(_("no command?\n"));
2476 if ((progn
= rindex(argv
[0], '/')) == NULL
)
2480 if (!strcmp(progn
, "activate"))
2481 activate
= 1; /* equivalent to `sfdisk -A' */
2482 #if 0 /* not important enough to deserve a name */
2483 else if (!strcmp(progn
, "unhide"))
2484 unhide
= 1; /* equivalent to `sfdisk -U' */
2489 while ((c
= getopt_long (argc
, argv
, short_opts
, long_opts
, NULL
)) != -1) {
2492 force
= 1; break; /* does not imply quiet */
2494 opt_out_geom
= 1; break;
2496 increment
= 1; break;
2498 case 'c' + PRINT_ID
:
2499 case 'c' + CHANGE_ID
:
2502 dump
= 1; /* fall through */
2504 opt_list
= 1; break;
2506 no_write
= 1; break;
2510 opt_size
= 1; break;
2512 set_format(*optarg
); break;
2517 show_extended
= 1; break;
2519 activatearg
= optarg
;
2520 activate
= 1; break;
2522 U
.cylinders
= atoi(optarg
); break;
2526 DOS_extended
= 1; break;
2528 U
.heads
= atoi(optarg
); break;
2532 one_only
= atoi(optarg
); break;
2534 restore_sector_file
= optarg
; break;
2536 save_sector_file
= optarg
; break;
2538 opt_reread
= 1; break;
2540 U
.sectors
= atoi(optarg
); break;
2553 /* undocumented flags */
2555 partitions_in_order
= 1; break;
2557 partitions_in_order
= 0; break;
2559 all_logicals_inside_outermost_extended
= 1; break;
2561 all_logicals_inside_outermost_extended
= 0; break;
2563 boxes
= NESTED
; break;
2565 boxes
= CHAINED
; break;
2567 boxes
= ONESECTOR
; break;
2571 no_reread
= 1; break;
2573 leave_last
= 1; break;
2577 if (optind
== argc
&& (opt_list
|| opt_out_geom
|| opt_size
|| verify
)) {
2578 /* try all known devices */
2581 while ((dev
= nextproc()) != NULL
) {
2582 if (is_ide_cdrom_or_tape(dev
))
2588 if (opt_list
|| verify
)
2593 printf(_("total: %llu blocks\n"), total_size
);
2598 if (optind
== argc
) {
2600 activate_usage(fdisk
? "sfdisk -A" : progn
);
2602 unhide_usage(fdisk
? "sfdisk -U" : progn
);
2607 if (opt_list
|| opt_out_geom
|| opt_size
|| verify
) {
2608 while (optind
< argc
) {
2610 do_geom(argv
[optind
], 0);
2612 do_size(argv
[optind
], 0);
2613 if (opt_list
|| verify
)
2614 do_list(argv
[optind
], 0);
2621 do_activate(argv
+optind
, argc
-optind
, activatearg
);
2625 do_unhide(argv
+optind
, argc
-optind
, unhidearg
);
2629 if ((do_id
& PRINT_ID
) != 0 && optind
!= argc
-2)
2630 fatal(_("usage: sfdisk --print-id device partition-number\n"));
2631 else if ((do_id
& CHANGE_ID
) != 0 && optind
!= argc
-3)
2632 fatal(_("usage: sfdisk --change-id device partition-number Id\n"));
2633 else if (optind
!= argc
-3 && optind
!= argc
-2)
2634 fatal(_("usage: sfdisk --id device partition-number [Id]\n"));
2635 do_change_id(argv
[optind
], argv
[optind
+1],
2636 (optind
== argc
-2) ? 0 : argv
[optind
+2]);
2640 if (optind
!= argc
-1)
2641 fatal(_("can specify only one device (except with -l or -s)\n"));
2646 else if (restore_sector_file
)
2647 restore_sectors(dev
);
2655 * H. Listing the current situation
2659 my_open (char *dev
, int rw
, int silent
) {
2662 mode
= (rw
? O_RDWR
: O_RDONLY
);
2663 fd
= open(dev
, mode
);
2664 if (fd
< 0 && !silent
) {
2667 fatal(_("cannot open %s read-write\n"), dev
);
2669 fatal(_("cannot open %s for reading\n"), dev
);
2675 do_list (char *dev
, int silent
) {
2677 struct disk_desc
*z
;
2679 fd
= my_open(dev
, 0, silent
);
2686 get_cylindersize(dev
, fd
, dump
? 1 : opt_list
? 0 : 1);
2687 get_partitions(dev
, fd
, z
);
2690 out_partitions(dev
, z
);
2693 if (partitions_ok(z
))
2694 warn(_("%s: OK\n"), dev
);
2701 do_geom (char *dev
, int silent
) {
2705 fd
= my_open(dev
, 0, silent
);
2709 R
= get_geometry(dev
, fd
, silent
);
2711 printf(_("%s: %ld cylinders, %ld heads, %ld sectors/track\n"),
2712 dev
, R
.cylinders
, R
.heads
, R
.sectors
);
2715 /* for compatibility with earlier fdisk: provide option -s */
2717 do_size (char *dev
, int silent
) {
2719 unsigned long long size
;
2721 fd
= my_open(dev
, 0, silent
);
2725 if (disksize(fd
, &size
)) {
2728 fatal(_("Cannot get size of %s\n"), dev
);
2733 size
/= 2; /* convert sectors to blocks */
2735 /* a CDROM drive without mounted CD yields MAXINT */
2736 if (silent
&& size
== ((1<<30)-1))
2740 printf("%s: %9llu\n", dev
, size
);
2742 printf("%llu\n", size
);
2748 * Activate: usually one wants to have a single primary partition
2749 * to be active. OS/2 fdisk makes non-bootable logical partitions
2750 * active - I don't know what that means to OS/2 Boot Manager.
2752 * Call: activate /dev/hda 2 5 7 make these partitions active
2753 * and the remaining ones inactive
2754 * Or: sfdisk -A /dev/hda 2 5 7
2756 * If only a single partition must be active, one may also use the form
2757 * sfdisk -A2 /dev/hda
2759 * With "activate /dev/hda" or "sfdisk -A /dev/hda" the active partitions
2760 * are listed but not changed. To get zero active partitions, use
2761 * "activate /dev/hda none" or "sfdisk -A /dev/hda none".
2762 * Use something like `echo ",,,*" | sfdisk -N2 /dev/hda' to only make
2763 * /dev/hda2 active, without changing other partitions.
2765 * A warning will be given if after the change not precisely one primary
2766 * partition is active.
2768 * The present syntax was chosen to be (somewhat) compatible with the
2769 * activate from the LILO package.
2772 set_active (struct disk_desc
*z
, char *pnam
) {
2775 pno
= asc_to_index(pnam
, z
);
2776 if (z
->partitions
[pno
].ptype
== DOS_TYPE
)
2777 z
->partitions
[pno
].p
.bootable
= 0x80;
2781 do_activate (char **av
, int ac
, char *arg
) {
2784 int rw
, i
, pno
, lpno
;
2785 struct disk_desc
*z
;
2789 rw
= (!no_write
&& (arg
|| ac
> 1));
2790 fd
= my_open(dev
, rw
, 0);
2793 get_cylindersize(dev
, fd
, 1);
2794 get_partitions(dev
, fd
, z
);
2796 if (!arg
&& ac
== 1) {
2797 /* list active partitions */
2798 for (pno
=0; pno
< z
->partno
; pno
++) {
2799 if (z
->partitions
[pno
].p
.bootable
) {
2800 lpno
= index_to_linux(pno
, z
);
2801 if (pno
== linux_to_index(lpno
, z
))
2802 printf("%s\n", partname(dev
, lpno
, 0));
2804 printf("%s#%d\n", dev
, pno
);
2805 if (z
->partitions
[pno
].p
.bootable
!= 0x80)
2806 warn(_("bad active byte: 0x%x instead of 0x80\n"),
2807 z
->partitions
[pno
].p
.bootable
);
2811 /* clear `active byte' everywhere */
2812 for (pno
=0; pno
< z
->partno
; pno
++)
2813 if (z
->partitions
[pno
].ptype
== DOS_TYPE
)
2814 z
->partitions
[pno
].p
.bootable
= 0;
2816 /* then set where desired */
2819 else for(i
=1; i
<ac
; i
++)
2820 set_active(z
, av
[i
]);
2822 /* then write to disk */
2823 if (write_partitions(dev
, fd
, z
))
2824 warn(_("Done\n\n"));
2829 for (pno
=0; pno
< z
->partno
&& pno
< 4; pno
++)
2830 if (z
->partitions
[pno
].p
.bootable
)
2833 warn(_("You have %d active primary partitions. This does not matter for LILO,\n"
2834 "but the DOS MBR will only boot a disk with 1 active partition.\n"), i
);
2838 set_unhidden (struct disk_desc
*z
, char *pnam
) {
2842 pno
= asc_to_index(pnam
, z
);
2843 id
= z
->partitions
[pno
].p
.sys_type
;
2844 if (id
== 0x11 || id
== 0x14 || id
== 0x16 || id
== 0x17)
2847 fatal(_("partition %s has id %x and is not hidden\n"), pnam
, id
);
2848 z
->partitions
[pno
].p
.sys_type
= id
;
2852 * maybe remove and make part of --change-id
2855 do_unhide (char **av
, int ac
, char *arg
) {
2858 struct disk_desc
*z
;
2863 fd
= my_open(dev
, rw
, 0);
2866 get_cylindersize(dev
, fd
, 1);
2867 get_partitions(dev
, fd
, z
);
2869 /* unhide where desired */
2871 set_unhidden(z
, arg
);
2872 else for(i
=1; i
<ac
; i
++)
2873 set_unhidden(z
, av
[i
]);
2875 /* then write to disk */
2876 if (write_partitions(dev
, fd
, z
))
2877 warn(_("Done\n\n"));
2883 do_change_id(char *dev
, char *pnam
, char *id
) {
2885 struct disk_desc
*z
;
2891 fd
= my_open(dev
, rw
, 0);
2894 get_cylindersize(dev
, fd
, 1);
2895 get_partitions(dev
, fd
, z
);
2897 pno
= asc_to_index(pnam
, z
);
2899 printf("%x\n", z
->partitions
[pno
].p
.sys_type
);
2902 i
= strtoul(id
, NULL
, 16);
2904 fatal(_("Bad Id %lx\n"), i
);
2905 z
->partitions
[pno
].p
.sys_type
= i
;
2907 if (write_partitions(dev
, fd
, z
))
2908 warn(_("Done\n\n"));
2914 do_reread(char *dev
) {
2917 fd
= my_open(dev
, 0, 0);
2918 if (reread_ioctl(fd
))
2919 do_warn(_("This disk is currently in use.\n"));
2923 * I. Writing the new situation
2927 do_fdisk(char *dev
){
2930 struct stat statbuf
;
2931 int interactive
= isatty(0);
2932 struct disk_desc
*z
;
2934 if (stat(dev
, &statbuf
) < 0) {
2936 fatal(_("Fatal error: cannot find %s\n"), dev
);
2938 if (!S_ISBLK(statbuf
.st_mode
)) {
2939 warn(_("Warning: %s is not a block device\n"), dev
);
2942 fd
= my_open(dev
, !no_write
, 0);
2944 if (!no_write
&& !no_reread
) {
2945 warn(_("Checking that no-one is using this disk right now ...\n"));
2946 if (reread_ioctl(fd
)) {
2947 do_warn(_("\nThis disk is currently in use - repartitioning is probably a bad idea.\n"
2948 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
2949 "Use the --no-reread flag to suppress this check.\n"));
2951 do_warn(_("Use the --force flag to overrule all checks.\n"));
2961 get_cylindersize(dev
, fd
, 0);
2962 get_partitions(dev
, fd
, z
);
2964 printf(_("Old situation:\n"));
2965 out_partitions(dev
, z
);
2967 if (one_only
&& (one_only_pno
= linux_to_index(one_only
, z
)) < 0)
2968 fatal(_("Partition %d does not exist, cannot change it\n"), one_only
);
2974 read_input(dev
, interactive
, z
);
2976 printf(_("New situation:\n"));
2977 out_partitions(dev
, z
);
2979 if (!partitions_ok(z
) && !force
) {
2981 fatal(_("I don't like these partitions - nothing changed.\n"
2982 "(If you really want this, use the --force option.)\n"));
2984 do_warn(_("I don't like this - probably you should answer No\n"));
2989 printf(_("Are you satisfied with this? [ynq] "));
2991 printf(_("Do you want to write this to disk? [ynq] "));
2992 answer
= c
= getchar();
2993 while (c
!= '\n' && c
!= EOF
)
2996 printf(_("\nsfdisk: premature end of input\n"));
2997 if (c
== EOF
|| answer
== 'q' || answer
== 'Q') {
2998 fatal(_("Quitting - nothing changed\n"));
2999 } else if (answer
== 'n' || answer
== 'N') {
3001 } else if (answer
== 'y' || answer
== 'Y') {
3004 printf(_("Please answer one of y,n,q\n"));
3011 if (write_partitions(dev
, fd
, z
))
3012 printf(_("Successfully wrote the new partition table\n\n"));
3016 reread_disk_partition(dev
, fd
);
3018 warn(_("If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)\n"
3019 "to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1\n"
3020 "(See fdisk(8).)\n"));
3022 sync(); /* superstition */