forked from gormanm/mmtests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-kvm.sh
executable file
·182 lines (167 loc) · 5.36 KB
/
run-kvm.sh
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
CONFIG=config
DIRNAME=`dirname $0`
SCRIPTDIR=`cd "$DIRNAME" && pwd`
. $SCRIPTDIR/$CONFIG
. $SCRIPTDIR/shellpacks/common.sh
. $SCRIPTDIR/shellpacks/common-config.sh
. $SHELLPACK_TOPLEVEL/kvm.conf
if [ "$MMTESTS_KVM_IMAGEDIR" = "" ]; then
echo No MMTESTS_KVM_IMAGEDIR
exit -1
fi
if [ "$MMTESTS_KVM_DISKIMAGE" = "" ]; then
echo No MMTESTS_KVM_DISKIMAGE
exit -1
fi
cd $SHELLPACK_TOPLEVEL
QEMU_PID=`cat qemu.pid 2> /dev/null`
if [ "$QEMU_PID" != "" ]; then
if [ "`ps -h --pid $QEMU_PID`" != "" ]; then
echo QEMU pid $QEMU_PID already appears to be running
exit -1
fi
fi
# Download image if necessary
if [ ! -e $MMTESTS_KVM_IMAGEDIR/mmtests-root.img ]; then
echo Downloading root image
mkdir -p $MMTESTS_KVM_IMAGEDIR
wget -O $MMTESTS_KVM_IMAGEDIR/mmtests-root.img $MMTESTS_KVM_DISKIMAGE
if [ $? -ne 0 ]; then
rm $MMTESTS_KVM_IMAGEDIR/mmtests-root.img
echo Failed to download root image
exit -1
fi
fi
# Create swap image if necessary
if [ ! -e $MMTESTS_KVM_IMAGEDIR/mmtests-swap.img ]; then
echo Creating swap image
dd if=/dev/zero of=$MMTESTS_KVM_IMAGEDIR/mmtests-swap.img ibs=1048576 count=$((MMTESTS_KVM_SWAPSIZE/1048576))
mkswap $MMTESTS_KVM_IMAGEDIR/mmtests-swap.img
fi
# Mount device for copying files
LOOP_DEVICE=`losetup -j $MMTESTS_KVM_IMAGEDIR/mmtests-root.img | head -1 | awk '{print $1}' | sed -e 's/://'`
if [ "$LOOP_DEVICE" = "" -o ! -e "$LOOP_DEVICE" ]; then
losetup -fv $MMTESTS_KVM_IMAGEDIR/mmtests-root.img || exit -1
LOOP_DEVICE=`losetup -j $MMTESTS_KVM_IMAGEDIR/mmtests-root.img | head -1 | awk '{print $1}' | sed -e 's/://'`
if [ "$LOOP_DEVICE" = "" -o ! -e "$LOOP_DEVICE" ]; then
echo Loop device \"$LOOP_DEVICE\" does not exist as expected
exit -1
fi
fi
echo Loop device: $LOOP_DEVICE
PART_DEVICE=/dev/mapper/`kpartx -av /dev/loop0 | head -1 | awk '{print $3}'`
if [ ! -e $PART_DEVICE ]; then
sleep 5
PART_DEVICE=/dev/mapper/`kpartx -av /dev/loop0 | head -1 | awk '{print $3}'`
if [ ! -e $PART_DEVICE ]; then
echo Partition device $PART_DEVICE does not exist as expected
exit -1
fi
fi
echo Part device: $PART_DEVICE
mkdir -p /tmp/mnt-mmtests
mount $PART_DEVICE /tmp/mnt-mmtests || exit -1
# Copy modules across if running a kernel
MODULES_DIRECTORY=/lib/modules
if [ ! -d $MODULES_DIRECTORY/$MMTESTS_KVM_KERNEL_VERSION ]; then
if [ "$INSTALL_KERNEL_DESTINATION" != "" ]; then
MODULES_DIRECTORY=$INSTALL_KERNEL_DESTINATION
else
MODULES_DIRECTORY=`pwd`/kernel-images
fi
fi
if [ -d $MODULES_DIRECTORY/$MMTESTS_KVM_KERNEL_VERSION/ ]; then
echo Copying modules across for $MMTESTS_KVM_KERNEL_VERSION
if [ ! -d /tmp/mnt-mmtests/lib/modules ]; then
echo Modules directory /tmp/mnt-mmtests/lib/modules does not exist as expected
umount /tmp/mnt-mmtests
exit -1
fi
if [ -e /tmp/mnt-mmtests/lib/modules/kvm-stale ]; then
STALE=`cat /tmp/mnt-mmtests/lib/modules/kvm-stale`
if [ "$STALE" != "" ]; then
echo Removing /lib/modules/$STALE
rm -rf /tmp/mnt-mmtests/lib/modules/$STALE
if [ $? -ne 0 ]; then
umount /tmp/mnt-mmtests
exit -1
fi
fi
rm /tmp/mnt-mmtests/lib/modules/kvm-stale
fi
echo Copying $MODULES_DIRECTORY/$MMTESTS_KVM_KERNEL_VERSION
cp -r $MODULES_DIRECTORY/$MMTESTS_KVM_KERNEL_VERSION /tmp/mnt-mmtests/lib/modules
echo $MMTESTS_KVM_KERNEL_VERSION > /tmp/mnt-mmtests/lib/modules/kvm-stale
else
echo WARNING: modules directory $MODULES_DIRECTORY/$MMTESTS_KVM_KERNEL_VERSION does not exist
fi
# Copy mmtests across
echo Syncing to /tmp/mnt-mmtests/$SHELLPACK_TOPLEVEL
chmod a+x /tmp/mnt-mmtests/root
rm -rf /tmp/mnt-mmtests/$SHELLPACK_TOPLEVEL
mkdir -p /tmp/mnt-mmtests/$SHELLPACK_TOPLEVEL
rmdir /tmp/mnt-mmtests/$SHELLPACK_TOPLEVEL
echo 'kvm-images/' > /tmp/mmtests.exclude
echo 'work/' >> /tmp/mmtests.exclude
echo 'kernel-images/' >> /tmp/mmtests.exclude
rsync --exclude-from /tmp/mmtests.exclude -a $SHELLPACK_TOPLEVEL/ /tmp/mnt-mmtests/$SHELLPACK_TOPLEVEL
rm /tmp/mmtests.exclude
echo Unmounting KVM device
umount /tmp/mnt-mmtests
ATTEMPT=1
SANITY=`lsof $PART_DEVICE`
while [ "$SANITY" != "" -a "$KVM_FORCE_START" != "yes" ]; do
ATTEMPT=$((ATTEMPT+1))
if [ $ATTEMPT -eq 10 ]; then
echo Giving up
exit -1
fi
echo Something still has $PART_DEVICE open, not safe. Attempt $ATTEMPT, override with KVM_FORCE_START=yes
echo cmd: lsof $PART_DEVICE
sleep 3
SANITY=`lsof $PART_DEVICE`
done
dmsetup remove $PART_DEVICE
losetup -d $LOOP_DEVICE
NUMCPUS=$((NUMCPUS-4))
if [ $NUMCPUS -le 0 ]; then
NUMCPUS=1
fi
if [ $NUMCPUS -ge 255 ]; then
NUMCPUS=250
fi
# -redir tcp:30022::22 \
qemu-system-x86_64 \
-serial telnet:localhost:2000,server \
-vnc :3 \
-no-reboot \
-smp cpus=$NUMCPUS \
-m $MMTESTS_KVM_MEMORY \
-drive file=$MMTESTS_KVM_IMAGEDIR/mmtests-root.img,if=virtio,cache=none \
-drive file=$MMTESTS_KVM_IMAGEDIR/mmtests-swap.img,if=virtio,cache=none \
-cpu Nehalem \
-no-reboot \
-s \
--enable-kvm \
-net nic,model=virtio \
-net user,hostfwd=tcp::30022-:22 \
-kernel $MMTESTS_KVM_KERNEL \
-initrd $MMTESTS_KVM_INITRD \
-append "$MMTESTS_KVM_APPEND" \
$@ &
QEMU_PID=$!
echo $QEMU_PID > $SHELLPACK_TOPLEVEL/qemu.pid
sleep 5
if [ "$MMTESTS_KVM_FOREGROUND" = "yes" ]; then
telnet localhost 2000
else
if [ "$MMTESTS_KVM_SERIAL_VISIBLE" = "yes" ]; then
telnet localhost 2000 | tee kvm-console.log &
else
telnet localhost 2000 > kvm-console.log &
fi
echo Waiting on KVM instance $QEMU_PID to reach ssh
wait_ssh_available localhost 30022
fi
exit $?