ZFS Storage Pool Setup

Creating a ZFS Storage Pool for LXD

This document provides a step-by-step guide to create a ZFS storage pool for use with LXD. The process involves either using an existing disk/partition or creating a file-based ZFS pool if no dedicated disk is available. In Method 2, we will use the dd command instead of truncate or fallocate to create the sparse file, as these commands may not be fully compatible with ZFS.


1. Overview

ZFS is a highly efficient and feature-rich file system that integrates seamlessly with LXD. It supports advanced features like snapshots, compression, deduplication, and data integrity checks. In this guide, we will create a ZFS storage pool for LXD using two methods:

  1. Using an existing disk or partition.
  2. Creating a file-based ZFS pool using the dd command (to ensure compatibility with ZFS).

2. Prerequisites

Before proceeding, ensure the following:

  • You have root or sudo privileges on the server.
  • The zfsutils-linux package is installed.
  • Sufficient disk space is available if creating a file-based pool.

To install ZFS utilities:

sudo apt update
sudo apt install zfsutils-linux

3. Method 1: Using an Existing Disk or Partition

Step 1: Identify the Disk or Partition

Use the lsblk or fdisk -l command to identify the disk or partition you want to use for the ZFS pool. For example:

lsblk

Output:

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0   100G  0 disk 
├─sda1        8:1    0   512M  0 part /boot
└─sda2        8:2    0  99.5G  0 part /
sdb           8:16   0   500G  0 disk 

Here, /dev/sdb is an unused disk that can be used for the ZFS pool.

Step 2: Create the ZFS Pool

Use the zpool command to create the ZFS pool:

sudo zpool create pssb1avd001 /dev/sdb

Replace:

  • pssb1avd001 with your desired pool name.
  • /dev/sdb with the actual disk or partition (e.g., /dev/sdb).

For example:

sudo zpool create pssb1avd001 /dev/sdb

Step 3: Verify the ZFS Pool

Check the status of the ZFS pool:

sudo zpool status

Output:

pool: pssb1avd001
state: ONLINE
scan: none requested
config:

    NAME        STATE     READ WRITE CKSUM
    pssb1avd001   ONLINE       0     0     0
      sdb       ONLINE       0     0     0

Step 4: Create the LXD Storage Pool

Once the ZFS pool is created, configure it as an LXD storage pool:

lxc storage create pssb1avd001 zfs source=pssb1avd001

Replace:

  • pssb1avd001 with your desired LXD storage pool name.
  • pssb1avd001 with the name of the ZFS pool.

Verify the LXD storage pool:

lxc storage list

4. Method 2: Creating a File-Based ZFS Pool Using dd

If you don’t have a dedicated disk or partition, you can create a file-based ZFS pool. Instead of using truncate or fallocate, we will use the dd command to create a properly formatted file for ZFS.

Step 1: Create the Disk Image File

Use the dd command to create a raw disk image file. For example, to create a 10GB file:

sudo dd if=/dev/zero of=/data3/lxc/pssb1avd001.img bs=1G count=10

Replace:

  • /data3/lxc/pssb1avd001.img with the desired file path.
  • bs=1G specifies the block size (1GB).
  • count=10 specifies the number of blocks (10 blocks = 10GB).

For example:

sudo dd if=/dev/zero of=/var/lib/lxd/zfs.img bs=1G count=10

Step 2: Create the ZFS Pool

Use the zpool command to create the ZFS pool using the disk image file:

sudo zpool create mc1apool01 /data3/lxc/pssb1avd001.img

Replace:

  • mc1apool01 with your desired ZFS pool name.
  • /data3/lxc/pssb1avd001.img with the path to the disk image file.

For example:

sudo zpool create mc1apool01 /var/lib/lxd/zfs.img

Step 3: Verify the ZFS Pool

Check the status of the ZFS pool:

sudo zpool status

Output:

pool: mc1apool01
state: ONLINE
scan: none requested
config:

    NAME            STATE     READ WRITE CKSUM
    mc1apool01     ONLINE       0     0     0
      /var/lib/lxd/zfs.img  ONLINE       0     0     0

Step 4: Create the LXD Storage Pool

Configure the ZFS pool as an LXD storage pool:

lxc storage create pssb1avd001 zfs source=mc1apool01

Replace:

  • pssb1avd001 with your desired LXD storage pool name.
  • mc1apool01 with the name of the ZFS pool.

For example:

lxc storage create lxd_storage zfs source=mc1apool01

Verify the LXD storage pool:

lxc storage list

5. Optional: Enable Compression

To improve performance and reduce disk usage, enable ZFS compression:

sudo zfs set compression=lz4 pssb1avd001

Replace pssb1avd001 with the name of your ZFS pool.


6. Troubleshooting

  • Issue: “Device already in use” error when creating the ZFS pool.

    • Solution: Ensure the disk or file is not already part of another pool or filesystem.
  • Issue: Insufficient disk space.

    • Solution: Increase the size of the disk image file using dd or allocate more disk space.
  • Issue: LXD storage pool creation fails.

    • Solution: Ensure the ZFS pool is properly created and accessible.

7. Conclusion

By following this guide, you can successfully create a ZFS storage pool for LXD using either an existing disk/partition or a file-based approach. Using the dd command ensures compatibility with ZFS and avoids issues associated with truncate or fallocate.

References