Linux: Working with Disks, fdisk and parted
-
During the installation of a Linux system, we assume that basic tasks such as creating a partition, formatting it, mounting it, etc. will be handled by the installation process itself. But after a system has been built we may still need to tackle these tasks and at the bottom of the storage layer tasks is the concept of drive partitioning and labelling and for this task we have a few standard tools, named fdisk and parted.
First some history. The long standing tool for handling Linux partitions has been fdisk. This is the tool that many of us "grew up with" so to speak and is venerable and well know, if a bit confusing. We expect it to exist on nearly any system that we run across and nearly everyone who manages Linux systems today is familiar with it. However, fdisk can only handle traditional MBR (Master Boot Record) based partition tables making it very limited (like 2TB and smaller systems and only those created with MBR.) The key replacement utility for fdisk is parted which can handle GPT partition tables.
Of course fdisk and parted are not the only games in town, there are many different tools for disk management, but these two are the most common and important. For new system administrators, learning parted is really all that is needed.
We will gloss over fdisk, but will address it later as an "advanced" topic for those needing to use it on limited systems or in special situations. Without going into unnecessary details, however, using fdisk is relatively straightforward. It is TUI (text user interface) drive with a full menu. All we need to do is supply it the device name of the drive that we want to manage with it and we can use its menu to handle the rest.
Assuming we want to work with the partitions on device
/dev/sdb
we would do this:fdisk /dev/sdb
And from there we would just use the menu to guide us. Once in fdisk hit h to see a menu. The fdisk utility must be understood that when changes are made, such as a new partition being created, that nothing is permanent until the utility is exited. If you quit before you are done, nothing is written to disk. You have to explicitly commit to disk and all changes are made at once.
In the latest version of CentOS, fdisk has experimental GPT support. It is not production ready yet but will be coming in the future so this tool may make a come back down the road.
Also of use from fdisk is a simple view of existing partitions. You can do with with the -l flag.
$ fdisk -l WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion. Disk /dev/vda: 21.5 GB, 21476933632 bytes, 41947136 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: gpt # Start End Size Type Name 1 2048 41945087 20G Linux filesyste primary
Very simple to use.
The more common, modern utility is parted and this is what we will be using most of the time, at least until new utilities take its place. Many people do not like parted and so replacements are often sought. Unlike fdisk, parted makes changes to the disk instantly so can be quite a bit more dangerous for those not aware of this.
We start parted on its own, no need to specify a disk.
# parted
Once in parted, we can select a disk that we want to work with:
(parted) select /dev/vda
Now we are looking at our
/dev/vda
disk drive. We can use theprint
command to see the current state of the drive:(parted) select /dev/vda Using /dev/vda (parted) print Model: Virtio Block Device (virtblk) Disk /dev/vda: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 21.5GB 21.5GB ext4 primary
The most common task in parted is creating partitions which you will do with the
mkpart
command inside of the tool. This command allows you to make a primary or extended partition and requires you to only provide the starting and the ending MB for it.(parted) mkpart primary 1024 32000
Getting used to parted can take time. Thankfully there are many online guides and you can simply type
help
at any time for more information. Exploring a menu driven tool completely here is not ideal as that has been covered thoroughly and, like many tasks, is one that would best be looked up and verified at the time of use. Most importantly, remember that changes to disk happen immediately and that you do not write the changes to disk after the fact as you do with fdisk.Part of a series on Linux Systems Administration by Scott Alan Miller