summaryrefslogtreecommitdiffstats
path: root/os-plugins/plugins/xen/files/run-virt.include
blob: 36a1d1f4c98ce97fc2e1fe97a9f4b8b4e23f02dc (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
# run-virt.include
# -----------------------------------------------------------------------------
# Copyright (c) 2010 - RZ Uni Freiburg
# Copyright (c) 2010 - 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 Xen 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"
  exit 1
fi

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

PLUGINCONFXEN="${PLUGINCONFROOT}/${self}"
# create TMPDIR for all users
mkdir -m 1777 /tmp/${self} 2>/dev/null
# dir for configs
confdir="/tmp/${self}/${USER}/${VM_ID}"
# use vm_shortname for VM name since vm_name can be very long
# add id so we can start more tahn one of the same image
vm_name="${vm_shortname}-${VM_ID}"
conffile=${confdir}/${vm_name}
# define dirs and files which can be removed after exit, be carefull!
rm -rf ${confdir} 2>/dev/null
mkdir -p ${confdir} 2>/dev/null

xen_kernel=${kernel:-"/boot/vmlinuz-xen"}
xen_ramdisk=${initramfs:-"/boot/initrd-xen"}
xen_extra="xencons=tty 3"
[ -n "${imgname}" ] &&  xen_disk="disk = [ 'file:${vmpath},hda1,r' ]"

writelog "Directories:"
writelog "\tConfig dir:\t\t$confdir"

################################################################################
### 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
  #permem=40
  #mem=$(expr ${totalmem} * ${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

# translate network kinds (nat, bridged, host-only)
case "${network_kind}" in
  bridge*)
    xen_vif="mac=$macaddr, bridge=br0"
  ;;
  *)
    xen_vif="mac=$macaddr"
esac
xen_dhcp="dhcp" # off

# translate boot, use if set else set to disk
xen_root=/dev/hda1
case ${boot} in
  n*|tftp)
    pxe_label=$(grep -o 'pxe_label param=.*"' ${xmlfile} \
                | sed -e "s/&.*;/; /g" | awk -F '"' '{print $2}')
    pxe_label=$(grep -A 3 "^LABEL *${pxe_label}" \
                ${xen_tftpdir}/pxelinux.cfg/default 2>/dev/null \
                | grep "^ *KERNEL ")
    if [ -n "${pxe_label}" ] && [ -n "${xen_tftpdir}" ]; then
      xen_root=/dev/nfs
      xen_kernel="${xen_tftpdir}/$(echo ${pxe_label} \
                  | awk -F " --- " '{print $2}' | awk '{print $1}')"
      xen_ramdisk="${xen_tftpdir}/$(echo ${pxe_label} \
                   | awk -F " --- " '{print $3}')"
    fi
  ;;
  # later maybe c|disk|hd*|sd*) for HD and d|cd*) for CD-ROM
  *)
    xen_root=/dev/hda1
  ;;
esac

[ ${diskless} -eq 0 ] && writelog "Diskimage:"
[ ${diskless} -eq 0 ] && writelog "\tMachine diskfile:\t$vmpath"
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"

################################################################################
### Pepare and configure virtual machine and disk image
################################################################################

. ${PLUGINCONFXEN}/machine.include > ${conffile}

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

# set the variables appropriately (several interfaces with different names)
VIRTCMD=$(which xm 2>/dev/null)
# '-c' for console, no graphical output available, later vnc possible
VIRTCMDOPTS="create -c ${conffile}"

# set headless mode
VIRTCMDHL=$(which xm 2>/dev/null)
VIRTCMDOPTSHL="create ${conffile}"