Guest User

Untitled

a guest
Nov 20th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Usage: ./script.sh arg1
  4. #
  5. # Arg1: the number of the loopback device to create
  6. # (must be a number that does not match the entries under
  7. # /dev/loop<N>)
  8. #
  9. # e.g.: if `ls /dev` shows /dev/loop{0,1,2}, here you can
  10. # use something like `10`.
  11. #
  12. # ps.: it depends on `btrfs-tools` and must be run as root.
  13.  
  14. set -o errexit
  15. set -o xtrace
  16.  
  17. readonly MOUNT_POINT_PREFIX="/mnt/btrfs"
  18.  
  19. main() {
  20. local btrfs_instance="$1"
  21.  
  22. if [[ -z $1 ]]; then
  23. echo "Usage: $0 <btrfs_instance_no>"
  24. exit 1
  25. fi
  26.  
  27. local btrfs_instance=$1
  28. local volume_number=1
  29.  
  30. setup_btrfs $btrfs_instance
  31. setup_base_volume $btrfs_instance
  32. for i in $(seq 3001 10000); do
  33. setup_volume $btrfs_instance $i
  34. done
  35. }
  36.  
  37. setup_base_volume() {
  38. local mount_point=$MOUNT_POINT_PREFIX-$1
  39. local base_volume=$mount_point/base_volume
  40.  
  41. echo "Setting up base volume
  42. MOUNT_POINT: $mount_point
  43. BASE_VOLUME: $base_volume
  44. "
  45.  
  46. btrfs subvolume create $base_volume
  47. dd if=/dev/zero of=$base_volume/file1 bs=1024 count=1024
  48. }
  49.  
  50. setup_volume() {
  51. local mount_point=$MOUNT_POINT_PREFIX-$1
  52. local base_volume=$mount_point/base_volume
  53. local new_volume=$mount_point/vol$2
  54.  
  55. echo "Snapshotting volume (base=$base_volume, new=$new_volume)"
  56.  
  57. btrfs subvolume snapshot $base_volume $new_volume
  58. }
  59.  
  60. setup_btrfs() {
  61. local loopback_device="/dev/loop$1"
  62. local backing_image="/img-$1"
  63. local mount_point="$MOUNT_POINT_PREFIX-$1"
  64.  
  65. echo "INFO: Setting up BTRFS
  66. LOOPBACK_DEVICE $loopback_device
  67. BACKING_IMAGE $backing_image
  68. MOUNT_POINT $mount_point
  69. "
  70.  
  71. mknod -m 0660 $loopback_device b 7 $1
  72.  
  73. fallocate -l 256M $backing_image
  74. losetup $loopback_device $backing_image
  75.  
  76. mkfs.btrfs $loopback_device
  77.  
  78.  
  79. mkdir -p $mount_point
  80. mount -t btrfs $loopback_device $mount_point
  81. }
  82.  
  83. main "$@"
Add Comment
Please, Sign In to add comment