Ubuntu 12.04 - ZFS Getting Started

Update 1st May 2017

This tutorial is way out of date as you should not be using Ubuntu 12.04 any more. However, I am leaving it here in case parts of it may be useful for later versions of Ubuntu.

ZFS is a combined file system and logical volume manager designed by Sun Microsystems. I discovered it in my quest to find out about BTRFS. People are stating that BTRFS is superior to ZFS, but BTRFS is not yet considered stable, so in the meantime I am using ZFS. In this tutorial we will be playing with ZFS in order to demonstrate its snapshot/restore capability.

Installation

    Lets install all the packages we need as well as updates to the kernel.
    sudo apt-get install python-software-properties
    sudo apt-add-repository ppa:zfs-native/stable -y
    sudo apt-get update && sudo apt-get install ubuntu-zfs -y
    
    You just updated the kernel with some modules, so you need to reboot.
    Now run the following command to ensure that ZFS has been set up.
    dmesg | grep ZFS
    If everything went ok, you will see output like below. If something went wrong, you will get no output.

Setting Up

    Now lets create some virtual block devices on which we will set up the ZFS filesystem. If you have some spare hard drives or SSDs plugged in, then you do not need to do this. Create a directory to put the virtual device(s) in:
    VIRT_DEVICE_DIR=/virt-devices
    sudo mkdir $VIRT_DEVICE_DIR
    
    Create a 100GB "sparse" image to play with:
    sudo dd if=/dev/zero of=$VIRT_DEVICE_DIR/1.img bs=1k count=1 seek=100M
    
    Create a ZFS pool with just this one device
    POOL_NAME=vol0
    sudo zpool create $POOL_NAME $VIRT_DEVICE_DIR/1.img
    
    Check the status
    sudo zpool list
    
    sudo zpool status
    
    Now lets mount this filesystem somewhere so that we can add files to it.
    MOUNT_POINT=/mnt/data
    DATASET_NAME=data
    sudo zfs create -o mountpoint=$MOUNT_POINT $POOL_NAME/$DATASET_NAME
    
    Set the mount point to be owned by ourselves so that we can place files within it.
    sudo chown $USER $MOUNT_POINT
    Test that the dataset was created with:
    sudo zfs list

Playing with Snapshots

Now that we have created our ZFS filesystem, we can take advantage as what I see as ZFS's main feauture, the ability to take instant snapshots.

    First create a file to check later.
    echo "my data" > $MOUNT_POINT/my-file.txt
    
    To see whether snapshots show in the results of zfslist run:
    sudo zpool get listsnapshots $POOL_NAME
    
    By default, the display of snapshots is disabled. The following command enables it:
    sudo zpool set listsnapshots=on $POOL_NAME
    
    The following command disables it if you want to change back later:
    sudo zpool set listsnapshots=off $POOL_NAME
    
    Now take a snapshot
    SNAPSHOT_NAME=snapshot1
    sudo zfs snapshot $POOL_NAME/$DATASET_NAME@$SNAPSHOT_NAME
    
    Now you should see the snapshot when you perform a zfs list:
    sudo zfs list
    
    Now to test the snapshot works, lets change the contents of the file we created just before taking the snapshot.
    echo "data changed" > $MOUNT_POINT/my-file.txt
    cat $MOUNT_POINT/my-file.txt
    
    Rollback to the snapshot:
    sudo zfs rollback $POOL_NAME/$DATASET_NAME@$SNAPSHOT_NAME
    
    Check the contents of the file:
    cat /mnt/data/my-file.txt
    
    If you got the message "my data" instead of "data changed" then everything went successfully! You now have a way to instantly restore your filesystem to points in time.

References

No comments:

Post a Comment