27

I recently upgraded my disk from a 128GB SSD to 512GB SSD. The / partition is encrypted with LUKS. I'm looking for help extending the partition to use all the free space on the new disk. I've already dd'd the old drive onto the new one:

[root@localhost ~]# fdisk -l /dev/sda
Disk /dev/sda: 477 GiB, 512110190592 bytes, 1000215216 sectors
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: 0x00009f33

Device     Boot   Start       End   Sectors   Size Id Type
/dev/sda1  *       2048   1026047   1024000   500M 83 Linux
/dev/sda2       1026048 250064895 249038848 118.8G 83 Linux

There's about 380GB of unused space after sda2.

More relevant info:

[root@localhost ~]# vgs
  VG             #PV #LV #SN Attr   VSize   VFree
  fedora_chocbar   1   3   0 wz--n- 118.75g 4.00m

[root@localhost ~]# lvs
  LV   VG             Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home fedora_chocbar -wi-a----- 85.55g                                                    
  root fedora_chocbar -wi-a----- 29.30g                                                    
  swap fedora_chocbar -wi-a-----  3.89g

[root@localhost ~]# pvs
  PV                    VG             Fmt  Attr PSize   PFree
  /dev/mapper/encrypted fedora_chocbar lvm2 a--  118.75g 4.00m

There seems to be a lot of info regarding how to do this, but very little explanation. I appreciate any help on this.

43

OK! The definitive answer finally. My steps to expand a LUKS encrypted volume...

  1. cryptsetup luksOpen /dev/sda2 crypt-volume to open the encrypted volume.
  2. parted /dev/sda to extend the partition. resizepart NUMBER END.
  3. vgchange -a n fedora_chocbar. Stop using the VG so you can do the next step.
  4. cryptsetup luksClose crypt-volume. Close the encrypted volume for the next steps.
  5. cryptsetup luksOpen /dev/sda2 crypt-volume. Open it again.
  6. cryptsetup resize crypt-volume. Will automatically resize the LUKS volume to the available space.
  7. vgchange -a y fedora_chocbar. Activate the VG.
  8. pvresize /dev/mapper/crypt-volume. Resize the PV.
  9. lvresize -l+100%FREE /dev/fedora_chocbar/home. Resize the LV for /home to 100% of the free space.
  10. e2fsck -f /dev/mapper/fedora_chocbar-home. Throw some fsck magic at the resized fs.
  11. resize2fs /dev/mapper/fedora_chocbar-home. Resize the filesystem in /home (automatically uses 100% free space)

I hope someone else finds this useful. I now have 300+GB for my test VMs on my laptop!

| improve this answer | |
  • 1
    Thanks, this was very helpful, it should be the accepted answer! – springloaded Aug 10 '17 at 15:48
  • 1
    The above also worked great for me. I had one step in there, let's call it step 7.5 where I had to unlock my physical volume: sudo pvchange -x y /dev/mapper/crypt-volume (via ubuntu docs help.ubuntu.com/community/ResizeEncryptedPartitions) – four43 Feb 27 '18 at 5:04
7

For those that come to the answer to find out how to simply resize a LUKS partition to the size of the resized container, the commands are as follows:

  • with LUKS encrypted volume opened and the opened volume mapped as opened-volume, execute

    sudo cryptsetup resize /dev/mapper/opened-volume
    
  • then resize the file system. If it is Ext4, you can resize a mounted file system

    sudo resize2fs /dev/mapper/opened-volume
    

I did both commands with a mounted file system with no interruption; it was possible to resize the container without unmounting it first because the encrypted volume was on a LVM logical volume (using lvresize)!

| improve this answer | |
1

Since this appears not to be a LVM specific question I want to mention that I successfully moved and extended one of my LUKS partitions 100GiB forward(!) with GParted on Fedora 28. It could not be done in Ubuntu 18.04 though and every other solution looked like a hassle of dd'ing around or backup and restore (I made a backup anyway). The LUKS container is automatically extended – IIRC GParted informs you about this – the only thing left is to extend the filesystem, so I had to do fsck.ext4 -f and resize2fs in my case.

A big thank you to the GParted developers.

| improve this answer | |
1

I found a much easier solution. Run Xubuntu live, install and launch partitionmanager. It can very well handle encrypted partitions.

Complete details are here: http://e1z.ca/devlog/encrypted_partition_resize.html

| improve this answer | |
0

Resize from command line with parted without LVM (on an ms-dos partition table for added fun).

I expanded my VPS from 50G to 150G.

Crypttab reads:

# cat /etc/crypttab 
vda5_crypt UUID=c5e67d21-6af4-4d55-a4bc-2978e50c00c3 none luks
vda6_crypt /dev/vda6 /dev/urandom cipher=aes-xts-plain64,size=256,swap

Sizes:

# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/vda5_crypt   47G   12G   35G  26% /
/dev/vda1               641M   63M  532M  11% /boot

The partition layout is:

# parted /dev/vda print                                                        
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 161GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  700MB   699MB   primary   ext4         boot
 2      701MB   53.7GB  53.0GB  extended
 5      701MB   51.7GB  51.0GB  logical
 6      51.7GB  53.7GB  1985MB  logical

As you can see my swap partition (partition 6) is in the way; i need to remove it to expand the partition. First switch off swap:

swapoff -a && free -lm

The 'Swap:' line should read all zeros.

Now, the tricky bit is to resize the partitions and restore them before rebooting and expanding (parted will complain that it cannot inform the kernel, I (I)gnored that several times).

First remove the swap partition:

parted /dev/vda
(parted) rm 6
Error: Partition(s) 6 on /dev/vda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a
result, the old partition(s) will remain in use.  You should reboot now before making further changes.
Ignore/Cancel? i 

Extend the logical partition (2) to hold the extended partitions, to the end of the disk (-1):

(parted) resizepart 2 -1

Resize logical partition 5, keeping space for the swap partition (parted will complain again, just go on and (i)gnore:

(parted) resizepart 5 -4G

Recreate the swap partition - as you can see from crypttab it is randomized on every boot so we do not bother with UUID - not good for suspend - but for servers and no suspend it is the preferred way. Create it from the end of the previous partitioin (157G) to the end of the disk (-1):

(parted) mkpart                                                           
Partition type?  primary/logical? l                                       
File system type?  [ext2]? linux-swap                                     
Start? 157GB                                                              
End? -1    

Check the results:

(parted) print                                                            
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 161GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End    Size    Type      File system     Flags
 1      1049kB  700MB  699MB   primary   ext4            boot
 2      701MB   161GB  160GB   extended
 5      701MB   157GB  156GB   logical
 6      157GB   161GB  3998MB  logical   linux-swap(v1)  lba
(parted) quit

Now reboot and you should normally boot the system, unlocking the partition.

After reboot, check your swapspace:

# swapon
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 3.7G   0B   -1

Time to expand! Use 'cryptsetup resize /dev/mapper/opened-volume' and 'resize2fs /dev/mapper/opened-volume'

cryptsetup resize /dev/mapper/vda5_crypt
resize2fs /dev/mapper/vda5_crypt

Resulting in my encrypted volume resized to 132G:

# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/vda5_crypt  144G   12G  132G   9% /
/dev/vda1               641M   63M  532M  11% /boot
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.