From 8f5701860d48977fadbfaa1ca652d76b6cb89759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sp=C3=B6hel?= Date: Wed, 14 Apr 2021 17:30:50 +0200 Subject: Adding basic functionality to start a VM. --- libexec/container/start-qemu | 115 ++++++------------------------------------- share/systemd/kvm@.service | 13 +++++ 2 files changed, 29 insertions(+), 99 deletions(-) create mode 100644 share/systemd/kvm@.service diff --git a/libexec/container/start-qemu b/libexec/container/start-qemu index b7953c1..491278c 100755 --- a/libexec/container/start-qemu +++ b/libexec/container/start-qemu @@ -125,9 +125,9 @@ case "${NAME}" in ;; esac -if [ ! -e "${MACHINES}/${NAME}" ] +if [ ! -e "${MACHINES}/${NAME}.img" ] then - echo "'${NAME}': no such container" >&2 + echo "'${NAME}': no such vm" >&2 exit 1 fi @@ -166,7 +166,9 @@ then fi HOST_ARCHITECTURE="$(dpkg --print-architecture)" -MACHINE_ARCHITECTURE="$(chroot ${MACHINES}/${NAME} dpkg --print-architecture)" +#FIXME +#MACHINE_ARCHITECTURE="$(chroot ${MACHINES}/${NAME} dpkg --print-architecture)" +MACHINE_ARCHITECTURE=amd64 case "${HOST_ARCHITECTURE}" in amd64) @@ -201,91 +203,6 @@ esac # config if [ -e "${CONFIG}/${NAME}.conf" ] then - CNT_OVERLAY="$(awk -Fcnt.overlay= '/^cnt.overlay=/ { print $2 }' ${CONFIG}/${NAME}.conf)" - CNT_OVERLAY_OPTIONS="$(awk -Fcnt.overlay-options= '/^cnt.overlay-options=/ { print $2 }' ${CONFIG}/${NAME}.conf)" - - if [ -n "${CNT_OVERLAY}" ] - then - CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" - - COUNT="0" - for CNT_OVERLAY in ${CNT_OVERLAYS} - do - DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" - DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" - DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" - DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" - - COUNT="$((${COUNT} + 1))" - CNT_OVERLAY_OPTION="$(echo ${CNT_OVERLAY_OPTIONS} | awk -F ';' "{ print \$${COUNT} }")" - - for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" - do - mkdir -p "${DIRECTORY}" - done - - if ! findmnt -n -o SOURCE "${DIRECTORY_MERGED}" | grep -qs '^cnt.overlay-' - then - if [ -n "${CNT_OVERLAY_OPTION}" ] - then - CNT_OVERLAY_OPTION="-o ${CNT_OVERLAY_OPTION}" - fi - - mount cnt.overlay-${NAME} -t overlay ${CNT_OVERLAY_OPTION} -olowerdir="${DIRECTORY_LOWER}",upperdir="${DIRECTORY_UPPER}",workdir="${DIRECTORY_WORK}",default_permissions "${DIRECTORY_MERGED}" - fi - done - fi - - BIND="$(awk -Fbind= '/^bind=/ { print $2 }' ${CONFIG}/${NAME}.conf)" - - if [ -n "${BIND}" ] - then - BINDS="$(echo ${BIND} | sed -e 's|;| |g')" - - for BIND in ${BINDS} - do - DIRECTORY="$(echo ${BIND} | awk -F: '{ print $1 }')" - - if [ ! -e "${DIRECTORY}" ] - then - echo "'${DIRECTORY}': creating non-existing directory for bind mounting" - mkdir -p "${DIRECTORY}" - fi - done - - BIND="" - - for DIRECTORIES in ${BINDS} - do - BIND="${BIND} --bind ${DIRECTORIES}" - done - fi - - BIND_RO="$(awk -Fbind-ro= '/^bind-ro=/ { print $2 }' ${CONFIG}/${NAME}.conf)" - - if [ -n "${BIND_RO}" ] - then - BINDS_RO="$(echo ${BIND_RO} | sed -e 's|;| |g')" - - for BIND_RO in ${BINDS_RO} - do - DIRECTORY="$(echo ${BIND_RO} | awk -F: '{ print $1 }')" - - if [ ! -e "${DIRECTORY}" ] - then - echo "'${DIRECTORY}': creating non-existing directory for bind-ro mounting" - mkdir -p "${DIRECTORY}" - fi - done - - BIND_RO="" - - for DIRECTORIES in ${BINDS_RO} - do - BIND_RO="${BIND_RO} --bind-ro ${DIRECTORIES}" - done - fi - BOOT="$(awk -Fboot= '/^boot=/ { print $2 }' ${CONFIG}/${NAME}.conf || echo yes)" case "${BOOT}" in @@ -311,7 +228,7 @@ then esac DIRECTORY="$(awk -Fdirectory= '/^directory=/ { print $2 }' ${CONFIG}/${NAME}.conf || echo ${MACHINES}/${NAMES})" - DIRECTORY="--directory ${DIRECTORY}" + DIRECTORY="-drive file=${DIRECTORY}.img,format=raw,if=virtio" DROP_CAPABILITY="$(awk -Fdrop-capability= '/^drop-capability=/ { print $2 }' ${CONFIG}/${NAME}.conf)" @@ -350,9 +267,10 @@ then *) for VETH in ${VETHS} do - NETWORK_VETH_EXTRA="${NETWORK_VETH_EXTRA} --network-veth-extra=${VETH}" INTERFACE="$(echo ${VETH} | awk -F: '{ print $1 }')" + NETWORK_VETH_EXTRA="${NETWORK_VETH_EXTRA} -netdev tap,id=${INTERFACE},ifname=${INTERFACE},script=no,downscript=no -device virtio-net-pci,netdev=${INTERFACE}" + if [ "$(echo ${INTERFACE} | wc -c)" -gt 15 ] then echo "'${INTERFACE}': name exceeds maximum of 15 characters, network might be not working." @@ -452,7 +370,7 @@ fi case "${SYSTEMCTL}" in true) - systemctl start ${PROGRAM}@${NAME}.service + systemctl start kvm@${NAME}.service # Post hooks for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" @@ -469,12 +387,6 @@ esac case "${START}" in true) - - case "${SET_PROPERTY}" in - true) - systemctl --runtime set-property ${NAME} ${BLOCK_IO_DEVICE_WEIGHT} ${BLOCK_IO_READ_BANDWIDTH} ${BLOCK_IO_WEIGHT} ${BLOCK_IO_WRITE_BANDWIDTH} ${CPU_QUOTA} ${CPU_SHARES} ${MEMORY_LIMIT} ${TASKS_MAX} - ;; - esac ;; *) @@ -486,8 +398,13 @@ case "${START}" in ;; esac - ${SETARCH} systemd-nspawn --keep-unit ${BIND} ${BIND_RO} ${BOOT} ${CAPABILITY} ${DIRECTORY} ${DROP_CAPABILITY} ${MACHINE} ${NETWORK_VETH_EXTRA} ${LINK_JOURNAL} ${REGISTER} - + qemu-system-x86_64 \ + -machine accel=kvm:tcg \ + ${DIRECTORY} \ + -m 4G \ + -name ${NAME} \ + ${NETWORK_VETH_EXTRA} \ + -bios /usr/share/qemu/OVMF.fd case "${VERBOSE}" in true) diff --git a/share/systemd/kvm@.service b/share/systemd/kvm@.service new file mode 100644 index 0000000..41fea21 --- /dev/null +++ b/share/systemd/kvm@.service @@ -0,0 +1,13 @@ +[Unit] +Description="Container: %i" +Documentation=man:compute-tools + +[Service] +Type=simple +ExecStart=/usr/bin/container start-qemu -n %i --nspawn +ExecStartPost=/usr/bin/container start-qemu -n %i --start +#ExecStopPost=/usr/bin/container stop -n %i --clean --no-notification +KillMode=mixed + +[Install] +WantedBy=multi-user.target -- cgit v1.2.3