summaryrefslogtreecommitdiffstats
path: root/src/os-plugins/plugins/qemukvm/files/run-virt.include
blob: de0109a35673a0525ecd7d8ee748bd68b0a9f074 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# run-virt.include
# -----------------------------------------------------------------------------
# Copyright (c) 2009..2010 - RZ Uni Freiburg
# Copyright (c) 2009..2012 - OpenSLX GmbH
#
# This program/file is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your feedback to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org
# -----------------------------------------------------------------------------
# run-virt.include
#    - Include script for running the QEMU/Linux KVM virtual machine on an 
#      OpenSLX client via the run-virt.sh or run-vmgrid.sh
################################################################################

################################################################################
### Include general configuration
################################################################################
if [ -f ${OPENSLX_DEFAULT_CONFDIR}/plugins/${self}/${self}.conf ]; then
  . ${OPENSLX_DEFAULT_CONFDIR}/plugins/${self}/${self}.conf
else 
  writelog "Problems reading config file of ${self} plugin"
  # TODO: exit wenn conf wichtig
  #exit 1
fi

################################################################################
### Declaration of default variables
################################################################################

PLUGINCONFQK="${PLUGINCONFROOT}/${self}"
PLUGINDIRQK=${OPENSLX_DEFAULT_DIR}/plugin-repo/${self}
# create TMPDIR for all users
mkdir -m 1777 /tmp/${self} 2>/dev/null
# TMPDIR
QKTMPDIR="/tmp/${self}/${USER}/${VM_ID}"
# define dirs and files which can be removed after exit, be carefull!
RMDIRS="${QKTMPDIR}"
rm -rf ${RMDIRS} 2>/dev/null
mkdir -m 1777 -p ${QKTMPDIR} 2>/dev/null
# vmpath is the path to the vm, here an image (img|qcow*|vmdk)
diskfile=${vmpath}

# check the file type
if echo ${imgname} | grep -iE "img|qcow|vmdk" >/dev/null 2>&1; then
  imgtype=$(echo ${imgname##*.} | tr "[a-z]" "[A-Z]")
else
  writelog "${imgname} is not a valid image type (img|qcow*|vmdk), exiting!"
  exit 1
fi

# disable kvm for a range of older operating systems
case "${vmostype}" in
  beos*)
    VIRTCMDOPTS="${VIRTCMDOPTS} -no-kvm"
    if [ "${totalmem}" -ge "1000" ]; then
      forcemem=512
    else
      forcemem=256
    fi
    # add some OS specific options if required
    emu="qemu"
  ;;
  win3*|win*3*|Win*3*)
    VIRTCMDOPTS="${VIRTCMDOPTS} -no-kvm"
    forcemem=32
    emu="qemu"
  ;;
  win95*|Win*95)
    VIRTCMDOPTS="${VIRTCMDOPTS} -no-kvm"
    forcemem=96
    emu="qemu"
  ;;
  win*)
    VIRTCMDOPTS="${VIRTCMDOPTS} -no-kvm"
    forcemem=256
    emu="qemu"
  ;;
esac

# hot keys ALT+CTRL+SHIFT (does not work properly!?)
#VIRTCMDOPTS="${VIRTCMDOPTS} -alt-grab"

# display name, remove blanks because of cmdline problems
displayname=$(echo ${displayname} | sed -e "s, ,-,g;s,(,[,g;s,),],g")
VIRTCMDOPTS="${VIRTCMDOPTS} -name ${displayname}"

writelog "Directories:"
writelog "\tTMPDIR:\t\t\t$QKTMPDIR"
writelog "Diskimage:"
writelog "\tDisk type:\t\t$imgtype"
writelog "\tDisk file:\t\t$diskfile"

################################################################################
### Hardware checks
################################################################################

# memory part equal to vmware plugin
# percentage of memory to use for virtualbox in standard case
if [ -n "${forcemem}" ]; then
  mem="${forcemem}"
else
  permem=30
  if [ "${totalmem}" -ge "1600" ]; then
    permem=40
  fi
#  # check if /tmp is on harddisk
#  if grep -qe "/dev/.* /tmp " /proc/mounts ; then
#    permem=60
#    id44="1"
#    # Hack, if more than 2,5G RAM use 40% of Ram and write vmem into Ram as well
#    # (40% vmware | 40% confdir(vmem...) | 20% host
#    # VMplayer 2+ issue
#    # TODO: makes this sense for vbox?
#    #if [ "${totalmem}" -ge "2500" ]; then
#      #permem=40
#      #rmdir ${snapshotdir}
#      #snapshotdirold=${snapshotdir}
#      #snapshotdir=/dev/shm/${self}/${USER}/${VM_ID}
#      #mkdir -p ${snapshotdir}
#      #ln -sf ${snapshotdir} ${snapshotdirold}
#    #fi
#  fi
  mem=$(expr ${totalmem} / 100 \* ${permem})
  if [ "${id44}" = "1" ]; then
    memhost=$(expr ${totalmem} - ${mem})
  else
    memhost=$(expr ${totalmem} - ${mem} - ${mem})
  fi
  # static first
  permem=50
  mem=$(expr ${totalmem} / 100 \* ${permem})
  if [ "${mem}" -lt "256" ] || [ "${memhost}" -lt "256" ]; then
    writelog "Memory out of range: ${mem} MB (guest) / ${memhost} MB (host)!"
    writelog "Min. 256 MB for host and guest!"
    exit 1
  fi
