How to Partition a Disk in Linux after Unmounting

How to Partition a Disk in Linux after Unmounting

Earlier, we’d taken a look at what it means to unmount a partition and what we need to do in order to achieve that. One of the reasons why we need to perform an action like unmounting is to partition a disk into several logical sections that can serve different purposes. Partitioning has many uses – for example, if you need to encrypt everything on a disk, you can create a separate partition for that very purpose. Or you can have a separate partition to use as an alternative boot. Or even another operation system!

There are many different uses for partitioning in Linux and in this tutorial, we’re going to see how to do a very simple job. Take an existing disk, and split it into two different primary partitions.

Splitting a Disk into Two Partitions

In this example, I’m going to partition one of the volumes attached to my Linux droplet from DigitalOcean. As you can see below, it’s currently mounted on the /mnt/partition-demo folder using the “lsblk” command:

Step 1: Unmount the Disk

Right now, the disk is one big 20G block with no subdivisions. The first thing to do is to unmount it. Check out the earlier tutorial here. Since it’s new, there are no processes using it and I can unmount it using the simple command:

umount /mnt/partition-demo

Once the partition is unmounted, we can use a number of utilities to do the job. I prefer to use “gdisk” in these examples as I find the interface a little less obtuse.

Step 2: Create a New Empty Partition Table

In the terminal, fire up the gdisk program:


It’ll ask you for the name of the device, and since I want to partition “sda”, I type in /dev/sda. This scans the existing partition table like this:

Since we’re going to split the disk into two, we’re going to have to wipe everything anyway. It’s easier to just create a brand new partition table. At the prompt, type “o” and confirm it:

Step 3: Create the First Partition

Once that’s done, we’re starting with a clean slate. In this example, I want to split the disk into two equal halves of 10GB each. To create the first partition, type “n” to create a new partition.

gdisk is smart enough to know that you probably want to create a new partition starting at the beginning of the disk. So it gives us the default partition number of “1” and you can just accept the default. Moreover, it gives us a suggestion for the first available sector (in this case 2048). We accept the defaults here as well:

Now we need to enter the last sector. Instead of calculating which sector corresponds to half the disk, gdisk allows us to specify the size of the partition instead. So I write “+10G” which tells gdisk that I want a last sector that’s 10GB away from the starting sector.

Next, we have to choose the filesystem for the disk. In this case, the current filesystem is “Linux filesystem” so we just leave it as it is. However, you can also type “L” to get the complete list of filesystems available and the GUID number corresponding to each. You can get the complete list of filesystems and the GUID codes halfway down this page. But like I said, in this case we don’t have to do anything.

We just press enter and our first partition is created!

Step 3: Create the Second Partition

We can verify our results so far by typing “i”. This will show us the results of the only existing partition right now – number 1. Like this:

We see that the last sector is “20973567” which corresponds to 10GB. Perfect. Now we create the second partition by pressing “n” again. This time our partition number is “2”. We see that the first available sector for partition 2 is one plus the last sector of partition 1. This makes perfect sense. Also, we want the second partition to finish off the remainder of the entire disk. So we just accept the default last sector value as well – basically expanding the second partition to use all available space. We choose the same Linux filesystem, press enter and we’re done!

Step 4: Write our Changes to Disk

So far all we’ve done is mess around with a temporary partition table. We haven’t actually written our changes and overwritten the existing disk partition. We do that now by typing “w” and confirming our changes:

It’s done now. No turning back!

Step 5: Verifying our Changes

We can now type “lsblk” at the command prompt and verify that our disk has been divided into two partitions as shown below:

And indeed we can see that the device “/dev/sda” has been split into “/dev/sda1” and “/dev/sda2”. Compare this to when we ran the command at the beginning of this tutorial. Note that these partitions haven’t been formatted or mounted, so we can’t use them just yet. But that’s another article altogether!

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclosure: We receive a compensation from some of the companies whose products are presented on our website.