Add a helper for blocking a uthread from VC ctx
[akaros.git] / scripts / ak-scripts / ak-launch-qemu.sh
1 #!/usr/bin/env bash
2 #
3 # Copyright (c) 2015 Google Inc.
4 # Kevin Klues <klueska@cs.berkeley.edu>
5 # See LICENSE for details.
6
7 init_script_default="\"Read from Akaross .config\""
8
9 function short_description() {
10         echo "Launch qemu with a running instance of Akaros"
11 }
12
13 function usage() {
14         echo "Usage:"
15         echo "    ${cmd} -h | --help"
16         echo "    ${cmd} [ --akaros-root=<ak> ]"
17         echo "    ${cmd//?/ } [ --init-script=<script> ]"
18         echo "    ${cmd//?/ } [ --qemu-cmd=<cmd> ]"
19         echo "    ${cmd//?/ } [ --cpu-type=<cpu> ]"
20         echo "    ${cmd//?/ } [ --num-cores=<nc> ]"
21         echo "    ${cmd//?/ } [ --memory-size=<ms> ]"
22         echo "    ${cmd//?/ } [ --network-card=<nc> ]"
23         echo "    ${cmd//?/ } [ --host-tcp-port=<htp> ]"
24         echo "    ${cmd//?/ } [ --akaros-tcp-port=<atp> ]"
25         echo "    ${cmd//?/ } [ --host-udp-port=<hup> ]"
26         echo "    ${cmd//?/ } [ --akaros-udp-port=<aup> ]"
27         echo "    ${cmd//?/ } [ --disable-kvm ]"
28         echo ""
29         echo "Options:"
30         echo "    -h --help               Display this screen and exit"
31         echo "    --akaros-root=<ak>      The path to the root of the akaros tree"
32         echo "                            [default: \$AKAROS_ROOT]"
33         echo "    --init-script=<script>  The path to a custom init script to run after boot"
34         echo "                            [default: ${init_script_default} ]"
35         echo "    --qemu-cmd=<cmd>        The actual qemu command to use"
36         echo "                            [default: qemu-system-x86_64]"
37         echo "    --cpu-type=<cpu>        The type of cpu to launch qemu with"
38         echo "                            [default: kvm64,+vmx]"
39         echo "    --num-cores=<nc>        The number of cores to launch qemu with"
40         echo "                            [default: 8]"
41         echo "    --memory-size=<ms>      The amount of memory to launch qemu with (kB)"
42         echo "                            [default: 4096]"
43         echo "    --network-card=<nc>     The network card to launch qemu qith"
44         echo "                            [default: e1000]"
45         echo "    --host-tcp-port=<htp>   The host TCP port to forward network traffic"
46         echo "                            [default: 5555]"
47         echo "    --akaros-tcp-port=<atp> The Akaros TCP port to receive network traffic"
48         echo "                            [default: 5555]"
49         echo "    --host-udp-port=<hup>   The host UDP port to forward network traffic"
50         echo "                            [default: 5555]"
51         echo "    --akaros-udp-port=<aup> The Akaros UDP port to receive network traffic"
52         echo "                            [default: 5555]"
53         echo "    --disable-kvm           Disable kvm for qemu"
54 }
55
56 function main() {
57         # Set these command line arguments before invoking main
58         # Check the sanity of our environment variables
59         check_vars akaros_root qemu_cmd init_script cpu_type num_cores \
60                    memory_size network_card host_tcp_port akaros_tcp_port \
61                    host_udp_port akaros_udp_port
62         check_dirs akaros_root
63         check_execs qemu_cmd
64         if [ "\"${init_script}\"" != "${init_script_default}" ]; then
65                 check_files init_script
66                 local init_script_set="true"
67         fi
68
69         # Set some local variables
70         local akaros_bin=${akaros_root}/kern/kfs/bin
71         local akaros_kernel=${akaros_root}/obj/kern/akaros-kernel
72         local akaros_config=${akaros_root}/.config
73         local akaros_config_backup=${akaros_root}/.config.backup
74         local akinit_script="/bin/ak-init.sh"
75         local akinit_script_path=${akaros_root}/kern/kfs/${akinit_script}
76         local qemu_network="-net nic,model=${network_card} \
77                             -net user,hostfwd=tcp::${host_tcp_port}-:${akaros_tcp_port},hostfwd=udp::${host_udp_port}-:${akaros_udp_port}"
78
79         # Launch the monitor if not launched yet and set the monitor tty
80         local monitor_tty="$(ak launch-qemu-monitor --print-tty-only)"
81         if [ "${monitor_tty}" != "" ]; then
82                 local qemu_monitor="-monitor ${monitor_tty}"
83         fi
84
85         # Output a warning if we are trying to enable kvm for qemu, but we are not
86         # part of the kvm group
87         if [ "${disable_kvm}" == "false" ]; then
88                 groups ${USER} | grep &>/dev/null '\bkvm\b'
89                 if [ "${?}" != "0" ]; then
90                         echo "You are not part of the kvm group!"
91                         echo "    This may cause problems with running qemu with kvm enabled."
92                         echo "    To disable kvm, rerun this script with --disable-kvm."
93                 fi
94                 local qemu_kvm="-enable-kvm"
95         fi
96
97         # Make a backup of ${akaros_config} and set the init script in it
98         if [ "${init_script_set}" == "true" ]; then
99                 echo "Setting custom init script"
100                 cp ${akaros_config} ${akaros_config_backup}
101                 cp ${init_script} ${akinit_script_path}
102                 if [ "$(grep 'CONFIG_RUN_INIT_SCRIPT=' ${akaros_config})" = "" ]; then
103                         echo "CONFIG_RUN_INIT_SCRIPT=y" >> ${akaros_config}
104                         echo "CONFIG_INIT_SCRIPT_PATH_AND_ARGS=\"${akinit_script}\"" >> ${akaros_config}
105                 else
106                         sed -ie 's#CONFIG_INIT_SCRIPT_PATH_AND_ARGS=.*#CONFIG_INIT_SCRIPT_PATH_AND_ARGS="'${akinit_script}'"#' ${akaros_config}
107                 fi
108         fi
109
110         # Rebuild akaros
111         echo "Rebuilding akaros"
112         cd ${akaros_root}
113         touch ${akaros_config}
114         make -j
115         cd - > /dev/null
116
117         # Restore the original ${akaros_config} and delete the init script
118         if [ "${init_script_set}" == "true" ]; then
119                 mv ${akaros_config_backup} ${akaros_config}
120                 rm ${akinit_script_path}
121         fi
122
123         # Rebuild akaros test and libs
124         echo "Rebuilding tests and libs"
125         cd ${akaros_root}
126         make -j install-libs
127         make -j tests
128         make fill-kfs
129         cd - > /dev/null
130
131         # Launching qemu
132         echo "Launching qemu"
133         local stty_state=$(stty -g)
134         stty raw
135         ${qemu_cmd} -s ${qemu_kvm} ${qemu_network} ${qemu_monitor} -cpu ${cpu_type} \
136                     -smp ${num_cores} -m ${memory_size} -kernel ${akaros_kernel} \
137                     -nographic
138         stty ${stty_state}
139 }
140