What are we doing here?

This blog includes a series of videos and references to help new users or enthusiasts better understand how to use open source and free technology tools. The quick links includes more information for accessing many of the tools covered along with other references to learn more for taking advantage of these tools.

Click HERE to see the full list of topics covered!
Showing posts with label Storage in Linux. Show all posts
Showing posts with label Storage in Linux. Show all posts

Changing the Partition Layout in Linux with Encryption

 


This is another important community information piece designed to help save other people hours of time doing Google searches. 

So on my current laptop, I have 2 partitions - the root directory / where all the operating system lives, and the /home directory. The /home directory is encrypted using the LUKS algorithm that most Linux distros make available during install. However, I needed to increase the size of the root directory /. This is tedious but doable. 

Steps:
Back up the whole /home directory. In Linux there are many hidden files and settings that need to get backed up so I recommend using the command line to ensure you get it all.
cp /home/* <back up destination>

Once that completes, shutdown the computer and boot into a Live instance. This is typically created on the USB drive that is most commonly used to install Linux on a bare-metal system these days. 

Once in the Live instance booted in the USB you need to delete the existing /home directory in order to increase the size of the root directory /. This is because partitions in Linux and most operating systems are mapped sequentially on the logical volume (in this case a since SSD, but could be a RAID group, a LUN, mapped storage, etc.). Since the root directory needed to increase in size, I needed to move the /home directory out of the way. 

I used the Gparted tool to do this. It can handle all of the deletion and resizing of the partitions. Alternatively, for this the Gnome Disks utility (image at top of article) also can handle the fairly basic task as well. 


In Gparted select the correct drive in the upper right-hand corner, then delete the already backed up /home directory partition. 

Click on the root directory / partition, and click 'Resize / Move'. I added another 30000 MiB or around 30 GB to the partition. 

Click on the remaining space in create a new partition using all the remaining space. This will become the new /home directory. 

Open the Disks utility which should be in most mainstream distros - Ubuntu, Fedora, Red Hat, CentOS, Linux Mint, etc. 

The Disks utility has a nice feature to allow for the LUKS encryption which I wanted, so I re-formatted the newly claimed space. Probably would have been faster to do all of this in the Disks utility, but I felt more comfortable with Gparted for the resizing. 

Disks also has an option to set the mount point of the partition, which we need to do. I personally edited the /etc/fstab table which is where Linux knows to mount what where at start up, but using the Disks utility also can control this. Changes to the mount options made in the Disks utility will also show up in the /etc/fstab table. 


Once that is done, it should be possible to mount the /home partition on the Linux Live instance running from the bootable USB and copy over all of the data from the backed up /home directory. Maybe it's saved somewhere else on a computer, or in an external hard drive, but again I recommend to run the reverse of the cp command to ensure EVERYTHING is placed back on the new partition. 
cp <back up destination>/* /home
Once all of that is done there is one more step that I missed. It won't cause data loss, but I found the computer kept taking a long time to boot. In Linux if the boot sequence is taking a long time, you can press 'Esc' to see what's happening - another reason I really like Linux. Something like this:

 dev-disk-by\x98756984\x984128b\x3d4657f2\x2d865f\x2d8025cb8d2d01.device: Job dev-disk-by\x98756984\x984128b\x3d4657f2\x2d865f\x2d8025cb8d2d01
.device/start timed out.

Now that was weird to me because I had changed the mount point already, and in the /etc/fstab I had removed the old mount point to /home. I somehow had thought this might happen - a moment of forethought that usually doesn't happen for me. I thought I had prepared for this.

Turns out that not only does /etc/fstab need to be adjusted, if the partition was encrypted from the beginning, there is an additional encryption setting that also tracks the devices that need decryption on boot. The file is /etc/crypttab 

Comment out the old partition that no longer exists and all is grand! 


So in short the steps are:
  • Backup the whole /home directory
  • Boot into a Live environment
  • Delete the BACKED UP /home directory partition
  • Resize the root directory '/' partition
  • Build a new partition for /home
  • Ensure the mount point is set, and double check /etc/fstab to ensure the old partition isn't listed. (Also ensure that you check the /etc/fstab of the SSD, not the USB environment.)
  • If the old partition was encrypted, comment or delete the line referring to the old partition in /etc/crypttab
  • Re-copy all the data from the backup to the new /home partition
  • Reboot into the OS on the SSD - everything should still work just as before but the partition sizes will have changed
I hope this can help as a reference for anyone who needs to adjust the partitions around on a Linux device. Linux is really flexible for these tasks, but always, always, always have a separate back up, if not two.