Increase a laptop’s hard disk size and its encrypted LUKS home partition
A situation that occurs often is realizing that a laptop’s hard drive
is too small for one’s needs. Unfortunately, laptops don’t have
space to hold a second hard drive, so usually people resort to
cumbersome solutions such as an additional external USB flash disk.
This comes with the burden of having to carry around another piece
of equipment.
However, there is a solution: just replace your laptop’s main hard
disk with a larger one.
I recently tinkered with a Fedora 23 laptop to increase the hard drive
from 128 to 256 Gb, and added the new free space to the encrypted home
partition. Here’s how to do that.
Note: For some of these steps you’ll need also a desktop
computer running any distribution of Linux.
1. Buy a new hard disk of the desired size. Make sure it is compatible with your laptop and fits into it.
2. Remove the old hard disk from your laptop.
3. Attach the two hard disks via standard SATA cables to the desktop computer, and power it on.
4. Ensure that the hard disks are recognized by the OS:
lsblk
The output of the command must show the two devices. For the sake of our example, let’s say that /dev/sda is the old hard disk and /dev/sdb is the new, larger hard disk.
5. Copy the content of the old hard disk into the new one:
cat /dev/sda > /dev/sdb
This might surprise you, as you probably learnt that the cat command
must be used with text files, and byte-per-byte copies are done via
the dd command (e.g. dd if=/dev/sda of=/dev/sdb bs=64K). However, in all
recent versions of Linux cat is perfectly able to handle binary
streams; you can try this for yourself by using it to copy a binary
file, for instance an image (cat image.jpg > image2.jpg).
The advantage of cat is that it will automatically choose an
optimized value for the blocksize while it’s cloning the hard disk
(while dd requires that you specify in advance a static blocksize
value; 64K in the previous example) and this will result in a faster copy. As a
reference, I managed to clone a 128 Gb SSD drive in less than one hour.
6. Once the cloning is finished, detach the new hard disk from the desktop computer and install it on the laptop.
7. Boot the laptop and the OS. It should work as usual, except that the hard drive (identified as /dev/sda) will have about 104 Gb of free space at the end.
8. Use your favourite disk partitioning tool (fdisk, gdisk, parted, etc.) to create a new partition on the free space available. Let’s assume this new partition is /dev/sda3.
9. Initialize the newly created partition for use with LVM:
pvcreate /dev/sda3
10. Add the new Physical Volume to the already existing Volume Group that contains the /home Logical Volume:
vgextend vg_main /dev/sda3
The steps that follow (taken from this blog post) assume that the /home partition is encrypted. If it is not, the procedure is simpler: just ignore the cryptsetup commands, and increase first the size of the /home Logical Volume and then the size of the underlying filesystem so that it takes up all free space.
11. Unmount the /home partition:
umount /home
12. Do a filesystem check of the encrypted /home partition:
fsck.ext4 /dev/mapper/luks-190b4a12-2c6f-4701-8c2f-0ebc89e7d72c
The lsblk command will tell you which LUKS volume matches your encrypted /home partition.
13. Remove the device mapping for the LUKS volume:
cryptsetup luksClose luks-190b4a12-2c6f-4701-8c2f-0ebc89e7d72c
14. Extend the Logical Volume to take up all remaining free space:
lvextend -l +100%FREE /dev/vg_main/lv_home
15. Map the LUKS volume to a temporary name:
cryptsetup luksOpen /dev/vg_main/lv_home tmpluks
16. Resize the LUKS volume:
cryptsetup resize tmpluks
17. Resize the filesystem:
resize2fs /dev/mapper/tmpluks
18. Reboot the laptop. Your new hard disk is now ready to use.