sfdisk
sfdisk
is a Scripted version of fdisk
It is part of util-linux
, just like fdisk
, so availability should be the same.
A partition table with a single partition that takes the whole disk can be
created with:
echo 'type=83' | sudo sfdisk /dev/sdX
and more complex partition tables are explained below.
To generate an example script, get the setup of one of your disks:
sudo sfdisk -d /dev/sda > sda.sfdisk
Sample output on my Lenovo T430 Windows 7 / Ubuntu dual boot:
label: dos
label-id: 0x7ddcbf7d
device: /dev/sda
unit: sectors
/dev/sda1 : start= 2048, size= 3072000, type=7, bootable
/dev/sda2 : start= 3074048, size= 195430105, type=7
/dev/sda3 : start= 948099072, size= 28672000, type=7
/dev/sda4 : start= 198504446, size= 749594626, type=5
/dev/sda5 : start= 198504448, size= 618891264, type=83
/dev/sda6 : start= 940277760, size= 7821312, type=82
/dev/sda7 : start= 817397760, size= 61437952, type=83
/dev/sda8 : start= 878837760, size= 61437500, type=83
Once you have the script saved to a file, you can apply it to sdX
with:
sudo sfdisk /dev/sdX < sda.sfdisk
For sfdisk
input, you can just omit the device names, and use lines of type:
start= 2048, size= 3072000, type=7, bootable
They are just ignored if present, and the device name is taken from the command line argument.
Some explanations:
- header lines: all optional:
partition lines:
start
: offset inside the disk at which the partition starts.
start
has very good defaults, and can often be ommited:
- on the first line,
start
is 2048, i.e. 1Mb (2048 + 512), which is a sane default for disk compatibility
- further
start
default to the first unallocated position
size
: man sfdisk
says: The default value of size indicates "as much as possible"
. So to fill the disk with a single partition use: /dev/sda : start=2048, type=83
type
: magic byte stored on the boot sector for each partition entry. Possible values: https://en.wikipedia.org/wiki/Partition_type On this example we observe:
7
(sda1
, 2
and 3
): filesystems that Windows supports. Preinstalled Windows stuff and Lenovo recovery partitions. sudo blkid
labels help identify them.
5
(sda4
): extended primary partition, which will contain other logical partitions (because we can only have 4 primary partitions with MBR)
83
(sda5
, 7
, and 8
): partitions which Linux supports. For me one home
, and two roots with different Ubuntu versions
82
(sd6
): swap
fdisk
can also read sfdisk
scripts with the I
command, which "sources" them during an interactive fdisk
session, allowing you further customization before writing the partition.
Tested on Ubuntu 16.04, sfdisk
2.27.1.
Format and populate the partitions an image file without sudo
This is a good way to learn to use sfdisk
without blowing up your hard disks: https://stackoverflow.com/questions/10949169/how-to-create-a-multi-partition-sd-disk-image-without-root-privileges/52850819#52850819
3I completely agree. Document your work for others to read. – Eric Duncan – 2015-11-26T13:55:02.200
1Note you have to have a leading tab on each line. – Stu – 2015-12-04T19:06:25.590
2Would you mind placing a big warning that a tab is required on each command character line as @Stu mentioned? I had to debug for a while just because copy-and-paste ignores tabs and replaces them with spaces, thus regex does not match with command lines. – ceremcem – 2016-03-27T01:30:12.327
9Instead of a warning about tabs, I've modified the sed to discard leading whitespace (not just tabs) and not expect a tab after the fdisk commands. This should cut and paste better. @ceremcem - good feedback and sorry to caused you any debug time. – user2070305 – 2016-04-10T15:15:50.327
3
sfdisk
is more automation friendly thanfdisk
– spuder – 2017-02-02T17:32:23.887I used this with success, right up until I happened to place a partition right where one already was, and fdisk wanted confirmation? Then I started using sfdisk which is just way easier and never asks for confirmation. Thanks to Ciro Santilli below. – TaborKelly – 2018-08-15T23:01:59.597