I had so much success with Proxmox for my little home server that I decided to install it for my big home server, the one doing the important stuff. Like before I’m going to ease the transition by first cloning the whole Ubuntu server into a VM in Proxmox, then work on migrating individual services out to their own containers or VMs one at a time. Much thanks to Jeff N for email support learning how to do this.
The interesting thing here is various methods for cloning a physical Linux system into a Proxmox VM and making it bootable. I did this once already using Clonezilla. This time I’m trying two other methods: cloning with a QEMU disk image and cloning using tar on the root filesystem. I’ve now gotten all 3 methods to work. I think the QEMU disk image is my favorite route, it’s relatively compact and straightforward. Every method could be useful though.
Battle plan
The server I’m starting with is this one, with this hardware. The challenge is I want to replace the Ubuntu server OS on the machine with Proxmox, with the former server now running as a VM under Proxmox. The nerds call this a P2V clone. The general challenge is how do you clone a Linux system and make it bootable? A particular challenge is I’m trying to do it in-place, I don’t have a second server to clone to. And in particular I want to re-use the existing 2TB SSD, and even though I’m only using 80GB or so of that any simple full mirror will take forever and waste the other 1920GB.
I’m particularly scared of the moment where I install Proxmox over the old server’s filesystem. Once that’s done the clock is ticking to get the new VM up and running so I have my important services back. So first I did a couple of dry runs installing Proxmox on an external USB drive I had lying around. Here’s the plan I came up with:
- Remove unneeded files from the server like the apt cache or the systemd journal.
- Make simple tar and/or rsync backups of all the files and put them in a safe place. This is the “in case of disaster” recovery option.
- Turn off some services on the server that I don’t want to start immediately when the server wakes up as a VM.
- Shut down the server and boot a Linux system rescue disk.
- Use the rescue disk to clone the old system disk via either qemu (path 1) or tar (path 2) to an external USB disc. See below for details.
- Install Proxmox on an external USB disc on the server hardware. Don’t wipe the old server system! We’re going to test stuff is basically going to work before committing.
- Create a VM in Proxmox for the newly virtualized server
- Copy the clone into the disk for the virtualized server. See below for details.
- Boot the clone VM and get it working.
- Once satisfied, repeat the whole process but install Proxmox onto the SSD instead. Burn the boats!
It helps to have some spare fast SSD USB discs lying around for these 80GB copies of things. I used several. One for copying server clone images to. One to install Proxmox on (temporarily). And a couple of small jump drives for booting Proxmox and rescue disk images.
What I’m not doing is what I did last time using Clonezilla to make the clone. That requires two running servers (the original and Proxmox). Also the destination disk needs to be as big as the source disk and have room left over for Proxmox; not possible with my replace-in-place plan.
One thing I mostly didn’t do was try to resize the root partition or disk image to be smaller, closer to the 80GB I actually need instead of the 2TB of the source disk. It would probably make things go smoother but all the various resize options are a little scary. I did experiment with this once I got the VM working, see notes here. For the real thing I did resize the btrfs filesystem itself to 200GB, but the partition and the disk are both still 2TB.
OVMF bios notes
I’m trying to do all this with EFI and Secure Boot. It worked on my physical server, it should work on the Proxmox VM too. Some notes on that:
- For EFI you must select the OVMF BIOS when creating the VM in Proxmox. It’s not the default.
- The OVMF EFI shell is weird. It’s a 5 second timeout, then a command line I didn’t understand, and then if you exit that you get to a simple TUI BIOS menu like you’d expect. You can also hit ESC to bypass the 5 seconds and command line.
- There’s a bunch of confusing boot config options. If you’re just trying to boot a specific thing, try Boot Maintenance Manager / Boot From File and then browse the filesystems. Ubuntu’s boot works from EFI/ubuntu/shimx64.efi
- I could not get OVMF secure boot to work in a VM with the System Rescue ISO. You’d try to launch it and it’d just return. It works if you turn off secure boot in OVMF. Maybe it’s an issue with the ISO virtualization.
Path 1: QEMU image clone of the whole disk
This method creates a .qcow2 file which is conceptually an image of every block on the source disk. I got the idea to try this from a Reddit discussion. You make the .qcow2 file and load that in to QEMU/KVM on Proxmox and you’re done. It’s pretty straightforward but some details tripped me up.
Making the QCOW2 file was relatively easy with these directions. The key command is qemu-img convert -O qcow2 /dev/sda disk.qcow2. Note this clones the entire disc, not just one partition, which means the partition table, GRUB, and the EFI stuff stays intact. Fortunately the QCOW2 format is smart about unused sectors so my 2TB disc resulted in a 80GB QCOW2 file and was relatively quick (10 or 20 minutes?). (There’s also a -c to compress which might help?)
Using the QCOW2 file on Proxmox was a bit trickier. These notes were useful to me. Basically you create a new empty VM in Proxmox with no install media, no virtual hard drive. Then you use qm disk import to create a new disk from your clone. It converts the QCOW2 file into whatever format Proxmox wants. In my case that was a 2TB raw file on BTRFS. Thankfully it’s a sparse file so it only uses 80GB of the disk. (ls -l shows 2TB, du and btrfs filesystem du show 80G. ZFS also makes sparse files.) This new disk had two partitions cloned from my Ubuntu install: /boot and /. But I didn’t have to do anything special for each partition to make them work.
There’s some subtle configuration things I had to do in Proxmox:
- It’s essential to select the OVMF BIOS for the VM so that EFI booting works.
- Proxmox wants an “EFI disk” configured. That seemed to be the default once I told it to use the OVMF BIOS. It’s only 500KB, not sure what’s in it, maybe the BIOS? It’s not your system’s boot partition.
- It’s important to specify a cache policy for a BTRFS disk image. The default is “no cache” which apparently causes corruption issues; I used “write back”. No need if you’re using ZFS on Proxmox.
- I had to fiddle with the boot order of things in both Proxmox and maybe the BIOS to get it to actually boot the partition for the cloned Linux system.
- I did not have to disable secure boot. It worked with my virtualized GRUB setup from the clone without changes.
- I had to set the CPU type to “host” for KVM virtualization to work, so that my Ubuntu system could load the
kvm_intel module that snapd wanted. This might cause a problem if you want to migrate a VM to another host in a Proxmox cluster. But it might also give a performance boost.
- The graphical console was a bit wonky under Ubuntu; sometimes it’d be distorted early in the boot and then fix itself in a couple of seconds. I tried setting the graphics hardware to “VMWare Compatible” and that fixed the distortion, but then sometimes boot would fail mysteriously. See below.
With all that in place, the cloned system booted in a VM! The old familiar GRUB menu and Ubuntu loaded like a champ. Really this was pretty easy.
I initially created a VM with no network device because I knew that DHCP meant my Proxmox server had now stolen the IP address the old server was using. Not good having a VM boot and use the same IP as its host! Fortunately systemd boots pretty nicely if there are no network devices. It gets messier if there are network devices that aren’t configuring themselves; there’s a 120 second timeout for that.
I had to make some changes to the newly virtualized Ubuntu server to make it cooperate better. I did this with Ubuntu’s own recovery mode, a rescue disk would also work. I did these without networking, then rebooted for networking.
setfont Uni1-VGA32x16 to make the font readable in the Proxmox graphical console
- Remove fstab entries for other physical disks. (Note this can prevent your system from booting if you don’t, in which case you need a rescue shell.) In the new setup I’m going to mount those disks in the Proxmox server and then NFS mount them in to the Ubuntu guest.
- Edit
/etc/netplan configs to use DHCP (for now). Also the new VM ethernet device is named ens18.
- Disable Syncthing in the guest VM. The Syncthing data is no longer mounted and I was scared it’d propagate a “delete all files!” message. I’ll need to get this set up again eventually, ideally as a new Proxmox container just for Syncthing.
- Disable all my cron jobs for things like blog updates, backups, etc. Again these need to be set up eventually or migrated to special purpose containers.
- Eventually I should install the
qemu-guest-agent and enable it in the Proxmox config so everything plays nice together.
Update on Path 1
I redid this whole process for the real server and ran in to some new problems.
The graphical console was giving me trouble. As noted above, there’s some bug in the default graphics hardware where the Ubuntu boot has a corrupted display for the first couple of seconds. That was hiding an error message, so I switched to “VMWare Compatible” graphics hardware. That looked fine but then my main Ubuntu system wouldn’t boot, displaying an error “hardware has no pitchlock” which froze everything in the Ubuntu boot sequence after about 0.6 seconds. Note this error was only findable in journalctl, it had long since scrolled off the screen. Switching back to “Default” display console the system boots. I should learn about SPICE and serial consoles.
The underlying error I was trying to find in the console is my restored virtual disk wasn’t mounting cleanly. There was a complaint about the GPT tables. I’m very grateful I went through the partition shrinking exercise because I sort of understood this; somehow the virtual disk changed size just a bit (a few megabytes?) from the physical disk, which breaks the secondary GPT table at the end of the disk. Fixing this isn’t too hard, this time I did it just by running fdisk and then issuing the w command with no changes. That rewrites the partition table and all seems well. gdisk is another option for fixing it, and maybe parted could. All this is very spooky. I can’t think why it failed the second time. I did resize the btrfs partition to 200GB before taking the new QCOW2 snapshot. But that shouldn’t change the full disk size and certainly not by just a few MB.
Path 2: tar clone of the root filesystem
Disk images are unwieldy; why not just clone the Linux root filesystem with tar or rsync or cpio? In theory this should work just like it would have in 1975. In practice it’s tricky because EFI booting and GRUB are such a mystery.
I found a nice blog post laying out how it should work. Basically you use tar on the whole system to create a snapshot (ideally when it’s offline). Then make a new disk with a bootable EFI partition and a root partition. Unpack the tar into the root partition, then ✨ just use grub-install ✨ to magically populate the EFI partition.
The GRUB step is the confusing bit. The guide above recommends using a chroot to run grub-install as if your old system were live. That’s what ended up working for me. (SystemRescue has its own idea for repairing GRUB. I couldn’t make either method work and it looks like the docs there may be several years out of date.)
Here’s the steps I followed.
Clone the old machine
- Boot the system with the server to a rescue shell so the filesystem is not live.
- Mount the rescue filesystem somewhere out of the way like
/tmp/source
- Tar the entire filesystem to an archive on an external USB drive. IIRC this is really as easy as
cd /tmp/source; tar caf /mnt/usb/disc.tar.zst --one-file-system . The /dev/ files are harmless, tar is smart about those. Because the disk is not being used live there’s no need to exclude sys, proc, etc, even the one-file-system is probably redundant (but harmless).
At this point you have a clean tar archive of your server. It’s a nice backup!
Create the new VM and get it ready
- Create the VM. I used systemrescue as the ISO for the boot media, then configured an empty disc for my new virtual server. See notes from Path 1 about OVMF BIOS, disk cache policy, and “host” CPU type.
- On the Proxmox server command line set it up so the USB device containing your root filesystem tar file is passed through to the new VM. (There’s no doubt other ways to get the data into the VM.)
- Boot the VM. In the OVMF bios menu, disable Secure boot in OVMF bios. Shouldn’t be necessary but it was the only way I could get the system rescue ISO to boot.
- Boot system rescue and get to a Linux shell
- Use parted to set up the new virtual disc. It starts completely empty.
- Label the disk as GPT partition
- Create one EFI system partition of 1GB. (See here).
mkpart p fat32 0% 1GiB
set 1 esp on
- Create a BTRFS partition for the rest of the disc
mkpart p btrfs 1GiB 100%
- Create filesystems on the partitions
mkfs.fat -F 32 /dev/sda1
mkfs.btrfs /dev/sda2
Now you have an empty Proxmox VM all ready to go to have your filesystem transplanted into it.
Copy the filesystem in
- While still in the rescue shell, mount the guest VM’s disc (
/dev/sda2 in above) to somewhere like /tmp/dest and mount the USB source with the tarball.
- Use
tar to unpack the entire root filesystem from the USB disc. cd /tmp/dest; tar -xaf disc.tar.zst should do it.
At this point your VM’s disc has a clone of the server, from the tar file. All we have to do is get it to boot! In theory you can do this from a rescue disk right away, but let’s go ahead and install a proper boot system. But first...
Fix the block IDs
First it’s necessary to fix a few files in your new virtualized server’s root partition. The problem is the block IDs of your filesystems have changed and anything that refers to them by ID has to be updated.
- Boot a rescue disk and mount the root filesystem to
/tmp/dest
- use blkid to identify the new blockids of the newly prepared filesystems.
- edit
/tmp/dest/etc/fstab to refer to these new block IDs. (or just use the /dev/sda names for now.)
- edit
/tmp/dest/etc/grub to include a line naming the block ID for the new filesystem.
Make the VM disk bootable
The last part is to let grub-install do its thing to set up the boot partition.
- Boot the rescue disk and mount
/tmp/dest
- Prepare bind-mounts into
/tmp/dest per step 7 of the guide.
chroot /tmp/dest and get a new shell in the Upside Down
- Mount the boot partition:
mount /dev/sda1 /boot/efi
- Cast a magic spell for the ✨ EFI variables ✨
mount -t efivarfs none /sys/firmware/efi/efivars
- Run
update-grub to pick up the new block ID you configured in /etc/grub
- Run
grub-install /dev/sda to install the EFI partition needed to boot
- Exit the chroot shell and shut down the VM
- In the Proxmox VM config, remove the SystemRescue ISO and the USB
- Edit the VM options to include your new boot device
- Boot!
This all actually worked and I had a working system. The downside is you have to make new partitions, new filesystems, and then install grub in a very weird way. The changed block IDs complicate things too. Overall this was much more complex than just making a disk image clone with QEMU or Clonezilla. But conceptually it might be “better”. I’m glad I learned how to do this but it’s not going to be my preferred method.
Path 3: restore from Proxmox backup
This is cheating, it’s not cloning a physical system. But just for comparison I tried using the Proxmox GUI to back up a working VM of my server to a USB disc and then restore it. It was very easy! This is the reward at the end of the tunnel. Next time I upgrade hardware I won’t have to reinstall anything other than Proxmox. Then I can just backup and restore VMs and containers. Or migrate them live in a cluster, maybe.
The backup of the ~80GB of actual disk data (160GB disk) was fairly quick (10 minutes?) and created a 56GB .vma.zst file. The restore took 20 minutes and took up 62GB of data on a ZFS volume.
Everything static was restored but the system had to be rebooted. Proxmox does support live system migration but that’s not what the backup does.
Thoughts
So that’s it, I now have tried four ways of migrating a working Linux server to new hardware. I’ve made all this sound very complicated. Honestly, it was complicated while I was figuring it out. I imagine half the readers are rolling their eyes saying “duh” and the other half are saying “wut?”. I’m not quite at the “duh” stage but I’m glad I’ve gotten past “wut?”. In particular I’m much more comfortable with manipulating Proxmox VMs, adding and editing hardware and disks and the like. And maybe GRUB is a little less of a mystery to me.
It’s remarkable how resilient a Linux install is to the hardware it’s running on. Virtualizing like this means changing every single hardware device, replacing them with virtual ones. Discs are entirely different. And yet everything still mostly works; putting in the new name for the network adapter was the only manual change required.