Backup and Restore Partition Information for Ubuntu LUKS LVM on a 250GB SSD
In this article, we will discuss the process of backing up and restoring partition information for an Ubuntu system with LUKS encryption and LVM on a 250GB SSD. This can be useful when migrating your system to a new drive or for creating a backup of your disk layout for disaster recovery purposes.
Current Partition Setup
In the scenario described, the Ubuntu 20.04 system's boot drive is a 250GB SSD. The current partition setup is a 120GB partition with the rest of the disk unallocated. This 120GB partition was created by imaging a 120GB SSD onto the larger 250GB SSD drive.
LUKS Encryption and LVM
LUKS (Linux Unified Key Setup) is a disk encryption specification widely used for hard drives and solid-state drives (SSDs). LVM (Logical Volume Management) is a method of allocating and managing disk storage space on Linux systems. Combining LUKS and LVM provides a flexible and secure strategy for managing disk partitions.
Backup Partition Information
Before restoring or resizing partitions, it's crucial to backup your partition information. This can be done using the pvdisplay, vgdisplay, and lsblk commands. These commands will display vital information about your Physical Volumes (PVs), Volume Groups (VGs), and Logical Volumes (LVs).
# Display information about Physical Volumes
sudo pvdisplay
# Display information about Volume Groups
sudo vgdisplay
# Display a tree overview of block devices, major-minor numbers, and their sizes.
sudo lsblk -o NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUIDYou can save the output to a file for further review or safekeeping. For instance, you can create a backup script containing these commands:
#!/bin/bash
# Back up partition information
date > disk_info_backup_`date +%Y-%m-%d_%H:%M`.txt
sudo pvdisplay >> disk_info_backup_`date +%Y-%m-%d_%H:%M`.txt
sudo vgdisplay >> disk_info_backup_`date +%Y-%m-%d_%H:%M`.txt
sudo lsblk -o NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID >> disk_info_backup_`date +%Y-%m-%d_%H:%M`.txtRestore Partition Information
Now that you have a backup of your partition information, you can start restoring the partitions. Firstly, ensure that you've recorded the UUID of the new LUKS partition. Double-check the UUID of the new LUKS partition with the one you're going to restore:
# Display information about the current LUKS partitions
sudo cryptsetup luksDump /dev/nvme0n1p3Next, open the LUKS container of the new partition and the original container with the same passphrase. Create a new physical volume using the original LVM metadata. Finally, reuse the existing logical volumes in the volume group.
# Open the existing and new LUKS partitions
sudo cryptsetup luksOpen /dev/nvme0n1p3 orig_crypto
sudo cryptsetup luksOpen /dev/nvme0n1p4 new_crypto
# Copy LVM metadata from original partition to the new partition
sudo pvcreate --uuid `pvdisplay /dev/nvme0n1p3 | grep UUID | cut -d ' ' -f 3` /dev/nvme0n1p4
# Remove the LVM metadata from the old partition (optional)
sudo pvremove /dev/nvme0n1p3
# Add the new partition to the volume group
sudo vgextend cryptvol /dev/nvme0n1p4
# Reuse the old logical volumes from the backup
sudo lvchange -ay /dev/mapper/cryptvol-root
sudo lvchange -ay /dev/mapper/cryptvol-home
sudo lvchange -ay /dev/mapper/cryptvol-swap_1Summary
- Backup your partition information using the
pvdisplay, vgdisplay, and lsblk commands.
- Restore the partition information carefully, ensuring that you have the correct UUID and passphrases.
pvdisplay, vgdisplay, and lsblk commands.References
- pvdisplay - Display physical volume characteristics
- vgdisplay - Display volume group characteristics
- luksDump - Dump the contents of an encrypted LUKS device
- pvcreate - Initialize physical volume(s) for use by LVM
- vgextend - Add one or more physical volumes to a volume group
- lvchange - Issue online change requests to LVM logical volumes
- lsblk - List block devices