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 <linux/unistd.h> /* _syscall */
49 #include <linux/hdreg.h> /* HDIO_GETGEO */
50 #include <linux/fs.h> /* BLKGETSIZE */
55 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
61 * C. About heads, sectors and cylinders
64 * F. The standard input
66 * H. Listing the current situation
67 * I. Writing the new situation
71 int force
= 0; /* 1: do what I say, even if it is stupid ... */
72 int quiet
= 0; /* 1: suppress all warnings */
73 int Linux
= 0; /* 1: suppress warnings irrelevant for Linux */
74 int DOS
= 0; /* 1: shift extended partitions by #sectors, not 1 */
75 int dump
= 0; /* 1: list in a format suitable for later input */
76 int verify
= 0; /* 1: check that listed partition is reasonable */
77 int no_write
= 0; /* 1: do not actually write to disk */
78 int no_reread
= 0; /* 1: skip the BLKRRPART ioctl test at startup */
79 int leave_last
= 0; /* 1: don't allocate the last cylinder */
81 char *save_sector_file
= NULL
;
82 char *restore_sector_file
= NULL
;
91 vfprintf (stderr
, s
, p
);
101 fprintf(stderr
, "\n" PROGNAME
": ");
102 vfprintf(stderr
, s
, p
);
107 fatal(char *s
, ...) {
112 fprintf(stderr
, "\n" PROGNAME
": ");
113 vfprintf(stderr
, s
, p
);
123 * sseek: seek to specified sector - return 0 on failure
125 * For >4GB disks lseek needs a > 32bit arg, and we have to use llseek.
126 * On the other hand, a 32 bit sector number is OK until 2TB.
127 * The routines _llseek and sseek below are the only ones that
128 * know about the loff_t type.
132 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
133 loff_t
*, res
, uint
, wh
);
137 sseek(char *dev
, unsigned int fd
, unsigned long s
) {
139 in
= ((loff_t
) s
<< 9);
143 if (_llseek (fd
, in
>>32, in
& 0xffffffff, &out
, SEEK_SET
) != 0) {
145 if ((out
= lseek(fd
, in
, SEEK_SET
)) != in
) {
148 error(_("seek error on %s - cannot seek to %lu\n"), dev
, s
);
153 error(_("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n"),
154 (uint
)(in
>>32), (uint
)(in
& 0xffffffff),
155 (uint
)(out
>>32), (uint
)(out
& 0xffffffff));
166 * We preserve all sectors read in a chain - some of these will
167 * have to be modified and written back.
171 unsigned long sectornumber
;
182 sectorhead
= s
->next
;
188 get_sector(char *dev
, int fd
, unsigned long sno
) {
191 for(s
= sectorhead
; s
; s
= s
->next
)
192 if(s
->sectornumber
== sno
)
195 if (!sseek(dev
, fd
, sno
))
198 if (!(s
= (struct sector
*) malloc(sizeof(struct sector
))))
199 fatal(_("out of memory - giving up\n"));
201 if (read(fd
, s
->data
, sizeof(s
->data
)) != sizeof(s
->data
)) {
203 error(_("read error on %s - cannot read sector %lu\n"), dev
, sno
);
208 s
->next
= sectorhead
;
210 s
->sectornumber
= sno
;
211 s
->to_be_written
= 0;
217 msdos_signature (struct sector
*s
) {
218 if (*(unsigned short *) (s
->data
+ 0x1fe) != 0xaa55) {
219 error(_("ERROR: sector %lu does not have an msdos signature\n"),
227 write_sectors(char *dev
, int fd
) {
230 for (s
= sectorhead
; s
; s
= s
->next
)
231 if (s
->to_be_written
) {
232 if (!sseek(dev
, fd
, s
->sectornumber
))
234 if (write(fd
, s
->data
, sizeof(s
->data
)) != sizeof(s
->data
)) {
236 error(_("write error on %s - cannot write sector %lu\n"),
237 dev
, s
->sectornumber
);
240 s
->to_be_written
= 0;
246 ulong_to_chars(unsigned long u
, char *uu
) {
256 chars_to_ulong(unsigned char *uu
) {
261 u
= (u
<< 8) | uu
[i
];
266 save_sectors(char *dev
, int fdin
) {
271 fdout
= open(save_sector_file
, O_WRONLY
| O_CREAT
, 0444);
273 perror(save_sector_file
);
274 error(_("cannot open partition sector save file (%s)\n"),
279 for (s
= sectorhead
; s
; s
= s
->next
)
280 if (s
->to_be_written
) {
281 ulong_to_chars(s
->sectornumber
, ss
);
282 if (!sseek(dev
, fdin
, s
->sectornumber
))
284 if (read(fdin
, ss
+4, 512) != 512) {
286 error(_("read error on %s - cannot read sector %lu\n"),
287 dev
, s
->sectornumber
);
290 if (write(fdout
, ss
, sizeof(ss
)) != sizeof(ss
)) {
292 error(_("write error on %s\n"), save_sector_file
);
299 void reread_disk_partition(char *dev
, int fd
);
302 restore_sectors(char *dev
) {
308 if (stat(restore_sector_file
, &statbuf
) < 0) {
309 perror(restore_sector_file
);
310 error(_("cannot stat partition restore file (%s)\n"),
311 restore_sector_file
);
314 if (statbuf
.st_size
% 516) {
315 error(_("partition restore file has wrong size - not restoring\n"));
318 if (!(ss
= (char *) malloc(statbuf
.st_size
))) {
319 error(_("out of memory?\n"));
322 fdin
= open(restore_sector_file
, O_RDONLY
);
324 perror(restore_sector_file
);
325 error(_("cannot open partition restore file (%s)\n"),
326 restore_sector_file
);
329 if (read(fdin
, ss
, statbuf
.st_size
) != statbuf
.st_size
) {
331 error(_("error reading %s\n"), restore_sector_file
);
335 fdout
= open(dev
, O_WRONLY
);
338 error(_("cannot open device %s for writing\n"), dev
);
343 ct
= statbuf
.st_size
/516;
345 sno
= chars_to_ulong(ss
);
346 if (!sseek(dev
, fdout
, sno
))
348 if (write(fdout
, ss
+4, 512) != 512) {
350 error(_("error writing sector %lu on %s\n"), sno
, dev
);
357 reread_disk_partition(dev
, fdout
);
363 * C. About heads, sectors and cylinders
367 * <linux/hdreg.h> defines HDIO_GETGEO and
368 * struct hd_geometry {
369 * unsigned char heads;
370 * unsigned char sectors;
371 * unsigned short cylinders;
372 * unsigned long start;
377 * We consider several geometries for a disk:
378 * B - the BIOS geometry, gotten from the kernel via HDIO_GETGEO
379 * F - the fdisk geometry
380 * U - the user-specified geometry
382 * 0 means unspecified / unknown
385 unsigned long cylindersize
;
386 unsigned long heads
, sectors
, cylinders
;
390 get_cylindersize(char *dev
, int fd
, int silent
) {
391 struct hd_geometry g
;
394 B
.heads
= B
.sectors
= B
.cylinders
= 0;
396 if (!ioctl(fd
, HDIO_GETGEO
, &g
)) {
400 B
.sectors
= g
.sectors
;
401 B
.cylinders
= g
.cylinders
;
407 B
.sectors
= U
.sectors
;
409 B
.cylinders
= U
.cylinders
;
411 B
.cylindersize
= B
.heads
* B
.sectors
;
414 if (g
.start
&& !force
) {
416 _("Warning: start=%d - this looks like a partition rather than\n"
417 "the entire disk. Using fdisk on it is probably meaningless.\n"
418 "[Use the --force option if you really want this]\n"), g
.start
);
421 if (B
.heads
!= g
.heads
)
422 warn(_("Warning: HDIO_GETGEO says that there are %d heads\n"),
424 if (B
.sectors
!= g
.sectors
)
425 warn(_("Warning: HDIO_GETGEO says that there are %d sectors\n"),
427 if (B
.cylinders
!= g
.cylinders
)
428 warn(_("Warning: HDIO_GETGEO says that there are %d cylinders\n"),
431 if (!B
.heads
|| !B
.sectors
|| !B
.cylinders
)
432 printf(_("Disk %s: cannot get geometry\n"), dev
);
434 warn(_("Warning: unlikely number of sectors (%d) - usually at most 63\n"
435 "This will give problems with all software that uses C/H/S addressing.\n"),
438 printf(_("\nDisk %s: %lu cylinders, %lu heads, %lu sectors/track\n"),
439 dev
, B
.cylinders
, B
.heads
, B
.sectors
);
442 typedef struct { unsigned char h
,s
,c
; } chs
; /* has some c bits in s */
443 chs zero_chs
= { 0,0,0 };
445 typedef struct { unsigned long h
,s
,c
; } longchs
;
446 longchs zero_longchs
;
449 longchs_to_chs (longchs aa
, struct geometry G
) {
452 if (aa
.h
< 256 && aa
.s
< 64 && aa
.c
< 1024) {
454 a
.s
= aa
.s
| ((aa
.c
>> 2) & 0xc0);
456 } else if (G
.heads
&& G
.sectors
) {
458 a
.s
= G
.sectors
| 0xc0;
466 chs_to_longchs (chs a
) {
472 aa
.c
= (aa
.c
<< 2) + a
.c
;
477 ulong_to_longchs (unsigned long sno
, struct geometry G
) {
480 if (G
.heads
&& G
.sectors
&& G
.cylindersize
) {
481 aa
.s
= 1 + sno
% G
.sectors
;
482 aa
.h
= (sno
/ G
.sectors
) % G
.heads
;
483 aa
.c
= sno
/ G
.cylindersize
;
491 longchs_to_ulong (longchs aa
, struct geometry G
) {
492 return (aa
.c
*G
.cylindersize
+ aa
.h
*G
.sectors
+ aa
.s
- 1);
496 ulong_to_chs (unsigned long sno
, struct geometry G
) {
497 return longchs_to_chs(ulong_to_longchs(sno
, G
), G
);
501 chs_to_ulong (chs a
, struct geometry G
) {
502 return longchs_to_ulong(chs_to_longchs(a
), G
);
506 is_equal_chs (chs a
, chs b
) {
507 return (a
.h
== b
.h
&& a
.s
== b
.s
&& a
.c
== b
.c
);
511 chs_ok (chs a
, char *v
, char *w
) {
512 longchs aa
= chs_to_longchs(a
);
515 if (is_equal_chs(a
, zero_chs
))
517 if (B
.heads
&& aa
.h
>= B
.heads
) {
518 warn(_("%s of partition %s has impossible value for head: "
519 "%d (should be in 0-%d)\n"), w
, v
, aa
.h
, B
.heads
-1);
522 if (B
.sectors
&& (aa
.s
== 0 || aa
.s
> B
.sectors
)) {
523 warn(_("%s of partition %s has impossible value for sector: "
524 "%d (should be in 1-%d)\n"), w
, v
, aa
.s
, B
.sectors
);
527 if (B
.cylinders
&& aa
.c
>= B
.cylinders
) {
528 warn(_("%s of partition %s has impossible value for cylinders: "
529 "%d (should be in 0-%d)\n"), w
, v
, aa
.c
, B
.cylinders
-1);
536 * D. About system Ids
539 #define EMPTY_PARTITION 0
540 #define EXTENDED_PARTITION 5
541 #define WIN98_EXTENDED 0x0f
542 #define DM6_AUX1PARTITION 0x51
543 #define DM6_AUX3PARTITION 0x53
544 #define DM6_PARTITION 0x54
545 #define EZD_PARTITION 0x55
546 #define LINUX_SWAP 0x82
547 #define LINUX_NATIVE 0x83
548 #define LINUX_EXTENDED 0x85
549 #define BSD_PARTITION 0xa5
551 /* List of partition types now in i386_sys_types.c */
554 sysname(unsigned char type
) {
557 for (s
= i386_sys_types
; s
->name
; s
++)
567 printf(_("Id Name\n\n"));
568 for (s
= i386_sys_types
; s
->name
; s
++)
569 printf("%2x %s\n", s
->type
, _(s
->name
));
573 is_extended(unsigned char type
) {
574 return (type
== EXTENDED_PARTITION
575 || type
== LINUX_EXTENDED
576 || type
== WIN98_EXTENDED
);
580 is_bsd(unsigned char type
) {
581 return (type
== BSD_PARTITION
);
585 * E. About partitions
588 /* MS/DOS partition */
591 unsigned char bootable
; /* 0 or 0x80 */
593 unsigned char sys_type
;
595 unsigned int start_sect
; /* starting sector counting from 0 */
596 unsigned int nr_sects
; /* nr of sectors in partition */
599 /* Unfortunately, partitions are not aligned, and non-Intel machines
600 are unhappy with non-aligned integers. So, we need a copy by hand. */
602 copy_to_int(unsigned char *cp
) {
613 copy_from_int(int m
, char *cp
) {
614 *cp
++ = (m
& 0xff); m
>>= 8;
615 *cp
++ = (m
& 0xff); m
>>= 8;
616 *cp
++ = (m
& 0xff); m
>>= 8;
621 copy_to_part(char *cp
, struct partition
*p
) {
623 p
->begin_chs
.h
= *cp
++;
624 p
->begin_chs
.s
= *cp
++;
625 p
->begin_chs
.c
= *cp
++;
627 p
->end_chs
.h
= *cp
++;
628 p
->end_chs
.s
= *cp
++;
629 p
->end_chs
.c
= *cp
++;
630 p
->start_sect
= copy_to_int(cp
);
631 p
->nr_sects
= copy_to_int(cp
+4);
635 copy_from_part(struct partition
*p
, char *cp
) {
637 *cp
++ = p
->begin_chs
.h
;
638 *cp
++ = p
->begin_chs
.s
;
639 *cp
++ = p
->begin_chs
.c
;
641 *cp
++ = p
->end_chs
.h
;
642 *cp
++ = p
->end_chs
.s
;
643 *cp
++ = p
->end_chs
.c
;
644 copy_from_int(p
->start_sect
, cp
);
645 copy_from_int(p
->nr_sects
, cp
+4);
648 /* Roughly speaking, Linux doesn't use any of the above fields except
649 for partition type, start sector and number of sectors. (However,
650 see also linux/drivers/scsi/fdomain.c.)
651 The only way partition type is used (in the kernel) is the comparison
652 for equality with EXTENDED_PARTITION (and these Disk Manager types). */
657 unsigned long sector
, offset
; /* disk location of this info */
659 struct part_desc
*ep
; /* extended partition containing this one */
666 outer_extended_partition(struct part_desc
*p
) {
673 is_parent(struct part_desc
*pp
, struct part_desc
*p
) {
683 struct part_desc partitions
[128];
687 /* determine where on the disk this information goes */
689 add_sector_and_offset(struct disk_desc
*z
) {
693 for (pno
= 0; pno
< z
->partno
; pno
++) {
694 p
= &(z
->partitions
[pno
]);
695 p
->offset
= 0x1be + (pno
%4)*sizeof(struct partition
);
696 p
->sector
= (p
->ep
? p
->ep
->start
: 0);
700 /* tell the kernel to reread the partition tables */
702 reread_ioctl(int fd
) {
703 if(ioctl(fd
, BLKRRPART
)) {
711 is_blockdev(int fd
) {
714 return(fstat(fd
, &statbuf
) == 0 && S_ISBLK(statbuf
.st_mode
));
717 /* reread after writing */
719 reread_disk_partition(char *dev
, int fd
) {
720 printf(_("Re-reading the partition table ...\n"));
723 sleep(3); /* superfluous since 1.3.20 */
725 if(reread_ioctl(fd
) && is_blockdev(fd
))
726 printf(_("The command to re-read the partition table failed\n"
727 "Reboot your system now, before using mkfs\n"));
731 printf(_("Error closing %s\n"), dev
);
736 /* find Linux name of this partition, assuming that it will have a name */
738 index_to_linux(int pno
, struct disk_desc
*z
) {
740 struct part_desc
*p
= &(z
->partitions
[0]);
741 for (i
=0; i
<pno
; i
++,p
++)
742 if(i
< 4 || (p
->size
> 0 && !is_extended(p
->p
.sys_type
)))
748 linux_to_index(int lpno
, struct disk_desc
*z
) {
750 struct part_desc
*p
= &(z
->partitions
[0]);
751 for (i
=0; i
<z
->partno
&& ct
< lpno
; i
++,p
++)
752 if((i
< 4 || (p
->size
> 0 && !is_extended(p
->p
.sys_type
)))
759 asc_to_index(char *pnam
, struct disk_desc
*z
) {
766 pno
= linux_to_index(pnum
, z
);
768 if (!(pno
>= 0 && pno
< z
->partno
))
769 fatal(_("%s: no such partition\n"), pnam
);
774 * List partitions - in terms of sectors, blocks or cylinders
781 int default_format
= F_MEGABYTE
;
782 int specified_format
= 0;
783 int show_extended
= 0;
792 printf(_("unrecognized format - using sectors\n"));
793 case 'S': specified_format
= F_SECTOR
; break;
794 case 'B': specified_format
= F_BLOCK
; break;
795 case 'C': specified_format
= F_CYLINDER
; break;
796 case 'M': specified_format
= F_MEGABYTE
; break;
801 unitsize(int format
) {
802 default_format
= (B
.cylindersize
? F_CYLINDER
: F_MEGABYTE
);
803 if (!format
&& !(format
= specified_format
))
804 format
= default_format
;
810 return B
.cylindersize
;
821 get_disksize(int format
) {
822 unsigned long cs
= B
.cylinders
;
823 if (cs
&& leave_last
)
825 return (cs
* B
.cylindersize
) / unitsize(format
);
829 out_partition_header(char *dev
, int format
, struct geometry G
) {
831 printf(_("# partition table of %s\n"), dev
);
832 printf(_("unit: sectors\n\n"));
836 default_format
= (G
.cylindersize
? F_CYLINDER
: F_MEGABYTE
);
837 if (!format
&& !(format
= specified_format
))
838 format
= default_format
;
842 printf(_("unimplemented format - using %s\n"),
843 G
.cylindersize
? _("cylinders") : _("sectors"));
845 if (G
.cylindersize
) {
846 printf(_("Units = cylinders of %lu bytes, blocks of 1024 bytes"
847 ", counting from %d\n\n"),
848 G
.cylindersize
<<9, increment
);
849 printf(_(" Device Boot Start End #cyls #blocks Id System\n"));
854 printf(_("Units = sectors of 512 bytes, counting from %d\n\n"),
856 printf(_(" Device Boot Start End #sectors Id System\n"));
859 printf(_("Units = blocks of 1024 bytes, counting from %d\n\n"),
861 printf(_(" Device Boot Start End #blocks Id System\n"));
864 printf(_("Units = megabytes of 1048576 bytes, blocks of 1024 bytes"
865 ", counting from %d\n\n"), increment
);
866 printf(_(" Device Boot Start End MB #blocks Id System\n"));
872 out_rounddown(int width
, unsigned long n
, unsigned long unit
, int inc
) {
873 printf("%*lu", width
, inc
+ n
/unit
);
875 putchar((n
% unit
) ? '+' : ' ');
880 out_roundup(int width
, unsigned long n
, unsigned long unit
, int inc
) {
881 if (n
== (unsigned long)(-1))
882 printf("%*s", width
, "-");
884 printf("%*lu", width
, inc
+ n
/unit
);
886 putchar(((n
+1) % unit
) ? '-' : ' ');
891 out_roundup_size(int width
, unsigned long n
, unsigned long unit
) {
892 printf("%*lu", width
, (n
+unit
-1)/unit
);
894 putchar((n
% unit
) ? '-' : ' ');
899 get_fdisk_geometry(struct part_desc
*p
) {
900 chs b
= p
->p
.end_chs
;
901 longchs bb
= chs_to_longchs(b
);
904 F
.cylindersize
= F
.heads
*F
.sectors
;
905 return (F
.sectors
!= B
.sectors
|| F
.heads
!= B
.heads
);
909 out_partition(char *dev
, int format
, struct part_desc
*p
,
910 struct disk_desc
*z
, struct geometry G
) {
911 unsigned long start
, end
, size
;
914 if (!format
&& !(format
= specified_format
))
915 format
= default_format
;
917 pno
= p
- &(z
->partitions
[0]); /* our index */
918 lpno
= index_to_linux(pno
, z
); /* name of next one that has a name */
919 if(pno
== linux_to_index(lpno
, z
)) /* was that us? */
920 printf("%8s%-2u", dev
, lpno
); /* yes */
921 else if(show_extended
)
925 putchar(dump
? ':' : ' ');
928 end
= p
->start
+ p
->size
- 1;
932 printf(_(" start=%9lu"), start
);
933 printf(_(", size=%8lu"), size
);
934 if (p
->ptype
== DOS_TYPE
) {
935 printf(_(", Id=%2x"), p
->p
.sys_type
);
936 if (p
->p
.bootable
== 0x80)
937 printf(_(", bootable"));
943 if(p
->ptype
!= DOS_TYPE
|| p
->p
.bootable
== 0)
945 else if(p
->p
.bootable
== 0x80)
948 printf(" ? "); /* garbage */
952 if (G
.cylindersize
) {
953 out_rounddown(6, start
, G
.cylindersize
, increment
);
954 out_roundup(6, end
, G
.cylindersize
, increment
);
955 out_roundup_size(6, size
, G
.cylindersize
);
956 out_rounddown(8, size
, 2, 0);
962 out_rounddown(9, start
, 1, increment
);
963 out_roundup(9, end
, 1, increment
);
964 out_rounddown(9, size
, 1, 0);
969 p
->sector
/2, ((p
->sector
& 1) ? 512 : 0) + p
->offset
);
971 out_rounddown(8, start
, 2, increment
);
972 out_roundup(8, end
, 2, increment
);
973 out_rounddown(8, size
, 2, 0);
976 out_rounddown(5, start
, 2048, increment
);
977 out_roundup(5, end
, 2048, increment
);
978 out_roundup_size(5, size
, 2048);
979 out_rounddown(8, size
, 2, 0);
982 if (p
->ptype
== DOS_TYPE
) {
984 p
->p
.sys_type
, sysname(p
->p
.sys_type
));
989 /* Is chs as we expect? */
990 if (!quiet
&& p
->ptype
== DOS_TYPE
) {
993 a
= (size
? ulong_to_chs(start
,G
) : zero_chs
);
995 aa
= chs_to_longchs(a
);
996 bb
= chs_to_longchs(b
);
997 if(a
.s
&& !is_equal_chs(a
, b
))
998 printf(_("\t\tstart: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
999 aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1000 a
= (size
? ulong_to_chs(end
,G
) : zero_chs
);
1002 aa
= chs_to_longchs(a
);
1003 bb
= chs_to_longchs(b
);
1004 if(a
.s
&& !is_equal_chs(a
, b
))
1005 printf(_("\t\tend: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1006 aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1007 if(G
.cylinders
&& G
.cylinders
< 1024 && bb
.c
> G
.cylinders
)
1008 printf(_("partition ends on cylinder %ld, beyond the end of the disk\n"),
1014 out_partitions(char *dev
, struct disk_desc
*z
) {
1015 int pno
, format
= 0;
1018 printf(_("No partitions found\n"));
1020 if (get_fdisk_geometry(&(z
->partitions
[0])))
1021 printf(_("Warning: The first partition looks like it was made\n"
1022 " for C/H/S=*/%ld/%ld (instead of %ld/%ld/%ld).\n"
1023 "For this listing I'll assume that geometry.\n"),
1024 F
.heads
, F
.sectors
, B
.cylinders
, B
.heads
, B
.sectors
);
1025 out_partition_header(dev
, format
, F
);
1026 for(pno
=0; pno
< z
->partno
; pno
++) {
1027 out_partition(dev
, format
, &(z
->partitions
[pno
]), z
, F
);
1028 if(show_extended
&& pno
%4==3)
1035 disj(struct part_desc
*p
, struct part_desc
*q
) {
1037 ((p
->start
+ p
->size
<= q
->start
)
1038 || (is_extended(p
->p
.sys_type
)
1039 && q
->start
+ q
->size
<= p
->start
+ p
->size
));
1043 pnumber(struct part_desc
*p
, struct disk_desc
*z
) {
1044 static char buf
[20];
1046 struct part_desc
*p0
= &(z
->partitions
[0]);
1048 this = index_to_linux(p
-p0
, z
);
1049 next
= index_to_linux(p
-p0
+1, z
);
1052 sprintf(buf
, "%d", this);
1054 sprintf(buf
, "[%d]", this);
1059 partitions_ok(struct disk_desc
*z
) {
1060 struct part_desc
*partitions
= &(z
->partitions
[0]), *p
, *q
;
1061 int partno
= z
->partno
;
1063 #define PNO(p) pnumber(p, z)
1065 /* Have at least 4 partitions been defined? */
1068 fatal(_("no partition table present.\n"));
1070 fatal(_("strange, only %d partitions defined.\n"), partno
);
1074 /* Are the partitions of size 0 marked empty?
1075 And do they have start = 0? And bootable = 0? */
1076 for (p
= partitions
; p
- partitions
< partno
; p
++)
1078 if(p
->p
.sys_type
!= EMPTY_PARTITION
)
1079 warn(_("Warning: partition %s has size 0 but is not marked Empty\n"),
1081 else if(p
->p
.bootable
!= 0)
1082 warn(_("Warning: partition %s has size 0 and is bootable\n"),
1084 else if(p
->p
.start_sect
!= 0)
1085 warn(_("Warning: partition %s has size 0 and nonzero start\n"),
1087 /* all this is probably harmless, no error return */
1090 /* Are the logical partitions contained in their extended partitions? */
1091 for (p
= partitions
+4; p
< partitions
+partno
; p
++)
1092 if (p
->ptype
== DOS_TYPE
)
1093 if (p
->size
&& !is_extended(p
->p
.sys_type
)) {
1095 if (p
->start
< q
->start
|| p
->start
+ p
->size
> q
->start
+ q
->size
) {
1096 warn(_("Warning: partition %s "), PNO(p
));
1097 warn(_("is not contained in partition %s\n"), PNO(q
));
1102 /* Are the data partitions mutually disjoint? */
1103 for (p
= partitions
; p
< partitions
+partno
; p
++)
1104 if (p
->size
&& !is_extended(p
->p
.sys_type
))
1105 for (q
= p
+1; q
< partitions
+partno
; q
++)
1106 if (q
->size
&& !is_extended(q
->p
.sys_type
))
1107 if(!((p
->start
> q
-> start
) ? disj(q
,p
) : disj(p
,q
))) {
1108 warn(_("Warning: partitions %s "), PNO(p
));
1109 warn(_("and %s overlap\n"), PNO(q
));
1113 /* Are the data partitions and the extended partition
1114 table sectors disjoint? */
1115 for (p
= partitions
; p
< partitions
+partno
; p
++)
1116 if (p
->size
&& !is_extended(p
->p
.sys_type
))
1117 for (q
= partitions
; q
< partitions
+partno
; q
++)
1118 if (is_extended(q
->p
.sys_type
))
1119 if (p
->start
<= q
->start
&& p
->start
+ p
->size
> q
->start
) {
1120 warn(_("Warning: partition %s contains part of "), PNO(p
));
1121 warn(_("the partition table (sector %lu),\n"), q
->start
);
1122 warn(_("and will destroy it when filled\n"));
1126 /* Do they start past zero and end before end-of-disk? */
1127 { unsigned long ds
= get_disksize(F_SECTOR
);
1128 for (p
= partitions
; p
< partitions
+partno
; p
++)
1131 warn(_("Warning: partition %s starts at sector 0\n"), PNO(p
));
1134 if (p
->size
&& p
->start
+ p
->size
> ds
) {
1135 warn(_("Warning: partition %s extends past end of disk\n"), PNO(p
));
1141 /* At most one chain of DOS extended partitions ? */
1142 /* It seems that the OS/2 fdisk has the additional requirement
1143 that the extended partition must be the fourth one */
1145 for (p
= partitions
; p
< partitions
+4; p
++)
1146 if (p
->p
.sys_type
== EXTENDED_PARTITION
)
1148 if (ect
> 1 && !Linux
) {
1149 warn(_("Among the primary partitions, at most one can be extended\n"));
1150 warn(_(" (although this is not a problem under Linux)\n"));
1156 * Do all partitions start at a cylinder boundary ?
1157 * (this is not required for Linux)
1158 * The first partition starts after MBR.
1159 * Logical partitions start slightly after the containing extended partn.
1161 if (B
.cylindersize
) {
1162 for(p
= partitions
; p
< partitions
+partno
; p
++)
1164 if(p
->start
% B
.cylindersize
!= 0
1165 && (!p
->ep
|| p
->start
/ B
.cylindersize
!= p
->ep
->start
/ B
.cylindersize
)
1166 && (p
->p
.start_sect
>= B
.cylindersize
)) {
1167 warn(_("Warning: partition %s does not start "
1168 "at a cylinder boundary\n"), PNO(p
));
1172 if((p
->start
+ p
->size
) % B
.cylindersize
) {
1173 warn(_("Warning: partition %s does not end "
1174 "at a cylinder boundary\n"), PNO(p
));
1181 /* Usually, one can boot only from primary partitions. */
1182 /* In fact, from a unique one only. */
1183 /* do not warn about bootable extended partitions -
1184 often LILO is there */
1186 for(p
= partitions
; p
< partitions
+partno
; p
++)
1187 if (p
->p
.bootable
) {
1189 pno
= p
- partitions
;
1190 else if (p
- partitions
< 4) {
1191 warn(_("Warning: more than one primary partition is marked "
1192 "bootable (active)\n"
1193 "This does not matter for LILO, but the DOS MBR will "
1194 "not boot this disk.\n"));
1197 if (p
- partitions
>= 4) {
1198 warn(_("Warning: usually one can boot from primary partitions "
1199 "only\nLILO disregards the `bootable' flag.\n"));
1203 if (pno
== -1 || pno
>= 4)
1204 warn(_("Warning: no primary partition is marked bootable (active)\n"
1205 "This does not matter for LILO, but the DOS MBR will "
1206 "not boot this disk.\n"));
1209 /* Is chs as we expect? */
1210 for(p
= partitions
; p
< partitions
+partno
; p
++)
1211 if(p
->ptype
== DOS_TYPE
) {
1214 a
= p
->size
? ulong_to_chs(p
->start
,B
) : zero_chs
;
1216 aa
= chs_to_longchs(a
);
1217 bb
= chs_to_longchs(b
);
1218 if (!chs_ok(b
, PNO(p
), "start"))
1220 if(a
.s
&& !is_equal_chs(a
, b
))
1221 warn(_("partition %s: start: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1222 PNO(p
), aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1223 a
= p
->size
? ulong_to_chs(p
->start
+ p
->size
- 1, B
) : zero_chs
;
1225 aa
= chs_to_longchs(a
);
1226 bb
= chs_to_longchs(b
);
1227 if (!chs_ok(b
, PNO(p
), "end"))
1229 if(a
.s
&& !is_equal_chs(a
, b
))
1230 warn(_("partition %s: end: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1231 PNO(p
), aa
.c
, aa
.h
, aa
.s
, bb
.c
, bb
.h
, bb
.s
);
1232 if(B
.cylinders
&& B
.cylinders
< 1024 && bb
.c
> B
.cylinders
)
1233 warn(_("partition %s ends on cylinder %ld, beyond the end of the disk\n"),
1243 extended_partition(char *dev
, int fd
, struct part_desc
*ep
, struct disk_desc
*z
) {
1246 unsigned long start
, here
, next
;
1247 int i
, moretodo
= 1;
1249 struct part_desc
*partitions
= &(z
->partitions
[0]);
1250 int pno
= z
->partno
;
1252 here
= start
= ep
->start
;
1257 if (!(s
= get_sector(dev
, fd
, here
)))
1260 if (!msdos_signature(s
))
1263 cp
= s
->data
+ 0x1be;
1265 if (pno
+4 >= SIZE(z
->partitions
)) {
1266 printf(_("too many partitions - ignoring those past nr (%d)\n"),
1273 for (i
=0; i
<4; i
++,cp
+= sizeof(struct partition
)) {
1274 partitions
[pno
].sector
= here
;
1275 partitions
[pno
].offset
= cp
- s
->data
;
1276 partitions
[pno
].ep
= ep
;
1277 copy_to_part(cp
,&p
);
1278 if (is_extended(p
.sys_type
)) {
1279 partitions
[pno
].start
= start
+ p
.start_sect
;
1281 printf(_("tree of partitions?\n"));
1283 next
= partitions
[pno
].start
; /* follow `upper' branch */
1286 partitions
[pno
].start
= here
+ p
.start_sect
;
1288 partitions
[pno
].size
= p
.nr_sects
;
1289 partitions
[pno
].ptype
= DOS_TYPE
;
1290 partitions
[pno
].p
= p
;
1299 #define BSD_DISKMAGIC (0x82564557UL)
1300 #define BSD_MAXPARTITIONS 8
1301 #define BSD_FS_UNUSED 0
1302 typedef unsigned char u8
;
1303 typedef unsigned short u16
;
1304 typedef unsigned int u32
;
1305 struct bsd_disklabel
{
1308 char d_typename
[16];
1309 char d_packname
[16];
1313 u16 d_npartitions
; /* number of partitions in following */
1315 struct bsd_partition
{ /* the partition table */
1316 u32 p_size
; /* number of sectors in partition */
1317 u32 p_offset
; /* starting sector */
1318 u32 p_fsize
; /* filesystem basic fragment size */
1319 u8 p_fstype
; /* filesystem type, see below */
1320 u8 p_frag
; /* filesystem fragments per block */
1321 u16 p_cpg
; /* filesystem cylinders per group */
1322 } d_partitions
[BSD_MAXPARTITIONS
]; /* actually may be more */
1326 bsd_partition(char *dev
, int fd
, struct part_desc
*ep
, struct disk_desc
*z
) {
1327 struct bsd_disklabel
*l
;
1328 struct bsd_partition
*bp
, *bp0
;
1329 unsigned long start
= ep
->start
;
1331 struct part_desc
*partitions
= &(z
->partitions
[0]);
1332 int pno
= z
->partno
;
1334 if (!(s
= get_sector(dev
,fd
,start
+1)))
1336 l
= (struct bsd_disklabel
*) (s
->data
);
1337 if (l
->d_magic
!= BSD_DISKMAGIC
)
1340 bp
= bp0
= &l
->d_partitions
[0];
1341 while (bp
- bp0
<= BSD_MAXPARTITIONS
) {
1342 if (pno
+1 >= SIZE(z
->partitions
)) {
1343 printf(_("too many partitions - ignoring those "
1344 "past nr (%d)\n"), pno
-1);
1347 if (bp
->p_fstype
!= BSD_FS_UNUSED
) {
1348 partitions
[pno
].start
= bp
->p_offset
;
1349 partitions
[pno
].size
= bp
->p_size
;
1350 partitions
[pno
].sector
= start
+1;
1351 partitions
[pno
].offset
= (char *)bp
- (char *)bp0
;
1352 partitions
[pno
].ep
= 0;
1353 partitions
[pno
].ptype
= BSD_TYPE
;
1362 msdos_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1365 struct partition pt
;
1367 struct part_desc
*partitions
= &(z
->partitions
[0]);
1368 int pno
= z
->partno
;
1370 if (!(s
= get_sector(dev
, fd
, start
)))
1373 if (!msdos_signature(s
))
1376 cp
= s
->data
+ 0x1be;
1377 copy_to_part(cp
,&pt
);
1379 /* If I am not mistaken, recent kernels will hide this from us,
1380 so we will never actually see traces of a Disk Manager */
1381 if (pt
.sys_type
== DM6_PARTITION
1382 || pt
.sys_type
== EZD_PARTITION
1383 || pt
.sys_type
== DM6_AUX1PARTITION
1384 || pt
.sys_type
== DM6_AUX3PARTITION
) {
1385 printf(_("detected Disk Manager - unable to handle that\n"));
1388 { unsigned int sig
= *(unsigned short *)(s
->data
+ 2);
1390 && *(unsigned short *)(s
->data
+ sig
) == 0x55aa
1391 && (1 & *(unsigned char *)(s
->data
+ sig
+ 2))) {
1392 printf(_("DM6 signature found - giving up\n"));
1397 for (pno
=0; pno
<4; pno
++,cp
+= sizeof(struct partition
)) {
1398 partitions
[pno
].sector
= start
;
1399 partitions
[pno
].offset
= cp
- s
->data
;
1400 copy_to_part(cp
,&pt
);
1401 partitions
[pno
].start
= start
+ pt
.start_sect
;
1402 partitions
[pno
].size
= pt
.nr_sects
;
1403 partitions
[pno
].ep
= 0;
1404 partitions
[pno
].p
= pt
;
1409 for (i
=0; i
<4; i
++) {
1410 if (is_extended(partitions
[i
].p
.sys_type
)) {
1411 if (!partitions
[i
].size
) {
1412 printf(_("strange..., an extended partition of size 0?\n"));
1415 extended_partition(dev
, fd
, &partitions
[i
], z
);
1417 if (is_bsd(partitions
[i
].p
.sys_type
)) {
1418 if (!partitions
[i
].size
) {
1419 printf(_("strange..., a BSD partition of size 0?\n"));
1422 bsd_partition(dev
, fd
, &partitions
[i
], z
);
1429 osf_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1434 sun_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1439 amiga_partition(char *dev
, int fd
, unsigned long start
, struct disk_desc
*z
) {
1444 get_partitions(char *dev
, int fd
, struct disk_desc
*z
) {
1447 if (!msdos_partition(dev
, fd
, 0, z
)
1448 && !osf_partition(dev
, fd
, 0, z
)
1449 && !sun_partition(dev
, fd
, 0, z
)
1450 && !amiga_partition(dev
, fd
, 0, z
)) {
1451 printf(_(" %s: unrecognized partition\n"), dev
);
1457 write_partitions(char *dev
, int fd
, struct disk_desc
*z
) {
1459 struct part_desc
*partitions
= &(z
->partitions
[0]), *p
;
1460 int pno
= z
->partno
;
1463 printf(_("-n flag was given: Nothing changed\n"));
1467 for (p
= partitions
; p
< partitions
+pno
; p
++) {
1468 s
= get_sector(dev
, fd
, p
->sector
);
1470 s
->to_be_written
= 1;
1471 copy_from_part(&(p
->p
), s
->data
+ p
->offset
);
1472 *(unsigned short *)(&(s
->data
[0x1fe])) = 0xaa55;
1474 if (save_sector_file
) {
1475 if (!save_sectors(dev
, fd
)) {
1476 fatal(_("Failed saving the old sectors - aborting\n"));
1480 if (!write_sectors(dev
, fd
)) {
1481 error(_("Failed writing the partition on %s\n"), dev
);
1488 * F. The standard input
1493 * <start> <size> <type> <bootable> <c,h,s> <c,h,s>
1494 * Fields are separated by whitespace or comma or semicolon possibly
1495 * followed by whitespace; initial and trailing whitespace is ignored.
1496 * Numbers can be octal, decimal or hexadecimal, decimal is default
1497 * The <c,h,s> parts can (and probably should) be omitted.
1498 * Bootable is specified as [*|-], with as default not-bootable.
1499 * Type is given in hex, without the 0x prefix, or is [E|S|L|X], where
1500 * L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), and E
1501 * is EXTENDED_PARTITION (5), X is LINUX_EXTENDED (85).
1502 * The default value of start is the first nonassigned sector/cylinder/...
1503 * The default value of size is as much as possible (until next
1504 * partition or end-of-disk).
1505 * .: end of chain of extended partitions.
1507 * On interactive input an empty line means: all defaults.
1508 * Otherwise empty lines are ignored.
1521 { 3, "bootable", 1 },
1531 * Read a line, split it into fields
1533 * (some primitive handwork, but a more elaborate parser seems
1540 read_stdin(unsigned char **fields
, unsigned char *line
, int fieldssize
, int linesize
) {
1541 unsigned char *lp
, *ip
;
1544 /* boolean true and empty string at start */
1547 for (fno
=0; fno
< fieldssize
; fno
++)
1548 fields
[fno
] = line
+ 1;
1551 /* read a line from stdin */
1552 lp
= fgets(line
+2, linesize
, stdin
);
1557 if (!(lp
= index(lp
, '\n')))
1558 fatal(_("long or incomplete input line - quitting\n"));
1561 /* remove comments, if any */
1562 if ((lp
= index(line
+2, '#')) != 0)
1565 /* recognize a few commands - to be expanded */
1566 if (!strcmp(line
+2, "unit: sectors")) {
1567 specified_format
= F_SECTOR
;
1571 /* dump style? - then bad input is fatal */
1572 if ((ip
= index(line
+2, ':')) != 0) {
1581 for(d
= dumpflds
; d
-dumpflds
< SIZE(dumpflds
); d
++) {
1582 if(!strncmp(ip
, d
->fldname
, strlen(d
->fldname
))) {
1583 ip
+= strlen(d
->fldname
);
1587 fields
[d
->fldno
] = line
;
1588 else if (*ip
== '=') {
1589 while(isspace(*++ip
)) ;
1590 fields
[d
->fldno
] = ip
;
1591 while(isalnum(*ip
)) /* 0x07FF */
1594 fatal(_("input error: `=' expected after %s field\n"),
1596 if (fno
<= d
->fldno
)
1600 if(*ip
!= ',' && *ip
!= ';')
1601 fatal(_("input error: unexpected character %c after %s field\n"),
1607 fatal(_("unrecognized input: %s\n"), ip
);
1610 /* split line into fields */
1613 while((c
= *ip
++) != 0) {
1614 if (!lp
[-1] && (c
== '\t' || c
== ' '))
1616 else if (c
== '\t' || c
== ' ' || c
== ',' || c
== ';') {
1618 if (fno
< fieldssize
)
1625 if (lp
== fields
[fno
-1])
1630 /* read a number, use default if absent */
1632 get_ul(char *u
, unsigned long *up
, unsigned long def
, int base
) {
1637 *up
= strtoul(u
, &nu
, base
);
1638 if (errno
== ERANGE
) {
1639 printf(_("number too big\n"));
1643 printf(_("trailing junk after number\n"));
1651 /* There are two common ways to structure extended partitions:
1652 as nested boxes, and as a chain. Sometimes the partitions
1653 must be given in order. Sometimes all logical partitions
1654 must lie inside the outermost extended partition.
1655 NESTED: every partition is contained in the surrounding partitions
1656 and is disjoint from all others.
1657 CHAINED: every data partition is contained in the surrounding partitions
1658 and disjoint from all others, but extended partitions may lie outside
1659 (insofar as allowed by all_logicals_inside_outermost_extended).
1660 ONESECTOR: all data partitions are mutually disjoint; extended partitions
1661 each use one sector only (except perhaps for the outermost one).
1663 int partitions_in_order
= 0;
1664 int all_logicals_inside_outermost_extended
= 1;
1665 enum { NESTED
, CHAINED
, ONESECTOR
} boxes
= NESTED
;
1667 /* find the default value for <start> - assuming entire units */
1669 first_free(int pno
, int is_extended
, struct part_desc
*ep
, int format
,
1670 unsigned long mid
, struct disk_desc
*z
) {
1671 unsigned long ff
, fff
;
1672 unsigned long unit
= unitsize(format
);
1673 struct part_desc
*partitions
= &(z
->partitions
[0]), *pp
= 0;
1675 /* if containing ep undefined, look at its container */
1676 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
)
1680 if (boxes
== NESTED
|| (boxes
== CHAINED
&& !is_extended
))
1682 else if (all_logicals_inside_outermost_extended
)
1683 pp
= outer_extended_partition(ep
);
1686 ff
= pp
? (pp
->start
+ unit
- 1) / unit
: 0;
1688 /* rounding up wastes almost an entire cylinder - round down
1689 and leave it to compute_start_sect() to fix the difference */
1690 ff
= pp
? pp
->start
/ unit
: 0;
1692 /* MBR and 1st sector of an extended partition are never free */
1697 for(pp
= partitions
; pp
< partitions
+pno
; pp
++) {
1698 if (!is_parent(pp
, ep
) && pp
->size
> 0) {
1699 if ((partitions_in_order
|| pp
->start
/ unit
<= ff
1700 || (mid
&& pp
->start
/ unit
<= mid
))
1701 && (fff
= (pp
->start
+ pp
->size
+ unit
- 1) / unit
) > ff
) {
1711 /* find the default value for <size> - assuming entire units */
1713 max_length(int pno
, int is_extended
, struct part_desc
*ep
, int format
,
1714 unsigned long start
, struct disk_desc
*z
) {
1716 unsigned long unit
= unitsize(format
);
1717 struct part_desc
*partitions
= &(z
->partitions
[0]), *pp
= 0;
1719 /* if containing ep undefined, look at its container */
1720 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
)
1724 if (boxes
== NESTED
|| (boxes
== CHAINED
&& !is_extended
))
1726 else if (all_logicals_inside_outermost_extended
)
1727 pp
= outer_extended_partition(ep
);
1729 fu
= pp
? (pp
->start
+ pp
->size
) / unit
: get_disksize(format
);
1731 for(pp
= partitions
; pp
< partitions
+pno
; pp
++)
1732 if (!is_parent(pp
, ep
) && pp
->size
> 0
1733 && pp
->start
/ unit
>= start
&& pp
->start
/ unit
< fu
)
1734 fu
= pp
->start
/ unit
;
1736 return (fu
> start
) ? fu
- start
: 0;
1739 /* compute starting sector of a partition inside an extended one */
1740 /* ep is 0 or points to surrounding extended partition */
1742 compute_start_sect(struct part_desc
*p
, struct part_desc
*ep
) {
1744 int inc
= (DOS
&& B
.sectors
) ? B
.sectors
: 1;
1747 if (ep
&& p
->start
+ p
->size
>= ep
->start
+ 1)
1748 delta
= p
->start
- ep
->start
- inc
;
1749 else if (p
->start
== 0 && p
->size
> 0)
1756 if (is_extended(p
->p
.sys_type
) && boxes
== ONESECTOR
)
1758 else if ((int)(p
->size
) <= 0) {
1759 warn(_("no room for partition descriptor\n"));
1764 : (is_extended(p
->p
.sys_type
) ?
1765 outer_extended_partition(ep
) : ep
)->start
);
1767 if (p
->p
.sys_type
== EMPTY_PARTITION
&& p
->size
== 0) {
1768 p
->p
.start_sect
= 0;
1769 p
->p
.begin_chs
= zero_chs
;
1770 p
->p
.end_chs
= zero_chs
;
1772 p
->p
.start_sect
= p
->start
- base
;
1773 p
->p
.begin_chs
= ulong_to_chs(p
->start
,B
);
1774 p
->p
.end_chs
= ulong_to_chs(p
->start
+ p
->size
- 1,B
);
1776 p
->p
.nr_sects
= p
->size
;
1780 /* build the extended partition surrounding a given logical partition */
1782 build_surrounding_extended(struct part_desc
*p
, struct part_desc
*ep
,
1783 struct disk_desc
*z
) {
1784 int inc
= (DOS
&& B
.sectors
) ? B
.sectors
: 1;
1785 int format
= F_SECTOR
;
1786 struct part_desc
*p0
= &(z
->partitions
[0]), *eep
= ep
->ep
;
1788 if (boxes
== NESTED
) {
1789 ep
->start
= first_free(ep
-p0
, 1, eep
, format
, p
->start
, z
);
1790 ep
->size
= max_length(ep
-p0
, 1, eep
, format
, ep
->start
, z
);
1791 if (ep
->start
> p
->start
|| ep
->start
+ ep
->size
< p
->start
+ p
->size
) {
1792 warn(_("cannot build surrounding extended partition\n"));
1796 ep
->start
= p
->start
;
1797 if(boxes
== CHAINED
)
1803 ep
->p
.nr_sects
= ep
->size
;
1805 ep
->p
.sys_type
= EXTENDED_PARTITION
;
1806 if (!compute_start_sect(ep
, eep
) || !compute_start_sect(p
, ep
)) {
1807 ep
->p
.sys_type
= EMPTY_PARTITION
;
1816 read_line(int pno
, struct part_desc
*ep
, char *dev
, int interactive
,
1817 struct disk_desc
*z
) {
1818 unsigned char line
[1000];
1819 unsigned char *fields
[11];
1820 int fno
, pct
= pno
%4;
1821 struct part_desc p
, *orig
;
1822 unsigned long ff
, ff1
, ul
, ml
, ml1
, def
;
1823 int format
, lpno
, is_extd
;
1828 lpno
= index_to_linux(pno
, z
);
1831 if (pct
== 0 && (show_extended
|| pno
== 0))
1833 warn("%8s%d: ", dev
, lpno
);
1836 /* read input line - skip blank lines when reading from a file */
1838 fno
= read_stdin(fields
, line
, SIZE(fields
), SIZE(line
));
1839 } while(fno
== RD_CMD
|| (fno
== 0 && !interactive
));
1840 if (fno
== RD_EOF
) {
1842 } else if (fno
> 10 && *(fields
[10]) != 0) {
1843 printf(_("too many input fields\n"));
1847 if (fno
== 1 && !strcmp(fields
[0], ".")) {
1852 /* use specified format, but round to cylinders if F_MEGABYTE specified */
1854 if (B
.cylindersize
&& specified_format
== F_MEGABYTE
)
1855 format
= F_CYLINDER
;
1857 orig
= (one_only
? &(oldp
.partitions
[pno
]) : 0);
1862 /* first read the type - we need to know whether it is extended */
1863 /* stop reading when input blank (defaults) and all is full */
1865 if (fno
== 0) { /* empty line */
1866 if (orig
&& is_extended(orig
->p
.sys_type
))
1868 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
1869 ml
= max_length(pno
, is_extd
, ep
, format
, ff
, z
);
1870 if (ml
== 0 && is_extd
== 0) {
1872 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
1873 ml
= max_length(pno
, is_extd
, ep
, format
, ff
, z
);
1875 if (ml
== 0 && pno
>= 4) {
1876 /* no free blocks left - don't read any further */
1877 warn(_("No room for more\n"));
1881 if (fno
< 3 || !*(fields
[2]))
1882 ul
= orig
? orig
->p
.sys_type
:
1883 (is_extd
|| (pno
> 3 && pct
== 1 && show_extended
))
1884 ? EXTENDED_PARTITION
: LINUX_NATIVE
;
1885 else if(!strcmp(fields
[2], "L"))
1887 else if(!strcmp(fields
[2], "S"))
1889 else if(!strcmp(fields
[2], "E"))
1890 ul
= EXTENDED_PARTITION
;
1891 else if(!strcmp(fields
[2], "X"))
1892 ul
= LINUX_EXTENDED
;
1893 else if (get_ul(fields
[2], &ul
, LINUX_NATIVE
, 16))
1896 warn(_("Illegal type\n"));
1900 is_extd
= is_extended(ul
);
1903 ff
= first_free(pno
, is_extd
, ep
, format
, 0, z
);
1904 ff1
= ff
* unitsize(format
);
1905 def
= orig
? orig
->start
: (pno
> 4 && pct
> 1) ? 0 : ff1
;
1906 if (fno
< 1 || !*(fields
[0]))
1909 if (get_ul(fields
[0], &ul
, def
/ unitsize(0), 0))
1911 p
.start
= ul
* unitsize(0);
1912 p
.start
-= (p
.start
% unitsize(format
));
1916 ml
= max_length(pno
, is_extd
, ep
, format
, p
.start
/ unitsize(format
), z
);
1917 ml1
= ml
* unitsize(format
);
1918 def
= orig
? orig
->size
: (pno
> 4 && pct
> 1) ? 0 : ml1
;
1919 if (fno
< 2 || !*(fields
[1]))
1922 if (get_ul(fields
[1], &ul
, def
/ unitsize(0), 0))
1924 p
.size
= ul
* unitsize(0) + unitsize(format
) - 1;
1925 p
.size
-= (p
.size
% unitsize(format
));
1928 warn(_("Warning: exceeds max allowable size (%lu)\n"), ml1
/ unitsize(0));
1932 if (p
.size
== 0 && pno
>= 4 && (fno
< 2 || !*(fields
[1]))) {
1933 warn(_("Warning: empty partition\n"));
1937 p
.p
.nr_sects
= p
.size
;
1939 if (p
.size
== 0 && !orig
) {
1940 if(fno
< 1 || !*(fields
[0]))
1942 if(fno
< 3 || !*(fields
[2]))
1943 p
.p
.sys_type
= EMPTY_PARTITION
;
1946 if (p
.start
< ff1
&& p
.size
> 0) {
1947 warn(_("Warning: bad partition start (earliest %lu)\n"),
1948 (ff1
+ unitsize(0) - 1) / unitsize(0));
1953 if (fno
< 4 || !*(fields
[3]))
1954 ul
= (orig
? orig
->p
.bootable
: 0);
1955 else if (!strcmp(fields
[3], "-"))
1957 else if (!strcmp(fields
[3], "*") || !strcmp(fields
[3], "+"))
1960 warn(_("unrecognized bootable flag - choose - or *\n"));
1965 if (ep
&& ep
->p
.sys_type
== EMPTY_PARTITION
) {
1966 if(!build_surrounding_extended(&p
, ep
, z
))
1969 if(!compute_start_sect(&p
, ep
))
1972 { longchs aa
= chs_to_longchs(p
.p
.begin_chs
), bb
;
1976 } else if (fno
< 7) {
1977 warn(_("partial c,h,s specification?\n"));
1979 } else if(get_ul(fields
[4], &bb
.c
, aa
.c
, 0) ||
1980 get_ul(fields
[5], &bb
.h
, aa
.h
, 0) ||
1981 get_ul(fields
[6], &bb
.s
, aa
.s
, 0))
1983 p
.p
.begin_chs
= longchs_to_chs(bb
,B
);
1985 { longchs aa
= chs_to_longchs(p
.p
.end_chs
), bb
;
1989 } else if (fno
< 10) {
1990 warn(_("partial c,h,s specification?\n"));
1992 } else if(get_ul(fields
[7], &bb
.c
, aa
.c
, 0) ||
1993 get_ul(fields
[8], &bb
.h
, aa
.h
, 0) ||
1994 get_ul(fields
[9], &bb
.s
, aa
.s
, 0))
1996 p
.p
.end_chs
= longchs_to_chs(bb
, B
);
1999 if (pno
> 3 && p
.size
&& show_extended
&& p
.p
.sys_type
!= EMPTY_PARTITION
2000 && (is_extended(p
.p
.sys_type
) != (pct
== 1))) {
2001 warn(_("Extended partition not where expected\n"));
2006 z
->partitions
[pno
] = p
;
2007 if (pno
>= z
->partno
)
2008 z
->partno
+= 4; /* reqd for out_partition() */
2011 out_partition(dev
, 0, &(z
->partitions
[pno
]), z
, B
);
2016 /* ep either points to the extended partition to contain this one,
2017 or to the empty partition that may become extended or is 0 */
2019 read_partition(char *dev
, int interactive
, int pno
, struct part_desc
*ep
,
2020 struct disk_desc
*z
) {
2021 struct part_desc
*p
= &(z
->partitions
[pno
]);
2025 *p
= oldp
.partitions
[pno
];
2026 if (one_only_pno
!= pno
)
2028 } else if (!show_extended
&& pno
> 4 && pno
%4)
2031 while (!(i
= read_line(pno
, ep
, dev
, interactive
, z
)))
2033 fatal(_("bad input\n"));
2041 if (pno
>= z
->partno
)
2047 read_partition_chain(char *dev
, int interactive
, struct part_desc
*ep
,
2048 struct disk_desc
*z
) {
2054 if (base
+4 > SIZE(z
->partitions
)) {
2055 printf(_("too many partitions\n"));
2059 if (!read_partition(dev
, interactive
, base
+i
, ep
, z
))
2061 for (i
=0; i
<4; i
++) {
2062 ep
= &(z
->partitions
[base
+i
]);
2063 if (is_extended(ep
->p
.sys_type
) && ep
->size
)
2067 /* nothing found - maybe an empty partition is going
2069 if (one_only
|| show_extended
)
2071 ep
= &(z
->partitions
[base
+1]);
2072 if (ep
->size
|| ep
->p
.sys_type
!= EMPTY_PARTITION
)
2079 read_input(char *dev
, int interactive
, struct disk_desc
*z
) {
2081 struct part_desc
*partitions
= &(z
->partitions
[0]), *ep
;
2083 for (i
=0; i
< SIZE(z
->partitions
); i
++)
2084 partitions
[i
] = zero_part_desc
;
2088 warn(_("Input in the following format; absent fields get a default value.\n"
2089 "<start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s>\n"
2090 "Usually you only need to specify <start> and <size> (and perhaps <type>).\n"));
2094 read_partition(dev
, interactive
, i
, 0, z
);
2095 for (i
=0; i
<4; i
++) {
2097 if (is_extended(ep
->p
.sys_type
) && ep
->size
)
2098 read_partition_chain(dev
, interactive
, ep
, z
);
2100 add_sector_and_offset(z
);
2104 * G. The command line
2107 static void version(void) {
2108 printf("%s %s %s (aeb@cwi.nl, %s)\n", PROGNAME
, _("version"), VERSION
, DATE
);
2114 printf(_("Usage: %s [options] device ...\n"), PROGNAME
);
2115 puts (_("device: something like /dev/hda or /dev/sda"));
2116 puts (_("useful options:"));
2117 puts (_(" -s [or --show-size]: list size of a partition"));
2118 puts (_(" -c [or --id]: print or change partition Id"));
2119 puts (_(" -l [or --list]: list partitions of each device"));
2120 puts (_(" -d [or --dump]: idem, but in a format suitable for later input"));
2121 puts (_(" -i [or --increment]: number cylinders etc. from 1 instead of from 0"));
2122 puts (_(" -uS, -uB, -uC, -uM: accept/report in units of sectors/blocks/cylinders/MB"));
2123 puts (_(" -T [or --list-types]:list the known partition types"));
2124 puts (_(" -D [or --DOS]: for DOS-compatibility: waste a little space"));
2125 puts (_(" -R [or --re-read]: make kernel reread partition table"));
2126 puts (_(" -N# : change only the partition with number #"));
2127 puts (_(" -n : do not actually write to disk"));
2128 puts (_(" -O file : save the sectors that will be overwritten to file"));
2129 puts (_(" -I file : restore these sectors again"));
2130 puts (_(" -v [or --version]: print version"));
2131 puts (_(" -? [or --help]: print this message"));
2132 puts (_("dangerous options:"));
2133 puts (_(" -g [or --show-geometry]: print the kernel's idea of the geometry"));
2134 puts (_(" -x [or --show-extended]: also list extended partitions on output\n"
2135 " or expect descriptors for them on input"));
2136 puts (_(" -L [or --Linux]: do not complain about things irrelevant for Linux"));
2137 puts (_(" -q [or --quiet]: suppress warning messages"));
2138 puts (_(" You can override the detected geometry using:"));
2139 puts (_(" -C# [or --cylinders #]:set the number of cylinders to use"));
2140 puts (_(" -H# [or --heads #]: set the number of heads to use"));
2141 puts (_(" -S# [or --sectors #]: set the number of sectors to use"));
2142 puts (_("You can disable all consistency checking with:"));
2143 puts (_(" -f [or --force]: do what I say, even if it is stupid"));
2148 activate_usage(char *progn
) {
2150 printf(_("%s device list active partitions on device\n"), progn
);
2151 printf(_("%s device n1 n2 ... activate partitions n1 ..., inactivate the rest\n"), progn
);
2152 printf(_("%s -An device activate partition n, inactivate the other ones\n"), PROGNAME
);
2157 unhide_usage(char *progn
) {
2161 static char short_opts
[] = "cdfgilnqsu:vx?1A::C:DH:I:LN:O:RS:TU::V";
2163 #define PRINT_ID 0400
2164 #define CHANGE_ID 01000
2166 static const struct option long_opts
[] = {
2167 { "change-id", no_argument
, NULL
, 'c' + CHANGE_ID
},
2168 { "print-id", no_argument
, NULL
, 'c' + PRINT_ID
},
2169 { "id", no_argument
, NULL
, 'c' },
2170 { "dump", no_argument
, NULL
, 'd' },
2171 { "force", no_argument
, NULL
, 'f' },
2172 { "show-geometry", no_argument
, NULL
, 'g' },
2173 { "increment", no_argument
, NULL
, 'i' },
2174 { "list", no_argument
, NULL
, 'l' },
2175 { "quiet", no_argument
, NULL
, 'q' },
2176 { "show-size", no_argument
, NULL
, 's' },
2177 { "unit", required_argument
, NULL
, 'u' },
2178 { "version", no_argument
, NULL
, 'v' },
2179 { "show-extended", no_argument
, NULL
, 'x' },
2180 { "help", no_argument
, NULL
, '?' },
2181 { "one-only", no_argument
, NULL
, '1' },
2182 { "cylinders", required_argument
, NULL
, 'C' },
2183 { "heads", required_argument
, NULL
, 'H' },
2184 { "sectors", required_argument
, NULL
, 'S' },
2185 { "activate", optional_argument
, NULL
, 'A' },
2186 { "DOS", no_argument
, NULL
, 'D' },
2187 { "Linux", no_argument
, NULL
, 'L' },
2188 { "re-read", no_argument
, NULL
, 'R' },
2189 { "list-types", no_argument
, NULL
, 'T' },
2190 { "unhide", optional_argument
, NULL
, 'U' },
2191 { "no-reread", no_argument
, NULL
, 160 },
2192 { "IBM", no_argument
, NULL
, 161 },
2193 { "leave-last", no_argument
, NULL
, 161 },
2194 /* undocumented flags - not all completely implemented */
2195 { "in-order", no_argument
, NULL
, 128 },
2196 { "not-in-order", no_argument
, NULL
, 129 },
2197 { "inside-outer", no_argument
, NULL
, 130 },
2198 { "not-inside-outer", no_argument
, NULL
, 131 },
2199 { "nested", no_argument
, NULL
, 132 },
2200 { "chained", no_argument
, NULL
, 133 },
2201 { "onesector", no_argument
, NULL
, 134 },
2202 { NULL
, 0, NULL
, 0 }
2205 /* default devices to list */
2206 static struct devd
{
2207 char *pref
, *letters
;
2209 { "hd", "abcdefgh" },
2216 is_ide_cdrom(char *device
) {
2217 /* No device was given explicitly, and we are trying some
2218 likely things. But opening /dev/hdc may produce errors like
2219 "hdc: tray open or drive not ready"
2220 if it happens to be a CD-ROM drive. So try to be careful.
2221 This only works since 2.1.73. */
2225 struct stat statbuf
;
2227 sprintf(buf
, "/proc/ide/%s/media", device
+5);
2228 procf
= fopen(buf
, "r");
2229 if (procf
!= NULL
&& fgets(buf
, sizeof(buf
), procf
))
2230 return !strncmp(buf
, "cdrom", 5);
2232 /* Now when this proc file does not exist, skip the
2233 device when it is read-only. */
2234 if (stat(device
, &statbuf
) == 0)
2235 return (statbuf
.st_mode
& 0222) == 0;
2240 void do_list(char *dev
, int silent
);
2241 void do_size(char *dev
, int silent
);
2242 void do_geom(char *dev
, int silent
);
2243 void do_fdisk(char *dev
);
2244 void do_reread(char *dev
);
2245 void do_change_id(char *dev
, char *part
, char *id
);
2246 void do_unhide(char **av
, int ac
, char *arg
);
2247 void do_activate(char **av
, int ac
, char *arg
);
2252 main(int argc
, char **argv
) {
2257 int opt_out_geom
= 0;
2263 char *activatearg
= 0;
2264 char *unhidearg
= 0;
2266 setlocale(LC_ALL
, "");
2267 bindtextdomain(PACKAGE
, LOCALEDIR
);
2268 textdomain(PACKAGE
);
2271 fatal(_("no command?\n"));
2272 if ((progn
= rindex(argv
[0], '/')) == NULL
)
2276 if (!strcmp(progn
, "activate"))
2277 activate
= 1; /* equivalent to `sfdisk -A' */
2278 #if 0 /* not important enough to deserve a name */
2279 else if (!strcmp(progn
, "unhide"))
2280 unhide
= 1; /* equivalent to `sfdisk -U' */
2285 while ((c
= getopt_long (argc
, argv
, short_opts
, long_opts
, NULL
)) != -1) {
2288 force
= 1; break; /* does not imply quiet */
2290 opt_out_geom
= 1; break;
2292 increment
= 1; break;
2294 case 'c' + PRINT_ID
:
2295 case 'c' + CHANGE_ID
:
2298 dump
= 1; /* fall through */
2300 opt_list
= 1; break;
2302 no_write
= 1; break;
2306 opt_size
= 1; break;
2308 set_format(*optarg
); break;
2313 show_extended
= 1; break;
2315 activatearg
= optarg
;
2316 activate
= 1; break;
2318 U
.cylinders
= atoi(optarg
); break;
2322 U
.heads
= atoi(optarg
); break;
2326 one_only
= atoi(optarg
); break;
2328 restore_sector_file
= optarg
; break;
2330 save_sector_file
= optarg
; break;
2332 opt_reread
= 1; break;
2334 U
.sectors
= atoi(optarg
); break;
2347 /* undocumented flags */
2349 partitions_in_order
= 1; break;
2351 partitions_in_order
= 0; break;
2353 all_logicals_inside_outermost_extended
= 1; break;
2355 all_logicals_inside_outermost_extended
= 0; break;
2357 boxes
= NESTED
; break;
2359 boxes
= CHAINED
; break;
2361 boxes
= ONESECTOR
; break;
2365 no_reread
= 1; break;
2367 leave_last
= 1; break;
2371 if (optind
== argc
&& (opt_list
|| opt_out_geom
|| opt_size
|| verify
)) {
2378 for(dp
= defdevs
; dp
-defdevs
< SIZE(defdevs
); dp
++) {
2381 sprintf(device
, "/dev/%s%c", dp
->pref
, *lp
++);
2382 if (!strcmp(dp
->pref
, "hd") && is_ide_cdrom(device
))
2388 if (opt_list
|| verify
)
2394 printf(_("total: %d blocks\n"), total_size
);
2399 if (optind
== argc
) {
2401 activate_usage(fdisk
? "sfdisk -A" : progn
);
2403 unhide_usage(fdisk
? "sfdisk -U" : progn
);
2408 if (opt_list
|| opt_out_geom
|| opt_size
|| verify
) {
2409 while (optind
< argc
) {
2411 do_geom(argv
[optind
], 0);
2413 do_size(argv
[optind
], 0);
2414 if (opt_list
|| verify
)
2415 do_list(argv
[optind
], 0);
2422 do_activate(argv
+optind
, argc
-optind
, activatearg
);
2426 do_unhide(argv
+optind
, argc
-optind
, unhidearg
);
2430 if ((do_id
& PRINT_ID
) != 0 && optind
!= argc
-2)
2431 fatal(_("usage: sfdisk --print-id device partition-number\n"));
2432 else if ((do_id
& CHANGE_ID
) != 0 && optind
!= argc
-3)
2433 fatal(_("usage: sfdisk --change-id device partition-number Id\n"));
2434 else if (optind
!= argc
-3 && optind
!= argc
-2)
2435 fatal(_("usage: sfdisk --id device partition-number [Id]\n"));
2436 do_change_id(argv
[optind
], argv
[optind
+1],
2437 (optind
== argc
-2) ? 0 : argv
[optind
+2]);
2441 if (optind
!= argc
-1)
2442 fatal(_("can specify only one device (except with -l or -s)\n"));
2447 else if (restore_sector_file
)
2448 restore_sectors(dev
);
2456 * H. Listing the current situation
2460 my_open (char *dev
, int rw
, int silent
) {
2463 mode
= (rw
? O_RDWR
: O_RDONLY
);
2464 fd
= open(dev
, mode
);
2465 if (fd
< 0 && !silent
) {
2467 fatal(_("cannot open %s %s\n"), dev
, rw
? _("read-write") : _("for reading"));
2473 do_list (char *dev
, int silent
) {
2475 struct disk_desc
*z
;
2477 fd
= my_open(dev
, 0, silent
);
2484 get_cylindersize(dev
, fd
, dump
? 1 : opt_list
? 0 : 1);
2485 get_partitions(dev
, fd
, z
);
2488 out_partitions(dev
, z
);
2491 if (partitions_ok(z
))
2492 warn(_("%s: OK\n"), dev
);
2499 do_geom (char *dev
, int silent
) {
2501 struct hd_geometry g
;
2503 fd
= my_open(dev
, 0, silent
);
2507 /* get_cylindersize(dev, fd, silent); */
2508 if (!ioctl(fd
, HDIO_GETGEO
, &g
))
2509 printf(_("%s: %d cylinders, %d heads, %d sectors/track\n"),
2510 dev
, g
.cylinders
, g
.heads
, g
.sectors
);
2512 printf(_("%s: unknown geometry\n"), dev
);
2515 /* for compatibility with earlier fdisk: provide option -s */
2517 do_size (char *dev
, int silent
) {
2521 fd
= my_open(dev
, 0, silent
);
2525 if(ioctl(fd
, BLKGETSIZE
, &size
)) {
2528 fatal(_("BLKGETSIZE ioctl failed for %s\n"), dev
);
2533 size
/= 2; /* convert sectors to blocks */
2535 /* a CDROM drive without mounted CD yields MAXINT */
2536 if (silent
&& size
== ((1<<30)-1))
2540 printf("%s: %9ld\n", dev
, size
);
2542 printf("%ld\n", size
);
2548 * Activate: usually one wants to have a single primary partition
2549 * to be active. OS/2 fdisk makes non-bootable logical partitions
2550 * active - I don't know what that means to OS/2 Boot Manager.
2552 * Call: activate /dev/hda 2 5 7 make these partitions active
2553 * and the remaining ones inactive
2554 * Or: sfdisk -A /dev/hda 2 5 7
2556 * If only a single partition must be active, one may also use the form
2557 * sfdisk -A2 /dev/hda
2559 * With "activate /dev/hda" or "sfdisk -A /dev/hda" the active partitions
2560 * are listed but not changed. To get zero active partitions, use
2561 * "activate /dev/hda none" or "sfdisk -A /dev/hda none".
2562 * Use something like `echo ",,,*" | sfdisk -N2 /dev/hda' to only make
2563 * /dev/hda2 active, without changing other partitions.
2565 * A warning will be given if after the change not precisely one primary
2566 * partition is active.
2568 * The present syntax was chosen to be (somewhat) compatible with the
2569 * activate from the LILO package.
2572 set_active (struct disk_desc
*z
, char *pnam
) {
2575 pno
= asc_to_index(pnam
, z
);
2576 z
->partitions
[pno
].p
.bootable
= 0x80;
2580 do_activate (char **av
, int ac
, char *arg
) {
2583 int rw
, i
, pno
, lpno
;
2584 struct disk_desc
*z
;
2588 rw
= (!no_write
&& (arg
|| ac
> 1));
2589 fd
= my_open(dev
, rw
, 0);
2592 get_cylindersize(dev
, fd
, 1);
2593 get_partitions(dev
, fd
, z
);
2595 if (!arg
&& ac
== 1) {
2596 /* list active partitions */
2597 for (pno
=0; pno
< z
->partno
; pno
++) {
2598 if (z
->partitions
[pno
].p
.bootable
) {
2599 lpno
= index_to_linux(pno
, z
);
2600 if (pno
== linux_to_index(lpno
, z
))
2601 printf("%s%d\n", dev
, lpno
);
2603 printf("%s#%d\n", dev
, pno
);
2604 if (z
->partitions
[pno
].p
.bootable
!= 0x80)
2605 warn(_("bad active byte: 0x%x instead of 0x80\n"),
2606 z
->partitions
[pno
].p
.bootable
);
2610 /* clear `active byte' everywhere */
2611 for (pno
=0; pno
< z
->partno
; pno
++)
2612 z
->partitions
[pno
].p
.bootable
= 0;
2614 /* then set where desired */
2617 else for(i
=1; i
<ac
; i
++)
2618 set_active(z
, av
[i
]);
2620 /* then write to disk */
2621 if(write_partitions(dev
, fd
, z
))
2622 warn(_("Done\n\n"));
2627 for (pno
=0; pno
< z
->partno
&& pno
< 4; pno
++)
2628 if (z
->partitions
[pno
].p
.bootable
)
2631 warn(_("You have %d active primary partitions. This does not matter for LILO,\n"
2632 "but the DOS MBR will only boot a disk with 1 active partition.\n"), i
);
2636 set_unhidden (struct disk_desc
*z
, char *pnam
) {
2640 pno
= asc_to_index(pnam
, z
);
2641 id
= z
->partitions
[pno
].p
.sys_type
;
2642 if (id
== 0x11 || id
== 0x14 || id
== 0x16 || id
== 0x17)
2645 fatal(_("partition %s has id %x and is not hidden\n"), pnam
, id
);
2646 z
->partitions
[pno
].p
.sys_type
= id
;
2650 * maybe remove and make part of --change-id
2653 do_unhide (char **av
, int ac
, char *arg
) {
2656 struct disk_desc
*z
;
2661 fd
= my_open(dev
, rw
, 0);
2664 get_cylindersize(dev
, fd
, 1);
2665 get_partitions(dev
, fd
, z
);
2667 /* unhide where desired */
2669 set_unhidden(z
, arg
);
2670 else for(i
=1; i
<ac
; i
++)
2671 set_unhidden(z
, av
[i
]);
2673 /* then write to disk */
2674 if(write_partitions(dev
, fd
, z
))
2675 warn(_("Done\n\n"));
2680 void do_change_id(char *dev
, char *pnam
, char *id
) {
2682 struct disk_desc
*z
;
2688 fd
= my_open(dev
, rw
, 0);
2691 get_cylindersize(dev
, fd
, 1);
2692 get_partitions(dev
, fd
, z
);
2694 pno
= asc_to_index(pnam
, z
);
2696 printf("%x\n", z
->partitions
[pno
].p
.sys_type
);
2699 i
= strtoul(id
, NULL
, 16);
2701 fatal(_("Bad Id %x\n"), i
);
2702 z
->partitions
[pno
].p
.sys_type
= i
;
2704 if(write_partitions(dev
, fd
, z
))
2705 warn(_("Done\n\n"));
2711 do_reread(char *dev
) {
2714 fd
= my_open(dev
, 0, 0);
2715 if(reread_ioctl(fd
))
2716 printf(_("This disk is currently in use.\n"));
2720 * I. Writing the new situation
2724 do_fdisk(char *dev
){
2727 struct stat statbuf
;
2728 int interactive
= isatty(0);
2729 struct disk_desc
*z
;
2731 if (stat(dev
, &statbuf
) < 0) {
2733 fatal(_("Fatal error: cannot find %s\n"), dev
);
2735 if (!S_ISBLK(statbuf
.st_mode
)) {
2736 warn(_("Warning: %s is not a block device\n"), dev
);
2739 fd
= my_open(dev
, !no_write
, 0);
2741 if(!no_write
&& !no_reread
) {
2742 warn(_("Checking that no-one is using this disk right now ...\n"));
2743 if(reread_ioctl(fd
)) {
2744 printf(_("\nThis disk is currently in use - repartitioning is probably a bad idea."
2745 "Umount all file systems, and swapoff all swap partitions on this disk."
2746 "Use the --no-reread flag to suppress this check.\n"));
2748 printf(_("Use the --force flag to overrule all checks.\n"));
2758 get_cylindersize(dev
, fd
, 0);
2759 get_partitions(dev
, fd
, z
);
2761 printf(_("Old situation:\n"));
2762 out_partitions(dev
, z
);
2764 if (one_only
&& (one_only_pno
= linux_to_index(one_only
, z
)) < 0)
2765 fatal(_("Partition %d does not exist, cannot change it\n"), one_only
);
2771 read_input(dev
, interactive
, z
);
2773 printf(_("New situation:\n"));
2774 out_partitions(dev
, z
);
2776 if (!partitions_ok(z
) && !force
) {
2778 fatal(_("I don't like these partitions - nothing changed.\n"
2779 "(If you really want this, use the --force option.)\n"));
2781 printf(_("I don't like this - probably you should answer No\n"));
2786 printf(_("Are you satisfied with this? [ynq] "));
2788 printf(_("Do you want to write this to disk? [ynq] "));
2789 answer
= c
= getchar();
2790 while (c
!= '\n' && c
!= EOF
)
2793 printf(_("\nsfdisk: premature end of input\n"));
2794 if (c
== EOF
|| answer
== 'q' || answer
== 'Q') {
2795 fatal(_("Quitting - nothing changed\n"));
2796 } else if (answer
== 'n' || answer
== 'N') {
2798 } else if (answer
== 'y' || answer
== 'Y') {
2801 printf(_("Please answer one of y,n,q\n"));
2808 if(write_partitions(dev
, fd
, z
))
2809 printf(_("Successfully wrote the new partition table\n\n"));
2813 reread_disk_partition(dev
, fd
);
2815 warn(_("If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)\n"
2816 "to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1\n"
2817 "(See fdisk(8).)\n"));
2819 sync(); /* superstition */