summaryrefslogblamecommitdiffstats
path: root/core/modules/run-virt/data/opt/openslx/scripts/systemd-run_virt_env
blob: 68afd18929830af0f08de50ef4946fc207c37936 (plain) (tree)
1
2
3
4

                                                                               

                                         











                                                                               

                                                                           







                                                                                
                                                               



                                        


                                                               

                                                              





                                                  




















                                                                                       
                                           


                                                                                







                                                                            



                                                       
                                                           
    
                                                


                                         
                                                        


                                                  

                                                             
                                                      

                                                           
      
                                               



                              
                                    
                 
                                                                                                     
  
                                                             

                                                                                      






                                                                                                            
 
                    
                                                              
          
                                                              


                                                                                        

                                                                                               



                    

                                                                              

































                                                                                                     





                                                              
                     
                             

    




                                                                                                        
                                                     


                                                           








                                                                                                                                                             

                                                                                                                     
                                                                                                                     

                        
               
                            





                                                 
#!/bin/ash
# -----------------------------------------------------------------------------
# Copyright (c) 2007..2016 - RZ Uni FR
# Copyright (c) 2007..2016 - OpenSLX GmbH
#
# This program 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 suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# systemd-vmchooser_env
#    - This is the preparation script for the configuration of the virtuali-
#      zation tools used later on.
#    - This creates '$VMCHOOSER_CONF_DIR/virtualization.conf' that is later
#      evaluated by the runvirt script.
################################################################################

################################################################################
### Define default dirs / get configs
################################################################################

export PATH=$PATH:/opt/openslx/bin:/opt/openslx/sbin

# source config file with vars (e.g. ipaddr, macaddr, HOSTNAME)
. /opt/openslx/config

# from plugins/vmchooser/XX_vmchooser.sh
VMCHOOSER_DIR="/opt/openslx/vmchooser"
VMCHOOSER_CONF_DIR="${VMCHOOSER_DIR}/config"
VMCHOOSER_VIRT_CONF="${VMCHOOSER_CONF_DIR}/virtualization.conf"
DHCP_NAT_CONF="${VMCHOOSER_CONF_DIR}/udhcpd-nat1.conf"

# setup more scratch space for virtual machines, if configured
for DIR in /tmp/virt /var/log/samba /run/samba; do
	if ! mkdir -p "$DIR"; then
		echo "Could not create '$DIR'!"
		exit 1
	fi
done

chmod a+rwxt /tmp/virt

# enable normal users to setup the extended virtual machine redo file space
# and image source via network block device (NBD only at the moment)
# allow to start/stop samba on virtual devices (latter one ignores multiple
# independent daemons) 

#cat >> /mnt/etc/sudoers << EOF
# allow to start and stop the special /tmp redo file space (RAM + NBD/NFS server RAID)
#ALL ALL=NOPASSWD: /sbin/losetup /dev/loop* 
#ALL ALL=NOPASSWD: /sbin/mdadm -C /dev/md0 -l linear --raid-devices=2 /dev/loop* /dev/*
#ALL ALL=NOPASSWD: /opt/openslx/rootfs/usr/sbin/nbd-client * * /dev/nbd*
# allow to configure and start/stop samba services
#ALL ALL=NOPASSWD: /opt/openslx/rootfs/bin/sed -i /etc/*/smb.conf -e *
#ALL ALL=NOPASSWD: /usr/sbin/nmbd -s /etc/*/smb.conf
#ALL ALL=NOPASSWD: /usr/sbin/smbd -s /etc/*/smb.conf
#ALL ALL=NOPASSWD: /opt/openslx/rootfs/usr/bin/killall nmbd smbd
#EOF

################################################################################
### Determine host's hardware configuration
################################################################################

# estimate free mem
MEM_FREE=$(grep -i "^memfree:" /proc/meminfo | awk '{print $2}')
MEM_CACHED=$(grep -i "^cached:" /proc/meminfo | awk '{print $2}')
MEM_SWAPCACHED=$(grep -i "^swapcached:" /proc/meminfo | awk '{print $2}')
[ -z "$MEM_FREE" ] && MEM_FREE="2048000"
[ -z "$MEM_CACHED" ] && MEM_CACHED="0"
[ -z "$MEM_SWAPCACHED" ] && MEM_SWAPCACHED="0"
MEM_TOTAL="$(expr $(expr $MEM_FREE + $MEM_CACHED + $MEM_SWAPCACHED) / 1024)"
echo "HOST_MEM_TOTAL=${MEM_TOTAL}" > "${VMCHOOSER_VIRT_CONF}"

