Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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 "$@"
Add Comment
Please, Sign In to add comment