Examples of the use of sfdisk 3.0 (to partition a disk)
Input lines have fields <start>,<size>,<type>... - see sfdisk.8.
Usually no <start> is given, and input lines start with a comma.
Before doing anything with a disk, make sure it is not in use;
unmount all its file systems, and say swapoff to its swap partitions.
(The final BLKRRPART ioctl will fail if anything else still uses
the disk, and you will have to reboot. It is easier to first make
sure that nothing uses the disk, e.g., by testing:
% umount /dev/sdb1
% sfdisk -R /dev/sdb
BLKRRPART: Device or resource busy
* Device busy for revalidation (usage=2)
% swapoff /dev/sdb3
% sfdisk -R /dev/sdb
* sdb: sdb1 < sdb5 sdb6 > sdb3
%
Note that the starred messages are kernel messages, that may be
logged somewhere, or written to some other console.
In sfdisk 3.01 sfdisk automatically does this check, unless told not to.)
1. One big partition:
sfdisk /dev/hda << EOF
;
EOF
(If there was garbage on the disk before, you may get error messages
like: `ERROR: sector 0 does not have an msdos signature'
and `/dev/hda: unrecognized partition'. This does not matter
if you write an entirely fresh partition table anyway.)
The output will be:
-----------------------------------------------------------------------
Old situation:
...
New situation:
Units = cylinders of 208896 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hda1 0+ 1023 1024- 208895+ 83 Linux native
Successfully wrote the new partition table
hda: hda1
-----------------------------------------------------------------------
Writing and rereading the partition table takes a few seconds -
don't be alarmed if nothing happens for six seconds or so.
2. Three primary partitions: two of size 50MB and the rest:
sfdisk /dev/hda -uM << EOF
,50
,50
;
EOF
-----------------------------------------------------------------------
New situation:
Units = megabytes of 1048576 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End MB #blocks Id System
/dev/hda1 0+ 50- 51- 51203+ 83 Linux native
/dev/hda2 50+ 100- 51- 51204 83 Linux native
/dev/hda3 100+ 203 104- 106488 83 Linux native
Successfully wrote the new partition table
hda: hda1 hda2 hda3
-----------------------------------------------------------------------
/dev/hda1 is one block (in fact only half a block) shorter than
/dev/hda2 because its start had to be shifted away from zero in
order to leave room for the Master Boot Record (MBR).
3. A 1MB OS2 Boot Manager partition, a 50MB DOS partition,
and three extended partitions (DOS D:, Linux swap, Linux):
sfdisk /dev/hda -uM << EOF
,1,a
,50,6
,,E
;
,20,4
,16,S
;
EOF
-----------------------------------------------------------------------
Device Boot Start End MB #blocks Id System
/dev/hda1 0+ 1- 2- 1223+ a OS/2 Boot Manager
/dev/hda2 1+ 51- 51- 51204 6 DOS 16-bit FAT >=32M
/dev/hda3 51+ 203 153- 156468 5 Extended
/dev/hda4 0 - 0 0 0 Empty
/dev/hda5 51+ 71- 21- 20603+ 4 DOS 16-bit FAT <32M
/dev/hda6 71+ 87- 17- 16523+ 82 Linux swap
/dev/hda7 87+ 203 117- 119339+ 83 Linux native
Successfully wrote the new partition table
hda: hda1 hda2 hda3 < hda5 hda6 hda7 >
-----------------------------------------------------------------------
All these rounded numbers look better in cylinder units:
% sfdisk -l /dev/hda
-----------------------------------------------------------------------
Device Boot Start End #cyls #blocks Id System
/dev/hda1 0+ 5 6- 1223+ a OS/2 Boot Manager
/dev/hda2 6 256 251 51204 6 DOS 16-bit FAT >=32M
/dev/hda3 257 1023 767 156468 5 Extended
/dev/hda4 0 - 0 0 0 Empty
/dev/hda5 257+ 357 101- 20603+ 4 DOS 16-bit FAT <32M
/dev/hda6 358+ 438 81- 16523+ 82 Linux swap
/dev/hda7 439+ 1023 585- 119339+ 83 Linux native
-----------------------------------------------------------------------
But still - why does /dev/hda5 not start on a cylinder boundary?
Because it is contained in an extended partition that does.
Of the chain of extended partitions, usually only the first is
shown. (The others have no name under Linux anyway.) But
these additional extended partitions can be made visible:
% sfdisk -l -x /dev/hda
-----------------------------------------------------------------------
Device Boot Start End #cyls #blocks Id System
/dev/hda1 0+ 5 6- 1223+ a OS/2 Boot Manager
/dev/hda2 6 256 251 51204 6 DOS 16-bit FAT >=32M
/dev/hda3 257 1023 767 156468 5 Extended
/dev/hda4 0 - 0 0 0 Empty
/dev/hda5 257+ 357 101- 20603+ 4 DOS 16-bit FAT <32M
- 358 1023 666 135864 5 Extended
- 257 256 0 0 0 Empty
- 257 256 0 0 0 Empty
/dev/hda6 358+ 438 81- 16523+ 82 Linux swap
- 439 1023 585 119340 5 Extended
- 358 357 0 0 0 Empty
- 358 357 0 0 0 Empty
/dev/hda7 439+ 1023 585- 119339+ 83 Linux native
- 439 438 0 0 0 Empty
- 439 438 0 0 0 Empty
- 439 438 0 0 0 Empty
-----------------------------------------------------------------------
Why the empty 4th input line? The description of the extended partitions
starts after that of the four primary partitions.
You force an empty partition with a ",0" input line, but here all
space was divided already, so the fourth partition became empty
automatically.
How did I know about 4,6,a,E,S? Well, E,S,L stand for Extended,
Swap and Linux. The other values are hexadecimal and come from
the table:
% sfdisk -T
Id Name
0 Empty
1 DOS 12-bit FAT
2 XENIX root
3 XENIX usr
4 DOS 16-bit FAT <32M
5 Extended
6 DOS 16-bit FAT >=32M
7 OS/2 HPFS or QNX or Advanced UNIX
8 AIX data
9 AIX boot or Coherent
a OS/2 Boot Manager
...
4. Preserving the sectors changed by sfdisk.
% sfdisk -O save-hdd-partition-sectors /dev/hda
...
will write the sectors overwritten by sfdisk to file.
If you notice that you trashed some partition, you may
be able to restore things by
% sfdisk -I save-hdd-partition-sectors /dev/hda
%
5. Preserving some old partitions.
% sfdisk -N2 /dev/hda
...
will only change the partition /dev/hda2, and leave the rest
unchanged. The most obvious application is to change an Id:
% sfdisk -N7 /dev/hda
,,63
%
-----------------------------------------------------------------------
Old situation:
Device Boot Start End #cyls #blocks Id System
/dev/hda1 0+ 5 6- 1223+ a OS/2 Boot Manager
...
/dev/hda6 358+ 438 81- 16523+ 82 Linux swap
/dev/hda7 439+ 1023 585- 119339+ 83 Linux native
New situation:
Device Boot Start End #cyls #blocks Id System
/dev/hda1 0+ 5 6- 1223+ a OS/2 Boot Manager
...
/dev/hda6 358+ 438 81- 16523+ 82 Linux swap
/dev/hda7 439+ 1023 585- 119339+ 63 GNU HURD
-----------------------------------------------------------------------
Note that changing a logical partition into an empty partition
will decrease the number of all subsequent logical partitions.
6. Deleting a partition.
At first I thought of having an option -X# for deleting partitions,
but there are several ways in which a partition can be deleted, and
it is probably better to handle this just as a general change.
% sfdisk -d /dev/hda > ohda
will write the current tables on the file `ohda'.
-----------------------------------------------------------------------
% cat ohda
# partition table of /dev/hda
unit: sectors
/dev/hda1 : start= 1, size= 40799, Id= 5
/dev/hda2 : start= 40800, size= 40800, Id=83
/dev/hda3 : start= 81600, size= 336192, Id=83
/dev/hda4 : start= 0, size= 0, Id= 0
/dev/hda5 : start= 2, size= 40798, Id=83
-----------------------------------------------------------------------
In order to delete the partition on /dev/hda3, edit this file
and feed the result to sfdisk again.
-----------------------------------------------------------------------
% emacs ohda
% cat ohda
# partition table of /dev/hda
unit: sectors
/dev/hda1 : start= 1, size= 40799, Id= 5
/dev/hda2 : start= 40800, size= 40800, Id=83
/dev/hda3 : start= 0, size= 0, Id= 0
/dev/hda4 : start= 0, size= 0, Id= 0
/dev/hda5 : start= 2, size= 40798, Id=83
% sfdisk /dev/hda < ohda
Old situation:
Units = cylinders of 208896 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hda1 0+ 99 100- 20399+ 5 Extended
/dev/hda2 100 199 100 20400 83 Linux native
/dev/hda3 200 1023 824 168096 83 Linux native
/dev/hda4 0 - 0 0 0 Empty
/dev/hda5 0+ 99 100- 20399 83 Linux native
New situation:
Units = sectors of 512 bytes, counting from 0
Device Boot Start End #sectors Id System
/dev/hda1 1 40799 40799 5 Extended
/dev/hda2 40800 81599 40800 83 Linux native
/dev/hda3 0 - 0 0 Empty
/dev/hda4 0 - 0 0 Empty
/dev/hda5 2 40799 40798 83 Linux native
Successfully wrote the new partition table
% sfdisk -l -V /dev/hda
Disk /dev/hda: 12 heads, 34 sectors, 1024 cylinders
Units = cylinders of 208896 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/hda1 0+ 99 100- 20399+ 5 Extended
/dev/hda2 100 199 100 20400 83 Linux native
/dev/hda3 0 - 0 0 0 Empty
/dev/hda4 0 - 0 0 0 Empty
/dev/hda5 0+ 99 100- 20399 83 Linux native
/dev/hda: OK
-----------------------------------------------------------------------
This is a good way of making changes: dump the current status
to file, edit the file, and feed it to sfdisk.
Preserving the file on some other disk could be useful:
if ever the MBR gets thrashed it can be used to restore
the old situation.