3 # QEMU trick to dive user in his own hoe environment
4 # as root in a virualized shadow of his own system
6 # (C) 2013 Pavel Pisa <pisa@cmp.felk.cvut.cz>
8 # THE TSCRIPT IS PROVIDED “AS IS” WITHOUT WARRANTY
9 # OF ANY KIND, EITHER EXPRESSED OR IMPLIED.
11 # There is no limitation of scrip use, modification
12 # and distribution or relicensing.
14 # Some used ideas are documented at CTU FEE A4M35OSP
16 # http://support.dce.felk.cvut.cz/osp/cviceni/2/
17 # http://support.dce.felk.cvut.cz/osp/cviceni/2-en/
19 # and Departemnt Of Control Engineering IT Wiki
20 # https://support.dce.felk.cvut.cz/
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"
33 QEMU_KVM_ENABLE="-enable-kvm"
34 #QEMU_OUTPUT="-vga cirrus -serial null"
35 QEMU_OUTPUT="-nographic -append console=ttyS0"
37 INITRD_DIR_ABS=$(readlink -f ${INITRD_DIR})
39 function libraries_for_binary()
41 ldd $1 | sed -n -e 's#^\t\(/[^ ]*\) .*$#\1#p' -e 's#^\t\([^ ]*\) => \([^ ]\+\) .*$#\2#p'
50 mkdir -p ${tgt_dir}/$(dirname ${src}) || return 1
51 cp -av ${src} ${tgt_dir}/${src#/} || return 1
57 if [ ${l:0:1} != '/' ] ; then
58 l=$(dirname ${src})/${l}
64 function setup_initrd_content()
66 mkdir -p ${INITRD_DIR_ABS}
67 mkdir -p ${INITRD_DIR_ABS}/bin
68 mkdir -p ${INITRD_DIR_ABS}/etc/init.d
70 LIBS=$(libraries_for_binary ${BUSYBOX})
71 if [ -n "${RESIZE}" ] ; then
72 LIBS+=" "$(libraries_for_binary ${RESIZE})
75 LIBS="$(for i in ${LIBS} ; do echo $i ; done | sort -u)"
78 deep_copy ${i} ${INITRD_DIR_ABS}
81 cp -a ${BUSYBOX} ${INITRD_DIR_ABS}/bin
82 ( cd ${INITRD_DIR_ABS}/bin && ln -s busybox sh )
84 if [ -n "${RESIZE}" ] ; then
85 cp -a ${RESIZE} ${INITRD_DIR_ABS}/bin
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#/}
99 root_dir_list="/proc /sys /dev /mnt /mnt/root /mnt/rootbase /mnt/overlay /tmp"
103 for d in $root_dir_list ; do
108 echo "Starting QEMU trick"
109 mount -t proc none /proc
110 mount -t sysfs none /sys
111 mount -t tmpfs none /dev
114 for m in ${MODULES_LIST} ; do
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
124 stty cols \$COLUMNS rows \$LINES
126 mv /dev/tty-backup /dev/tty
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
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
147 rm -f ${rootmnt}/etc/rc2.d/* ${rootmnt}/etc/rc3.d/* ${rootmnt}/etc/rc4.d/* ${rootmnt}/etc/rc5.d/*
149 rm -rf ${rootmnt}/etc/fstab.d ${rootmnt}/etc/fstab
150 echo >${rootmnt}/etc/fstab
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
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
160 if [ -e ${rootmnt}/bin/bash ] ; then
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
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
176 exec switch_root ${rootmnt} /sbin/init
179 ) >${INITRD_DIR_ABS}/init
181 chmod 755 ${INITRD_DIR_ABS}/init
186 function build_initrd_gen_init_cpio()
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
201 dir /mnt/root 755 0 0
202 dir /mnt/rootbase 755 0 0
203 dir /mnt/overlay 755 0 0
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"
210 $GEN_INIT_CPIO filelist | gzip > ${RAMDISK_ARCHIVE}
215 function build_initrd_cpio()
217 ( cd ${INITRD_DIR_ABS} && find . | cpio --quiet -H newc -o ) | gzip -9 > ${RAMDISK_ARCHIVE}
220 function run_virt_system()
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 \
228 ${QEMU_KVM_ENABLE} ${QEMU_OUTPUT}
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
238 function clean_setup()
240 rm -rf ${RAMDISK_ARCHIVE} filelist ${INITRD_DIR_ABS}
243 if [ "$(whoami)" == "root" ] ; then
244 echo "$0: !!! TOO DANGEROUS TO RUN AS ROOT !!!"
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
255 build_initrd_cpio || exit 1
257 echo "=== run_virt_system ==="
258 run_virt_system || exit 1