Setsid && nohup for qemu process

This commit is contained in:
Mikhail Klementev 2018-06-30 20:24:44 +00:00
parent f460e500b9
commit 82011c214c

View File

@ -7,14 +7,17 @@ if [[ "$1" == "build" && "$2" != "" ]]; then
NIX_PATH=$NIX_PATH:. nix-build '<nixpkgs/nixos>' -A config.system.build.vm -I nixos-config=nix/${2}.nix || exit 1
NIX_SYSTEM=$(realpath result/system)
mkdir -p bin
sed "s;NIX_SYSTEM_PLACEHOLDER;${NIX_SYSTEM};" qemu/qemu.template > bin/appvm.${2}
sed -i "s;NAME_PLACEHOLDER;${2};" bin/appvm.${2}
sed -i "s;NIX_DISK_IMAGE_PLACEHOLDER;${APPVM_PATH}/qemu/qcow2/${2}.qcow2;" bin/appvm.${2}
RAND_HASH=$(head /dev/urandom | md5sum | awk '{ print $1 }')
VM_BIN_PATH=$(realpath qemu/bin/qemu.${RAND_HASH}.${2})
sed "s;NIX_SYSTEM_PLACEHOLDER;${NIX_SYSTEM};" qemu/qemu.template > ${VM_BIN_PATH}
sed -i "s;NAME_PLACEHOLDER;${2};" ${VM_BIN_PATH}
sed -i "s;NIX_DISK_IMAGE_PLACEHOLDER;${APPVM_PATH}/qemu/qcow2/${2}.qcow2;" ${VM_BIN_PATH}
RANDOM_PORT=$(/usr/bin/python -c 'import random; print(random.randint(1024,65535))')
# TODO Check for port collisions
sed -i "s;PORT_PLACEHOLDER;${RANDOM_PORT};" bin/appvm.${2}
echo -e "#!/bin/bash\nremote-viewer -f spice://127.200.0.1:${RANDOM_PORT}" > bin/appgui.${2}
chmod +x bin/app{vm,gui}.${2}
sed -i "s;PORT_PLACEHOLDER;${RANDOM_PORT};" ${VM_BIN_PATH}
echo -e "#!/bin/bash\npgrep -f ${RAND_HASH} || {\n\tnohup setsid ${VM_BIN_PATH} >/dev/null 2>&1 &\n\tsleep 1s\n}\nremote-viewer -f spice://127.200.0.1:${RANDOM_PORT}" > bin/appvm.${2}
chmod +x ${VM_BIN_PATH}
chmod +x bin/appvm.${2}
unlink result
else
echo "Usage: $0 build APPLICATION"