Add support for GPG signature enforcement on booted
[petitboot] / test / hotplug-device.sh
1 #!/bin/bash
2
3
4 devmapper_name=meep
5 file=test.ext2
6
7 if [ $# -lt 1 ]
8 then
9         cat <<EOF
10 usage: $0 [add [path]|remove]
11         if <path> is specified, initialise the contents of the hotplugged
12         device from the directory at <path>
13 EOF
14         exit 1
15 fi
16
17 sudo true
18
19 set -ex
20
21 case "$1"
22 in
23         add)
24                 dd if=/dev/zero of=$file bs=1k count=640
25                 mkfs.ext2 -F -m0 $file
26
27                 loopdev=$(sudo losetup -f --show $loopdev $file)
28                 echo loop device is $loopdev
29
30                 if [ -d $2 ]
31                 then
32                         mkdir -p mount.tmp
33                         sudo mount $loopdev mount.tmp
34                         sudo rsync -av $2 mount.tmp/
35                         sudo umount mount.tmp
36                 fi
37
38                 blocksize=$(sudo blockdev --getsize $loopdev)
39                 echo block size = $blocksize
40
41                 sudo dmsetup create $devmapper_name \
42                         --table "0 $blocksize linear $loopdev 0"
43         ;;
44         remove)
45                 set +e
46                 awk '/^\/dev\/mapper\/'$devmapper_name'/ {print $2}' \
47                                 /proc/mounts |
48                 while read mountpoint
49                 do
50                         sudo umount $mountpoint
51                 done
52                 sudo dmsetup remove $devmapper_name
53
54                 sudo losetup -j $file | cut -f1 -d: |
55                 while read loopdev
56                 do
57                         sudo losetup -d $loopdev
58                 done
59         ;;
60
61 esac