docker-kvm-qemu-libvirt/libvirtd.sh

135 lines
4.0 KiB
Bash

#!/usr/bin/bash
set -xe
# HACK
# Use hosts's /dev to see new devices and allow macvtap
mkdir /dev.container && {
mount --rbind /dev /dev.container
mount --rbind /host/dev /dev
# Keep some devices from the containerinal /dev
keep() { mount --rbind /dev.container/$1 /dev/$1 ; }
keep shm
keep mqueue
# Keep ptmx/pts for pty creation
keep pts
mount --rbind /dev/pts/ptmx /dev/ptmx
# Use the container /dev/kvm if available
[[ -e /dev.container/kvm ]] && keep kvm
}
mkdir /sys.net.container && {
mount --rbind /sys/class/net /sys.net.container
mount --rbind /host/sys/class/net /sys/class/net
}
mkdir /sys.devices.container && {
mount --rbind /sys/devices /sys.devices.container
mount --rbind /host/sys/devices /sys/devices
}
# load modules
modprobe ip6_tables -d /host
# enable nested virtualzation
modprobe kvm_intel nested=1 -d /host
# If no cpuacct,cpu is present, symlink it to cpu,cpuacct
# Otherwise libvirt and our emulator get confused
if [ ! -f "/host/sys/fs/cgroup/cgroup.controllers" ]; then
if [ ! -d "/host/sys/fs/cgroup/cpuacct,cpu" ]; then
echo "Creating cpuacct,cpu cgroup symlink"
mount -o remount,rw /host/sys/fs/cgroup
cd /host/sys/fs/cgroup
ln -s cpu,cpuacct cpuacct,cpu
mount -o remount,ro /host/sys/fs/cgroup
fi
fi
mount --rbind /host/sys/fs/cgroup /sys/fs/cgroup
mkdir -p /var/log/libvirt
touch /var/log/libvirt/qemu.log
chown qemu:qemu /var/log/libvirt/qemu.log
# clean previously configured value into /etc/libvirt/libvirtd.conf
sed -i 's/^log_level.*//g' /etc/libvirt/libvirtd.conf
sed -i 's/^log_outputs.*//g' /etc/libvirt/libvirtd.conf
# replace commented default value to appropriate configuration
sed -i "s/^#log_level.*/log_level = $LOG_LEVEL/g" /etc/libvirt/libvirtd.conf
sed -i "s/^#log_outputs.*/log_outputs=\"$LOG_LEVEL:file:\/var\/log\/libvirt\/libvirtd.log\"/g" /etc/libvirt/libvirtd.conf
sed -i "s/^#tcp_port.*/tcp_port=\"$LIBVIRTD_TCP_PORT\"/g" /etc/libvirt/libvirtd.conf
# clean previously configured value into /etc/libvirt/virtlogd.conf
sed -i 's/^log_level.*//g' /etc/libvirt/virtlogd.conf
sed -i 's/^log_outputs.*//g' /etc/libvirt/virtlogd.conf
# replace commented default value to appropriate configuration
sed -i "s/^#log_level.*/log_level = $LOG_LEVEL/g" /etc/libvirt/virtlogd.conf
sed -i "s/^#log_outputs.*/log_outputs=\"$LOG_LEVEL:file:\/var\/log\/libvirt\/libvirtd.log\"/g" /etc/libvirt/virtlogd.conf
# We create the network on a file basis to not
# have to wait for libvirtd to come up
if [[ -n "$LIBVIRTD_DEFAULT_NETWORK" ]]; then
mkdir -p /etc/libvirt/qemu/networks/autostart
cat > /etc/libvirt/qemu/networks/default.xml <<EOX
<!-- Generated by libvirtd.sh container script -->
<network>
<name>default</name>
<!-- spanning tree on and 0 forward delay-->
<bridge name='virbr0' stp='on' delay='0'/>
<forward mode='nat'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
EOX
ln -s /etc/libvirt/qemu/networks/default.xml /etc/libvirt/qemu/networks/autostart/default.xml
fi
# We create the storage pool on a file basis to not
# have to wait for libvirtd to come up
if [[ -n "$LIBVIRTD_DEFAULT_POOL_PATH" ]]; then
mkdir -p /etc/libvirt/storage/autostart
cat > /etc/libvirt/storage/pool_1.xml <<EOX
<!-- Generated by libvirtd.sh container script -->
<pool type='dir'>
<name>pool_1</name>
<target>
<path>$LIBVIRTD_DEFAULT_POOL_PATH</path>
</target>
</pool>
EOX
ln -s /etc/libvirt/storage/pool_1.xml /etc/libvirt/storage/autostart/pool_1.xml
fi
echo "cgroup_controllers = []" >> /etc/libvirt/qemu.conf
echo "namespaces = []" >> /etc/libvirt/qemu.conf
/usr/sbin/virtlogd &
#Define cleanup procedure
cleanup() {
echo "Container stopped, performing cleanup..."
if [[ -n "$LIBVIRTD_DEFAULT_NETWORK" ]]; then
echo "Container stopped, destroy default network ..."
virsh net-destroy default
fi
}
#Trap SIGTERM
trap 'cleanup' SIGTERM
#Execute command
# "${@}" &
/usr/sbin/libvirtd -ld
#Wait
wait $!
#Cleanup
cleanup