Created
November 20, 2018 18:45
-
-
Save cirocosta/d9cbe8c6aff9773000dbb1c771eb7427 to your computer and use it in GitHub Desktop.
Creates a loopback device w/ btrfs, creates a base volume and then a bunch of COWs based on the base volume.
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 3001 10000); 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 256M $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