Add space between text and url in ak-code-review
[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-port=<hp> ]"
24         echo "    ${cmd//?/ } [ --akaros-port=<ap> ]"
25         echo "    ${cmd//?/ } [ --disable-kvm ]"
26         echo ""
27         echo "Options:"
28         echo "    -h --help               Display this screen and exit"
29         echo "    --akaros-root=<ak>      The path to the root of the akaros tree"
30         echo "                            [default: \$AKAROS_ROOT]"
31         echo "    --init-script=<script>  The path to a custom init script to run after boot"
32         echo "                            [default: ${init_script_default} ]"
33         echo "    --qemu-cmd=<cmd>        The actual qemu command to use"
34         echo "                            [default: qemu-system-x86_64]"
35         echo "    --cpu-type=<cpu>        The type of cpu to launch qemu with"
36         echo "                            [default: kvm64,+vmx]"
37         echo "    --num-cores=<nc>        The number of cores to launch qemu with"
38         echo "                            [default: 8]"
39         echo "    --memory-size=<ms>      The amount of memory to launch qemu with (kB)"
40         echo "                            [default: 4096]"
41         echo "    --network-card=<nc>     The network card to launch qemu qith"
42         echo "                            [default: e1000]"
43         echo "    --host-port=<hp>        The host port to forward network traffic"
44         echo "                            [default: 5555]"
45         echo "    --akaros-port=<hp>      The akaros port to receive network traffic"
46         echo "                            [default: 5555]"
47         echo "    --disable-kvm           Disable kvm for qemu"
48 }
49
50 function main() {
51         # Set these command line arguments before invoking main
52         # Check the sanity of our environment variables
53         check_vars akaros_root qemu_cmd init_script cpu_type num_cores \
54                    memory_size network_card host_port akaros_port
55         check_dirs akaros_root
56         check_execs qemu_cmd
57         if [ "\"${init_script}\"" != "${init_script_default}" ]; then
58                 check_files init_script
59                 local init_script_set="true"
60         fi
61
62         # Set some local variables
63         local akaros_bin=${akaros_root}/kern/kfs/bin
64         local akaros_kernel=${akaros_root}/obj/kern/akaros-kernel
65         local akaros_config=${akaros_root}/.config
66         local akaros_config_backup=${akaros_root}/.config.backup
67         local akinit_script="/bin/ak-init.sh"
68         local akinit_script_path=${akaros_root}/kern/kfs/${akinit_script}
69         local qemu_network="-net nic,model=${network_card} \
70                             -net user,hostfwd=tcp::${host_port}-:${akaros_port}"
71
72         # Launch the monitor if not launched yet and set the monitor tty
73         local monitor_tty="$(ak launch-qemu-monitor --print-tty-only)"
74         if [ "${monitor_tty}" != "" ]; then
75                 local qemu_monitor="-monitor ${monitor_tty}"
76         fi
77
78         # Output a warning if we are trying to enable kvm for qemu, but we are not
79         # part of the kvm group
80         if [ "${disable_kvm}" == "false" ]; then
81                 groups ${USER} | grep &>/dev/null '\bkvm\b'
82                 if [ "${?}" != "0" ]; then
83                         echo "You are not part of the kvm group!"
84                         echo "    This may cause problems with running qemu with kvm enabled."
85                         echo "    To disable kvm, rerun this script with --disable-kvm."
86                 fi
87                 local qemu_kvm="-enable-kvm"
88         fi
89
90         # Make a backup of ${akaros_config} and set the init script in it
91         if [ "${init_script_set}" == "true" ]; then
92                 echo "Setting custom init script"
93                 cp ${akaros_config} ${akaros_config_backup}
94                 cp ${init_script} ${akinit_script_path}
95                 if [ "$(grep 'CONFIG_RUN_INIT_SCRIPT=' ${akaros_config})" = "" ]; then
96                         echo "CONFIG_RUN_INIT_SCRIPT=y" >> ${akaros_config}
97                         echo "CONFIG_INIT_SCRIPT_PATH_AND_ARGS=\"${akinit_script}\"" >> ${akaros_config}
98                 else
99                         sed -ie 's#CONFIG_INIT_SCRIPT_PATH_AND_ARGS=.*#CONFIG_INIT_SCRIPT_PATH_AND_ARGS="'${akinit_script}'"#' ${akaros_config}
100                 fi
101         fi
102
103         # Rebuild akaros
104         echo "Rebuilding akaros"
105         cd ${akaros_root}
106         touch ${akaros_config}
107         make -j
108         cd - > /dev/null
109
110         # Restore the original ${akaros_config} and delete the init script
111         if [ "${init_script_set}" == "true" ]; then
112                 mv ${akaros_config_backup} ${akaros_config}
113                 rm ${akinit_script_path}
114         fi
115
116         # Rebuild akaros test and libs
117         echo "Rebuilding tests and libs"
118         cd ${akaros_root}
119         make -j install-libs
120         make -j tests
121         make fill-kfs
122         cd - > /dev/null
123
124         # Launching qemu
125         echo "Launching qemu"
126         local stty_state=$(stty -g)
127         stty raw
128         ${qemu_cmd} -s ${qemu_kvm} ${qemu_network} ${qemu_monitor} -cpu ${cpu_type} \
129                     -smp ${num_cores} -m ${memory_size} -kernel ${akaros_kernel} \
130                     -nographic
131         stty ${stty_state}
132 }
133