fi

VIRTCMDOPTS="${VIRTCMDOPTS} -m ${mem}"

# network adaptor alternatives: rtl8139, pcnet, e1000
network_card=${network_card:=pcnet}
VIRTCMDOPTS="${VIRTCMDOPTS} -net nic,macaddr=${macaddr},model=${network_card}"

# define net kind and apply script
qemu_ifscr="script=${PLUGINCONFQK}/ifup,downscript=${PLUGINCONFQK}/ifdown"
case "${network_kind}" in
  bridge*)
    VIRTCMDOPTS="${VIRTCMDOPTS} -net tap,ifname=tapbridge${VM_ID},${qemu_ifscr}"
    sudo /opt/openslx/rootfs/sbin/tunctl -t tapbridge${VM_ID} -u ${USER} \
      >/dev/null 2>&1
    sudo /opt/openslx/rootfs/usr/sbin/brctl addif br0 tapbridge${VM_ID}
    POSTRUN="sudo /opt/openslx/rootfs/sbin/tunctl -d tapbridge${VM_ID}"
  ;;
  nat)
    VIRTCMDOPTS="${VIRTCMDOPTS} -net tap,ifname=tapnat${VM_ID},${qemu_ifscr}"
    sudo /opt/openslx/rootfs/sbin/tunctl -t tapnat${VM_ID} -u ${USER} \
      >/dev/null 2>&1
    POSTRUN="sudo /opt/openslx/rootfs/sbin/tunctl -d tapnat${VM_ID}"
  ;;
  host*)
    VIRTCMDOPTS="${VIRTCMDOPTS} -net tap,ifname=taphost${VM_ID},${qemu_ifscr}"
    sudo /opt/openslx/rootfs/sbin/tunctl -t taphost${VM_ID} -u ${USER} \
      >/dev/null 2>&1
    POSTRUN="sudo /opt/openslx/rootfs/sbin/tunctl -d taphost${VM_ID}"
  ;;
  *)
    network_kind="user"
    VIRTCMDOPTS="${VIRTCMDOPTS} -net user"
  ;;
esac

# translate boot, use if set else set to HardDisk
if [ -n "${boot}" ]; then
  case ${boot} in
    n*) # usually support for a,c,d,n, stands for Floppy, HD, CD-ROM, Network
      boot="n"
      # TODO: does not work yet, can be removed if tap1 solution better
      if [ "${network_kind}" = "user" ] && [ -n "${virtualbox_tftpdir}" ]; then
        VIRTCMDOPTS="${VIRTCMDOPTS},tftp=${virtualbox_tftpdir}"
      fi
    ;;
    # later maybe c|disk|hd*|sd*) for HD and d|cd*) for CD-ROM
    *) 
      boot="c"
    ;;
  esac
else
  boot="c"
fi

# external GUI port
vncport="590${VM_ID}"

# TODO: most of the following does not make much sense yet ...
# virtual fd/cd/dvd and drive devices, floppy b: for configuration
# if $floppy_0 from run-virt.include set then fdtest="TRUE"
fdtest=
fdtest=${floppy_0:+"TRUE"}
# if $fdtest not set floppy0="FALSE", else "TRUE"
floppy0=${fdtest:-"FALSE"}
floppy1="TRUE"
floppy1name="/var/lib/virt/vmchooser/loopimg/fd.img"
# if $cdrom_0 from run-virt.include set then cdtest="TRUE"
cdtest=
cdtest=${cdrom_0:+"TRUE"}
# if $cdtest not set cdrom0="FALSE", else "TRUE"
cdrom0=${cdtest:-"FALSE"}
# if $cdrom_1 from run-virt.include set then cdtest="TRUE"
cdtest=
cdtest=${cdrom_1:+"TRUE"}
# if $cdtest not set cdrom1="FALSE", else "TRUE"
cdrom1=${cdtest:-"FALSE"}
# ide is expected default, test for the virtual disk image type should
# be done while creating the runscripts ...
ide="TRUE"
hddrv="ide"

# special feature for real and virtual floppy disks
[ ${floppy0} == TRUE ] && VIRTCMDOPTS="${VIRTCMDOPTS} -fda /dev/fd0"
#VIRTCMDOPTS="${VIRTCMDOPTS} -fdb ${floppy1name}"

# audio
VIRTCMDOPTS="${VIRTCMDOPTS} -soundhw all"

# serial
[ ${serial} == ttyS0 ] && VIRTCMDOPTS="${VIRTCMDOPTS} -serial /dev/ttyS0"

