Created
March 6, 2019 01:34
-
-
Save cirocosta/aefc52e9df7d35b4db7f5961ac8a0917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: ./script.sh arg1 | |
# | |
# Arg1: the number of the loopback device to create | |
# (must be a number that does not match the entries under | |
# /dev/loop<N>) | |
# | |
# e.g.: if `ls /dev` shows /dev/loop{0,1,2}, here you can | |
# use something like `10`. | |
# | |
# ps.: it depends on `btrfs-tools` and must be run as root. | |
set -o errexit | |
set -o xtrace | |
readonly MOUNT_POINT_PREFIX="/mnt/btrfs" | |
main() { | |
local btrfs_instance="$1" | |
if [[ -z $1 ]]; then | |
echo "Usage: $0 <btrfs_instance_no>" | |
exit 1 | |
fi | |
local btrfs_instance=$1 | |
local volume_number=1 | |
setup_btrfs $btrfs_instance | |
setup_base_volume $btrfs_instance | |
for i in $(seq 1 3); do | |
setup_volume $btrfs_instance $i | |
done | |
} | |
setup_base_volume() { | |
local mount_point=$MOUNT_POINT_PREFIX-$1 | |
local base_volume=$mount_point/base_volume | |
echo "Setting up base volume | |
MOUNT_POINT: $mount_point | |
BASE_VOLUME: $base_volume | |
" | |
btrfs subvolume create $base_volume | |
dd if=/dev/zero of=$base_volume/file1 bs=1024 count=1024 | |
} | |
setup_volume() { | |
local mount_point=$MOUNT_POINT_PREFIX-$1 | |
local base_volume=$mount_point/base_volume | |
local new_volume=$mount_point/vol$2 | |
echo "Snapshotting volume (base=$base_volume, new=$new_volume)" | |
btrfs subvolume snapshot $base_volume $new_volume | |
} | |
setup_btrfs() { | |
local loopback_device="/dev/loop$1" | |
local backing_image="/img-$1" | |
local mount_point="$MOUNT_POINT_PREFIX-$1" | |
echo "INFO: Setting up BTRFS | |
LOOPBACK_DEVICE $loopback_device | |
BACKING_IMAGE $backing_image | |
MOUNT_POINT $mount_point | |
" | |
mknod -m 0660 $loopback_device b 7 $1 | |
fallocate -l 2G $backing_image | |
losetup $loopback_device $backing_image | |
mkfs.btrfs $loopback_device | |
mkdir -p $mount_point | |
mount -t btrfs $loopback_device $mount_point | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment