1 Examples of the use of sfdisk 3.0 (to partition a disk)
2 Input lines have fields <start>,<size>,<type>... - see sfdisk.8.
3 Usually no <start> is given, and input lines start with a comma.
5 Before doing anything with a disk, make sure it is not in use;
6 unmount all its file systems, and say swapoff to its swap partitions.
7 (The final BLKRRPART ioctl will fail if anything else still uses
8 the disk, and you will have to reboot. It is easier to first make
9 sure that nothing uses the disk, e.g., by testing:
12 BLKRRPART: Device or resource busy
13 * Device busy for revalidation (usage=2)
16 * sdb: sdb1 < sdb5 sdb6 > sdb3
18 Note that the starred messages are kernel messages, that may be
19 logged somewhere, or written to some other console.
20 In sfdisk 3.01 sfdisk automatically does this check, unless told not to.)
23 sfdisk /dev/hda << EOF
27 (If there was garbage on the disk before, you may get error messages
28 like: `ERROR: sector 0 does not have an msdos signature'
29 and `/dev/hda: unrecognized partition'. This does not matter
30 if you write an entirely fresh partition table anyway.)
33 -----------------------------------------------------------------------
37 Units = cylinders of 208896 bytes, blocks of 1024 bytes, counting from 0
39 Device Boot Start End #cyls #blocks Id System
40 /dev/hda1 0+ 1023 1024- 208895+ 83 Linux native
41 Successfully wrote the new partition table
43 -----------------------------------------------------------------------
44 Writing and rereading the partition table takes a few seconds -
45 don't be alarmed if nothing happens for six seconds or so.
48 2. Three primary partitions: two of size 50MB and the rest:
49 sfdisk /dev/hda -uM << EOF
54 -----------------------------------------------------------------------
56 Units = megabytes of 1048576 bytes, blocks of 1024 bytes, counting from 0
58 Device Boot Start End MB #blocks Id System
59 /dev/hda1 0+ 50- 51- 51203+ 83 Linux native
60 /dev/hda2 50+ 100- 51- 51204 83 Linux native
61 /dev/hda3 100+ 203 104- 106488 83 Linux native
62 Successfully wrote the new partition table
64 -----------------------------------------------------------------------
65 /dev/hda1 is one block (in fact only half a block) shorter than
66 /dev/hda2 because its start had to be shifted away from zero in
67 order to leave room for the Master Boot Record (MBR).
70 3. A 1MB OS2 Boot Manager partition, a 50MB DOS partition,
71 and three extended partitions (DOS D:, Linux swap, Linux):
72 sfdisk /dev/hda -uM << EOF
81 -----------------------------------------------------------------------
82 Device Boot Start End MB #blocks Id System
83 /dev/hda1 0+ 1- 2- 1223+ a OS/2 Boot Manager
84 /dev/hda2 1+ 51- 51- 51204 6 DOS 16-bit FAT >=32M
85 /dev/hda3 51+ 203 153- 156468 5 Extended
86 /dev/hda4 0 - 0 0 0 Empty
87 /dev/hda5 51+ 71- 21- 20603+ 4 DOS 16-bit FAT <32M
88 /dev/hda6 71+ 87- 17- 16523+ 82 Linux swap
89 /dev/hda7 87+ 203 117- 119339+ 83 Linux native
90 Successfully wrote the new partition table
91 hda: hda1 hda2 hda3 < hda5 hda6 hda7 >
92 -----------------------------------------------------------------------
93 All these rounded numbers look better in cylinder units:
95 -----------------------------------------------------------------------
96 Device Boot Start End #cyls #blocks Id System
97 /dev/hda1 0+ 5 6- 1223+ a OS/2 Boot Manager
98 /dev/hda2 6 256 251 51204 6 DOS 16-bit FAT >=32M
99 /dev/hda3 257 1023 767 156468 5 Extended
100 /dev/hda4 0 - 0 0 0 Empty
101 /dev/hda5 257+ 357 101- 20603+ 4 DOS 16-bit FAT <32M
102 /dev/hda6 358+ 438 81- 16523+ 82 Linux swap
103 /dev/hda7 439+ 1023 585- 119339+ 83 Linux native
104 -----------------------------------------------------------------------
105 But still - why does /dev/hda5 not start on a cylinder boundary?
106 Because it is contained in an extended partition that does.
107 Of the chain of extended partitions, usually only the first is
108 shown. (The others have no name under Linux anyway.) But
109 these additional extended partitions can be made visible:
110 % sfdisk -l -x /dev/hda
111 -----------------------------------------------------------------------
112 Device Boot Start End #cyls #blocks Id System
113 /dev/hda1 0+ 5 6- 1223+ a OS/2 Boot Manager
114 /dev/hda2 6 256 251 51204 6 DOS 16-bit FAT >=32M
115 /dev/hda3 257 1023 767 156468 5 Extended
116 /dev/hda4 0 - 0 0 0 Empty
118 /dev/hda5 257+ 357 101- 20603+ 4 DOS 16-bit FAT <32M
119 - 358 1023 666 135864 5 Extended
120 - 257 256 0 0 0 Empty
121 - 257 256 0 0 0 Empty
123 /dev/hda6 358+ 438 81- 16523+ 82 Linux swap
124 - 439 1023 585 119340 5 Extended
125 - 358 357 0 0 0 Empty
126 - 358 357 0 0 0 Empty
128 /dev/hda7 439+ 1023 585- 119339+ 83 Linux native
129 - 439 438 0 0 0 Empty
130 - 439 438 0 0 0 Empty
131 - 439 438 0 0 0 Empty
132 -----------------------------------------------------------------------
134 Why the empty 4th input line? The description of the extended partitions
135 starts after that of the four primary partitions.
136 You force an empty partition with a ",0" input line, but here all
137 space was divided already, so the fourth partition became empty
140 How did I know about 4,6,a,E,S? Well, E,S,L stand for Extended,
141 Swap and Linux. The other values are hexadecimal and come from
150 4 DOS 16-bit FAT <32M
152 6 DOS 16-bit FAT >=32M
153 7 OS/2 HPFS or QNX or Advanced UNIX
155 9 AIX boot or Coherent
160 4. Preserving the sectors changed by sfdisk.
161 % sfdisk -O save-hdd-partition-sectors /dev/hda
163 will write the sectors overwritten by sfdisk to file.
164 If you notice that you trashed some partition, you may
165 be able to restore things by
166 % sfdisk -I save-hdd-partition-sectors /dev/hda
169 5. Preserving some old partitions.
170 % sfdisk -N2 /dev/hda
172 will only change the partition /dev/hda2, and leave the rest
173 unchanged. The most obvious application is to change an Id:
174 % sfdisk -N7 /dev/hda
177 -----------------------------------------------------------------------
180 Device Boot Start End #cyls #blocks Id System
181 /dev/hda1 0+ 5 6- 1223+ a OS/2 Boot Manager
183 /dev/hda6 358+ 438 81- 16523+ 82 Linux swap
184 /dev/hda7 439+ 1023 585- 119339+ 83 Linux native
188 Device Boot Start End #cyls #blocks Id System
189 /dev/hda1 0+ 5 6- 1223+ a OS/2 Boot Manager
191 /dev/hda6 358+ 438 81- 16523+ 82 Linux swap
192 /dev/hda7 439+ 1023 585- 119339+ 63 GNU HURD
193 -----------------------------------------------------------------------
194 Note that changing a logical partition into an empty partition
195 will decrease the number of all subsequent logical partitions.
197 6. Deleting a partition.
198 At first I thought of having an option -X# for deleting partitions,
199 but there are several ways in which a partition can be deleted, and
200 it is probably better to handle this just as a general change.
201 % sfdisk -d /dev/hda > ohda
202 will write the current tables on the file `ohda'.
203 -----------------------------------------------------------------------
205 # partition table of /dev/hda
208 /dev/hda1 : start= 1, size= 40799, Id= 5
209 /dev/hda2 : start= 40800, size= 40800, Id=83
210 /dev/hda3 : start= 81600, size= 336192, Id=83
211 /dev/hda4 : start= 0, size= 0, Id= 0
212 /dev/hda5 : start= 2, size= 40798, Id=83
213 -----------------------------------------------------------------------
214 In order to delete the partition on /dev/hda3, edit this file
215 and feed the result to sfdisk again.
216 -----------------------------------------------------------------------
219 # partition table of /dev/hda
222 /dev/hda1 : start= 1, size= 40799, Id= 5
223 /dev/hda2 : start= 40800, size= 40800, Id=83
224 /dev/hda3 : start= 0, size= 0, Id= 0
225 /dev/hda4 : start= 0, size= 0, Id= 0
226 /dev/hda5 : start= 2, size= 40798, Id=83
227 % sfdisk /dev/hda < ohda
229 Units = cylinders of 208896 bytes, blocks of 1024 bytes, counting from 0
231 Device Boot Start End #cyls #blocks Id System
232 /dev/hda1 0+ 99 100- 20399+ 5 Extended
233 /dev/hda2 100 199 100 20400 83 Linux native
234 /dev/hda3 200 1023 824 168096 83 Linux native
235 /dev/hda4 0 - 0 0 0 Empty
236 /dev/hda5 0+ 99 100- 20399 83 Linux native
238 Units = sectors of 512 bytes, counting from 0
240 Device Boot Start End #sectors Id System
241 /dev/hda1 1 40799 40799 5 Extended
242 /dev/hda2 40800 81599 40800 83 Linux native
243 /dev/hda3 0 - 0 0 Empty
244 /dev/hda4 0 - 0 0 Empty
245 /dev/hda5 2 40799 40798 83 Linux native
246 Successfully wrote the new partition table
247 % sfdisk -l -V /dev/hda
249 Disk /dev/hda: 12 heads, 34 sectors, 1024 cylinders
250 Units = cylinders of 208896 bytes, blocks of 1024 bytes, counting from 0
252 Device Boot Start End #cyls #blocks Id System
253 /dev/hda1 0+ 99 100- 20399+ 5 Extended
254 /dev/hda2 100 199 100 20400 83 Linux native
255 /dev/hda3 0 - 0 0 0 Empty
256 /dev/hda4 0 - 0 0 0 Empty
257 /dev/hda5 0+ 99 100- 20399 83 Linux native
259 -----------------------------------------------------------------------
260 This is a good way of making changes: dump the current status
261 to file, edit the file, and feed it to sfdisk.
262 Preserving the file on some other disk could be useful:
263 if ever the MBR gets thrashed it can be used to restore