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
33 #define PROGNAME "sfdisk"
34 #define VERSION "3.07"
38 #include <stdlib.h> /* atoi, free */
39 #include <stdarg.h> /* varargs */
40 #include <unistd.h> /* read, write */
41 #include <fcntl.h> /* O_RDWR */
42 #include <errno.h> /* ERANGE */
43 #include <string.h> /* index() */
46 #include <sys/ioctl.h>
48 #include <sys/utsname.h>
49 #include <linux/unistd.h> /* _syscall */
50 #include <linux/hdreg.h> /* HDIO_GETGEO */
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
;
93 vfprintf (stderr
, s
, p
);
103 fprintf(stderr
, "\n" PROGNAME
": ");
104 vfprintf(stderr
, s
, p
);
109 fatal(char *s
, ...) {
114 fprintf(stderr
, "\n" PROGNAME
": ");
115 vfprintf(stderr
, s
, p
);
125 * sseek: seek to specified sector - return 0 on failure
127 * For >4GB disks lseek needs a > 32bit arg, and we have to use llseek.
128 * On the other hand, a 32 bit sector number is OK until 2TB.
129 * The routines _llseek and sseek below are the only ones that
130 * know about the loff_t type.
132 * Note: we use 512-byte sectors here, irrespective of the hardware ss.
134 #if !defined (__alpha__) && !defined (__ia64__)
136 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
137 loff_t
*, res
, uint
, wh
);
141 sseek(char *dev
, unsigned int fd
, unsigned long s
) {
143 in
= ((loff_t
) s
<< 9);
146 #if !defined (__alpha__) && !defined (__ia64__)
147 if (_llseek (fd
, in
>>32, in
& 0xffffffff, &out
, SEEK_SET
) != 0) {
149 if ((out
= lseek(fd
, in
, SEEK_SET
)) != in
) {
152 error(_("seek error on %s - cannot seek to %lu\n"), dev
, s
);
157 error(_("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n"),
158 (uint
)(in
>>32), (uint
)(in
& 0xffffffff),
159 (uint
)(out
>>32), (uint
)(out
& 0xffffffff));
170 * We preserve all sectors read in a chain - some of these will
171 * have to be modified and written back.
175 unsigned long sectornumber
;
186 sectorhead
= s
->next
;
191 static struct sector
*
192 get_sector(char *dev
, int fd
, unsigned long sno
) {
195 for(s
= sectorhead
; s
; s
= s
->next
)
196 if(s
->sectornumber
== sno
)
199 if (!sseek(dev
, fd
, sno
))
202 if (!(s
= (struct sector
*) malloc(sizeof(struct sector
))))
203 fatal(_("out of memory - giving up\n"));
205 if (read(fd
, s
->data
, sizeof(s
->data
)) != sizeof(s
->data
)) {
206 if (errno
) /* 0 in case we read past end-of-disk */
208 error(_("read error on %s - cannot read sector %lu\n"), dev
, sno
);
213 s
->next
= sectorhead
;
215 s
->sectornumber
= sno
;
216 s
->to_be_written
= 0;
222 msdos_signature (struct sector
*s
) {
223 if (*(unsigned short *) (s
->data
+ 0x1fe) != 0xaa55) {
224 error(_("ERROR: sector %lu does not have an msdos signature\n"),
232 write_sectors(char *dev
, int fd
) {
235 for (s
= sectorhead
; s
; s
= s
->next
)
236 if (s
->to_be_written
) {
237 if (!sseek(dev
, fd
, s
->sectornumber
))
239 if (write(fd
, s
->data
, sizeof(s
->data
)) != sizeof(s
->data
)) {
241 error(_("write error on %s - cannot write sector %lu\n"),
242 dev
, s
->sectornumber
);
245 s
->to_be_written
= 0;
251 ulong_to_chars(unsigned long u
, char *uu
) {
261 chars_to_ulong(unsigned char *uu
) {
266 u
= (u
<< 8) | uu
[i
];
271 save_sectors(char *dev
, int fdin
) {
276 fdout
= open(save_sector_file
, O_WRONLY
| O_CREAT
, 0444);
278 perror(save_sector_file
);
279 error(_("cannot open partition sector save file (%s)\n"),
284 for (s
= sectorhead
; s
; s
= s
->next
)
285 if (s
->to_be_written
) {
286 ulong_to_chars(s
->sectornumber
, ss
);
287 if (!sseek(dev
, fdin
, s
->sectornumber
))
289 if (read(fdin
, ss
+4, 512) != 512) {
291 error(_("read error on %s - cannot read sector %lu\n"),
292 dev
, s
->sectornumber
);
295 if (write(fdout
, ss
, sizeof(ss
)) != sizeof(ss
)) {
297 error(_("write error on %s\n"), save_sector_file
);
304 static void reread_disk_partition(char *dev
, int fd
);
307 restore_sectors(char *dev
) {
313 if (stat(restore_sector_file
, &statbuf
) < 0) {
314 perror(restore_sector_file
);
315 error(_("cannot stat partition restore file (%s)\n"),
316 restore_sector_file
);
319 if (statbuf
.st_size
% 516) {
320 error(_("partition restore file has wrong size - not restoring\n"));
323 if (!(ss
= (char *) malloc(statbuf
.st_size
))) {
324 error(_("out of memory?\n"));
327 fdin
= open(restore_sector_file
, O_RDONLY
);
329 perror(restore_sector_file
);
330 error(_("cannot open partition restore file (%s)\n"),
331 restore_sector_file
);
334 if (read(fdin
, ss
, statbuf
.st_size
) != statbuf
.st_size
) {
336 error(_("error reading %s\n"), restore_sector_file
);
340 fdout
= open(dev
, O_WRONLY
);
343 error(_("cannot open device %s for writing\n"), dev
);
348 ct
= statbuf
.st_size
/516;
350 sno
= chars_to_ulong(ss
);
351 if (!sseek(dev
, fdout
, sno
))
353 if (write(fdout
, ss
+4, 512) != 512) {
355 error(_("error writing sector %lu on %s\n"), sno
, dev
);
362 reread_disk_partition(dev
, fdout
);
368 * C. About heads, sectors and cylinders
372 * <linux/hdreg.h> defines HDIO_GETGEO and
373 * struct hd_geometry {
374 * unsigned char heads;
375 * unsigned char sectors;
376 * unsigned short cylinders;
377 * unsigned long start;
380 * For large disks g.cylinders is truncated, so we use BLKGETSIZE.
384 * We consider several geometries for a disk:
385 * B - the BIOS geometry, gotten from the kernel via HDIO_GETGEO
386 * F - the fdisk geometry
387 * U - the user-specified geometry
389 * 0 means unspecified / unknown
392 unsigned long cylindersize
;
393 unsigned long heads
, sectors
, cylinders
;
397 static struct geometry
398 get_geometry(char *dev
, int fd
, int silent
) {
399 struct hd_geometry g
;
403 if (ioctl(fd
, BLKGETSIZE
, &size
)) {
406 printf(_("Disk %s: cannot get size\n"), dev
);
408 if (ioctl(fd
, HDIO_GETGEO
, &g
)) {
409 g
.heads
= g
.sectors
= g
.cylinders
= g
.start
= 0;
411 printf(_("Disk %s: cannot get geometry\n"), dev
);
414 R
.sectors
= g
.sectors
;
415 R
.cylindersize
= R
.heads
* R
.sectors
;
416 R
.cylinders
= (R
.cylindersize
? size
/ R
.cylindersize
: 0);
422 get_cylindersize(char *dev
, int fd
, int silent
) {
425 R
= get_geometry(dev
, fd
, silent
);
427 B
.heads
= (U
.heads
? U
.heads
: R
.heads
);
428 B
.sectors
= (U
.sectors
? U
.sectors
: R
.sectors
);
429 B
.cylinders
= (U
.cylinders
? U
.cylinders
: R
.cylinders
);
431 B
.cylindersize
= B
.heads
* B
.sectors
;
433 if (R
.start
&& !force
) {
435 _("Warning: start=%lu - this looks like a partition rather than\n"
436 "the entire disk. Using fdisk on it is probably meaningless.\n"
437 "[Use the --force option if you really want this]\n"), R
.start
);
441 if (R
.heads
&& B
.heads
!= R
.heads
)
442 warn(_("Warning: HDIO_GETGEO says that there are %lu heads\n"),
444 if (R
.sectors
&& B
.sectors
!= R
.sectors
)
445 warn(_("Warning: HDIO_GETGEO says that there are %lu sectors\n"),
447 if (R
.cylinders
&& B
.cylinders
!= R
.cylinders
448 && B
.cylinders
< 65536 && R
.cylinders
< 65536)
449 warn(_("Warning: BLKGETSIZE/HDIO_GETGEO says that there are %lu cylinders\n"),
453 warn(_("Warning: unlikely number of sectors (%lu) - usually at most 63\n"
454 "This will give problems with all software that uses C/H/S addressing.\n"),
457 printf(_("\nDisk %s: %lu cylinders, %lu heads, %lu sectors/track\n"),
458 dev
, B
.cylinders
, B
.heads
, B
.sectors
);
461 typedef struct { unsigned char h
,s
,c
; } chs
; /* has some c bits in s */
462 chs zero_chs
= { 0,0,0 };
464 typedef struct { unsigned long h
,s
,c
; } longchs
;
465 longchs zero_longchs
;
468 longchs_to_chs (longchs aa
, struct geometry G
) {
471 if (aa
.h
< 256 && aa
.s
< 64 && aa
.c
< 1024) {
473 a
.s
= aa
.s
| ((aa
.c
>> 2) & 0xc0);
475 } else if (G
.heads
&& G
.sectors
) {
477 a
.s
= G
.sectors
| 0xc0;
485 chs_to_longchs (chs a
) {
491 aa
.c
= (aa
.c
<< 2) + a
.c
;
496 ulong_to_longchs (unsigned long sno
, struct geometry G
) {
499 if (G
.heads
&& G
.sectors
&& G
.cylindersize
) {
500 aa
.s
= 1 + sno
% G
.sectors
;
501 aa
.h
= (sno
/ G
.sectors
) % G
.heads
;
502 aa
.c
= sno
/ G
.cylindersize
;
510 ulong_to_chs (unsigned long sno
, struct geometry G
) {
511 return longchs_to_chs(ulong_to_longchs(sno
, G
), G
);
516 longchs_to_ulong (longchs aa
, struct geometry G
) {
517 return (aa
.c
*G
.cylindersize
+ aa
.h
*G
.sectors
+ aa
.s
- 1);
521 chs_to_ulong (chs a
, struct geometry G
) {
522 return longchs_to_ulong(chs_to_longchs(a
), G
);
527 is_equal_chs (chs a
, chs b
) {
528 return (a
.h
== b
.h
&& a
.s
== b
.s
&& a
.c
== b
.c
);
532 chs_ok (chs a
, char *v
, char *w
) {
533 longchs aa
= chs_to_longchs(a
);
536 if (is_equal_chs(a
, zero_chs
))
538 if (B
.heads
&& aa
.h
>= B
.heads
) {
539 warn(_("%s of partition %s has impossible value for head: "
540 "%lu (should be in 0-%lu)\n"), w
, v
, aa
.h
, B
.heads
-1);
543 if (B
.sectors
&& (aa
.s
== 0 || aa
.s
> B
.sectors
)) {
544 warn(_("%s of partition %s has impossible value for sector: "
545 "%lu (should be in 1-%lu)\n"), w
, v
, aa
.s
, B
.sectors
);
548 if (B
.cylinders
&& aa
.c
>= B
.cylinders
) {
549 warn(_("%s of partition %s has impossible value for cylinders: "
550 "%lu (should be in 0-%lu)\n"), w
, v
, aa
.c
, B
.cylinders
-1);
557 * D. About system Ids
560 #define EMPTY_PARTITION 0
561 #define EXTENDED_PARTITION 5
562 #define WIN98_EXTENDED 0x0f
563 #define DM6_AUX1PARTITION 0x51
564 #define DM6_AUX3PARTITION 0x53
565 #define DM6_PARTITION 0x54
566 #define EZD_PARTITION 0x55
567 #define LINUX_SWAP 0x82
568 #define LINUX_NATIVE 0x83
569 #define LINUX_EXTENDED 0x85
570 #define BSD_PARTITION 0xa5
572 /* List of partition types now in i386_sys_types.c */
575 sysname(unsigned char type
) {
578 for (s
= i386_sys_types
; s
->name
; s
++)
588 printf(_("Id Name\n\n"));
589 for (s
= i386_sys_types
; s
->name
; s
++)
590 printf("%2x %s\n", s
->type
, _(s
->name
));
594 is_extended(unsigned char type
) {
595 return (type
== EXTENDED_PARTITION
596 || type
== LINUX_EXTENDED
597 || type
== WIN98_EXTENDED
);
601 is_bsd(unsigned char type
) {
602 return (type
== BSD_PARTITION
);
606 * E. About partitions
609 /* MS/DOS partition */
612 unsigned char bootable
; /* 0 or 0x80 */
614 unsigned char sys_type
;
616 unsigned int start_sect
; /* starting sector counting from 0 */
617 unsigned int nr_sects
; /* nr of sectors in partition */
620 /* Unfortunately, partitions are not aligned, and non-Intel machines
621 are unhappy with non-aligned integers. So, we need a copy by hand. */
623 copy_to_int(unsigned char *cp
) {
634 copy_from_int(int m
, char *cp
) {
635 *cp
++ = (m
& 0xff); m
>>= 8;
636 *cp
++ = (m
& 0xff); m
>>= 8;
637 *cp
++ = (m
& 0xff); m
>>= 8;
642 copy_to_part(char *cp
, struct partition
*p
) {
644 p
->begin_chs
.h
= *cp
++;
645 p
->begin_chs
.s
= *cp
++;
646 p
->begin_chs
.c
= *cp
++;
648 p
->end_chs
.h
= *cp
++;
649 p
->end_chs
.s
= *cp
++;
650 p
->end_chs
.c
= *cp
++;
651 p
->start_sect
= copy_to_int(cp
);
652 p
->nr_sects
= copy_to_int(cp
+4);
656 copy_from_part(struct partition
*p
, char *cp
) {
658 *cp
++ = p
->begin_chs
.h
;
659 *cp
++ = p
->begin_chs
.s
;
660 *cp
++ = p
->begin_chs
.c
;
662 *cp
++ = p
->end_chs
.h
;
663 *cp
++ = p
->end_chs
.s
;
664 *cp
++ = p
->end_chs
.c
;
665 copy_from_int(p
->start_sect
, cp
);
666 copy_from_int(p
->nr_sects
, cp
+4);
669 /* Roughly speaking, Linux doesn't use any of the above fields except
670 for partition type, start sector and number of sectors. (However,
671 see also linux/drivers/scsi/fdomain.c.)
672 The only way partition type is used (in the kernel) is the comparison
673 for equality with EXTENDED_PARTITION (and these Disk Manager types). */
678 unsigned long sector
, offset
; /* disk location of this info */
680 struct part_desc
*ep
; /* extended partition containing this one */
686 static struct part_desc
*
687 outer_extended_partition(struct part_desc
*p
) {
694 is_parent(struct part_desc
*pp
, struct part_desc
*p
) {
704 struct part_desc partitions
[128];
708 /* determine where on the disk this information goes */
710 add_sector_and_offset(struct disk_desc
*z
) {
714 for (pno
= 0; pno
< z
->partno
; pno
++) {
715 p
= &(z
->partitions
[pno
]);
716 p
->offset
= 0x1be + (pno
%4)*sizeof(struct partition
);
717 p
->sector
= (p
->ep
? p
->ep
->start
: 0);
721 /* tell the kernel to reread the partition tables */
723 reread_ioctl(int fd
) {
724 if(ioctl(fd
, BLKRRPART
)) {
732 is_blockdev(int fd
) {
735 return(fstat(fd
, &statbuf
) == 0 && S_ISBLK(statbuf
.st_mode
));
738 /* reread after writing */
740 reread_disk_partition(char *dev
, int fd
) {
741 printf(_("Re-reading the partition table ...\n"));
744 sleep(3); /* superfluous since 1.3.20 */
746 if(reread_ioctl(fd
) && is_blockdev(fd
))
747 printf(_("The command to re-read the partition table failed\n"
748 "Reboot your system now, before using mkfs\n"));
752 printf(_("Error closing %s\n"), dev
);
757 /* find Linux name of this partition, assuming that it will have a name */
759 index_to_linux(int pno
, struct disk_desc
*z
) {
761 struct part_desc
*p
= &(z
->partitions
[0]);
762 for (i
=0; i
<pno
; i
++,p
++)
763 if(i
< 4 || (p
->size
> 0 && !is_extended(p
->p
.sys_type
)))
769 linux_to_index(int lpno
, struct disk_desc
*z
) {
771 struct part_desc
*p
= &(z
->partitions
[0]);
772 for (i
=0; i
<z
->partno
&& ct
< lpno
; i
++,p
++)
773 if((i
< 4 || (p
->size
> 0 && !is_extended(p
->p
.sys_type
)))
780 asc_to_index(char *pnam
, struct disk_desc
*z
) {
787 pno
= linux_to_index(pnum
, z
);
789 if (!(pno
>= 0 && pno
< z
->partno
))
790 fatal(_("%s: no such partition\n"), pnam
);
795 * List partitions - in terms of sectors, blocks or cylinders
802 int default_format
= F_MEGABYTE
;
803 int specified_format
= 0;
804 int show_extended
= 0;
813 printf(_("unrecognized format - using sectors\n"));
814 case 'S': specified_format
= F_SECTOR
; break;
815 case 'B': specified_format
= F_BLOCK
; break;
816 case 'C': specified_format
= F_CYLINDER
; break;
817 case 'M': specified_format
= F_MEGABYTE
; break;
822 unitsize(int format
) {
823 default_format
= (B
.cylindersize
? F_CYLINDER
: F_MEGABYTE
);
824 if (!format
&& !(format
= specified_format
))
825 format
= default_format
;
831 return B
.cylindersize
;
842 get_disksize(int format
) {
843 unsigned long cs
= B
.cylinders
;
844 if (cs
&& leave_last
)
846 return (cs
* B
.cylindersize
) / unitsize(format
);
850 out_partition_header(char *dev
, int format
, struct geometry G
) {
852 printf(_("# partition table of %s\n"), dev
);
853 printf("unit: sectors\n\n");
857 default_format
= (G
.cylindersize
? F_CYLINDER
: F_MEGABYTE
);
858 if (!format
&& !(format
= specified_format
))
859 format
= default_format
;
863 printf(_("unimplemented format - using %s\n"),
864 G
.cylindersize
? _("cylinders") : _("sectors"));
866 if (G
.cylindersize
) {
867 printf(_("Units = cylinders of %lu bytes, blocks of 1024 bytes"
868 ", counting from %d\n\n"),
869 G
.cylindersize
<<9, increment
);
870 printf(_(" Device Boot Start End #cyls #blocks Id System\n"));
875 printf(_("Units = sectors of 512 bytes, counting from %d\n\n"),
877 printf(_(" Device Boot Start End #sectors Id System\n"));
880 printf(_("Units = blocks of 1024 bytes, counting from %d\n\n"),
882 printf(_(" Device Boot Start End #blocks Id System\n"));
885 printf(_("Units = megabytes of 1048576 bytes, blocks of 1024 bytes"
886 ", counting from %d\n\n"), increment
);
887 printf(_(" Device Boot Start End MB #blocks Id System\n"));
893 out_rounddown(int width
, unsigned long n
, unsigned long unit
, int inc
) {
894 printf("%*lu", width
, inc
+ n
/unit
);
896 putchar((n
% unit
) ? '+' : ' ');
901 out_roundup(int width
, unsigned long n
, unsigned long unit
, int inc
) {
902 if (n
== (unsigned long)(-1))
903 printf("%*s", width
, "-");
905 printf("%*lu", width
, inc
+ n
/unit
);
907 putchar(((n
+1) % unit
) ? '-' : ' ');
912 out_roundup_size(int width
, unsigned long n
, unsigned long unit
) {
913 printf("%*lu", width
, (n
+unit
-1)/unit
);
915 putchar((n
% unit
) ? '-' : ' ');
920 get_fdisk_geometry(struct part_desc
*p
) {
921 chs b
= p
->p
.end_chs
;
922 longchs bb
= chs_to_longchs(b
);
925 F
.cylindersize
= F
.heads
*F
.sectors
;
926 return (F
.sectors
!= B
.sectors
|| F
.heads
!= B
.heads
);
930 out_partition(char *dev
, int format
, struct part_desc
*p
,
931 struct disk_desc
*z
, struct geometry G
) {
932 unsigned long start
, end
, size
;
935 if (!format
&& !(format
= specified_format
))
936 format
= default_format
;
938 pno
= p
- &(z
->partitions
[0]); /* our index */
939 lpno
= index_to_linux(pno
, z
); /* name of next one that has a name */
940 if(pno
== linux_to_index(lpno
, z
)) /* was that us? */
941 printf("%s", partname(dev
, lpno
, 10)); /* yes */
942 else if(show_extended
)
946 putchar(dump
? ':' : ' ');
949 end
= p
->start
+ p
->size
- 1;
953 printf(" start=%9lu", start
);
954 printf(", size=%8lu", size
);
955 if (p
->ptype
== DOS_TYPE
) {
956 printf(", Id=%2x", p
->p
.sys_type
);
957 if (p
->p
.bootable
== 0x80)
958 printf(", bootable");
964 if(p
->ptype
!= DOS_TYPE
|| p
->p
.bootable
== 0)
966 else if(p
->p
.bootable
== 0x80)
969 printf(" ? "); /* garbage */
973 if (G
.cylindersize
) {
974 out_rounddown(6, start
, G
.cylindersize
, increment
);
975 out_roundup(6, end
, G
.cylindersize
, increment
);
976 out_roundup_size(6, size
, G
.cylindersize
);
977 out_rounddown(8, size
, 2, 0);
983 out_rounddown(9, start
, 1, increment
);
984 out_roundup(9, end
, 1, increment
);
985 out_rounddown(9, size
, 1, 0);
990 p
->sector
/2, ((p
->sector
& 1) ? 512 : 0) + p
->offset
);
992 out_rounddown(8, start
, 2, increment
);
993 out_roundup(8, end
, 2, increment
);
994 out_rounddown(8, size
, 2, 0);
997 out_rounddown(5, start
, 2048, increment
);
998 out_roundup(5, end
, 2048, increment
);
999 out_roundup_size(5, size
, 2048);
1000 out_rounddown(8, size
, 2, 0);
1003 if (p
->ptype
== DOS_TYPE
) {
1005 p
->p
.sys_type
, sysname(p
->p
.sys_type
));
1010 /* Is chs as we expect? */
1011 if (!quiet
&& p
->ptype
== DOS_TYPE
) {
1014 a
= (size
? ulong_to_chs(start
,G
) : zero_chs
);
1016 aa
= chs_to_longchs(a
);
1017 bb
= chs_to_longchs(b
);
1018 if(a
.s
&& !is_equal_chs(a
, b
))
1019 printf(_("\t\tstart: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1020 aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1021 a
= (size
? ulong_to_chs(end
,G
) : zero_chs
);
1023 aa
= chs_to_longchs(a
);
1024 bb
= chs_to_longchs(b
);
1025 if(a
.s
&& !is_equal_chs(a
, b
))
1026 printf(_("\t\tend: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1027 aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1028 if(G
.cylinders
&& G
.cylinders
< 1024 && bb
.c
> G
.cylinders
)
1029 printf(_("partition ends on cylinder %ld, beyond the end of the disk\n"),
1035 out_partitions(char *dev
, struct disk_desc
*z
) {
1036 struct part_desc
*p
;
1037 int pno
, format
= 0;
1040 printf(_("No partitions found\n"));
1042 for (pno
=0; pno
< z
->partno
; pno
++) {
1043 p
= &(z
->partitions
[pno
]);
1044 if (p
->size
!= 0 && p
->p
.sys_type
!= 0) {
1045 if (get_fdisk_geometry(p
) && !dump
)
1047 _("Warning: The first partition looks like it was made\n"
1048 " for C/H/S=*/%ld/%ld (instead of %ld/%ld/%ld).\n"
1049 "For this listing I'll assume that geometry.\n"),
1050 F
.heads
, F
.sectors
, B
.cylinders
, B
.heads
, B
.sectors
);
1054 out_partition_header(dev
, format
, F
);
1055 for(pno
=0; pno
< z
->partno
; pno
++) {
1056 out_partition(dev
, format
, &(z
->partitions
[pno
]), z
, F
);
1057 if(show_extended
&& pno
%4==3)
1064 disj(struct part_desc
*p
, struct part_desc
*q
) {
1066 ((p
->start
+ p
->size
<= q
->start
)
1067 || (is_extended(p
->p
.sys_type
)
1068 && q
->start
+ q
->size
<= p
->start
+ p
->size
));
1072 pnumber(struct part_desc
*p
, struct disk_desc
*z
) {
1073 static char buf
[20];
1075 struct part_desc
*p0
= &(z
->partitions
[0]);
1077 this = index_to_linux(p
-p0
, z
);
1078 next
= index_to_linux(p
-p0
+1, z
);
1081 sprintf(buf
, "%d", this);
1083 sprintf(buf
, "[%d]", this);
1088 partitions_ok(struct disk_desc
*z
) {
1089 struct part_desc
*partitions
= &(z
->partitions
[0]), *p
, *q
;
1090 int partno
= z
->partno
;
1092 #define PNO(p) pnumber(p, z)
1094 /* Have at least 4 partitions been defined? */
1097 fatal(_("no partition table present.\n"));
1099 fatal(_("strange, only %d partitions defined.\n"), partno
);
1103 /* Are the partitions of size 0 marked empty?
1104 And do they have start = 0? And bootable = 0? */
1105 for (p
= partitions
; p
- partitions
< partno
; p
++)
1107 if(p
->p
.sys_type
!= EMPTY_PARTITION
)
1108 warn(_("Warning: partition %s has size 0 but is not marked Empty\n"),
1110 else if(p
->p
.bootable
!= 0)
1111 warn(_("Warning: partition %s has size 0 and is bootable\n"),
1113 else if(p
->p
.start_sect
!= 0)
1114 warn(_("Warning: partition %s has size 0 and nonzero start\n"),
1116 /* all this is probably harmless, no error return */
1119 /* Are the logical partitions contained in their extended partitions? */
1120 for (p
= partitions
+4; p
< partitions
+partno
; p
++)
1121 if (p
->ptype
== DOS_TYPE
)
1122 if (p
->size
&& !is_extended(p
->p
.sys_type
)) {
1124 if (p
->start
< q
->start
|| p
->start
+ p
->size
> q
->start
+ q
->size
) {
1125 warn(_("Warning: partition %s "), PNO(p
));
1126 warn(_("is not contained in partition %s\n"), PNO(q
));
1131 /* Are the data partitions mutually disjoint? */
1132 for (p
= partitions
; p
< partitions
+partno
; p
++)
1133 if (p
->size
&& !is_extended(p
->p
.sys_type
))
1134 for (q
= p
+1; q
< partitions
+partno
; q
++)
1135 if (q
->size
&& !is_extended(q
->p
.sys_type
))
1136 if(!((p
->start
> q
-> start
) ? disj(q
,p
) : disj(p
,q
))) {
1137 warn(_("Warning: partitions %s "), PNO(p
));
1138 warn(_("and %s overlap\n"), PNO(q
));
1142 /* Are the data partitions and the extended partition
1143 table sectors disjoint? */
1144 for (p
= partitions
; p
< partitions
+partno
; p
++)
1145 if (p
->size
&& !is_extended(p
->p
.sys_type
))
1146 for (q
= partitions
; q
< partitions
+partno
; q
++)
1147 if (is_extended(q
->p
.sys_type
))
1148 if (p
->start
<= q
->start
&& p
->start
+ p
->size
> q
->start
) {
1149 warn(_("Warning: partition %s contains part of "), PNO(p
));
1150 warn(_("the partition table (sector %lu),\n"), q
->start
);
1151 warn(_("and will destroy it when filled\n"));
1155 /* Do they start past zero and end before end-of-disk? */
1156 { unsigned long ds
= get_disksize(F_SECTOR
);
1157 for (p
= partitions
; p
< partitions
+partno
; p
++)
1160 warn(_("Warning: partition %s starts at sector 0\n"), PNO(p
));
1163 if (p
->size
&& p
->start
+ p
->size
> ds
) {
1164 warn(_("Warning: partition %s extends past end of disk\n"), PNO(p
));
1170 /* At most one chain of DOS extended partitions ? */
1171 /* It seems that the OS/2 fdisk has the additional requirement
1172 that the extended partition must be the fourth one */
1174 for (p
= partitions
; p
< partitions
+4; p
++)
1175 if (p
->p
.sys_type
== EXTENDED_PARTITION
)
1177 if (ect
> 1 && !Linux
) {
1178 warn(_("Among the primary partitions, at most one can be extended\n"));
1179 warn(_(" (although this is not a problem under Linux)\n"));
1185 * Do all partitions start at a cylinder boundary ?
1186 * (this is not required for Linux)
1187 * The first partition starts after MBR.
1188 * Logical partitions start slightly after the containing extended partn.
1190 if (B
.cylindersize
) {
1191 for(p
= partitions
; p
< partitions
+partno
; p
++)
1193 if(p
->start
% B
.cylindersize
!= 0
1194 && (!p
->ep
|| p
->start
/ B
.cylindersize
!= p
->ep
->start
/ B
.cylindersize
)
1195 && (p
->p
.start_sect
>= B
.cylindersize
)) {
1196 warn(_("Warning: partition %s does not start "
1197 "at a cylinder boundary\n"), PNO(p
));
1201 if((p
->start
+ p
->size
) % B
.cylindersize
) {
1202 warn(_("Warning: partition %s does not end "
1203 "at a cylinder boundary\n"), PNO(p
));
1210 /* Usually, one can boot only from primary partitions. */
1211 /* In fact, from a unique one only. */
1212 /* do not warn about bootable extended partitions -
1213 often LILO is there */
1215 for(p
= partitions
; p
< partitions
+partno
; p
++)
1216 if (p
->p
.bootable
) {
1218 pno
= p
- partitions
;
1219 else if (p
- partitions
< 4) {
1220 warn(_("Warning: more than one primary partition is marked "
1221 "bootable (active)\n"
1222 "This does not matter for LILO, but the DOS MBR will "
1223 "not boot this disk.\n"));
1226 if (p
- partitions
>= 4) {
1227 warn(_("Warning: usually one can boot from primary partitions "
1228 "only\nLILO disregards the `bootable' flag.\n"));
1232 if (pno
== -1 || pno
>= 4)
1233 warn(_("Warning: no primary partition is marked bootable (active)\n"
1234 "This does not matter for LILO, but the DOS MBR will "
1235 "not boot this disk.\n"));
1238 /* Is chs as we expect? */
1239 for(p
= partitions
; p
< partitions
+partno
; p
++)
1240 if(p
->ptype
== DOS_TYPE
) {
1243 a
= p
->size
? ulong_to_chs(p
->start
,B
) : zero_chs
;
1245 aa
= chs_to_longchs(a
);
1246 bb
= chs_to_longchs(b
);
1247 if (!chs_ok(b
, PNO(p
), "start"))
1249 if(a
.s
&& !is_equal_chs(a
, b
))
1250 warn(_("partition %s: start: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1251 PNO(p
), aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1252 a
= p
->size
? ulong_to_chs(p
->start
+ p
->size
- 1, B
) : zero_chs
;
1254 aa
= chs_to_longchs(a
);
1255 bb
= chs_to_longchs(b
);
1256 if (!chs_ok(b
, PNO(p
), "end"))
1258 if(a
.s
&& !is_equal_chs(a
, b
))
1259 warn(_("partition %s: end: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1260 PNO(p
), aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1261 if(B
.cylinders
&& B
.cylinders
< 1024 && bb
.c
> B
.cylinders
)
1262 warn(_("partition %s ends on cylinder %ld, beyond the end of the disk\n"),
1272 extended_partition(char *dev
, int fd
, struct part_desc
*ep
, struct disk_desc
*z
) {
1275 unsigned long start
, here
, next
;
1276 int i
, moretodo
= 1;
1278 struct part_desc
*partitions
= &(z
->partitions
[0]);
1279 int pno
= z
->partno
;
1281 here
= start
= ep
->start
;
1283 if (B
.cylindersize
&& start
% B
.cylindersize
) {
1286 here
= start
-= (start
% B
.cylindersize
);
1287 printf(_("Warning: shifted start of the extd partition from %ld to %ld\n"),
1289 printf(_("(For listing purposes only. Do not change its contents.)\n"));
1291 printf(_("Warning: extended partition does not start at a "
1292 "cylinder boundary.\n"));
1293 printf(_("DOS and Linux will interpret the contents differently.\n"));
1300 if (!(s
= get_sector(dev
, fd
, here
)))
1303 if (!msdos_signature(s
))
1306 cp
= s
->data
+ 0x1be;
1308 if (pno
+4 >= SIZE(z
->partitions
)) {
1309 printf(_("too many partitions - ignoring those past nr (%d)\n"),
1316 for (i
=0; i
<4; i
++,cp
+= sizeof(struct partition
)) {
1317 partitions
[pno
].sector
= here
;
1318 partitions
[pno
].offset
= cp
- s
->data
;
1319 partitions
[pno
].ep
= ep
;
1320 copy_to_part(cp
,&p
);
1321 if (is_extended(p
.sys_type
)) {
1322 partitions
[pno
].start
= start
+ p
.start_sect
;
1324 printf(_("tree of partitions?\n"));
1326 next
= partitions
[pno
].start
; /* follow `upper' branch */
1329 partitions
[pno
].start
= here
+ p
.start_sect
;
1331 partitions
[pno
].size
= p
.nr_sects
;
1332 partitions
[pno
].ptype
= DOS_TYPE
;
1333 partitions
[pno
].p
= p
;
1342 #define BSD_DISKMAGIC (0x82564557UL)
1343 #define BSD_MAXPARTITIONS 8
1344 #define BSD_FS_UNUSED 0
1345 typedef unsigned char u8
;
1346 typedef unsigned short u16
;
1347 typedef unsigned int u32
;
1348 struct bsd_disklabel
{
1351 char d_typename
[16];
1352 char d_packname
[16];
1356 u16 d_npartitions
; /* number of partitions in following */
1358 struct bsd_partition
{ /* the partition table */
1359 u32 p_size
; /* number of sectors in partition */
1360 u32 p_offset
; /* starting sector */
1361 u32 p_fsize
; /* filesystem basic fragment size */
1362 u8 p_fstype
; /* filesystem type, see below */
1363 u8 p_frag
; /* filesystem fragments per block */
1364 u16 p_cpg
; /* filesystem cylinders per group */
1365 } d_partitions
[BSD_MAXPARTITIONS
]; /* actually may be more */
1369 bsd_partition(char *dev
, int fd
, struct part_desc
*ep
, struct disk_desc
*z
) {
1370 struct bsd_disklabel
*l
;
1371 struct bsd_partition
*bp
, *bp0
;
1372 unsigned long start
= ep
->start
;
1374 struct part_desc
*partitions
= &(z
->partitions
[0]);
1375 int pno
= z
->partno
;
1377 if (!(s
= get_sector(dev
,fd
,start
+1)))
1379 l
= (struct bsd_disklabel
*) (s
->data
);
1380 if (l
->d_magic
!= BSD_DISKMAGIC
)
1383 bp
= bp0
= &l
->d_partitions
[0];
1384 while (bp
- bp0
<= BSD_MAXPARTITIONS
) {
1385 if (pno
+1 >= SIZE(z
->partitions
)) {
1386 printf(_("too many partitions - ignoring those "
1387 "past nr (%d)\n"), pno
-1);
1390 if (bp
->p_fstype
!= BSD_FS_UNUSED
) {
1391 partitions
[pno
].start
= bp
->p_offset
;
1392 partitions
[pno
].size
= bp
->p_size
;
1393 partitions
[pno
].sector
= start
+1;
1394 partitions
[pno
].offset
= (char *)bp
- (char *)bp0
;
1395 partitions
[pno
].ep
= 0;
1396 partitions
[pno
].ptype
= BSD_TYPE
;
1404 #define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r))
1407 linux_version_code(void) {
1408 struct utsname my_utsname
;
1411 if (uname(&my_utsname
) == 0) {
1412 p
= atoi(strtok(my_utsname
.release
, "."));
1413 q
= atoi(strtok(NULL
, "."));
1414 r
= atoi(strtok(NULL
, "."));
1415 return MAKE_VERSION(p
,q
,r
);
1421 msdos_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1424 struct partition pt
;
1426 struct part_desc
*partitions
= &(z
->partitions
[0]);
1427 int pno
= z
->partno
;
1428 int bsd_later
= (linux_version_code() >= MAKE_VERSION(2,3,40));
1430 if (!(s
= get_sector(dev
, fd
, start
)))
1433 if (!msdos_signature(s
))
1436 cp
= s
->data
+ 0x1be;
1437 copy_to_part(cp
,&pt
);
1439 /* If I am not mistaken, recent kernels will hide this from us,
1440 so we will never actually see traces of a Disk Manager */
1441 if (pt
.sys_type
== DM6_PARTITION
1442 || pt
.sys_type
== EZD_PARTITION
1443 || pt
.sys_type
== DM6_AUX1PARTITION
1444 || pt
.sys_type
== DM6_AUX3PARTITION
) {
1445 printf(_("detected Disk Manager - unable to handle that\n"));
1448 { unsigned int sig
= *(unsigned short *)(s
->data
+ 2);
1450 && *(unsigned short *)(s
->data
+ sig
) == 0x55aa
1451 && (1 & *(unsigned char *)(s
->data
+ sig
+ 2))) {
1452 printf(_("DM6 signature found - giving up\n"));
1457 for (pno
=0; pno
<4; pno
++,cp
+= sizeof(struct partition
)) {
1458 partitions
[pno
].sector
= start
;
1459 partitions
[pno
].offset
= cp
- s
->data
;
1460 copy_to_part(cp
,&pt
);
1461 partitions
[pno
].start
= start
+ pt
.start_sect
;
1462 partitions
[pno
].size
= pt
.nr_sects
;
1463 partitions
[pno
].ep
= 0;
1464 partitions
[pno
].p
= pt
;
1469 for (i
=0; i
<4; i
++) {
1470 if (is_extended(partitions
[i
].p
.sys_type
)) {
1471 if (!partitions
[i
].size
) {
1472 printf(_("strange..., an extended partition of size 0?\n"));
1475 extended_partition(dev
, fd
, &partitions
[i
], z
);
1477 if (!bsd_later
&& is_bsd(partitions
[i
].p
.sys_type
)) {
1478 if (!partitions
[i
].size
) {
1479 printf(_("strange..., a BSD partition of size 0?\n"));
1482 bsd_partition(dev
, fd
, &partitions
[i
], z
);
1487 for (i
=0; i
<4; i
++) {
1488 if (is_bsd(partitions
[i
].p
.sys_type
)) {
1489 if (!partitions
[i
].size
) {
1490 printf(_("strange..., a BSD partition of size 0?\n"));
1493 bsd_partition(dev
, fd
, &partitions
[i
], z
);
1502 osf_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1507 sun_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1512 amiga_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1517 get_partitions(char *dev
, int fd
, struct disk_desc
*z
) {
1520 if (!msdos_partition(dev
, fd
, 0, z
)
1521 && !osf_partition(dev
, fd
, 0, z
)
1522 && !sun_partition(dev
, fd
, 0, z
)
1523 && !amiga_partition(dev
, fd
, 0, z
)) {
1524 printf(_(" %s: unrecognized partition\n"), dev
);
1530 write_partitions(char *dev
, int fd
, struct disk_desc
*z
) {
1532 struct part_desc
*partitions
= &(z
->partitions
[0]), *p
;
1533 int pno
= z
->partno
;
1536 printf(_("-n flag was given: Nothing changed\n"));
1540 for (p
= partitions
; p
< partitions
+pno
; p
++) {
1541 s
= get_sector(dev
, fd
, p
->sector
);
1543 s
->to_be_written
= 1;
1544 copy_from_part(&(p
->p
), s
->data
+ p
->offset
);
1545 *(unsigned short *)(&(s
->data
[0x1fe])) = 0xaa55;
1547 if (save_sector_file
) {
1548 if (!save_sectors(dev
, fd
)) {
1549 fatal(_("Failed saving the old sectors - aborting\n"));
1553 if (!write_sectors(dev
, fd
)) {
1554 error(_("Failed writing the partition on %s\n"), dev
);
1561 * F. The standard input
1566 * <start> <size> <type> <bootable> <c,h,s> <c,h,s>
1567 * Fields are separated by whitespace or comma or semicolon possibly
1568 * followed by whitespace; initial and trailing whitespace is ignored.
1569 * Numbers can be octal, decimal or hexadecimal, decimal is default
1570 * The <c,h,s> parts can (and probably should) be omitted.
1571 * Bootable is specified as [*|-], with as default not-bootable.
1572 * Type is given in hex, without the 0x prefix, or is [E|S|L|X], where
1573 * L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), and E
1574 * is EXTENDED_PARTITION (5), X is LINUX_EXTENDED (85).
1575 * The default value of start is the first nonassigned sector/cylinder/...
1576 * The default value of size is as much as possible (until next
1577 * partition or end-of-disk).
1578 * .: end of chain of extended partitions.
1580 * On interactive input an empty line means: all defaults.
1581 * Otherwise empty lines are ignored.
1594 { 3, "bootable", 1 },
1604 * Read a line, split it into fields
1606 * (some primitive handwork, but a more elaborate parser seems
1613 read_stdin(unsigned char **fields
, unsigned char *line
, int fieldssize
, int linesize
) {
1614 unsigned char *lp
, *ip
;
1617 /* boolean true and empty string at start */
1620 for (fno
=0; fno
< fieldssize
; fno
++)
1621 fields
[fno
] = line
+ 1;
1624 /* read a line from stdin */
1625 lp
= fgets(line
+2, linesize
, stdin
);
1630 if (!(lp
= index(lp
, '\n')))
1631 fatal(_("long or incomplete input line - quitting\n"));
1634 /* remove comments, if any */
1635 if ((lp
= index(line
+2, '#')) != 0)
1638 /* recognize a few commands - to be expanded */
1639 if (!strcmp(line
+2, "unit: sectors")) {
1640 specified_format
= F_SECTOR
;
1644 /* dump style? - then bad input is fatal */
1645 if ((ip
= index(line
+2, ':')) != 0) {
1654 for(d
= dumpflds
; d
-dumpflds
< SIZE(dumpflds
); d
++) {
1655 if(!strncmp(ip
, d
->fldname
, strlen(d
->fldname
))) {
1656 ip
+= strlen(d
->fldname
);
1660 fields
[d
->fldno
] = line
;
1661 else if (*ip
== '=') {
1662 while(isspace(*++ip
)) ;
1663 fields
[d
->fldno
] = ip
;
1664 while(isalnum(*ip
)) /* 0x07FF */
1667 fatal(_("input error: `=' expected after %s field\n"),
1669 if (fno
<= d
->fldno
)
1673 if(*ip
!= ',' && *ip
!= ';')
1674 fatal(_("input error: unexpected character %c after %s field\n"),
1680 fatal(_("unrecognized input: %s\n"), ip
);
1683 /* split line into fields */
1686 while((c
= *ip
++) != 0) {
1687 if (!lp
[-1] && (c
== '\t' || c
== ' '))
1689 else if (c
== '\t' || c
== ' ' || c
== ',' || c
== ';') {
1691 if (fno
< fieldssize
)
1698 if (lp
== fields
[fno
-1])
1703 /* read a number, use default if absent */
1705 get_ul(char *u
, unsigned long *up
, unsigned long def
, int base
) {
1710 *up
= strtoul(u
, &nu
, base
);
1711 if (errno
== ERANGE
) {
1712 printf(_("number too big\n"));
1716 printf(_("trailing junk after number\n"));
1724 /* There are two common ways to structure extended partitions:
1725 as nested boxes, and as a chain. Sometimes the partitions
1726 must be given in order. Sometimes all logical partitions
1727 must lie inside the outermost extended partition.
1728 NESTED: every partition is contained in the surrounding partitions
1729 and is disjoint from all others.
1730 CHAINED: every data partition is contained in the surrounding partitions
1731 and disjoint from all others, but extended partitions may lie outside
1732 (insofar as allowed by all_logicals_inside_outermost_extended).
1733 ONESECTOR: all data partitions are mutually disjoint; extended partitions
1734 each use one sector only (except perhaps for the outermost one).
1736 int partitions_in_order
= 0;
1737 int all_logicals_inside_outermost_extended
= 1;
1738 enum { NESTED
, CHAINED
, ONESECTOR
} boxes
= NESTED
;
1740 /* find the default value for <start> - assuming entire units */
1741 static unsigned long
1742 first_free(int pno
, int is_extended
, struct part_desc
*ep
, int format
,
1743 unsigned long mid
, struct disk_desc
*z
) {
1744 unsigned long ff
, fff
;
1745 unsigned long unit
= unitsize(format
);
1746 struct part_desc
*partitions
= &(z
->partitions
[0]), *pp
= 0;
1748 /* if containing ep undefined, look at its container */
1749 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
)
1753 if (boxes
== NESTED
|| (boxes
== CHAINED
&& !is_extended
))
1755 else if (all_logicals_inside_outermost_extended
)
1756 pp
= outer_extended_partition(ep
);
1759 ff
= pp
? (pp
->start
+ unit
- 1) / unit
: 0;
1761 /* rounding up wastes almost an entire cylinder - round down
1762 and leave it to compute_start_sect() to fix the difference */
1763 ff
= pp
? pp
->start
/ unit
: 0;
1765 /* MBR and 1st sector of an extended partition are never free */
1770 for(pp
= partitions
; pp
< partitions
+pno
; pp
++) {
1771 if (!is_parent(pp
, ep
) && pp
->size
> 0) {
1772 if ((partitions_in_order
|| pp
->start
/ unit
<= ff
1773 || (mid
&& pp
->start
/ unit
<= mid
))
1774 && (fff
= (pp
->start
+ pp
->size
+ unit
- 1) / unit
) > ff
) {
1784 /* find the default value for <size> - assuming entire units */
1785 static unsigned long
1786 max_length(int pno
, int is_extended
, struct part_desc
*ep
, int format
,
1787 unsigned long start
, struct disk_desc
*z
) {
1789 unsigned long unit
= unitsize(format
);
1790 struct part_desc
*partitions
= &(z
->partitions
[0]), *pp
= 0;
1792 /* if containing ep undefined, look at its container */
1793 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
)
1797 if (boxes
== NESTED
|| (boxes
== CHAINED
&& !is_extended
))
1799 else if (all_logicals_inside_outermost_extended
)
1800 pp
= outer_extended_partition(ep
);
1802 fu
= pp
? (pp
->start
+ pp
->size
) / unit
: get_disksize(format
);
1804 for(pp
= partitions
; pp
< partitions
+pno
; pp
++)
1805 if (!is_parent(pp
, ep
) && pp
->size
> 0
1806 && pp
->start
/ unit
>= start
&& pp
->start
/ unit
< fu
)
1807 fu
= pp
->start
/ unit
;
1809 return (fu
> start
) ? fu
- start
: 0;
1812 /* compute starting sector of a partition inside an extended one */
1813 /* ep is 0 or points to surrounding extended partition */
1815 compute_start_sect(struct part_desc
*p
, struct part_desc
*ep
) {
1817 int inc
= (DOS
&& B
.sectors
) ? B
.sectors
: 1;
1820 if (ep
&& p
->start
+ p
->size
>= ep
->start
+ 1)
1821 delta
= p
->start
- ep
->start
- inc
;
1822 else if (p
->start
== 0 && p
->size
> 0)
1829 if (is_extended(p
->p
.sys_type
) && boxes
== ONESECTOR
)
1831 else if ((int)(p
->size
) <= 0) {
1832 warn(_("no room for partition descriptor\n"));
1837 : (is_extended(p
->p
.sys_type
) ?
1838 outer_extended_partition(ep
) : ep
)->start
);
1840 if (p
->p
.sys_type
== EMPTY_PARTITION
&& p
->size
== 0) {
1841 p
->p
.start_sect
= 0;
1842 p
->p
.begin_chs
= zero_chs
;
1843 p
->p
.end_chs
= zero_chs
;
1845 p
->p
.start_sect
= p
->start
- base
;
1846 p
->p
.begin_chs
= ulong_to_chs(p
->start
,B
);
1847 p
->p
.end_chs
= ulong_to_chs(p
->start
+ p
->size
- 1,B
);
1849 p
->p
.nr_sects
= p
->size
;
1853 /* build the extended partition surrounding a given logical partition */
1855 build_surrounding_extended(struct part_desc
*p
, struct part_desc
*ep
,
1856 struct disk_desc
*z
) {
1857 int inc
= (DOS
&& B
.sectors
) ? B
.sectors
: 1;
1858 int format
= F_SECTOR
;
1859 struct part_desc
*p0
= &(z
->partitions
[0]), *eep
= ep
->ep
;
1861 if (boxes
== NESTED
) {
1862 ep
->start
= first_free(ep
-p0
, 1, eep
, format
, p
->start
, z
);
1863 ep
->size
= max_length(ep
-p0
, 1, eep
, format
, ep
->start
, z
);
1864 if (ep
->start
> p
->start
|| ep
->start
+ ep
->size
< p
->start
+ p
->size
) {
1865 warn(_("cannot build surrounding extended partition\n"));
1869 ep
->start
= p
->start
;
1870 if(boxes
== CHAINED
)
1876 ep
->p
.nr_sects
= ep
->size
;
1878 ep
->p
.sys_type
= EXTENDED_PARTITION
;
1879 if (!compute_start_sect(ep
, eep
) || !compute_start_sect(p
, ep
)) {
1880 ep
->p
.sys_type
= EMPTY_PARTITION
;
1889 read_line(int pno
, struct part_desc
*ep
, char *dev
, int interactive
,
1890 struct disk_desc
*z
) {
1891 unsigned char line
[1000];
1892 unsigned char *fields
[11];
1893 int fno
, pct
= pno
%4;
1894 struct part_desc p
, *orig
;
1895 unsigned long ff
, ff1
, ul
, ml
, ml1
, def
;
1896 int format
, lpno
, is_extd
;
1901 lpno
= index_to_linux(pno
, z
);
1904 if (pct
== 0 && (show_extended
|| pno
== 0))
1906 warn("%s:", partname(dev
, lpno
, 10));
1909 /* read input line - skip blank lines when reading from a file */
1911 fno
= read_stdin(fields
, line
, SIZE(fields
), SIZE(line
));
1912 } while(fno
== RD_CMD
|| (fno
== 0 && !interactive
));
1913 if (fno
== RD_EOF
) {
1915 } else if (fno
> 10 && *(fields
[10]) != 0) {
1916 printf(_("too many input fields\n"));
1920 if (fno
== 1 && !strcmp(fields
[0], ".")) {
1925 /* use specified format, but round to cylinders if F_MEGABYTE specified */
1927 if (B
.cylindersize
&& specified_format
== F_MEGABYTE
)
1928 format
= F_CYLINDER
;
1930 orig
= (one_only
? &(oldp
.partitions
[pno
]) : 0);
1935 /* first read the type - we need to know whether it is extended */
1936 /* stop reading when input blank (defaults) and all is full */
1938 if (fno
== 0) { /* empty line */
1939 if (orig
&& is_extended(orig
->p
.sys_type
))
1941 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
1942 ml
= max_length(pno
, is_extd
, ep
, format
, ff
, z
);
1943 if (ml
== 0 && is_extd
== 0) {
1945 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
1946 ml
= max_length(pno
, is_extd
, ep
, format
, ff
, z
);
1948 if (ml
== 0 && pno
>= 4) {
1949 /* no free blocks left - don't read any further */
1950 warn(_("No room for more\n"));
1954 if (fno
< 3 || !*(fields
[2]))
1955 ul
= orig
? orig
->p
.sys_type
:
1956 (is_extd
|| (pno
> 3 && pct
== 1 && show_extended
))
1957 ? EXTENDED_PARTITION
: LINUX_NATIVE
;
1958 else if(!strcmp(fields
[2], "L"))
1960 else if(!strcmp(fields
[2], "S"))
1962 else if(!strcmp(fields
[2], "E"))
1963 ul
= EXTENDED_PARTITION
;
1964 else if(!strcmp(fields
[2], "X"))
1965 ul
= LINUX_EXTENDED
;
1966 else if (get_ul(fields
[2], &ul
, LINUX_NATIVE
, 16))
1969 warn(_("Illegal type\n"));
1973 is_extd
= is_extended(ul
);
1976 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
1977 ff1
= ff
* unitsize(format
);
1978 def
= orig
? orig
->start
: (pno
> 4 && pct
> 1) ? 0 : ff1
;
1979 if (fno
< 1 || !*(fields
[0]))
1982 if (get_ul(fields
[0], &ul
, def
/ unitsize(0), 0))
1984 p
.start
= ul
* unitsize(0);
1985 p
.start
-= (p
.start
% unitsize(format
));
1989 ml
= max_length(pno
, is_extd
, ep
, format
, p
.start
/ unitsize(format
), z
);
1990 ml1
= ml
* unitsize(format
);
1991 def
= orig
? orig
->size
: (pno
> 4 && pct
> 1) ? 0 : ml1
;
1992 if (fno
< 2 || !*(fields
[1]))
1995 if (get_ul(fields
[1], &ul
, def
/ unitsize(0), 0))
1997 p
.size
= ul
* unitsize(0) + unitsize(format
) - 1;
1998 p
.size
-= (p
.size
% unitsize(format
));
2001 warn(_("Warning: given size (%lu) exceeds max allowable size (%lu)\n"),
2002 (p
.size
+ unitsize(0) - 1) / unitsize(0), ml1
/ unitsize(0));
2006 if (p
.size
== 0 && pno
>= 4 && (fno
< 2 || !*(fields
[1]))) {
2007 warn(_("Warning: empty partition\n"));
2011 p
.p
.nr_sects
= p
.size
;
2013 if (p
.size
== 0 && !orig
) {
2014 if(fno
< 1 || !*(fields
[0]))
2016 if(fno
< 3 || !*(fields
[2]))
2017 p
.p
.sys_type
= EMPTY_PARTITION
;
2020 if (p
.start
< ff1
&& p
.size
> 0) {
2021 warn(_("Warning: bad partition start (earliest %lu)\n"),
2022 (ff1
+ unitsize(0) - 1) / unitsize(0));
2027 if (fno
< 4 || !*(fields
[3]))
2028 ul
= (orig
? orig
->p
.bootable
: 0);
2029 else if (!strcmp(fields
[3], "-"))
2031 else if (!strcmp(fields
[3], "*") || !strcmp(fields
[3], "+"))
2034 warn(_("unrecognized bootable flag - choose - or *\n"));
2039 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
) {
2040 if(!build_surrounding_extended(&p
, ep
, z
))
2043 if(!compute_start_sect(&p
, ep
))
2046 { longchs aa
= chs_to_longchs(p
.p
.begin_chs
), bb
;
2050 } else if (fno
< 7) {
2051 warn(_("partial c,h,s specification?\n"));
2053 } else if(get_ul(fields
[4], &bb
.c
, aa
.c
, 0) ||
2054 get_ul(fields
[5], &bb
.h
, aa
.h
, 0) ||
2055 get_ul(fields
[6], &bb
.s
, aa
.s
, 0))
2057 p
.p
.begin_chs
= longchs_to_chs(bb
,B
);
2059 { longchs aa
= chs_to_longchs(p
.p
.end_chs
), bb
;
2063 } else if (fno
< 10) {
2064 warn(_("partial c,h,s specification?\n"));
2066 } else if(get_ul(fields
[7], &bb
.c
, aa
.c
, 0) ||
2067 get_ul(fields
[8], &bb
.h
, aa
.h
, 0) ||
2068 get_ul(fields
[9], &bb
.s
, aa
.s
, 0))
2070 p
.p
.end_chs
= longchs_to_chs(bb
, B
);
2073 if (pno
> 3 && p
.size
&& show_extended
&& p
.p
.sys_type
!= EMPTY_PARTITION
2074 && (is_extended(p
.p
.sys_type
) != (pct
== 1))) {
2075 warn(_("Extended partition not where expected\n"));
2080 z
->partitions
[pno
] = p
;
2081 if (pno
>= z
->partno
)
2082 z
->partno
+= 4; /* reqd for out_partition() */
2085 out_partition(dev
, 0, &(z
->partitions
[pno
]), z
, B
);
2090 /* ep either points to the extended partition to contain this one,
2091 or to the empty partition that may become extended or is 0 */
2093 read_partition(char *dev
, int interactive
, int pno
, struct part_desc
*ep
,
2094 struct disk_desc
*z
) {
2095 struct part_desc
*p
= &(z
->partitions
[pno
]);
2099 *p
= oldp
.partitions
[pno
];
2100 if (one_only_pno
!= pno
)
2102 } else if (!show_extended
&& pno
> 4 && pno
%4)
2105 while (!(i
= read_line(pno
, ep
, dev
, interactive
, z
)))
2107 fatal(_("bad input\n"));
2115 if (pno
>= z
->partno
)
2121 read_partition_chain(char *dev
, int interactive
, struct part_desc
*ep
,
2122 struct disk_desc
*z
) {
2128 if (base
+4 > SIZE(z
->partitions
)) {
2129 printf(_("too many partitions\n"));
2133 if (!read_partition(dev
, interactive
, base
+i
, ep
, z
))
2135 for (i
=0; i
<4; i
++) {
2136 ep
= &(z
->partitions
[base
+i
]);
2137 if (is_extended(ep
->p
.sys_type
) && ep
->size
)
2141 /* nothing found - maybe an empty partition is going
2143 if (one_only
|| show_extended
)
2145 ep
= &(z
->partitions
[base
+1]);
2146 if (ep
->size
|| ep
->p
.sys_type
!= EMPTY_PARTITION
)
2153 read_input(char *dev
, int interactive
, struct disk_desc
*z
) {
2155 struct part_desc
*partitions
= &(z
->partitions
[0]), *ep
;
2157 for (i
=0; i
< SIZE(z
->partitions
); i
++)
2158 partitions
[i
] = zero_part_desc
;
2162 warn(_("Input in the following format; absent fields get a default value.\n"
2163 "<start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s>\n"
2164 "Usually you only need to specify <start> and <size> (and perhaps <type>).\n"));
2168 read_partition(dev
, interactive
, i
, 0, z
);
2169 for (i
=0; i
<4; i
++) {
2171 if (is_extended(ep
->p
.sys_type
) && ep
->size
)
2172 read_partition_chain(dev
, interactive
, ep
, z
);
2174 add_sector_and_offset(z
);
2178 * G. The command line
2181 static void version(void) {
2182 printf("%s %s %s (aeb@cwi.nl, %s)\n", PROGNAME
, _("version"), VERSION
, DATE
);
2188 printf(_("Usage: %s [options] device ...\n"), PROGNAME
);
2189 puts (_("device: something like /dev/hda or /dev/sda"));
2190 puts (_("useful options:"));
2191 puts (_(" -s [or --show-size]: list size of a partition"));
2192 puts (_(" -c [or --id]: print or change partition Id"));
2193 puts (_(" -l [or --list]: list partitions of each device"));
2194 puts (_(" -d [or --dump]: idem, but in a format suitable for later input"));
2195 puts (_(" -i [or --increment]: number cylinders etc. from 1 instead of from 0"));
2196 puts (_(" -uS, -uB, -uC, -uM: accept/report in units of sectors/blocks/cylinders/MB"));
2197 puts (_(" -T [or --list-types]:list the known partition types"));
2198 puts (_(" -D [or --DOS]: for DOS-compatibility: waste a little space"));
2199 puts (_(" -R [or --re-read]: make kernel reread partition table"));
2200 puts (_(" -N# : change only the partition with number #"));
2201 puts (_(" -n : do not actually write to disk"));
2202 puts (_(" -O file : save the sectors that will be overwritten to file"));
2203 puts (_(" -I file : restore these sectors again"));
2204 puts (_(" -v [or --version]: print version"));
2205 puts (_(" -? [or --help]: print this message"));
2206 puts (_("dangerous options:"));
2207 puts (_(" -g [or --show-geometry]: print the kernel's idea of the geometry"));
2208 puts (_(" -x [or --show-extended]: also list extended partitions on output\n"
2209 " or expect descriptors for them on input"));
2210 puts (_(" -L [or --Linux]: do not complain about things irrelevant for Linux"));
2211 puts (_(" -q [or --quiet]: suppress warning messages"));
2212 puts (_(" You can override the detected geometry using:"));
2213 puts (_(" -C# [or --cylinders #]:set the number of cylinders to use"));
2214 puts (_(" -H# [or --heads #]: set the number of heads to use"));
2215 puts (_(" -S# [or --sectors #]: set the number of sectors to use"));
2216 puts (_("You can disable all consistency checking with:"));
2217 puts (_(" -f [or --force]: do what I say, even if it is stupid"));
2222 activate_usage(char *progn
) {
2224 printf(_("%s device list active partitions on device\n"), progn
);
2225 printf(_("%s device n1 n2 ... activate partitions n1 ..., inactivate the rest\n"), progn
);
2226 printf(_("%s -An device activate partition n, inactivate the other ones\n"), PROGNAME
);
2231 unhide_usage(char *progn
) {
2235 static char short_opts
[] = "cdfgilnqsu:vx?1A::C:DH:I:LN:O:RS:TU::V";
2237 #define PRINT_ID 0400
2238 #define CHANGE_ID 01000
2240 static const struct option long_opts
[] = {
2241 { "change-id", no_argument
, NULL
, 'c' + CHANGE_ID
},
2242 { "print-id", no_argument
, NULL
, 'c' + PRINT_ID
},
2243 { "id", no_argument
, NULL
, 'c' },
2244 { "dump", no_argument
, NULL
, 'd' },
2245 { "force", no_argument
, NULL
, 'f' },
2246 { "show-geometry", no_argument
, NULL
, 'g' },
2247 { "increment", no_argument
, NULL
, 'i' },
2248 { "list", no_argument
, NULL
, 'l' },
2249 { "quiet", no_argument
, NULL
, 'q' },
2250 { "show-size", no_argument
, NULL
, 's' },
2251 { "unit", required_argument
, NULL
, 'u' },
2252 { "version", no_argument
, NULL
, 'v' },
2253 { "show-extended", no_argument
, NULL
, 'x' },
2254 { "help", no_argument
, NULL
, '?' },
2255 { "one-only", no_argument
, NULL
, '1' },
2256 { "cylinders", required_argument
, NULL
, 'C' },
2257 { "heads", required_argument
, NULL
, 'H' },
2258 { "sectors", required_argument
, NULL
, 'S' },
2259 { "activate", optional_argument
, NULL
, 'A' },
2260 { "DOS", no_argument
, NULL
, 'D' },
2261 { "DOS-extended", no_argument
, NULL
, 'E' },
2262 { "Linux", no_argument
, NULL
, 'L' },
2263 { "re-read", no_argument
, NULL
, 'R' },
2264 { "list-types", no_argument
, NULL
, 'T' },
2265 { "unhide", optional_argument
, NULL
, 'U' },
2266 { "no-reread", no_argument
, NULL
, 160 },
2267 { "IBM", no_argument
, NULL
, 161 },
2268 { "leave-last", no_argument
, NULL
, 161 },
2269 /* undocumented flags - not all completely implemented */
2270 { "in-order", no_argument
, NULL
, 128 },
2271 { "not-in-order", no_argument
, NULL
, 129 },
2272 { "inside-outer", no_argument
, NULL
, 130 },
2273 { "not-inside-outer", no_argument
, NULL
, 131 },
2274 { "nested", no_argument
, NULL
, 132 },
2275 { "chained", no_argument
, NULL
, 133 },
2276 { "onesector", no_argument
, NULL
, 134 },
2277 { NULL
, 0, NULL
, 0 }
2280 /* default devices to list */
2281 static struct devd
{
2282 char *pref
, *letters
;
2284 { "hd", "abcdefgh" },
2291 is_ide_cdrom(char *device
) {
2292 /* No device was given explicitly, and we are trying some
2293 likely things. But opening /dev/hdc may produce errors like
2294 "hdc: tray open or drive not ready"
2295 if it happens to be a CD-ROM drive. So try to be careful.
2296 This only works since 2.1.73. */
2300 struct stat statbuf
;
2302 sprintf(buf
, "/proc/ide/%s/media", device
+5);
2303 procf
= fopen(buf
, "r");
2304 if (procf
!= NULL
&& fgets(buf
, sizeof(buf
), procf
))
2305 return !strncmp(buf
, "cdrom", 5);
2307 /* Now when this proc file does not exist, skip the
2308 device when it is read-only. */
2309 if (stat(device
, &statbuf
) == 0)
2310 return (statbuf
.st_mode
& 0222) == 0;
2315 static void do_list(char *dev
, int silent
);
2316 static void do_size(char *dev
, int silent
);
2317 static void do_geom(char *dev
, int silent
);
2318 static void do_fdisk(char *dev
);
2319 static void do_reread(char *dev
);
2320 static void do_change_id(char *dev
, char *part
, char *id
);
2321 static void do_unhide(char **av
, int ac
, char *arg
);
2322 static void do_activate(char **av
, int ac
, char *arg
);
2327 main(int argc
, char **argv
) {
2332 int opt_out_geom
= 0;
2338 char *activatearg
= 0;
2339 char *unhidearg
= 0;
2341 setlocale(LC_ALL
, "");
2342 bindtextdomain(PACKAGE
, LOCALEDIR
);
2343 textdomain(PACKAGE
);
2346 fatal(_("no command?\n"));
2347 if ((progn
= rindex(argv
[0], '/')) == NULL
)
2351 if (!strcmp(progn
, "activate"))
2352 activate
= 1; /* equivalent to `sfdisk -A' */
2353 #if 0 /* not important enough to deserve a name */
2354 else if (!strcmp(progn
, "unhide"))
2355 unhide
= 1; /* equivalent to `sfdisk -U' */
2360 while ((c
= getopt_long (argc
, argv
, short_opts
, long_opts
, NULL
)) != -1) {
2363 force
= 1; break; /* does not imply quiet */
2365 opt_out_geom
= 1; break;
2367 increment
= 1; break;
2369 case 'c' + PRINT_ID
:
2370 case 'c' + CHANGE_ID
:
2373 dump
= 1; /* fall through */
2375 opt_list
= 1; break;
2377 no_write
= 1; break;
2381 opt_size
= 1; break;
2383 set_format(*optarg
); break;
2388 show_extended
= 1; break;
2390 activatearg
= optarg
;
2391 activate
= 1; break;
2393 U
.cylinders
= atoi(optarg
); break;
2397 DOS_extended
= 1; break;
2399 U
.heads
= atoi(optarg
); break;
2403 one_only
= atoi(optarg
); break;
2405 restore_sector_file
= optarg
; break;
2407 save_sector_file
= optarg
; break;
2409 opt_reread
= 1; break;
2411 U
.sectors
= atoi(optarg
); break;
2424 /* undocumented flags */
2426 partitions_in_order
= 1; break;
2428 partitions_in_order
= 0; break;
2430 all_logicals_inside_outermost_extended
= 1; break;
2432 all_logicals_inside_outermost_extended
= 0; break;
2434 boxes
= NESTED
; break;
2436 boxes
= CHAINED
; break;
2438 boxes
= ONESECTOR
; break;
2442 no_reread
= 1; break;
2444 leave_last
= 1; break;
2448 if (optind
== argc
&& (opt_list
|| opt_out_geom
|| opt_size
|| verify
)) {
2455 for(dp
= defdevs
; dp
-defdevs
< SIZE(defdevs
); dp
++) {
2458 sprintf(device
, "/dev/%s%c", dp
->pref
, *lp
++);
2459 if (!strcmp(dp
->pref
, "hd") && is_ide_cdrom(device
))
2465 if (opt_list
|| verify
)
2471 printf(_("total: %d blocks\n"), total_size
);
2476 if (optind
== argc
) {
2478 activate_usage(fdisk
? "sfdisk -A" : progn
);
2480 unhide_usage(fdisk
? "sfdisk -U" : progn
);
2485 if (opt_list
|| opt_out_geom
|| opt_size
|| verify
) {
2486 while (optind
< argc
) {
2488 do_geom(argv
[optind
], 0);
2490 do_size(argv
[optind
], 0);
2491 if (opt_list
|| verify
)
2492 do_list(argv
[optind
], 0);
2499 do_activate(argv
+optind
, argc
-optind
, activatearg
);
2503 do_unhide(argv
+optind
, argc
-optind
, unhidearg
);
2507 if ((do_id
& PRINT_ID
) != 0 && optind
!= argc
-2)
2508 fatal(_("usage: sfdisk --print-id device partition-number\n"));
2509 else if ((do_id
& CHANGE_ID
) != 0 && optind
!= argc
-3)
2510 fatal(_("usage: sfdisk --change-id device partition-number Id\n"));
2511 else if (optind
!= argc
-3 && optind
!= argc
-2)
2512 fatal(_("usage: sfdisk --id device partition-number [Id]\n"));
2513 do_change_id(argv
[optind
], argv
[optind
+1],
2514 (optind
== argc
-2) ? 0 : argv
[optind
+2]);
2518 if (optind
!= argc
-1)
2519 fatal(_("can specify only one device (except with -l or -s)\n"));
2524 else if (restore_sector_file
)
2525 restore_sectors(dev
);
2533 * H. Listing the current situation
2537 my_open (char *dev
, int rw
, int silent
) {
2540 mode
= (rw
? O_RDWR
: O_RDONLY
);
2541 fd
= open(dev
, mode
);
2542 if (fd
< 0 && !silent
) {
2544 fatal(_("cannot open %s %s\n"), dev
, rw
? _("read-write") : _("for reading"));
2550 do_list (char *dev
, int silent
) {
2552 struct disk_desc
*z
;
2554 fd
= my_open(dev
, 0, silent
);
2561 get_cylindersize(dev
, fd
, dump
? 1 : opt_list
? 0 : 1);
2562 get_partitions(dev
, fd
, z
);
2565 out_partitions(dev
, z
);
2568 if (partitions_ok(z
))
2569 warn(_("%s: OK\n"), dev
);
2576 do_geom (char *dev
, int silent
) {
2580 fd
= my_open(dev
, 0, silent
);
2584 R
= get_geometry(dev
, fd
, silent
);
2586 printf(_("%s: %ld cylinders, %ld heads, %ld sectors/track\n"),
2587 dev
, R
.cylinders
, R
.heads
, R
.sectors
);
2590 /* for compatibility with earlier fdisk: provide option -s */
2592 do_size (char *dev
, int silent
) {
2596 fd
= my_open(dev
, 0, silent
);
2600 if(ioctl(fd
, BLKGETSIZE
, &size
)) {
2603 fatal(_("BLKGETSIZE ioctl failed for %s\n"), dev
);
2608 size
/= 2; /* convert sectors to blocks */
2610 /* a CDROM drive without mounted CD yields MAXINT */
2611 if (silent
&& size
== ((1<<30)-1))
2615 printf("%s: %9ld\n", dev
, size
);
2617 printf("%ld\n", size
);
2623 * Activate: usually one wants to have a single primary partition
2624 * to be active. OS/2 fdisk makes non-bootable logical partitions
2625 * active - I don't know what that means to OS/2 Boot Manager.
2627 * Call: activate /dev/hda 2 5 7 make these partitions active
2628 * and the remaining ones inactive
2629 * Or: sfdisk -A /dev/hda 2 5 7
2631 * If only a single partition must be active, one may also use the form
2632 * sfdisk -A2 /dev/hda
2634 * With "activate /dev/hda" or "sfdisk -A /dev/hda" the active partitions
2635 * are listed but not changed. To get zero active partitions, use
2636 * "activate /dev/hda none" or "sfdisk -A /dev/hda none".
2637 * Use something like `echo ",,,*" | sfdisk -N2 /dev/hda' to only make
2638 * /dev/hda2 active, without changing other partitions.
2640 * A warning will be given if after the change not precisely one primary
2641 * partition is active.
2643 * The present syntax was chosen to be (somewhat) compatible with the
2644 * activate from the LILO package.
2647 set_active (struct disk_desc
*z
, char *pnam
) {
2650 pno
= asc_to_index(pnam
, z
);
2651 z
->partitions
[pno
].p
.bootable
= 0x80;
2655 do_activate (char **av
, int ac
, char *arg
) {
2658 int rw
, i
, pno
, lpno
;
2659 struct disk_desc
*z
;
2663 rw
= (!no_write
&& (arg
|| ac
> 1));
2664 fd
= my_open(dev
, rw
, 0);
2667 get_cylindersize(dev
, fd
, 1);
2668 get_partitions(dev
, fd
, z
);
2670 if (!arg
&& ac
== 1) {
2671 /* list active partitions */
2672 for (pno
=0; pno
< z
->partno
; pno
++) {
2673 if (z
->partitions
[pno
].p
.bootable
) {
2674 lpno
= index_to_linux(pno
, z
);
2675 if (pno
== linux_to_index(lpno
, z
))
2676 printf("%s\n", partname(dev
, lpno
, 0));
2678 printf("%s#%d\n", dev
, pno
);
2679 if (z
->partitions
[pno
].p
.bootable
!= 0x80)
2680 warn(_("bad active byte: 0x%x instead of 0x80\n"),
2681 z
->partitions
[pno
].p
.bootable
);
2685 /* clear `active byte' everywhere */
2686 for (pno
=0; pno
< z
->partno
; pno
++)
2687 z
->partitions
[pno
].p
.bootable
= 0;
2689 /* then set where desired */
2692 else for(i
=1; i
<ac
; i
++)
2693 set_active(z
, av
[i
]);
2695 /* then write to disk */
2696 if(write_partitions(dev
, fd
, z
))
2697 warn(_("Done\n\n"));
2702 for (pno
=0; pno
< z
->partno
&& pno
< 4; pno
++)
2703 if (z
->partitions
[pno
].p
.bootable
)
2706 warn(_("You have %d active primary partitions. This does not matter for LILO,\n"
2707 "but the DOS MBR will only boot a disk with 1 active partition.\n"), i
);
2711 set_unhidden (struct disk_desc
*z
, char *pnam
) {
2715 pno
= asc_to_index(pnam
, z
);
2716 id
= z
->partitions
[pno
].p
.sys_type
;
2717 if (id
== 0x11 || id
== 0x14 || id
== 0x16 || id
== 0x17)
2720 fatal(_("partition %s has id %x and is not hidden\n"), pnam
, id
);
2721 z
->partitions
[pno
].p
.sys_type
= id
;
2725 * maybe remove and make part of --change-id
2728 do_unhide (char **av
, int ac
, char *arg
) {
2731 struct disk_desc
*z
;
2736 fd
= my_open(dev
, rw
, 0);
2739 get_cylindersize(dev
, fd
, 1);
2740 get_partitions(dev
, fd
, z
);
2742 /* unhide where desired */
2744 set_unhidden(z
, arg
);
2745 else for(i
=1; i
<ac
; i
++)
2746 set_unhidden(z
, av
[i
]);
2748 /* then write to disk */
2749 if(write_partitions(dev
, fd
, z
))
2750 warn(_("Done\n\n"));
2756 do_change_id(char *dev
, char *pnam
, char *id
) {
2758 struct disk_desc
*z
;
2764 fd
= my_open(dev
, rw
, 0);
2767 get_cylindersize(dev
, fd
, 1);
2768 get_partitions(dev
, fd
, z
);
2770 pno
= asc_to_index(pnam
, z
);
2772 printf("%x\n", z
->partitions
[pno
].p
.sys_type
);
2775 i
= strtoul(id
, NULL
, 16);
2777 fatal(_("Bad Id %lx\n"), i
);
2778 z
->partitions
[pno
].p
.sys_type
= i
;
2780 if(write_partitions(dev
, fd
, z
))
2781 warn(_("Done\n\n"));
2787 do_reread(char *dev
) {
2790 fd
= my_open(dev
, 0, 0);
2791 if(reread_ioctl(fd
))
2792 printf(_("This disk is currently in use.\n"));
2796 * I. Writing the new situation
2800 do_fdisk(char *dev
){
2803 struct stat statbuf
;
2804 int interactive
= isatty(0);
2805 struct disk_desc
*z
;
2807 if (stat(dev
, &statbuf
) < 0) {
2809 fatal(_("Fatal error: cannot find %s\n"), dev
);
2811 if (!S_ISBLK(statbuf
.st_mode
)) {
2812 warn(_("Warning: %s is not a block device\n"), dev
);
2815 fd
= my_open(dev
, !no_write
, 0);
2817 if(!no_write
&& !no_reread
) {
2818 warn(_("Checking that no-one is using this disk right now ...\n"));
2819 if(reread_ioctl(fd
)) {
2820 printf(_("\nThis disk is currently in use - repartitioning is probably a bad idea.\n"
2821 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
2822 "Use the --no-reread flag to suppress this check.\n"));
2824 printf(_("Use the --force flag to overrule all checks.\n"));
2834 get_cylindersize(dev
, fd
, 0);
2835 get_partitions(dev
, fd
, z
);
2837 printf(_("Old situation:\n"));
2838 out_partitions(dev
, z
);
2840 if (one_only
&& (one_only_pno
= linux_to_index(one_only
, z
)) < 0)
2841 fatal(_("Partition %d does not exist, cannot change it\n"), one_only
);
2847 read_input(dev
, interactive
, z
);
2849 printf(_("New situation:\n"));
2850 out_partitions(dev
, z
);
2852 if (!partitions_ok(z
) && !force
) {
2854 fatal(_("I don't like these partitions - nothing changed.\n"
2855 "(If you really want this, use the --force option.)\n"));
2857 printf(_("I don't like this - probably you should answer No\n"));
2862 printf(_("Are you satisfied with this? [ynq] "));
2864 printf(_("Do you want to write this to disk? [ynq] "));
2865 answer
= c
= getchar();
2866 while (c
!= '\n' && c
!= EOF
)
2869 printf(_("\nsfdisk: premature end of input\n"));
2870 if (c
== EOF
|| answer
== 'q' || answer
== 'Q') {
2871 fatal(_("Quitting - nothing changed\n"));
2872 } else if (answer
== 'n' || answer
== 'N') {
2874 } else if (answer
== 'y' || answer
== 'Y') {
2877 printf(_("Please answer one of y,n,q\n"));
2884 if(write_partitions(dev
, fd
, z
))
2885 printf(_("Successfully wrote the new partition table\n\n"));
2889 reread_disk_partition(dev
, fd
);
2891 warn(_("If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)\n"
2892 "to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1\n"
2893 "(See fdisk(8).)\n"));
2895 sync(); /* superstition */