/**/ How to create disk partitions in Linux? - Dextutor Linux

How to create disk partitions in Linux?

Partitioning a disk allows you to create separate sections within the disk inorder to manage your data more efficiently. For example, in Windows you have C, D or E drives. There are various uses of partitioning a disk like:
a) Allows to boot different OS from same disk
b) Allocate separate partition to different users
c) Creating separate area for OS virtual memory swapping
In this post we will cover how to create disk partitions in Linux using the fdisk utility.

How to create disk partitions in Linux?

Prerequisite

  1. root user access
  2. free disk

The process to create disk partitions

Step1: List the disks

Use the lsblk command to list the exiting disks along with partitions. An alternate command is fdisk -l.

How to create disk partitions in Linux? listing the disks

The output shows that two disks sdb and sdc are both un-partitioned whereas sda is partitioned into sda1 and sda2. Think of sda disk as your hard disk which is partitioned into C(sda1) and D(sda2) drive. All the devices are listed in /dev directory. So the path of these disks are /dev/sda, /dev/sdb, /dev/sdc and so on.

listed the /dev directory

Note: If you do not have a free disk like /dev/sdb or /dev/sdc, add a new virtual disk from your Virtual Box or VMWare software. As an alternate, you can run the below commands to add a disk temporarily.

#truncate -s 5g /tmp/myfile.trunc
#losetup /dev/loop1 /tmp/myfile.trunc

This will add a disk /dev/loop1 whose size is 5Gb. Keep in mind that on the next reboot, the disk and the created partition(s) will be lost.

Step2: Specify the disk to partition

As the root user run the fdisk command by specifying the disk name as an argument. I will use the /dev/sdc disk for partitioning.

#fdisk /dev/sdc

using fdisk o create disk partitions in Linux

Step3: Request a new partition

Press ‘n‘ to request for a new partition. On pressing “enter” key, the system will ask for partition type, either primary or extended. Press ‘p‘ for primary and ‘e‘ for extended partition. The default is primary. maximum 4 primary partitions can be created. If you intend to create more then create 3 primary partitions and 1 extended partition. Then this extended partition can be used as a container to create more logical partitions can be created.

Step4: Specify partition number, first sector and last sector

Next, assign a partition number. Then specify the size of partition. For size you need to tell two things: first sector and last sector. The best way is to choose the default value of First sector (manual specification may result in wastage of sectors if you are not aware about the details). Simple press “enter” key to choose the default starting sector. F theor Last sector you can either specify the sector or give the exact size. Its better that you specify the size in Mb or Gb as shown in the example below.

So, here I choose the size to be 1Gb and the partition is created.

Step5: Specify the partition type

The default partition type is “Linux” as shown in the previous figure. If you want to change it to something else then press the ‘t’ key. If you don’t know the type code then use the ‘L’ key to see a list of all the available types.

Step6: Save the Partition Table

Use the ‘w‘ command to save the partition table. It is must to do this otherwise the partition will not be created.

Step7: Initiate kernel re-read of partition table

Use the partprobe command to force a re-read of the partition table

Now, re-run the lsblk command and you will see the partition.

How to create disk partitions in Linux?


Creating File systems

A partition can not be used until a file system is applied over it. There are different file systems but the most popular ones are xfs and ext4. mkfs command can be used for the same

#mkfs.xfs /dev/sdc1
or
#mkfs -t xfs /dev/sdc1
mkfs command to apply file system


Mounting file system

Finally, to access the partition we need to attach it to the directory structure. So, first create a mount point, which is nothing but a directory using the mkdir command.

#mkdir /mydisk

There are two ways to mount: manually or persistently

Manually mounting the file system

Use the below command to manually mount the device or partition at the mount point

#mount /dev/sdc1 /mydisk

and start creating or accessing files as shown below.

However, on the next reboot, the file system will not be automatically mounted. For a persistent mount as an admin you need to make the entry in the /etc/fstab file

Persistently mounting the file system

To permanently mount a device at a mount point add an entry for that device in the /etc/fstab file. /etc/fstab is a white space-delimited file with six fields.

[root@localhost ~]# nano /etc/fstab

#in the file write

/dev/sdc1 /mydisk xfs defaults 0 0

The first filed is the device name, second is the mount point, third is the file system type, fourth field applies default mount settings. The last two fields are the dump flag and the fsck order.

Caution:

A wrong entry in /etc/fstab file my render the system unbootable. Hence, use the

#mount -a

command. If the command returns no error this means everything is perfect.

Related Topics to How to create disk partitions in Linux?