My name is Philipp C. Heckel and I write about nerdy things.
This site moved here recently from blog.philippheckel.com!

How-To: Move your existing Linux install to ZFS on Root


Linux

How-To: Move your existing Linux install to ZFS on Root


Ever since I joined Datto two years ago, ZFS has been part of my work every day. And every day, I am amazed how great it is. So naturally, I wanted to move my existing Linux Mint 18 installation to boot off of ZFS. Why, you may wonder? Well that’s easy. Because now I can snapshot my root file system, I can roll back if I need to, and I can restore individual files in a heartbeat.

It took a bit of fiddling in the beginning, but once you know how it works, it’s a piece of cake. This short post shows you how to move your existing Linux installation to ZFS on root (preferably Ubuntu 16.04+ based, may work for others).


Content


1. Requirements and Assumptions

For this example, we’re assuming that you’re running an Ubuntu 16.04+ based operating system (Ubuntu, Kubuntu, Xubuntu, Lubuntu, Linux Mint, …), and that you have another partition (to be used for the ZFS pool) with at least the amount of disk space that your current root partition has. If that is not the case, you can follow step 3 to make room.

2. This example: Kubuntu 16.04

In this example, I’ll be using a regular Kubuntu 16.04 installation. This could be your laptop or workstation, with any derivative of Ubuntu installed.

3. Creating the ZFS pool partition (optional)

Optional (if you have no spare partition): As you can see in the screenshot above, lsblk shows us that the machine has no extra partition for a new ZFS pool (yet), so we’ll have to split the root partition to make room for the ZFS pool partition. If you already have a disk/partition that you want to use as a pool, you can skip this step.

To modify your root partition, you need to first boot an Ubuntu/Kubuntu/Mint Live CD/USB, so you can fiddle with the partition layout in gparted or the KDE Partition Manager. I have opted to boot into an Ubuntu Live CD (to make it clear that this is not your live system).

To split your root partition,

  • Open gparted or KDE Partition Manager
  • Select your root partition, right click and select “Resize”, enter the target size and hit “Apply”
  • Then create a new (unformatted) partition in the empty space after the old root partition
  • Click Apply (Warning: Back up your system before you do this. There is a chance of data loss!)

It should look something like this (if you’re using gparted):

After you hit “Apply” and after you rebooted into your actual system, your disk layout changed and you have a new partition (here: /dev/sda3). This is the partition we will use to create the ZFS pool:

4. Creating the root ZFS pool, and copying your OS

Now that we have a free partition for our new pool, let’s first install ZFS (if you don’t have it already):

Then, identify the newly created partition and create the root pool:

Now create the root pool and the root dataset you want to use. I usually call my pools tank and put my systems in tank/os/<osname>, but this is really up to you:

After you created the new dataset, it is auto-mounted by ZFS at /tank/os/kubuntu1604. You may now start copying your running system to this location:

This may take a while, depending on your disk I/O speed and load; and it may even throw some warnings. Usually these warnings can be safely ignored if they are about files in /proc.

5. Make system bootable

Now that we have the dataset of our new root file system (tank/os/kubuntu1604), we need to tell our bootloader (here: grub2) that it should boot into the new system. To do that, we need to chroot into the system and run update-grub and grub-install:

That’s it. During the reboot, if you press ESC, you can see that the grub menu will show the new system as “Ubuntu”, and the old system as “Ubuntu 16.04.1 LTS (16.04) on /dev/sda1”. That means if you’re having trouble with your ZFS on Root based system, you can always go back to your old system:

Once you booted, you can verify that you’re indeed running on ZFS with zfs list or mount:

6. Snapshot your system regularly, accessing snapshots

You can (and should) now snapshot your root file system, and you can even roll back if you really want to. The first thing you should do is set up a cronjob to regularly snapshot your system:

Once you’ve done that, you can always restore individual files by accessing the ZFS directory in /.zfs/snapshot/<snapshotname>, like this:

Or, if you really messed up, you can also completely roll back to an old snapshot (use with caution, as running programs may expect the disk contents to be different!):

3 Comments

  1. Fermulator

    don’t forgot to export the pool before reboot!


  2. xoxo

    What about expanding the zfs partition into the old root partition after the transfer?


  3. Dani Kalmar

    The above procedure does not work if “/boot/efi’ is mounted to a unique partition.
    grub-install fails with error saying EFI directory not found.
    In my case I had to issue the following before “chroot”: (zpool1 used instead of tank)

    cd /zpool1/os/ubuntu/boot
    mount –bind /boot/efi efi
    cd /zpool1/os/ubuntu


Leave a comment

I'd very much like to hear what you think of this post. Feel free to leave a comment. I usually respond within a day or two, sometimes even faster. I will not share or publish your e-mail address anywhere.