summaryrefslogtreecommitdiffstats
path: root/test/hotplug-device.sh
blob: 8084521a343f84b0b5fa35567a53f70eb0220bd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash


devmapper_name=meep
file=test.ext2

if [ $# -lt 1 ]
then
	cat <<EOF
usage: $0 [add [path]|remove]
	if <path> is specified, initialise the contents of the hotplugged
	device from the directory at <path>
EOF
	exit 1
fi

sudo true

set -ex

case "$1"
in
	add)
		dd if=/dev/zero of=$file bs=1k count=640
		mkfs.ext2 -F -m0 $file

		loopdev=$(sudo losetup -f --show $loopdev $file)
		echo loop device is $loopdev

		if [ -d $2 ]
		then
			mkdir -p mount.tmp
			sudo mount $loopdev mount.tmp
			sudo rsync -av $2 mount.tmp/
			sudo umount mount.tmp
		fi

		blocksize=$(sudo blockdev --getsize $loopdev)
		echo block size = $blocksize

		sudo dmsetup create $devmapper_name \
			--table "0 $blocksize linear $loopdev 0"
	;;
	remove)
		set +e
		awk '/^\/dev\/mapper\/'$devmapper_name'/ {print $2}' \
				/proc/mounts |
		while read mountpoint
		do
			sudo umount $mountpoint
		done
		sudo dmsetup remove $devmapper_name

		sudo losetup -j $file | cut -f1 -d: |
		while read loopdev
		do
			sudo losetup -d $loopdev
		done
	;;

esac
OpenPOWER on IntegriCloud