Show more

Confused by the difference between primary, logical and extended drives? Yeah, me too. You know who isn’t? Gabe Stone, one of our Senior SRE Software Developers. He’s here to cut through the fog of partitioning for you. Read Part 1 here!


Just like Ahhhhnuld promised – I’m back!

Thanks for hanging in. I promise I’ll make it worth your while – not in a monetary sense, but rich in intellectual riches and fame. (Ok, maybe not the fame.)

If you recall from Part I,  we had some extra free space on our hard drive and we wanted to our Linux operating system to use it.

To do that, we needed to extend a partition on the drive, which to reiterate is essentially a container for data. Partitions must be present on a drive if you want to do anything useful with it.

In addition, the partition was a logical one, which is a little different from a “regular” partition in that you can have as many as you want, but you can’t boot an operating system off of one. (That is something only a primary (aka “regular”) partition can do.)

Now that you have the Cliffs Notes version of the previous article, let’s frame the rest of this one. To resize a logical partition:

  1. Modify the partition table
  2. Reboot your system
  3. Resize the logical “disk”
  4. Resize the partition on the logical “disk”
  5. Resize the file system on the partition

Oh that sounds easy. I’ll probably destroy my hard drive in the process. Why should I trust you?

Come on, I won’t lead you astray. Just don’t ask me how to fix your car.

So the first step will probably be the “worst” of it, and it really isn’t that bad. We’ll be using a utility called sfdisk to tell the operating system about the new partition size. So let’s get down to it, shall we?

We need the name of the disk that our logical partitions are on. The name of the device is usually something like /dev/sdX where X is a letter.

Remember that fdisk command you ran in the last part which listed each disk device and its partitions? Well it lists the name of each disk device (along with its partitions):

Disk /dev/sda: 931.53 GiB, 1000204886016 bytes, 1953525168 sectors

There ya go! The name of the disk is /dev/sda.

So now type (for example) sfdisk /dev/sda and press Enter. You’ll get a printout of that disk’s partition table (like you saw in fdisk) along with a >>> prompt at the bottom.

We need to tell the operating system about our new partition size, and to do that, we’ll be modifying the disk’s partition table, one line/partition at a time. Mostly we’ll just be redefining what is already in the table, except when we get to our extended partition. When we get to that spot, we’re going to be telling Linux that it needs to use the rest of the available space. So let’s begin with what you might see after you get into the sfdisk program:

Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xe59ec859

Old situation:

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 499711 497664 243M 83 Linux
/dev/sda2 501758 104855551 104353794 49.8G 5 Extended
/dev/sda5 501760 104855551 104353792 49.8G 83 Linux

Type 'help' to get more information.
>>>

So we have three partitions here. The first one is our boot partition, and we won’t be changing anything on that one, so we type:

2048,497664,83,*

Each one of these comma-delimited values corresponds to a column in the above table: (Start), (Sectors), (Id), (Boot). In other words, with this line, we are telling sfdisk to not change this partition at all by keeping the same start location, partition length, partition type and boot flag.

Press Enter. Now we get to our extended partition. Here’s where the rubber meets the information superhighway. Type the following:

501758,,5

See what we did there?

No, Einstein. Enlighten me.
My goodness. Get up on the wrong side this morning? Sheesh.

Anyway, for the extended partition:

  • First value: We are telling it to not change the start location of the partition (501758).
  • Second value: Leave it blank (,,) which translates to “use all the available space after that”.
  • Third value: We are telling it to keep the same partition type (extended).

There is no other value after that on this line since this partition isn’t bootable, just like the logical partitions it contains.

Press Enter. Then at the prompt, enter one more value and press Enter again:

501760

This line deals with that third partition which is the actual logical volume container which is within the extended partition. However, we can’t resize it yet, since we haven’t booted the operating system with the new table that will tell it that the extended partition has been enlarged. So here on this line, we basically tell Linux to not change the start of the logical container, in effect leaving everything else as it was there for now.

Now at the prompt, type quit and press Enter. It’ll ask you if you want to write to disk. If you think you made any kind of mistake hit “N” to back out. Otherwise, breathe, be confident, and hit that “Y” key!

Hard part’s over! The rest is gravy.

1. Reboot your system.

2. Login, sudo up, then type pvresize /dev/sdaX. Press Enter. This tells the operating system to resize the “disk” containing the logical partition(s) to the size that it sees for that underlying partition (Bingo! those were your sfdisk changes).

3. OK, so the extended partition has been enlarged, and then after that we resized the logical “disk” itself. Now we in essence need to get the logical partitions on that “disk” extended! We’ll use two utilities for this. First, type lvdisplay -v. Here’s some example output:

--- Logical volume ---
LV Path /dev/ubuntu-template-vg/root
LV Name root
VG Name ubuntu-template-vg
LV UUID uUbcvu-OZ3s-meW5-Ft1b-WjDh-6AWB-3wajJR
LV Write Access read/write
LV Creation host, time ubuntu-template, 2017-02-14 16:21:33 -0500
LV Status available
# open 1
LV Size 245.52 GiB
Current LE 62853
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

--- Logical volume ---
LV Path /dev/ubuntu-template-vg/swap_1
LV Name swap_1
VG Name ubuntu-template-vg
LV UUID 5uKEmo-rtcE-n0E4-ZXwo-nqfH-20Bj-VXqKee
LV Write Access read/write
LV Creation host, time ubuntu-template, 2017-02-14 16:21:33 -0500
LV Status available
# open 2
LV Size 4.00 GiB
Current LE 1024
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1

(yeah, I know they are called logical volumes here, but it is easier to think of them as partitions. Work with me here.)

What we need to note is that LV Path above, which is essentially where on our drive the operating system has designated as the location where the logical volume “lives” so it can be mounted (i.e. brought into the operating system for use). We don’t want the logical volume that has the path with the word swap in it (take my word for it), so the other logical volume/partition is the one we want. Note its LV Path/dev/ubuntu-template-vg/root.

Now we need to extend that bad boy by typing lvextend -l +100%FREE /dev/ubuntu-template-vg/root. That will extend the logical volume/partition to fill up 100% of the remaining space.

5. Drum roll… Finally, we just need to simply resize the file system on that logical volume/partition: resize2fs /dev/ubuntu-template-vg/root.

Woah, Bazinga! We’re actually done?!

That’s right my friend! Congratulations, you made it through, wiser, and with a bigger disk.

Thanks for journeying with me through PartitionLand. Kind of an unsettling place to visit to be sure, but I was honoured to be your guide. Now that’ll be 100 bucks, no checks without ID.

Similar posts

No Comments

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.