# determine number of cpu cores
CPU_CORES=$(grep '^processor.*:' /proc/cpuinfo | wc -l)
if [ "${CPU_CORES}" -gt "1" 2>/dev/null ]; then
  echo "CPU_CORES=${CPU_CORES}" >> "${VMCHOOSER_VIRT_CONF}"
else
  echo 'CPU_CORES=1' >> "${VMCHOOSER_VIRT_CONF}"
fi

# general machine architecture (32/64bit)
echo "HOST_ARCH=$(uname -m)" >> "${VMCHOOSER_VIRT_CONF}"

# check for VT enabled CPU
if grep -q '^flags.*\<vmx\>' "/proc/cpuinfo"; then
    echo "VTFLAG=1" >> "${VMCHOOSER_VIRT_CONF}"
    echo 'KVM_MODULE="kvm_intel"' >> "${VMCHOOSER_VIRT_CONF}"
  elif grep -q '^flags.*\<svm\>' "/proc/cpuinfo"; then
    echo "VTFLAG=1" >> "${VMCHOOSER_VIRT_CONF}"
    echo 'KVM_MODULE="kvm_amd"' >> "${VMCHOOSER_VIRT_CONF}"
  else
    echo "VTFLAG=0" >> "${VMCHOOSER_VIRT_CONF}"
fi

# write mac
if [ -n "$SLX_PXE_MAC" ]; then
	HOSTMACADDR="${SLX_PXE_MAC}"
else ## Fallback:
	HOSTMACADDR="$(ip a | grep ether | grep -o -E -i '([0-9a-f]{2}:){5}[0-9a-f]{2}' | head -n 1)"
fi
echo "HOSTMACADDR='$HOSTMACADDR'" >> "${VMCHOOSER_VIRT_CONF}"
# TODO: We should only generate the suffix here (last 3 octets) as the first 3 are
# dependant on the virtualizer/emulator. Check if any run-virt.include still relies on
# $macguestpart/$macaddr (now VM_MAC_ADDR). If so, fix it to use its specific first 3 bytes
# and append $MACADDRSUFFIX. For now, keep MACADDRPREFIX as vmware's defaults and overwrite
# it if needed.
MACADDRPREFIX='00:50:56'
MACADDRSUFFIX="$(echo "$HOSTMACADDR" | awk -F ":" '{print "%VMID%:" $(NF-1) ":" $NF}' | tr '[a-z]' '[A-Z]')"
echo "MACADDRPREFIX='$MACADDRPREFIX'" >> "${VMCHOOSER_VIRT_CONF}"
echo "MACADDRSUFFIX='$MACADDRSUFFIX'" >> "${VMCHOOSER_VIRT_CONF}"

# read in ip address
echo "HOSTIP=${SLX_PXE_CLIENT_IP}" >> "${VMCHOOSER_VIRT_CONF}"
# hostname
echo "HOSTNAME=\"$(hostname -s)\"" >> "${VMCHOOSER_VIRT_CONF}"

# get further hw information and check for traces of available optical and floppy drives
j=0
for CDROM in $(dmesg|grep -i "Attached scsi CD-ROM sr" | sed "s,.*Attached scsi CD-ROM ,,"); do
    echo "CDROM_$j=/dev/${CDROM}" >> "${VMCHOOSER_VIRT_CONF}"
    j=$(expr $j + 1)
done

j=0
for FLOPPY in $(dmesg|grep -i "Floppy drive"|sed "s,.*(s): ,,;s, is .*,,"); do
    echo "FLOPPY_$j=/dev/${FLOPPY}" >> "${VMCHOOSER_VIRT_CONF}"
    j=$(expr $j + 1)
done

################################################################################
### Setup VM networking
################################################################################