writelog "Virtual Hardware:"
writelog "\tGuest RAM:\t\t${mem} MB"
# echo nur wenn memhost gesetzt
[ -n "${memhost}" ] && writelog "\tHost RAM:\t\t${memhost} MB"
writelog "\tMAC address:\t\t$macaddr"
writelog "\tNetwork card:\t\t${network_card}"
writelog "\tNetwork kind:\t\t${network_kind}"
#writelog "\tCD-ROM1:\t${cdrom0}"
#writelog "\tCD-ROM2:\t${cdrom1}"
#writelog "\tFloppy_A:\t${floppy0}"
#writelog "\tFloppy_B:\t${floppy1}"
#writelog "\tShared Folders 'home':\t/home/${USER}"

################################################################################
### finally set env for run-virt.sh
################################################################################

# configure and start samba service to provide user's home directory
if [ -f /usr/sbin/smbd ] ; then
  sudo /opt/openslx/rootfs/bin/sed -i /etc/qemu/smb.conf -e "s,$USER,USER,"
  sudo /usr/sbin/nmbd -s /etc/qemu/smb.conf
  sudo /usr/sbin/smbd -s /etc/qemu/smb.conf
fi


#-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]
#       [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]
#       [,cache=writethrough|writeback|none][,format=f][,serial=s]
#       [,boot=on|off]
#-snapshot       write to temporary files instead of disk image files
#-usb            enable the USB driver (will be the default soon)
#-usbdevice name add the host or guest USB device 'name'
#-nographic      disable graphical output and redirect serial I/Os to console
#-vga [std|cirrus|vmware|none]                select video card type
#-vnc display    start a VNC server on display
#-tftp dir       allow tftp access to files in dir [-net user]
#-bootp file     advertise file in BOOTP replies
#-no-fd-bootchk  disable boot signature checking for floppy disks
#-no-acpi        disable ACPI
#-kernel bzImage use 'bzImage' as kernel image
#-append cmdline use 'cmdline' as kernel command line
#-initrd file    use 'file' as initial ram disk
#-serial dev     redirect the serial port to char device 'dev'
#-parallel dev   redirect the parallel port to char device 'dev'
#-monitor dev    redirect the monitor to char device 'dev'
#-pidfile file   write PID to 'file'
#-d item1,...    output log to /tmp/qemu.log (use -d ? for a list of log items)
#-L path         set the directory for the BIOS, VGA BIOS and keymaps
#-bios file      set the filename for the BIOS
#-no-kvm         disable KVM hardware virtualization
#-enable-nesting enable support for running a VM inside the VM (AMD only)
#-no-reboot      exit instead of rebooting
#-no-shutdown    stop before shutdown
#-loadvm [tag|id]
#                start right away with a saved state (loadvm in monitor)
#-daemonize      daemonize QEMU after initializing
#-kvm-shadow-memory megs set the amount of shadow pages to be allocated
#-mem-path       set the path to hugetlbfs/tmpfs mounted directory, also
#                enables allocation of guest memory with huge pages
#-mem-prealloc   toggles preallocation of -mem-path backed physical memory
#                at startup.  Default is enabled.
#-option-rom rom load a file, rom, into the option ROM space
#-localtime      set the real time clock to local time [default=utc]
#-startdate      select initial date of the clock
#-icount [N|auto]
#                enable virtual instruction counter with 2^N clock ticks per instruction
#-echr chr       set terminal escape character instead of ctrl-a
#-virtioconsole c
#                set virtio console
#-show-cursor    show cursor

# check for kvm modules
if [ -c /dev/kvm -a "x${emu}" != "xqemu" ]; then
  for tool in kvm qemu-kvm; do
    VIRTCMD=$(which ${tool} 2>/dev/null)
    [ -n "$VIRTCMD" ] && break
  done
else
  VIRTCMD=$(which qemu-system-i386 2>/dev/null)
  writelog "KVM available but /dev/kvm not found!"
  writelog "Trying fallback to QEMU emulation."
fi

if [ -z "${VIRTCMD}" ]; then
  writelog "No QEMU/KVM found, exiting!"
  rm -rf ${RMDIRS}
  exit 1
elif [ "${VIRTCMD##*/}" = "qemu-system-i386" ]; then
  writelog "User space emulation:\t\t${VIRTCMD}"
else
  writelog "User space virtualization:\t${VIRTCMD}"
fi

# using snapshots
VIRTCMDOPTS="${VIRTCMDOPTS} -snapshot"

# TODO: boot als var, -boot n, tftp...
VIRTCMDOPTS="${VIRTCMDOPTS} -boot c"

# set headless mode
# define first, you do not want VIRTCMDOPTS from graphical start
VIRTCMDHL=${VIRTCMD}
VIRTCMDOPTSHL="${VIRTCMDOPTS} -nographic ${diskfile}"

# graphical start
# vga std/cirrus/vmware?
#VIRTCMDOPTS="${VIRTCMDOPTS} -vga std -full-screen ${diskfile}"
VIRTCMDOPTS="${VIRTCMDOPTS} ${diskfile}"