🌐 🇵🇱 Polski · 🇬🇧 EN
Disk partitioning is one of the many steps required when preparing a system for operation. In every operating system, physical disks are divided into logical partitions, providing space for data storage. There are also special types of partitions, such as RAID, which offer increased performance, redundancy, or both at once. LVM, similar to RAID, is a more advanced type of partition that allows for easier management by creating larger data storage spaces. The only point to keep in mind is to have a well-thought-out action plan before you start creating and organizing partitions. You should consider what partition size you will need in the future and how the filesystem will grow. How the data will be distributed. Planning will make system administration more pleasant and save you the pain associated with migrating partitions.
PRIMARY PARTITIONS
Working with disks is an important part of managing space for data storage in the system. Partitions define the amount of space for the system and its users. During system installation, there are automatic partition wizards, but you can always add new ones if the need arises. We have two different tools at our disposal for partitioning disks, these are :
# fdisk - Disk partitioning tool
# parted - Another disk partitioning tool
While "fdisk" is more common and frequently used, there is a gradual trend towards using "parted" more often because it is more flexible. You can also use the "cat" command on the "/proc" file containing system information. This file is created when the system starts and contains current information regarding its parameters.
8 0 20971520 hda
8 1 512000 hda1
8 2 20458496 hda2
8 16 8388608 hdb
8 32 8388608 hdc
8 48 8388608 hdd
If we are using SCSI instead of IDE disks, we need to slightly modify the command to the following form :
# cat /proc/partitions | grep sd
In the result, you can see different disks : hda, hdb, hdc, and hdd. The first two letters determine which disk we are dealing with : SCSI (sd) or IDE (hd). The next letter (a,b,c,d...) indicates which disk it is in order, and the digit at the end informs which partition it is on the given disk. Above in the example, you can see that we have 4 disks (a,b,c,d), where the first one (a) has 3 partitions (0,1,2). Let's start with the "fdisk" command.
fdisk command syntax: # fdisk [options] device
fdisk command options:
- -b Specify sector size on disk
- -h Specify head number on disk
- -l Current partition table
You can just as well use the second command : # parted
parted command syntax : # parted [options] device [options]
parted command options:
- -l - Partition table
- -s - Do not use prompt in interactions (use in scripts)
To see all partitions using the # parted command : # parted -l
CREATING PARTITIONS
FDISK
To begin, we will use "fdisk" to create a partition.
Step 1 - select disk : # fdisk /dev/hdb
Step 2 - See what options are available: "m" command for help
- a - toggle bootable flag
- b - edit bsd disk label
- c - toggle DOS compatibility flag
- d - delete a partition
- l - list known partition types
- m - display menu
- n - new partition
- o - create a new empty DOS partition table
- p - display partition table
- q - quit without saving changes
- s - create a new empty SUN disklabel
- t - change partition ID
- u - change display units
- v - verify partition table
- w - write table to disk and exit
- x - extra functionality (experts only)
As you can see, we have plenty of options to choose from when using the "fdisk" tool.
Step 3 - Using the "p" switch, let's see the current partition table
If we see the disks, we can select a new unused disk (added to increase space size) and start partitioning.
Step 4 - Create a new partition using the "n" option.
It is easiest, of course, to operate with size represented in megabytes. This way, the first partition will be created in a few seconds.
Step 5 - Create a second partition
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (549-1044, default 549):
Using default value 549
Last cylinder or +size or +sizeM or +sizeK (8322-16644, default16644):
Using default value 1044
For this partition, there is no need to specify its size; by default, all free space will be used to create it. When asked for the size, we don't enter anything and just confirm with ENTER.
Command (m for help): p
Disk /dev/hdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004b72d
Device Boot Start End Blocks Id System
/dev/hdb1 1 548 4401778+ 83 Linux
/dev/hdb2 549 1044 3984120 83 Linux
You can already see two created partitions with information about the blocks they occupy.
Step 7 - Save changes to disk.
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Now the two new partitions have been created and saved to the disk. You can verify their existence, but before you do, the kernel should create and read the new partition table to ensure that all disks and partitions are recognized as correct. For this purpose, we will use the command: "partprobe". Sometimes when we create partitions, the system displays a message that a system restart is needed after creating a partition, which is not always advisable in production environments, and here "partprobe" also works perfectly.
partprobe command syntax: # partprobe [options] [device_name]
partprobe command options:
- -d - For outdated information in the operating system
- -s - Display summary.
Step 8 - Run the partprobe command on the newly created disk.
# partprobe /dev/hdb in the result we will not see anything on the screen.
It is a good idea to run the partprobe command before the fdisk -l command. Partprobe will force a re-read of the current partition table and provide the latest information, which will prevent various inconsistencies or strange errors.
# fdisk -l
Disk /dev/hda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00021654
Device Boot Start End Blocks Id System
/dev/hda1 * 1 64 512000 83 Linux
/dev/hda2 64 2611 20458496 8e Linux LVM
Disk /dev/hdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004b72d
Device Boot Start End Blocks Id System
/dev/hdb1 1 548 4401778+ 83 Linux
/dev/hdb2 549 1044 3984120 83 Linux
Disk /dev/hdc: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/hdc doesn’t contain a valid partition table
Disk /dev/hdd: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00079351
Disk /dev/hdd doesn’t contain a valid partition table
PARTED
We have already created a partition using the "fdisk" command, now let's do it with the "parted" tool. We will create the same partitions on the third disk.
Step 1 - Invoke the "parted" tool for the next disk.
# parted /dev/hdc or if it is a SCSI disk # parted /dev/sdc
Step 2 - Overview of the possibilities offered by the "parted" tool
# help
(parted) help
check NUMBER do a simple check on the file
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to
another
help [COMMAND] prints general help, or help on
mklabel,mktable LABEL-TYPE create a new disklabel
(partition)
mkfs NUMBER FS-TYPE make a FS-TYPE file system on
mkpart PART-TYPE [FS-TYPE] START END make a partition
mkpartfs PART-TYPE FS-TYPE START END make a partition with a
file system
move NUMBER START END move partition NUMBER
name NUMBER NAME name partition NUMBER as NAME
print [free|NUMBER|all] display the partition table, or all
quit exit program
rescue START END rescue a lost partition near START
resize NUMBER START END resize partition NUMBER and its rm NUMBER delete partition NUMBER select DEVICE choose the device to
edit set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on
unit UNIT set the default unit to UNIT
NOTE
The process of creating a label for a disk using the parted tool causes data to be deleted from the disk. Do not use the parted tool for disk labels if there is any data on them.
Step 3 - Create a new partition (very similar to fdisk)
(parted) mkpart
Partition type? primary/extended? primary
File system type? [ext2]? ext4
Start? 0
End? 4294m
At this point, an error message may appear suggesting that such a setting is not the best for achieving optimal performance, but for now, we can ignore it. (here we are practicing the parted tool, not super configurations for corporations )
Step 4 - Create a second partition
(parted) mkpart
Partition type? primary/extended? primary
File system type? [ext2]? ext4
Start? 4294m
End? 8590m
Step 5 - Before saving changes, we can verify if everything is correct.
(parted) print
Model: VBOX HARDDISK (ide)
Disk /dev/hdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 512B 4294MB 4294MB primary
2 4295MB 8590MB 4295MB primary
Step 6 - Exit the tool and save changes
# (parted) quit
Step 7 - Update the partition table in the system kernel
# partprobe
Step 8 - Verify if all partitions were created correctly
# parted -l
Model: VBOX HARDDISK (ide)
Disk /dev/hda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 525MB 524MB primary ext4 boot
2 525MB 21.5GB 20.9GB primary lvm
Model: VBOX HARDDISK (ide)
Disk /dev/hdb: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 32.3kB 4507MB 4507MB primary
2 4507MB 8587MB 4080MB primary
Model: VBOX HARDDISK (ide)
Disk /dev/hdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 512B 4294MB 4294MB primary
2 4295MB 8590MB 4295MB primary
Error: Unable to open /dev/hdd - unrecognised disk label.
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1044, default 1):
Use default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1044, default1044): +4294M
Since the disk was divided in half using megabytes as the unit, you can use the syntax :
- +sizeM for megabytes
- +sizeK for kilobytes
- +size for cylinders
Comments