]> rtime.felk.cvut.cz Git - pisa/qemu-utils.git/blob - qemu-run-trick/qemu-setup-and-run
dfdfe3dc0ac4458121ce58ac9b0d8b3e9cf61538
[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"
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     fi
95   done
96
97   rootmnt=/mnt/root
98   init=/sbin/init
99   root_dir_list="/proc /sys /dev /mnt /mnt/root /mnt/rootbase /mnt/overlay /tmp"
100
101   (
102     echo '#!/bin/sh'
103     for d in $root_dir_list ; do
104        echo "mkdir -p $d"
105     done
106
107     cat <<EOF
108 echo "Starting QEMU trick"
109 mount -t proc none /proc
110 mount -t sysfs none /sys
111 mount -t tmpfs none /dev
112 depmod
113 EOF
114     for m in ${MODULES_LIST} ; do
115       echo modprobe $m
116     done
117     cat <<EOF
118 mdev -s
119 if [ -e /bin/resize ] ; then
120   mv /dev/tty /dev/tty-backup
121   ln -s /dev/console /dev/tty
122   /bin/resize >/tmp/term-size
123   . /tmp/term-size
124   stty cols \$COLUMNS rows \$LINES
125   rm -f /dev/tty
126   mv /dev/tty-backup /dev/tty
127 fi
128 mount -t 9p -o ro,trans=virtio root /mnt/rootbase
129 mount -t tmpfs overlay /mnt/overlay
130 mount -n -t aufs -o dirs=/mnt/overlay=rw:/mnt/rootbase=ro unionfs ${rootmnt}
131 mount -n -o move /dev ${rootmnt}/dev
132 mount -n -o move /sys ${rootmnt}/sys
133 mount -n -o move /proc ${rootmnt}/proc
134 mount -t 9p -o trans=virtio home ${rootmnt}/home
135 mount -t tmpfs none ${rootmnt}/root
136 mount -t tmpfs none ${rootmnt}/tmp
137 mount -t tmpfs none ${rootmnt}/run
138 mount -t tmpfs none ${rootmnt}/var/log
139 mount -t tmpfs none ${rootmnt}/var/tmp
140
141 rm ${rootmnt}/var/lib/urandom/random-seed 
142 rm -rf ${rootmnt}/etc/shadow
143 echo >${rootmnt}/etc/shadow
144 rm -rf ${rootmnt}/etc/ssh
145 mkdir ${rootmnt}/etc/ssh
146
147 rm -f ${rootmnt}/etc/rc2.d/* ${rootmnt}/etc/rc3.d/* ${rootmnt}/etc/rc4.d/* ${rootmnt}/etc/rc5.d/*
148
149 rm -rf ${rootmnt}/etc/fstab.d ${rootmnt}/etc/fstab
150 echo >${rootmnt}/etc/fstab
151
152 rm -rf ${rootmnt}/var/lib/dpkg/lock ${rootmnt}/var/lib/apt/lists/lock ${rootmnt}/var/cache/apt/archives/lock
153 rm -rf ${rootmnt}/etc/apt/apt.conf.d/*etckeeper ${rootmnt}/var/lib/dpkg/triggers/Lock
154
155 rm -rf ${rootmnt}/etc/network/interfaces.d ${rootmnt}/etc/network/interfaces
156 echo "auto lo eth0" >${rootmnt}/etc/network/interfaces
157 echo "iface lo inet loopback" >>${rootmnt}/etc/network/interfaces
158 echo "iface eth0 inet dhcp" >>${rootmnt}/etc/network/interfaces
159
160 if  [ -e ${rootmnt}/bin/bash ] ; then
161   shell=/bin/bash
162 else
163   shell=/bin/sh
164 fi
165
166 mv ${rootmnt}/etc/inittab ${rootmnt}/etc/inittab.orig
167 cp ${rootmnt}/etc/inittab.orig ${rootmnt}/etc/inittab
168 sed -i -e '/\/sbin\/.*getty/d' ${rootmnt}/etc/inittab
169 #echo "1:2345:respawn:/bin/sh" >>${rootmnt}/etc/inittab
170 #echo "T0:1234:respawn:/bin/sh" >>${rootmnt}/etc/inittab
171
172 echo "1:2345:respawn:/sbin/agetty -n -l \${shell} -o '-l' tty1 38400 linux" >>${rootmnt}/etc/inittab
173 echo "2:23:respawn:/sbin/agetty -n -l \${shell} -o '-l' tty2 38400 linux" >>${rootmnt}/etc/inittab
174 echo "T0:23:respawn:/sbin/agetty -n -l \${shell} -o '-l' -L ttyS0 9600 xterm" >>${rootmnt}/etc/inittab
175
176 exec switch_root ${rootmnt} /sbin/init
177 EOF
178
179   ) >${INITRD_DIR_ABS}/init
180
181   chmod 755 ${INITRD_DIR_ABS}/init
182
183   return 0
184 }
185
186 function build_initrd_gen_init_cpio()
187 {
188
189   (
190     cat <<EOF
191 dir /dev 755 0 0
192 nod /dev/tty0 644 0 0 c 4 0
193 nod /dev/tty1 644 0 0 c 4 1
194 nod /dev/tty2 644 0 0 c 4 2
195 nod /dev/tty3 644 0 0 c 4 3
196 nod /dev/tty4 644 0 0 c 4 4
197 #slink /init bin/busybox 700 0 0
198 dir /proc 755 0 0
199 dir /sys 755 0 0
200 dir /mnt 755 0 0
201 dir /mnt/root 755 0 0
202 dir /mnt/rootbase 755 0 0
203 dir /mnt/overlay 755 0 0
204 EOF
205     find ${INITRD_DIR} -mindepth 1 -type d -printf "dir /%P %m 0 0\n"
206     find ${INITRD_DIR} -type f -printf "file /%P %p %m 0 0\n"
207     find ${INITRD_DIR} -type l -printf "slink /%P %l %m 0 0\n"
208   ) > filelist
209
210   $GEN_INIT_CPIO filelist | gzip > ${RAMDISK_ARCHIVE}
211
212   return 0
213 }
214
215 function build_initrd_cpio()
216 {
217   ( cd ${INITRD_DIR_ABS} && find . | cpio --quiet -H newc -o  ) | gzip -9 > ${RAMDISK_ARCHIVE}
218 }
219
220 function run_virt_system()
221 {
222   $QEMU -enable-kvm -kernel ${KERNEL_IMAGE} \
223         -initrd ${RAMDISK_ARCHIVE} ${QEMU_MEMORY} \
224         -virtfs local,path=/,security_model=none,mount_tag=root \
225         -virtfs local,path=/home,security_model=none,mount_tag=home \
226         -net nic,macaddr=be:be:be:10:00:01,model=virtio,vlan=0 \
227         -net user,vlan=0 \
228         ${QEMU_KVM_ENABLE} ${QEMU_OUTPUT}
229
230 #     -chardev can,id=canbus0,port=can0
231 #     -device pci-can,chardev=canbus0,model=SJA1000
232 #     -device apohw -vga cirrus
233 #     -device mf624,port=55555
234
235   return $?
236 }
237
238 function clean_setup()
239 {
240   rm -rf ${RAMDISK_ARCHIVE} filelist ${INITRD_DIR_ABS}
241 }
242
243 if [ "$(whoami)" == "root" ] ; then
244   echo "$0: !!! TOO DANGEROUS TO RUN AS ROOT !!!"
245   exit 1;
246 fi
247
248 clean_setup || exit 1
249 echo "=== setup_initrd_content ==="
250 setup_initrd_content || exit 1
251 echo "=== build_initrd ==="
252 if [ -n "${GEN_INIT_CPIO}" ] ; then
253   build_initrd_gen_init_cpio || exit 1
254 else
255   build_initrd_cpio || exit 1
256 fi
257 echo "=== run_virt_system ==="
258 run_virt_system || exit 1