Creating partitions on GNULinux

There are many GNU applications that allow us to partition our drives, such as parted or gparted, this last one has graphical user interface (GUI)… But there’s one method, which is the one I like the most, as it is very versatile, which is using fdisk and mkfs commands. One for manipulate the disk partition table and the another for building the file system itself. It can even be implemented this way in remote systems.

We begin by opening a root console, or change to the root user as well.

su -

The next thing we need to do is listing all our devices detected in the system, in order to identify our new disk. We’ll use for that the command fdisk.

# fdisk --list
...
Disk /dev/sda: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: ST2000LM015-2E81
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
...

We enter the disk by fdisk to perform a partition table on it.

# fdisk /dev/sda

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xb28e0255.

Command (m for help): 

On fdisk we will be able to perform other interesting tasks, such as getting lists of partitions, see the free partitioning space, see and change to the different types of partitions that can be applied, etc. Let’s check the help, for now we just need to add a new partition (n) in the new disk and write table (w) to this.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

In my case, since I only need two partitions, they will be both primary. If more than four are needed, you should add an extended one.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

I’ll use default options to add a 100GB partition.

Select (default p): [Enter]

Using default response p.
Partition number (1-4, default 1): [Enter]
First sector (2048-3907029167, default 2048): [Enter]
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-3907029167, default 3907029167): +100G      

Created a new partition 1 of type 'Linux' and of size 100 GiB.

I’ll create the next partition using the remaining hard disk space.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): [Enter]

Using default response p.
Partition number (2-4, default 2): [Enter]
First sector (209717248-3907029167, default 209717248): [Enter]
Last sector, +/-sectors or +/-size{K,M,G,T,P} (209717248-3907029167, default 3907029167): [Enter]

Created a new partition 2 of type 'Linux' and of size 1.7 TiB.

Once the partitions have been created, we need to write them to the partition table (w).

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

We can check our newly created partitions with fdisk again.

# fdisk --list
...
Disk /dev/sda: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: ST2000LM015-2E81
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xb28e0255

Device     Boot     Start        End    Sectors  Size Id Type
/dev/sda1            2048  209717247  209715200  100G 83 Linux
/dev/sda2       209717248 3907029167 3697311920  1.7T 83 Linux
...

Create with mkfs an ext4 filesystem in each of the partitions.

# mkfs.ext4 /dev/sda1
# mkfs.ext4 /dev/sda2

Creating the mount points, where the new partitions will be allocated.

# mkdir /mnt/tools
# mkdir /mnt/backup

The next important thing is to change the owner of the mount points in order that you can write on them (user:group), since now the owner was the root user.

# chown -R miguel:miguel /mnt/backup/
# chown -R miguel:miguel /mnt/tools/

We mount the partitions on their mount points.

# mount -t auto /dev/sda1 /mnt/tools
# mount -t auto /dev/sda2 /mnt/backup

Finally, we add two lines in our fstab file in order to get our partitions mounted in the next system boot.

# nano /etc/fstab
#device        mountpoint             fstype    options  dump   fsck
/dev/sda1    /mnt/tools    ext4    defaults    0    1
/dev/sda2    /mnt/backup   ext4    defaults    0    1