# there will be three types of network interfaces for virtual machines available
# all will be configured for every type of virtualization tool
# a) br0 is the direct host bridge into the local LAN (br0 already exists)
#    vmnet0 for VMware
#    vboxnet0 for VirtualBox
#    kvmnet0* for Qemu/KVM
# b) nat1 is an internal bridge which "NATes" into the local LAN (with DHCP)
#    vmnet1 for VMware
#    vboxnet1 for VirtualBox
#    kvmnet1* for Qemu/KVM
# c) vsw2 is an internal host bridge to connect software defined LANs to
#    vmnet2 for VMware
#    vboxnet2 for VirtualBox
#    kvmnet2* for Qemu/KVM

# creating and configuring nat0
# 192.168.101.0/24 is vm nat. If you ever change this there are a couple of other files
# where you'd need to make changes, so think twice before doing so. ;)
brctl addbr nat1
ip link set dev nat1 up
ip addr add 192.168.101.1/24 dev nat1
echo "1" >/proc/sys/net/ipv4/conf/nat1/forwarding
echo "1" >/proc/sys/net/ipv4/conf/br0/forwarding 2>/dev/null
# iptables masquerade rule is now inserted by /opt/openslx/iptables/rules.d/50-virt-nat1-masquerading
### iptables -t nat -A POSTROUTING -o br0 -s 192.168.0.0/16 -j MASQUERADE

for wait in 1 1 2 2 3 end; do
	[ -n "$SLX_DNS" ] && [ -n "$SLX_NET_SEARCH" ] && break
	if [ "$wait" == "end" ]; then
		echo "No DNS config found, using google dns"
		break
	fi
	echo "Waiting for DNS & search-domain config.."
	sleep "$wait"
	. /opt/openslx/config
done

getips () {
	# Output in one line by using echo without quotes
	echo $(nslookup "$1" 2>/dev/null | grep -A 3 '^Name:' | grep '^Address .*: ' | awk '{print $3}')
}

# read the DNS configuration and configure the udhcpd
[ -z "${SLX_DNS}" ] && SLX_DNS="8.8.8.8 8.8.4.4"
[ -z "${SLX_NET_DOMAIN}" ] && SLX_NET_DOMAIN="virtual.site"
[ -z "${SLX_NET_SEARCH}" ] && SLX_NET_SEARCH="virtual.site"
# WINS - if not supplied, try to get it from the search base of our ldap config
if [ -z "${SLX_NET_WINS}" ]; then
	DC=$(grep -m1 -i '^BASE\s*DC=' "/etc/ldap.conf" | grep -o -i 'DC=.*' | sed -r 's/\s*,\s*DC=/./gI;s/^\s*DC=//I')
	[ -z "$DC" ] && DC=$(grep -m1 -i '^ldap_search_base\s*=\s*DC=' "/etc/sssd/sssd.conf" | grep -o -i 'DC=.*'  | sed -r 's/\s*,\s*DC=/./gI;s/^\s*DC=//I')
	[ -n "$DC" ] && SLX_NET_WINS=$(getips "$DC")
fi
[ -z "${SLX_NET_WINS}" ] && SLX_NET_WINS=$(getips "$SLX_NET_DOMAIN")
[ -z "${SLX_NET_WINS}" ] && SLX_NET_WINS="0.0.0.0"
sed -i "s#%DNSSERVER%#${SLX_DNS}#;s#%DOMAIN%#${SLX_NET_DOMAIN}#;s#%SEARCH%#${SLX_NET_SEARCH}#;s#%WINS%#${SLX_NET_WINS}#" "${DHCP_NAT_CONF}"

# Make sure the primary vm running (we most likely never run more than one at a time anyways) always gets the same ip
echo "static_lease $(echo "$MACADDRPREFIX:$MACADDRSUFFIX" | sed 's/%VMID%/01/') 192.168.101.20" >> "${DHCP_NAT_CONF}"

mkdir -p /var/lib/udhcpd
# TODO: systemd
udhcpd -S "${DHCP_NAT_CONF}"

# creating and configuring vsw2
brctl addbr vsw2
ip link set dev vsw2 up
echo "1" >/proc/sys/net/ipv4/conf/vsw2/forwarding