Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 83 lines (75 sloc) 2.22 KB
#!/bin/bash
set -e
ARG_SELF=$0
while [ $# -gt 0 ]; do
case "$1" in
--pool=*)
ARG_POOL="${1#*=}"
;;
--path=*)
ARG_PATH="${1#*=}"
;;
mount|unmount|list|fstrim|defrag)
COMMAND="$1"
;;
*)
echo "
LXD BTRFS Manager
Usage:
lxd-btrfs-manager mount [--pool=<name>]
lxd-btrfs-manager unmount [--pool=<name>]
lxd-btrfs-manager list [--pool=<name>]
lxd-btrfs-manager fstrim [--pool=<name>]
lxd-btrfs-manager defrag [--pool=<name>] [--path=<path>]
Options:
--pool=<name> Select a custom BTRFS pool name. Defaults to first found pool if empty.
--path=<path> Only defrag the specified sub-path. Defaults to the complete pool if empty.
"
exit 1
esac
shift
done
if [ -z "$ARG_POOL" ]; then ARG_POOL=$(lxc storage list | awk 'FNR == 4 {print $2}'); fi
if [ -z "$ARG_PATH" ]; then ARG_PATH="/"; fi
btrfs filesystem show $ARG_POOL >/dev/null
LOOPFILE_PATH=$(btrfs filesystem show $ARG_POOL | awk 'FNR == 3 {print $8}')
IMAGEFILE_PATH=$(losetup | grep $LOOPFILE_PATH | awk 'FNR == 1 {print $6}')
BTRFS_UUID=$(btrfs filesystem show $ARG_POOL | awk 'FNR == 1 {print $4}')
TEMP_MOUNT_PATH=/tmp/btrfs_$BTRFS_UUID
case "$COMMAND" in
mount)
mkdir $TEMP_MOUNT_PATH
mount $LOOPFILE_PATH $TEMP_MOUNT_PATH
echo "Mounted LXD storage $ARG_POOL on $TEMP_MOUNT_PATH"
;;
unmount)
umount $TEMP_MOUNT_PATH
rmdir $TEMP_MOUNT_PATH
echo "Unmounted LXD storage $ARG_POOL from $TEMP_MOUNT_PATH"
;;
list)
$ARG_SELF mount --pool=$ARG_POOL
btrfs subvolume list -t --sort=path $TEMP_MOUNT_PATH
$ARG_SELF unmount --pool=$ARG_POOL
if [ -n "$IMAGEFILE_PATH" ]; then filesize $IMAGEFILE_PATH; fi
btrfs filesystem show $ARG_POOL
;;
defrag)
$ARG_SELF mount --pool=$ARG_POOL
echo "Defragmenting path $ARG_PATH on BTRFS pool $ARG_POOL.."
btrfs filesystem defragment -r $TEMP_MOUNT_PATH/$ARG_PATH 2>/dev/null || true
$ARG_SELF unmount --pool=$ARG_POOL
echo "Done."
;;
fstrim)
echo "Trimming BTRFS pool $ARG_POOL.."
$ARG_SELF mount --pool=$ARG_POOL
fstrim -v $TEMP_MOUNT_PATH
$ARG_SELF unmount --pool=$ARG_POOL
if [ -n "$IMAGEFILE_PATH" ]; then
echo "Trimming loop image file.."
fstrim -v $(dirname $IMAGEFILE_PATH);
fi
echo "Done."
;;
esac
You can’t perform that action at this time.