Managing hard disk partitions using fdisk in Linux
There are several partitions in a single hard drive even for the most basic installation of Linux. Linux provides tools like fdisk to set partitions. This is a command line tool that have the basic functions like view, set, modify and delete partitions on a disk. All devices in Linux are named in the /dev directory according to special files. A SATA hard disk is typically named /dev/sda, and to list the hard disks on your system, use the lshw command :
sudo lshw -class disk
This will list the hard drives and optical drives attached to the system.
In order to list non-interactive information of your first hard drive, use :
sudo fdisk -l /dev/sda
The output will typically look like this.
From the output, we can see that the first partition is the largest Linux partition, and named /dev/sda1, which is also a root or system partition since it is the only Linux partition. And an extended partition is named sda2, which can be separated into multiple logical partitions. While sda5 is the only logical partition in the extended partition and is used as swap space.
Create a new partition
To generate a new partition for the second disk (/dev/sdb), run fdisk in its interactive mode:
sudo fdisk /dev/sdb
Then, type m for help menu or p for the current partition list. And use n for a new primary partition.
Next, create a primary partition by enter p, then select a partition number and the size of the partition, which is 1 for this case. On the test system, sdb is 100GB, so we will enter +50GB for a 50GB partition. After saving it to the disk, type w to exit.
Deleting and setting the partition type
The partition will be deleted automatically by typing d. If you have many of them, the system will ask which partition to delete. In the event you made a mistake, just use q to quit without saving.
The partition type for Windows and Linux is different. Some systems use FAT rather than NTFS such as swap space and older version of Windows, while FreeBSD, OpenBSD or Mac OS X have their own partition ids.
Use the t command to change the partition number and partition code. You can type L to see the list again instead of entering the partition type.
Format and mount
After the partition is created, it needs to be formatted. It is best to format the partition with its native operating system like Windows for id 7. Use mkfs.ext3 or mkfs.ext4 commands for Linux:
sudo mkfs.ext4 /dev/sdb1
To mount the file system, use the command:
sudo mount /dev/sdb1 /home/annie/mediastore/
fdisk is a multipurpose tool but as always, it is better to backup your data beforehand. However, there are limitations such as that it is not designed for larger partitions.