Thursday, September 21, 2023

Backup and restore a Linux installation

Recently, I upgraded my HP Omen 2017 laptop NVME SSD to a bigger one, the original SSD was a Toshiba SSD 256GB, PCIe 3.0, replaced by a Crucial P5 Plus 2TB NVME SSD PCIe 4.0. This kind of old laptop has PCIe 3.0 system but since PCIe is backward compatible against it's predecessor, it's still able to run PCIe 4.0 with of course at PCIe 3.0 speed. The old SSD has left around 40GB or 15% of empty spaces, despite how smart I manage to keep the empty spaces, I felt exhausted to keep fight on the limitation. I decided it's time to ugprade the old SSD, thinking of getting more bigger spaces, faster video editing and reduced gaming load time advantages, etc. Anyway, this HP Omen laptop also has a pre-installed 2.5" 1TB harddisk which I use to store larger files such as videos, games and backups.

Backup and restore Windows system image

As usual, this laptop come with pre-installed Windows 10, my upgrade procedure was by creating a system image from the old SSD then restore the image to the new SSD, I used a Seagate 1TB Expansion Portable Drive (USB interface) to store the image. After the system image backup and restore, I had to expand the main Windows partition to be bigger size then create (or move to be precisely) a new Windows recovery partition as the previous recovery partition was overlapped by the expansion.

Backup the root filesystem

The laptop is UEFI compatible and also Windows 10 system partition is EFI. When expanding the Windows partition, I left some gigabytes for Linux partitions, they are the EFI boot partition and root partition. I already have Fedora 38 installation on my other system so I thought to copy the Fedora install instead of install a fresh Fedora 38 then messing up with apps and settings I have to install or set later. My Fedora filesystem is pretty well organized, such as I don't store larger files, projects, etc. in the main partition, but mount other parititions and set the mount points into the root filesystem's directories such as /data mount point. This strategy allows me to keep root filesystem clean from contents that outside system scope. To backup my another Fedora root filesystem, I booted the system from a USB stick drive that has Fedora 38 live install, then attach my Seagate USB drive and run these following commands as superuser.

Create external USB drive mount point and mount it, if not already mounted by desktop manager.

mkdir /mnt/external_drive
mount /dev/sdb1 /mnt/external_drive

In these following commands, the root partition is /dev/nvme0n1p2 and the EFI partition /dev/nvme0n1p1. The root filesystem is EXT4 and the EFI is FAT16.

Mount EFI filesystem and make the tarball.

mkdir /mnt/efi
mount /dev/nvme0n1p1 /mnt/efi
cd /mnt/efi
tar cf /mnt/external_drive/efi.tar .

Mount root filesystem and make the tarball.

mkdir /mnt/rootfs
mount /dev/nvme0n1p2 /mnt/rootfs
cd /mnt/rootfs
tar --same-owner --same-permissions -cf /mnt/external_drive/rootfs.tar .

Note that --same-owner and --same-permissions arguments are necessary to preserve files's (and directories) owner and permission upon restoration (that is, extracting the tarball). For the EFI partition, we don't need to preverse owner and permission since it is a FAT16 filesystem. Optionally, compression can be applied with longer tar's time caveat.

Unmount all mount points.

umount /mnt/rootfs
umount /mnt/efi
umount /mnt/external_drive

Restore the root filesystem

Now I have Fedora 38 root filesystem and EFI tarballs from another system and ready to be restored in the HP Omen laptop. I still need the Fedora 38 Live USB stick to boot and of course the Seagate Portable Drive. I created 2 additional partitions in the new SSD, first is the EFI partition (FAT16) and second is Linux root partition (EXT4). I don't go into detail how to create these partitions and formatting them, the tools are just gparted and mkfs. The HP Omen's EFI partition is /dev/nvme0n1p4 and Linux root partition is /dev/nvme0n1p5, the Seagate drive is /dev/sdc1.

Here all the commands, basically they are just the same as in backup section, only differs in partition name.

mkdir /mnt/external_drive
mount /dev/sdc1 /mnt/external_drive

mkdir /mnt/efi
mount /dev/nvme0n1p4 /mnt/efi
tar -xf /mnt/external_drive/efi.tar -C /mnt/efi

mkdir /mnt/rootfs
mount /dev/nvme0n1p5 /mnt/rootfs
tar -xf /mnt/external_drive/rootfs.tar -C /mnt/rootfs

Looks good so far, I have almost-working Fedora Linux on my HP Omen laptop, but not too close.

SELinux issue

Fedora Linux runnig Security-Enhanced Linux (SELinux), that means all processes and files are labeled in a way the represents security-relevant information. This information is called the SELinux context. That means, when we copy all these root filesystem files into a new filesystem, it could make the SELinux context also change and might result files access or process problems. To fix this, we can disable SELinux security operation mode momentary then enable that later. The SELinux configuration is located in /etc/selinux/config.

SELINUX=disabled

I also wrote a post in the past that cover this SELinux issue.

Boot setting

Now the HP Omen laptop has 2 EFI partitions, one is for Windows and another for Fedora Linux. In this post, I don't want to cover how to adjust the new Linux boot setting, basically all I did was changing root partition location in GRUB's config and Linux EFI's files. I didn't make GRUB as the default boot manager replacing Windows boot manager, I just hit a startup key that allow me to select which EFI partition to boot. Maybe it's not a convenient solution but I'll use this Fedora Linux on my HP Omen occasionally.