]> rtime.felk.cvut.cz Git - pisa/qemu-utils.git/blob - qemu-run-trick/qemu-setup-and-run
qemu-run-trick: use more descriptive labels during mount.
[pisa/qemu-utils.git] / qemu-run-trick / qemu-setup-and-run
1 #!/bin/bash
2 #
3 # QEMU trick to dive user in his own hoe environment
4 # as root in a virualized shadow of his own system
5 #
6 # (C) 2013 Pavel Pisa <pisa@cmp.felk.cvut.cz>
7 #
8 # THE TSCRIPT IS PROVIDED “AS IS” WITHOUT WARRANTY
9 # OF ANY KIND, EITHER EXPRESSED OR IMPLIED.
10 #
11 # There is no limitation of scrip use, modification
12 # and distribution or relicensing.
13 #
14 # Some used ideas are documented at CTU FEE A4M35OSP
15 # excercises pages
16 #   http://support.dce.felk.cvut.cz/osp/cviceni/2/
17 #   http://support.dce.felk.cvut.cz/osp/cviceni/2-en/
18 #
19 # and Departemnt Of Control Engineering IT Wiki
20 #   https://support.dce.felk.cvut.cz/
21 #
22
23 QEMU=qemu-system-x86_64
24 BUSYBOX=$(which busybox)
25 RESIZE=$(which resize)
26 KERNEL_VERSION=$(uname -r)
27 KERNEL_IMAGE=/boot/vmlinuz-${KERNEL_VERSION}
28 RAMDISK_ARCHIVE=ramdisk.cpio
29 #GEN_INIT_CPIO=$(dirname $0)/gen_init_cpio
30 INITRD_DIR=initrd_content
31 MODULES_LIST="virtio virtio_ring virtio_pci virtio_net virtio_net fscache 9pnet 9pnet_virtio 9p aufs overlay"
32 QEMU_MEMORY="-m 1024"
33 QEMU_KVM_ENABLE="-enable-kvm"
34 #QEMU_OUTPUT="-vga cirrus -serial null"
35 QEMU_OUTPUT="-nographic -append console=ttyS0"
36
37 INITRD_DIR_ABS=$(readlink -f ${INITRD_DIR})
38
39 function libraries_for_binary()
40 {
41   ldd $1 | sed -n -e 's#^\t\(/[^ ]*\) .*$#\1#p' -e 's#^\t\([^ ]*\) => \([^ ]\+\) .*$#\2#p'
42 }
43
44 function deep_copy()
45 {
46   src=$1
47   tgt_dir=$2
48
49   while true ; do
50     mkdir -p ${tgt_dir}/$(dirname ${src}) || return 1
51     cp -av ${src} ${tgt_dir}/${src#/} || return 1
52
53     l=$(readlink ${src})
54     if [ -z "$l" ] ; then
55       return 0
56     fi
57     if [ ${l:0:1} != '/' ] ; then
58       l=$(dirname ${src})/${l}
59     fi
60     src=${l}
61   done
62 }
63
64 function setup_initrd_content()
65 {
66   mkdir -p ${INITRD_DIR_ABS}
67   mkdir -p ${INITRD_DIR_ABS}/bin
68   mkdir -p ${INITRD_DIR_ABS}/etc/init.d
69
70   LIBS=$(libraries_for_binary ${BUSYBOX})
71   if [ -n "${RESIZE}" ] ; then
72     LIBS+=" "$(libraries_for_binary ${RESIZE})
73   fi
74
75   LIBS="$(for i in ${LIBS} ; do echo $i ; done | sort -u)"
76
77   for i in ${LIBS}; do
78     deep_copy ${i} ${INITRD_DIR_ABS}
79   done
80
81   cp -a ${BUSYBOX} ${INITRD_DIR_ABS}/bin
82   ( cd ${INITRD_DIR_ABS}/bin && ln -s busybox sh )
83
84   if [ -n "${RESIZE}" ] ; then
85     cp -a ${RESIZE} ${INITRD_DIR_ABS}/bin
86   fi
87
88
89   for m in ${MODULES_LIST} ; do
90     mf=$(find /lib/modules/${KERNEL_VERSION} -name ${m}.ko )
91     if [ -n "${mf}" ] ; then
92       mkdir -p "${INITRD_DIR_ABS}/$(dirname ${mf})"
93       cp -a "${mf}" "${INITRD_DIR_ABS}/${mf#/}"
94     else
95       echo "Module ${m} not found"
96     fi
97   done
98
99   rootmnt=/mnt/root
100   init=/sbin/init
101   root_dir_list="/proc /sys /dev /mnt /mnt/root /mnt/rootbase /mnt/overlay /tmp"
102
103   (
104     echo '#!/bin/sh'
105     for d in $root_dir_list ; do
106        echo "mkdir -p $d"
107     done
108
109     cat <<EOF
110 echo "Starting QEMU trick"
111 mount -t proc none /proc
112 mount -t sysfs none /sys
113 mount -t tmpfs none /dev
114 depmod
115 EOF
116     for m in ${MODULES_LIST} ; do
117       echo modprobe $m
118     done
119     cat <<EOF
120 mdev -s
121 if [ -e /bin/resize ] ; then
122   mv /dev/tty /dev/tty-backup
123   ln -s /dev/console /dev/tty
124   /bin/resize >/tmp/term-size
125   . /tmp/term-size
126   stty cols \$COLUMNS rows \$LINES
127   rm -f /dev/tty
128   mv /dev/tty-backup /dev/tty
129 fi
130 mount -t 9p -o ro,trans=virtio root /mnt/rootbase
131 mount -t tmpfs overlay /mnt/overlay
132 mkdir -p /mnt/overlay/data
133
134 if grep -q aufs /proc/filesystems ; then
135   echo "Using aufs to remap root"
136   mount -n -t aufs -o dirs=/mnt/overlay/data=rw:/mnt/rootbase=ro aufs-root ${rootmnt}
137 else
138   echo "Using overlay to remap root"
139   mkdir -p /mnt/overlay/work
140   mount -n -t overlay -o upperdir=/mnt/overlay/data,workdir=/mnt/overlay/work,lowerdir=/mnt/rootbase overlay-root ${rootmnt}
141 fi
142
143 mount -n -o move /dev ${rootmnt}/dev
144 mount -n -o move /sys ${rootmnt}/sys
145 mount -n -o move /proc ${rootmnt}/proc
146 mount -t 9p -o trans=virtio home ${rootmnt}/home
147 mount -t tmpfs none ${rootmnt}/root
148 mount -t tmpfs none ${rootmnt}/tmp
149 mount -t tmpfs none ${rootmnt}/run
150 mount -t tmpfs none ${rootmnt}/var/log
151 mount -t tmpfs none ${rootmnt}/var/tmp
152
153 rm ${rootmnt}/var/lib/urandom/random-seed 
154 rm -rf ${rootmnt}/etc/shadow
155 echo >${rootmnt}/etc/shadow
156 rm -rf ${rootmnt}/etc/ssh
157 mkdir ${rootmnt}/etc/ssh
158
159 rm -f ${rootmnt}/etc/rc2.d/* ${rootmnt}/etc/rc3.d/* ${rootmnt}/etc/rc4.d/* ${rootmnt}/etc/rc5.d/*
160
161 rm -rf ${rootmnt}/etc/fstab.d ${rootmnt}/etc/fstab
162 echo >${rootmnt}/etc/fstab
163
164 rm -rf ${rootmnt}/var/lib/dpkg/lock ${rootmnt}/var/lib/apt/lists/lock ${rootmnt}/var/cache/apt/archives/lock
165 rm -rf ${rootmnt}/etc/apt/apt.conf.d/*etckeeper ${rootmnt}/var/lib/dpkg/triggers/Lock
166
167 rm -rf ${rootmnt}/etc/network/interfaces.d ${rootmnt}/etc/network/interfaces
168 echo "auto lo eth0" >${rootmnt}/etc/network/interfaces
169 echo "iface lo inet loopback" >>${rootmnt}/etc/network/interfaces
170 echo "iface eth0 inet dhcp" >>${rootmnt}/etc/network/interfaces
171
172 if  [ -e ${rootmnt}/bin/bash ] ; then
173   shell=/bin/bash
174 else
175   shell=/bin/sh
176 fi
177
178 mv ${rootmnt}/etc/inittab ${rootmnt}/etc/inittab.orig
179 cp ${rootmnt}/etc/inittab.orig ${rootmnt}/etc/inittab
180 sed -i -e '/\/sbin\/.*getty/d' ${rootmnt}/etc/inittab
181 #echo "1:2345:respawn:/bin/sh" >>${rootmnt}/etc/inittab
182 #echo "T0:1234:respawn:/bin/sh" >>${rootmnt}/etc/inittab
183
184 echo "1:2345:respawn:/sbin/agetty -n -l \${shell} -o '-l' tty1 38400 linux" >>${rootmnt}/etc/inittab
185 echo "2:23:respawn:/sbin/agetty -n -l \${shell} -o '-l' tty2 38400 linux" >>${rootmnt}/etc/inittab
186 echo "T0:23:respawn:/sbin/agetty -n -l \${shell} -o '-l' -L ttyS0 9600 xterm" >>${rootmnt}/etc/inittab
187
188 exec switch_root ${rootmnt} /sbin/init
189 EOF
190
191   ) >${INITRD_DIR_ABS}/init
192
193   chmod 755 ${INITRD_DIR_ABS}/init
194
195   return 0
196 }
197
198 function build_initrd_gen_init_cpio()
199 {
200
201   (
202     cat <<EOF
203 dir /dev 755 0 0
204 nod /dev/tty0 644 0 0 c 4 0
205 nod /dev/tty1 644 0 0 c 4 1
206 nod /dev/tty2 644 0 0 c 4 2
207 nod /dev/tty3 644 0 0 c 4 3
208 nod /dev/tty4 644 0 0 c 4 4
209 #slink /init bin/busybox 700 0 0
210 dir /proc 755 0 0
211 dir /sys 755 0 0
212 dir /mnt 755 0 0
213 dir /mnt/root 755 0 0
214 dir /mnt/rootbase 755 0 0
215 dir /mnt/overlay 755 0 0
216 EOF
217     find ${INITRD_DIR} -mindepth 1 -type d -printf "dir /%P %m 0 0\n"
218     find ${INITRD_DIR} -type f -printf "file /%P %p %m 0 0\n"
219     find ${INITRD_DIR} -type l -printf "slink /%P %l %m 0 0\n"
220   ) > filelist
221
222   $GEN_INIT_CPIO filelist | gzip > ${RAMDISK_ARCHIVE}
223
224   return 0
225 }
226
227 function build_initrd_cpio()
228 {
229   ( cd ${INITRD_DIR_ABS} && find . | cpio --quiet -H newc -o  ) | gzip -9 > ${RAMDISK_ARCHIVE}
230 }
231
232 function run_virt_system()
233 {
234   $QEMU -enable-kvm -kernel ${KERNEL_IMAGE} \
235         -initrd ${RAMDISK_ARCHIVE} ${QEMU_MEMORY} \
236         -virtfs local,path=/,security_model=none,mount_tag=root \
237         -virtfs local,path=/home,security_model=none,mount_tag=home \
238         -net nic,macaddr=be:be:be:10:00:01,model=virtio,vlan=0 \
239         -net user,vlan=0 \
240         ${QEMU_KVM_ENABLE} ${QEMU_OUTPUT}
241
242 #     -chardev can,id=canbus0,port=can0
243 #     -device pci-can,chardev=canbus0,model=SJA1000
244 #     -device apohw -vga cirrus
245 #     -device mf624,port=55555
246
247   return $?
248 }
249
250 function clean_setup()
251 {
252   rm -rf ${RAMDISK_ARCHIVE} filelist ${INITRD_DIR_ABS}
253 }
254
255 if [ "$(whoami)" == "root" ] ; then
256   echo "$0: !!! TOO DANGEROUS TO RUN AS ROOT !!!"
257   exit 1;
258 fi
259
260 clean_setup || exit 1
261 echo "=== setup_initrd_content ==="
262 setup_initrd_content || exit 1
263 echo "=== build_initrd ==="
264 if [ -n "${GEN_INIT_CPIO}" ] ; then
265   build_initrd_gen_init_cpio || exit 1
266 else
267   build_initrd_cpio || exit 1
268 fi
269 echo "=== run_virt_system ==="
270 run_virt_system || exit 1