LXD moving containers/snapshots to another drive
Solution 1
The big problem with LXD is that (currently) it only seems to support just one storage pool. In particular, this becomes somewhat of an issue if you would like to run part of your containers in one disk volume and part in another. Say, have some "fast" containers in an SSD volume and some "slow" containers in an HDD volume.
Should you wish to only run your containers in a single volume, the solution is simple as hell:
- stop your containers
- stop LXD (service lxd stop)
- move your whole /var/lib/lxd directory to your new storage pool
- create a symlink to your new storage pool
- start LXD (service lxd start)
- you're done
For instance, if you have your new storage mounted under /mnt/largepool in a subdirectory named lxd, then create the link like this:
ln -s /mnt/largepool/lxd /var/lib/lxd
This way, you will have your containers on your new storage volume.
Please beware that if you are using BTRFS or ZFS as storage backends, you might wish to create the necessary subvolumes on your new storage first, so your containers happen to be located in their own subvolumes. For instance, if you have btrfs and have containers named c1 and c2 and have the directory /mnt/largepool/lxd/containers already in place, before the actual moving of files, create subvolumes:
btrfs su create /mnt/largepool/lxd/containers/c1
btrfs su create /mnt/largepool/lxd/containers/c2
This would make it easy to create container snapshots afterwards.
I hope this information was helpful.
Solution 2
LXD is based on a sqlite db so to change pool location just change the db:
install sqlite3 client if you don't have one
echo "UPDATE config SET value='new_pool_lxd' WHERE \ key='storage.zfs_pool_name';"|sqlite3 /var/lib/lxd/lxd.db
assuming your lxd db in default location
then check
echo "SELECT * FROM config;"|sqlite3 /var/lib/lxd/lxd.db
1|storage.zfs_pool_name|new_pool_lxd
hope this helps
Related videos on Youtube
lepe
I work as a server administrator / developer in Japan. I also assist some companies to strengthen their network security as freelancer.
Updated on September 18, 2022Comments
-
lepe over 1 year
The default path for LXD is
/var/lib/lxd
, so containers are in/var/lib/lxd/containers
and snapshots are placed in/var/lib/lxd/snapshots
.The /var/ partition is getting full, so I'm planning to use another partition for containers and snapshots which has much more space.
My current options are:
- Create a symlink to the new directory (in the other drive)
- Bind (mount) the new directory into the current one
- Change some lxd setting (if exists) which points to a directory in the other drive
Not sure which method is easier and if there are any other ways to achieve this easily.
I'm worried about having problems with apparmor, the container's permissions or causing issues in lxd/lxc.
Which would be the correct (or best) way to move them?
(April 2017) UPDATE ******************
LXD 2.9+ supports multiple storage pools.
-
lepe over 7 yearsSo there is no lxd setting to change the pool location? Thanks for the extra advice about btrfs and zfs, I will keep that in mind.
-
Alvils Berzins over 7 yearsI think it is possible to change the location if you bother to recompile LXD. And yet, a symlink is the simplest solution and it will "just work".
-
lepe over 7 yearsfor some reason the table
config
is empty in my setup. Is that normal? -
Alex Barchiesi over 7 yearsno it's not. Try a dpkg-reconfigure lxd and see what happens
-
lepe over 7 yearsI reconfigured but still the same.
echo "SELECT * FROM config;"|sqlite3 /var/lib/lxd/lxd.db
shows nothing (using a clean installation in a test server -- Ubuntu 16.04 --). -
Alex Barchiesi over 7 yearswhat if you create a LXD container ? Where is the container rootfs located ?
-
lepe over 7 yearsthe rootfs is located at
/var/lib/lxd/containers/*****/rootfs/
. -
Alex Barchiesi over 7 yearsthen should really appear in the db, which user are you querying with ?
-
code_dredd over 4 yearsI know I'm late, but you can change the mountpoints using ZFS directly. I had to do this kind of thing when upgrading from the
apt
-basedlxd
version3.0.3
to thesnap
-based version3.17
after running/snap/bin/lxd.migrate
. Basically, in my case, it wassnap stop lxd && zfs unmount tank/lxd && rm -r /var/snap/lxd/common/lxd && zfs set mountpoint=/var/snap/lxd/common/lxd tank/lxd && zfs mount tank/lxd && reboot
, making sure to use the correct paths for your own case.