5

my disk is encrypted, but boot partition is by default 512MB how to increase boot partition?

also how to increase boot partition during fresh new install?

ubuntu 20.04

1 Answers1

0

Background

I originally installed Ubuntu 20.04, which unfortunately has a bug in that it creates a /boot partition that is much too small. Any time I would install updates, I would run into errors because /boot wouldn't have enough space in it.

Caveats

I just did this ... but I don't recommend it. I'm rather technical, but it was very time consuming and at various points I was afraid I had completely wiped my data. I also wasn't able to find a current reference for how to do this.

Having said that, while I've never worked with encryption, I do think it was made easier by having some previous knowledge of LVMs and partitioning. It was also made easier by the fact that GParted did what I think is the most dangerous part: resizing the partition.

I'll try to add explanations as much as possible but if you don't understand the steps, again, I would recommend instead backing up your data and doing a clean install of Ubuntu (22.04 or later so the bug is fixed). It would be much easier.

Most of the steps below are specific to the system I ran them an and will need to be updated for your system, and they may contain errors as I typed them from another computer and was unable to copy and paste.

Steps

  1. (Recommended) Clean up disk space

    This will make some of the process quicker as there will be less data to back up and less to move

    I like to use Disk Usage Analyzer:

    sudo apt install baobab
    
  2. Back up everything

    ⚠️ This is very risky and there's a strong possibility you'll lose all of your data. Back up anything you want to keep.

  3. Open GParted (install if necessary) and identify partitions

    For example, this is what I see:

    /dev/nvme0n1p1 fat32 /boot/efi
    /dev/nvme0n1p2 ext4 /boot
    /dev/nvme0n1p3 [Encypted] lvm2 pv vgubuntu

    You can also use lsblk, e.g.

    nvme0n1           259:0    0 953.9G  0 disk
    ├─nvme0n1p1       259:1    0   512M  0 part  /boot/efi
    ├─nvme0n1p2       259:2    0   732M  0 part  /boot
    └─nvme0n1p3       259:3    0 952.7G  0 part
      └─nvme0n1p3_crypt
                      252:0    0 952.6G  0 crypt
        ├─vgubuntu-root
        │             252:1    0 920.6G  0 lvm   /
        └─vgubuntu-swap_1
                      252:2    0    32G  0 lvm   [SWAP]
    
  4. Create a live USB installation of Ubuntu and boot to it

  5. (Optional) List partitions with lsblk

    nvme0n1           259:0    0 953.9G  0 disk
    ├─nvme0n1p1       259:1    0   512M  0 part
    ├─nvme0n1p2       259:2    0   732M  0 part
    └─nvme0n1p3       259:3    0 952.7G  0 part
    
  6. Open the encrypted partition

    sudo cryptsetup luksOpen /dev/nvme0n1p3 nvme0n1p3_crypt
    

    ⓘ This allows modifying the LVM inside the encrypted partition

  7. List partitions again with lsblk

    Confirm you can see the partitions inside the encrypted partition (everything under nvme0n1p3 in this case):

    nvme0n1           259:0    0 953.9G  0 disk
    ├─nvme0n1p1       259:1    0   512M  0 part
    ├─nvme0n1p2       259:2    0   732M  0 part
    └─nvme0n1p3       259:3    0 952.7G  0 part
      └─nvme0n1p3_crypt
                      252:0    0 952.6G  0 crypt
        ├─vgubuntu-root
        │             252:1    0 920.6G  0 lvm
        └─vgubuntu-swap_1
                      252:2    0    32G  0 lvm
    
  8. Activate LVM volumes

    sudo vgscan --mknodes
    sudo vgchange -ay
    
  9. Make sure the volume group and logical volumes are visible

    sudo pvdisplay
    sudo lvdisplay
    
  10. Calculate the new size of the root volume

    The /boot partition should be 2.0 GB and is currently 732 MB (from my example), so I want to increase the size of the /boot partition by 1.3 and decrease the size of the root partition by the same amount, so the final size of the root partition will be 919.3 GB

  11. Shrink the contents of the encrypted partition

    ⓘ We'll be shrinking the file system, then shrinking the logical volume, then shrinking the physical volume, shrinking the LUKS, all so we can finally shrink the physical partition in a later step

    1. Check filesystem integrity, e.g.

      sudo e2fsck -f /dev/mapper/vgubuntu-root
      
    2. Resize the filesystem

      For example:

      sudo resize2fs /dev/mapper/vgubuntu-root 827G
      

      ⓘ To be save, use a size 90% smaller than the size you calculated above. This allows margin for error, and we will then expand the filesystem after to fill all available space.

    3. Check the filesystem again, e.g.

      sudo e2fsck -f /dev/mapper/vgubuntu-root
      
    4. Resize the logical volume

      Again, to be safe, we'll us a size smaller than we need for now and resize it later to fill the whole physical volume. This time we'll use a size 95% smaller:

      sudo lvreduce -L 873G /dev/mapper/vgubuntu-root
      
    5. Expand the filesystem to fit the logical volume, e.g.

      sudo resize2fs /dev/mapper/vgubuntu-root
      
    6. Shrink the physical volume

      1. List the logical volumes again

        sudo lvdisplay
        
      2. Note the size of the swap partition

        In my case, the swap partition was 32.00 GiB

      3. Remove the swap partition

        sudo lvremove /dev/vgubuntu/swap_1
        

        ⓘ This is necessary because we shrank the root partition, leaving empty space in the volume group between the root partition and the swap partition

      4. Shrink the physical volume

        As above, we'll use a smaller size to be safe. Pick a size between the size of the logical volume we just resized and the final desired size, which in this example would be 951.34 GiB (919.34 GiB plus 32.00 GiB for swap)

        sudo pvresize --setphysicalvolumesize 900G /dev/mapper/nvme0n1p3_crypt
        

        Note the output, e.g. "Pretending size is 1887436800 not 1997826048 sectors."

    7. Close the LVM

      sudo vgchange -an
      
    8. Close the encrypted volume

      sudo cryptsetup luksClose nvme0n1p3_crypt
      
  12. Resize LUKS mapping

    ⚠️ I'm not actually sure if this part is necessary (https://unix.stackexchange.com/a/416356)

    Again, we're going to play it safe here. We're going to pick a number between the numbers of the last command, so (1887436800+1997826048)/2 = 1942631424

    sudo cryptsetup -b 1942631424 resize nvme0n1p3_crypt
    
  13. Resize the encrypted partition

    1. Open GParted

    2. Figure out how much to shrink the partition

      1. My boot partition is 732.00 MiB and I want it to be 2 GB, so 2048 - 732 = 1316 MiB
    3. Right-click the encrypted partition > Open Encryption

    4. Right-click the encrypted partition > Resize/Move

    5. Set the new size > Resize

    6. Click the green Apply all operations button

    7. Right-click the partition again > Deactivate

    8. Right-click the partition again > Close Encryption

  14. Move the encrypted partition

    1. In GParted, right-click the partition again > Resize/Move

    2. Adjust the partition so all the free space is at the beginning of the partition > Resize/Move

    3. Click the green Apply all operations button

      ⓘ This will take a while

  15. Resize the boot partition

    1. In GParted, right-click the boot partition > Resize/Move

    2. Expand it to fill all of the available free space > Resize/Move

    3. Click the green Apply all operations button

    4. Exit GParted

  16. Resize everything in the encrypted partition to fill the available space

    ⓘ We'll be doing the opposite of what we did before: grow the physical volume, grow the logical volume, then grow the filesystem

    1. Open the encrypted partition

      sudo cryptsetup luksOpen /dev/nvme0n1p3 nvme0n1p3_crypt
      
    2. (Optional) Check the status of the encrypted partition

      sudo cryptsetup status nvme0n1p3_crypt
      

      It should have automatically resized to the new size of the partition

    3. Resize the physical volume to fill the available space

      sudo pvresize /dev/mapper/nvme0n1p3_crypt
      
    4. Resize the root logical volume

      Take the size of the physical volume (951.3 GiB in our example) and subtract the size of the swap you will create (32.00 GiB in our example, so 919.3 GiB), and use that to resize the logical volume, e.g.

      sudo lvresize -L 919.3G /dev/vgubuntu/root
      
    5. Re-create the swap logical volume

      We'll tell it to use all of the available free space in the volume group:

      sudo lvcreate -l 100%FREE -n swap_1 vgubuntu
      
    6. Format the swap partition

      sudo mkswap /dev/mapper/vgubuntu-swap_1
      
    7. Check the filesystem again, e.g.

      sudo e2fsck -f /dev/mapper/vgubuntu-root
      
    8. Expand the filesystem to fit the logical volume

      sudo resize2fs /dev/mapper/vgubuntu-root
      
    9. Check the filesystem again, e.g.

      sudo e2fsck -f /dev/mapper/vgubuntu-root
      
    10. Close the LVM

      sudo vgchange -an
      
    11. Close the encrypted volume

      sudo cryptsetup luksClose nvme0n1p3_crypt
      
  17. Reboot and pray your system still works and you don't need to restore from backup

References

bmaupin
  • 5,371