-
Notifications
You must be signed in to change notification settings - Fork 1
/
changeimg
executable file
·77 lines (66 loc) · 1.96 KB
/
changeimg
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/sh
# A tool to manipulate a VDI image. Useful for adding/removing users,
# changing passwords, setting up ssh keys, etc. without starting the VM.
# Requires 'shift', so might not work with all shells. We are interested
# in dash and bash only anyway.
set -e
OP="$1"
fatal() {
printf %s\\n "fatal error: $@"
exit 1
}
setup() {
DISK="$2"
[ ! -f "$DISK" ] && fatal "usage: $0 setup <harddisk.vdi>"
[ ! -b /dev/nbd0 ] && fatal "/dev/nbd0 not found, maybe run modprobe nbd?"
if [ -z "`pgrep qemu-nbd`" ]; then
qemu-nbd -c /dev/nbd0 "$DISK" || fatal "qemu-nbd -c /dev/nbd0 failed"
fi
[ ! -b /dev/mapper/nbd0p1 ] && kpartx -sa /dev/nbd0
[ ! -b /dev/mapper/nbd0p1 ] && fatal "kpartx -sa /dev/nbd0 failed"
mkdir -p target
if [ ! -f target/vmlinuz ]; then
mount /dev/mapper/nbd0p1 target || fatal "mount /dev/mapper/nbd0p1 failed"
fi
echo Setup successful
}
cleanup() {
if [ -f target/vmlinuz ]; then
umount target || fatal "umount target failed"
fi
[ -b /dev/mapper/nbd0p1 ] && kpartx -d /dev/nbd0
[ -b /dev/mapper/nbd0p1 ] && fatal "kpartx -d /dev/nbd0 failed"
[ -n "`pgrep qemu-nbd`" ] && qemu-nbd -d /dev/nbd0
[ -n "`pgrep qemu-nbd`" ] && fatal "seems like qemu-nbd still running"
echo Cleanup successful
}
local_run() {
SCRIPT="$2"
[ ! -x "$SCRIPT" ] && fatal "usage: changeimg local_run <executable> <args>"
[ ! -f target/vmlinuz ] && fatal "run setup first"
cp "$SCRIPT" target/tmp/liox_tmp
shift;
exec chroot target tmp/liox_tmp "$@"
}
chroot() {
[ ! -f target/vmlinuz ] && fatal "run setup first"
[ -z "$2" ] && exec chroot target /bin/bash -i
shift;
exec chroot target "$@"
}
case $OP in
setup )
setup "$@"
;;
local_run )
local_run "$@"
;;
chroot )
chroot "$@"
;;
cleanup )
cleanup "$@"
;;
* )
echo "Usage: $0 (setup|local_run|chroot|cleanup)" ;;
esac