• *Wellcome Guest This Forum is created to help each other. Join us in our journey.

    * Guest Earn Piont Every Post, like, comment,and etc.

Sfdisk Tutorials

M

Mats Tage Axelsson

Guest
Partitioning is vital for system administration. This is the reason the partitioning software comes in so many variants. fdisk and cfdisk are made to be interactive. With parted, you can create everything with commands. Those are the most commonly used ones; sfdisk is not very common. It does have many features, but you can use it for scripts to a much higher degree. For a long time, sfdisk lagged behind on supporting GPT since version 2.26, it does support GPT.

UEFI


This program is still defaulting to MBR, so you have to explicitly state that you are using GPT.

Backing Up


Before you start working with your disk, you have to back up any of your important data to other media! This is an assumption that you must have a clear mind from the start. For making sure that you can restore your current state or implement it on another disk, you can dump the table.

$ sfdisk –dump /dev/sda > sda-tables.txt

The result goes, like text, straight to standard output. In the command above, the file is easy to read. You can also use this to put everything back on the disk. This is what it looks like.

label: gpt
label-id: C9247CFD-5AF7-4AB1-9F62-CDDDFCC12982
device: /dev/sda
unit: sectors
first-lba: 34
last-lba: 976773134
sector-size: 512
/dev/sda1 : start= 2048, size= 1021952, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B
/dev/sda2 : start= 1024000, size= 614400, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B,
name="EFI system partition"
/dev/sda3 : start= 1638400, size= 126093312, type=E6D6D379-F507-44C2-A23C-238F2A3DF928
/dev/sda9 : start= 623642624, size= 353130496, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
/dev/sda10 : start= 127731712, size= 303263744, type=E6D6D379-F507-44C2-A23C-238F2A3DF928
/dev/sda11 : start= 430995456, size= 192647168, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4

To bring it back or put it on a new disk, you use the restore option. More exactly, you just pipe this result into your next invocation of sfdisk.

$ sudo sfdisk /dev/sda < sda-tables.txt

Incidentally, this is a nice example of a correctly formatted command file for partitioning a disk. All parts are optional, this makes it possible to have another disk connected to the same machine and partition it the same. When you remove ‘dev/sdaX’ in the above file, you can partition any disk with the file.

Interactive Mode


During interactive mode, you need to know what you are doing. The opening display is sparse. You will see the type of disk and its size. When the disk is empty, you will NOT see the old situation, which makes it disconcerting to get started. Nevertheless, the procedure is strict. You have four values to put in for each partition separated by a comma, for default values, you just put the comma.

Before you start, create a label. This example is for a GPT partition.

$ echo "label: gpt" | sudo sfdisk /dev/sdc

This is the way Sfdisk is designed to run, but let’s start with interactive mode.

A simple partitioning runs.



$ sfdisk /dev/sdc
...
# The prompt changes to '>>>'
>>> ,350M, U
>>> ,10G, L
>>> ,,S
>>> write
# The result shows up. Confirm!

The data is written on the disk, and you can start formatting your partitions. As simple as this is, it is also error-prone. Using scripts is the main idea of sfdisk. Let’s go through the options and then the scripting language.

Setting disk label and partition labels


You can also use sfdisk with a command at a time. To do this, you use the parameters with dashes. Many of these commands, you can set with the script files. Setting the disk-label can be done in two ways, you saw one earlier in this tutorial.

$ sfdisk –label /dev/sdc gpt

This sets your disk to become a gpt disk. You have the option to stay with dos or more advisable, use the LegacyBIOSBootable flag for the gpt when you use hardware that does not support gpt. This is rare, so most likely, you will use this flag only for a memory stick that you want, to be able to boot even on old hardware.

You can also set labels for each partition. See the commands below.

$ sfdisk –part-label /dev/sdc 1 boot $ sfdisk –part-label /dev/sdc 2 home

Note the difference between part-label and disk-label. The disk-label only gives a supporting label for other software to use. The disk-label makes the whole disk, either gpt or dos.

Creating scripts


If you have chosen to use sfdisk, you probably have a reason to do so. Some of those reasons may be that you want to make many identical disks. Using fdisk, you can partition an entire disk with one command. Another reason may be that you want to make a new disk with the same scheme as the first one. The simple way to create a script is the dump command from earlier.

$ sfdisk –dump /dev/sdc

Using the output as a guide makes it easier to get started, just remember to check the documentation before doing anything rash. You can, for example, edit the file from before by removing the disk. In the example, the dump came from ‘/dev/sda’. If you remove that part, you still have a valid file.

start= 1024000, size= 614400, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B,
name="EFI system partition"

All other parts of the file are also acceptable as commands. Copy in or take away what you want and pipe it into sfdisk.

$ sfdisk /dev/sdc < Disk-tables.txt

Be careful! All commands will be run immediately. Although it will show you the resulting table and ask you to confirm the destruction of the disk you are working with.

Conclusion


This has been a short foray into the power and weakness of using the sfdisk program for your new disks. As you can see from this cursory examination, you can use this program for many things, especially if you have planned before what you need to do. There is also a great degree of freedom in adding sfdisk to scripts.

Credit to linux
 

Latest threads

Top