11

I have an Ubuntu Mate Trusty installation on VB which I want to migrate to a physical laptop. By following this guide i was able to get it up and running. Only issue now is I want to expand the current partition to fill up the rest of the disk, and it is encrypted and using LVM. Gparted doesn't support this type of operation, so I have to resort to the CLI. However, I don't understand anything about manually resizing partitions and all the guides I can find are either focused on shrinking the partition or are horribly outdated.

Can some partition guru provide a guide for me to follow?

Thanks

4 Answers 4

5

After fiddling around for a long time, and failing multiple times (thank god for backups) I was able to resize it. Here goes my guide.

This assumes you have a partition table like mine: sda1 is swap and around 250MB, sda2 is a container with sda5 (our crypt volume) and a lot of free space in front of sda2, like so

|sda1|sda2 (sda5)|free space|

Boot to a LiveCD of your favourite distro. Run gparted and shrink your sda1 partition by around 20MB. This is because fdisk requires a buffer of unallocated space between the start of sda2 and sda5, dunno why. While you're at it increase the size of sda2 as far as it can go, all the way to the right. You should end up with

|sda1|free space|sda2 (sda5)|

Go ahead and launch fdisk

sudo fdisk /dev/sda

press p to print out the current partition scheme. Write down where sda5 starts.

press d to delete first sda5 then sda2. Scary.

Now press n to create a new volume. Press e to select type extended and number it 2. The defaults for start and end should be ok.

Now create a new partition, type is Linux (should be default) and number it 5. The start value should be the same as the one you wrote down for sda5 before, the end value is the default one (as far as it can go).

Press p again to be sure that everything is OK and press w to write your changes to disk.

Reboot into your live CD again.

If you open up gparted you should now have

|sda1|sda2 (free space) (sda5)|

Follow these steps:

Decrypt your file system.

sudo cryptsetup luksOpen /dev/sda5 crypt1

Get the live CD to recognize (activate) your LVM.

sudo vgscan --mknodes
sudo vgchange -ay

Resize the Crypt.

sudo cryptsetup resize crypt1

Resize the (LVM) Physical Volume.

sudo pvresize /dev/mapper/crypt1

Resize your root (LVM) Logical Volume.

Unlock the (LVM) Physical Volume.

 sudo pvchange -x y /dev/mapper/crypt1

Resize the (LVM) Physical Volume.

 sudo lvresize -l +100%FREE /dev/ubuntu-vg/root

Re-lock the physical volume.

 sudo pvchange -x n /dev/mapper/crypt1

Resize the filesystem.

sudo e2fsck -f /dev/mapper/ubuntu--vg-root
sudo resize2fs -p /dev/mapper/ubuntu--vg-root

Now you should be OK to reboot and if everything went well you should have a bigger encrypted partition.

Note: This method has the downside of robbing you of around 20MB of swap. If the swap resize isn't done, fdisk won't let you write sda5 in the required position (which may lead to corruption, I suspect). This caveat is necessary until someone explains exactly what fdisk is doing behind the scenes and if there is any way to get it to do our bidding.

2
  • 1
    Perfect! Worked great for me - I cloned my laptop hard-drive to a larger one, and wanted to use the extra space. Only difference was mine boots with UEFI, so the partition is /dev/sda3 not sda5, so made changes accordingly. Also I did not seem to need to take the 20 MB out of sda2, it worked fine for me just changing /dev/sda3. Thanks!
    – seanlano
    Aug 28, 2017 at 12:28
  • Thanks! just 2 notes. Before starting, I had to resize the extended partition (sda2) that contains the encrypted one (sda5). I also skipped the 3rd before last command, did the last 2 resize command and just then locked using the command I skipped.
    – ozma
    Mar 4, 2019 at 20:44
4

I just found a much simpler solution, needing just gparted under a live system boot and then one more command. It works with Ubuntu 20.04 and up.

  1. Create full backups, because any messing around with partitions and LVM can really lead to full data losses.

  2. Boot from a live CD or installation medium. Because gparted cannot modify mounted disks.

  3. Start gparted.

  4. Decrypt. Right-click on the partition and select "Open Encryption".

  5. Resize. Right-click on the partition and select "Resize/Move". You can try to resize so that it includes all available space, but gparted may tell you that this is impossible because it would "allocate more sectors than available". In that case, leave ~10 MiB of free space at the end.

  6. Apply. Click the "Apply" button in gparted to actually do the resizing.

  7. Reboot to your installation. All remaining tasks affect only LVM volumes inside the now-resized partition, and can be done with the partition mounted.

  8. Determine the LV Path of the LVM volume you want to resize by looking at the output of sudo lvdisplay. Here, we assume it's /dev/ubuntu-vg/root, which is the default for the volume mounted to / in Ubuntu installations.

  9. Resize the LVM volume and its filesystem. For that, execute the following command, using the LV path you determined. It will automatically extend the volume to use all space available in the partition:

    sudo lvextend -l +100%FREE -r /dev/ubuntu-vg/root
    
3
  • 1
    For some reason, my password failed at the Open Encryption step when using GParted installed on an Ubuntu USB drive, but not when using GParted on a non-installed (live) Ubuntu USB drive. Nov 8, 2021 at 11:26
  • 1
    Thanks for this. I was able to follow these directions almost exactly to extend an encrypted volume in Proxmox. The only differences were: (1) extend the volume in PVE (qm resize {vmid} {deviceid} +16G) with the VM shut down; (2) I could run these from the VM directly, so no need to open encryption in gparted; (3) I had to resize the outer partition first, then the LVM partition (which did require the encryption pwd); (4) no need to restart between gparted and lvextend. Aug 28, 2022 at 19:48
  • it worked for me very well, steps are so much simpler than anything I've seen on this topic. It REALLY extends "cryptosetup" to encrypt new space unlike some another solution I've tried before.
    – Nishi
    Apr 16, 2023 at 16:36
0

After "cryptsetup resize" just didn't work I've got to this solution:

Make sure the /dev/sdX fits for your disk! Note that I am not sure yet if it will impact grub. Run from ubuntu live cd.

  • open gparted
  • resize /dev/sda2 to the desired size using gparted
  • decrypt:

    sudo cryptsetup luksOpen /dev/sda5 crypt1

  • resize /dev/sda5 to the desired size using gparted

You can make sure everything went okay by mounting and browsing your drive

sudo mount /dev/mapper/ubuntu--vg-root
-1

I'm trying to follow these instructions and I do have a concern. For me - /dev/sda5 is the extended volume and contains the OS and everything. Once you fdisk, delete and re-create the partitions, then write those changes to disk - does this not erase all that information?

I tried the method as listed, and when I reboot back into the live cd, and then get to the part of "sudo cryptsetup resize crypt1" - I get a message saying "Device crypt1 is not active" and nothing works after that.

I am trying this on a vbox instance, so at least I've been making backups but I have not been able to get it work properly yet.

What am I missing?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .