From 22228aed899db86d548e7d0722ec06c5d3bb0485 Mon Sep 17 00:00:00 2001 From: Kroese Date: Sat, 22 Apr 2023 18:18:56 +0200 Subject: [PATCH 1/5] Update check.yml --- .github/workflows/check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 40bfce0..0c9165c 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -11,4 +11,4 @@ jobs: - name: Run ShellCheck uses: ludeeus/action-shellcheck@master env: - SHELLCHECK_OPTS: -x -e SC2002 -e SC2223 -e SC2034 -e SC2064 + SHELLCHECK_OPTS: -x -e SC2001 -e SC2002 -e SC2223 -e SC2034 -e SC2064 From a6a03a56a9106bbc7b8cfb259f4893e1ebd6bb45 Mon Sep 17 00:00:00 2001 From: Kroese Date: Sat, 22 Apr 2023 18:23:29 +0200 Subject: [PATCH 2/5] Sync --- run/disk.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/run/disk.sh b/run/disk.sh index 129b620..9eb88f3 100644 --- a/run/disk.sh +++ b/run/disk.sh @@ -24,6 +24,7 @@ if [ -f "${DATA}" ]; then if [ "$ALLOCATE" = "N" ]; then + # Resize file by changing its length truncate -s "${DATA_SIZE}" "${DATA}"; else @@ -38,6 +39,7 @@ if [ -f "${DATA}" ]; then echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 84 fi + # Resize file by allocating more space if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then echo "ERROR: Could not allocate a file for the data disk." && exit 85 fi @@ -65,10 +67,9 @@ fi if [ ! -f "${DATA}" ]; then - # Create an empty file - if [ "$ALLOCATE" = "N" ]; then + # Create an empty file truncate -s "${DATA_SIZE}" "${DATA}" else @@ -81,6 +82,7 @@ if [ ! -f "${DATA}" ]; then echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 86 fi + # Create an empty file if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then rm -f "${DATA}" echo "ERROR: Could not allocate a file for the data disk." && exit 87 From e3f9b5a6dbe465405cd98e417d9ae4eefa84e305 Mon Sep 17 00:00:00 2001 From: Kroese Date: Sat, 22 Apr 2023 18:26:07 +0200 Subject: [PATCH 3/5] Sync --- run/network.sh | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/run/network.sh b/run/network.sh index bc4b261..9c2e424 100644 --- a/run/network.sh +++ b/run/network.sh @@ -26,7 +26,6 @@ configureDHCP() { IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/) ip l add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge - ip address add "${IP}" dev "${VM_NET_VLAN}" ip link set dev "${VM_NET_VLAN}" up @@ -54,17 +53,6 @@ configureDHCP() { ip a flush "${VM_NET_TAP}" - # Create /dev/vhost-net - if [ ! -c /dev/vhost-net ]; then - mknod /dev/vhost-net c 10 238 - chmod 660 /dev/vhost-net - fi - - if [ ! -c /dev/vhost-net ]; then - echo -n "Error: VHOST interface not available. Please add the following " - echo "docker variable to your container: --device=/dev/vhost-net" && exit 85 - fi - TAP_NR=$(>/dev/vhost-net; then echo -n "ERROR: VHOST can not be found. Please add the following docker " echo "variable to your container: --device=/dev/vhost-net" && exit 22 @@ -101,22 +95,24 @@ configureNAT () { VM_NET_IP='20.20.20.21' #Create bridge with static IP for the VM guest - brctl addbr dockerbridge + ip link add dev dockerbridge type bridge ip addr add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge ip link set dockerbridge up #QEMU Works with taps, set tap to the bridge created ip tuntap add dev "${VM_NET_TAP}" mode tap ip link set "${VM_NET_TAP}" up promisc on - brctl addif dockerbridge "${VM_NET_TAP}" + ip link set dev "${VM_NET_TAP}" master dockerbridge #Add internet connection to the VM iptables -t nat -A POSTROUTING -o "${VM_NET_DEV}" -j MASQUERADE iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -p tcp -j DNAT --to $VM_NET_IP iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -p udp -j DNAT --to $VM_NET_IP - # Hack for guest VMs complaining about "bad udp checksums in 5 packets" - iptables -A POSTROUTING -t mangle -p udp --dport bootpc -j CHECKSUM --checksum-fill || true + if (( KERNEL > 4 )); then + # Hack for guest VMs complaining about "bad udp checksums in 5 packets" + iptables -A POSTROUTING -t mangle -p udp --dport bootpc -j CHECKSUM --checksum-fill || true + fi #Enable port forwarding flag [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]] && sysctl -w net.ipv4.ip_forward=1 @@ -131,11 +127,17 @@ configureNAT () { NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0" # Build DNS options from container /etc/resolv.conf - mapfile -t nameservers < <(grep '^nameserver' /etc/resolv.conf | sed 's/nameserver //') - searchdomains=$(grep '^search' /etc/resolv.conf | sed 's/search //' | sed 's/ /,/g') + + if [ "$DEBUG" = "Y" ]; then + echo "/etc/resolv.conf:" && echo && cat /etc/resolv.conf && echo + fi + + mapfile -t nameservers < <(grep '^nameserver' /etc/resolv.conf | sed 's/\t/ /g' | sed 's/nameserver //' | sed 's/ //g') + searchdomains=$(grep '^search' /etc/resolv.conf | sed 's/\t/ /g' | sed 's/search //' | sed 's/#.*//' | sed 's/\s*$//g' | sed 's/ /,/g') domainname=$(echo "$searchdomains" | awk -F"," '{print $1}') for nameserver in "${nameservers[@]}"; do + nameserver=$(echo "$nameserver" | sed 's/#.*//' ) if ! [[ "$nameserver" =~ .*:.* ]]; then [[ -z "$DNS_SERVERS" ]] && DNS_SERVERS="$nameserver" || DNS_SERVERS="$DNS_SERVERS,$nameserver" fi @@ -151,7 +153,8 @@ configureNAT () { [[ -z $(hostname -d) ]] || DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-name,$(hostname -d)" fi - [ "$DEBUG" = "Y" ] && echo && echo "$DNSMASQ $DNSMASQ_OPTS" + DNSMASQ_OPTS=$(echo "$DNSMASQ_OPTS" | sed 's/\t/ /g' | tr -s ' ' | sed 's/^ *//') + [ "$DEBUG" = "Y" ] && echo "$DNSMASQ $DNSMASQ_OPTS" && echo $DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS} } @@ -177,7 +180,7 @@ GATEWAY=$(ip r | grep default | awk '{print $3}') if [ "$DEBUG" = "Y" ]; then IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/) - echo "Info: Container IP is ${IP} with gateway ${GATEWAY}" + echo "Info: Container IP is ${IP} with gateway ${GATEWAY}" && echo fi From 76d49f5b0222e413118cd1ab4141f090b8b1b841 Mon Sep 17 00:00:00 2001 From: Kroese Date: Sat, 22 Apr 2023 18:32:19 +0200 Subject: [PATCH 4/5] Sync --- run/run.sh | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/run/run.sh b/run/run.sh index f0231c4..2e0dd99 100755 --- a/run/run.sh +++ b/run/run.sh @@ -3,8 +3,8 @@ set -eu # Docker environment variabeles -: ${BOOT:=''}. # URL of the ISO file -: ${DEBUG:=''}. # Enable debug mode +: ${BOOT:=''} # URL of the ISO file +: ${DEBUG:='N'} # Enable debug mode : ${ALLOCATE:='Y'} # Preallocate diskspace : ${CPU_CORES:='1'} # Amount of CPU cores : ${DISK_SIZE:='16G'} # Initial data disk size @@ -13,6 +13,8 @@ set -eu echo "Starting QEMU for Docker v${VERSION}..." STORAGE="/storage" +KERNEL=$(uname -r | cut -b 1) + [ ! -d "$STORAGE" ] && echo "Storage folder (${STORAGE}) not found!" && exit 69 [ ! -f "/run/run.sh" ] && echo "Script must run inside Docker container!" && exit 60 @@ -48,7 +50,14 @@ RAM_OPTS=$(echo "-m ${RAM_SIZE}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') CPU_OPTS="-smp ${CPU_CORES},sockets=1,cores=${CPU_CORES},threads=1" SERIAL_OPTS="-serial mon:stdio -device virtio-serial-pci,id=virtio-serial0,bus=pcie.0,addr=0x3" EXTRA_OPTS="-device virtio-balloon-pci,id=balloon0 -object rng-random,id=rng0,filename=/dev/urandom -device virtio-rng-pci,rng=rng0" + ARGS="${DEF_OPTS} ${CPU_OPTS} ${RAM_OPTS} ${KVM_OPTS} ${MON_OPTS} ${SERIAL_OPTS} ${NET_OPTS} ${DISK_OPTS} ${EXTRA_OPTS}" +ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ') + +if [ "$DEBUG" = "Y" ]; then + echo -n "qemu-system-x86_64 " + echo "${ARGS}" && echo +fi set -m ( @@ -56,4 +65,8 @@ set -m ) set +m -pidwait -F "${_QEMU_PID}" & wait $! +if (( KERNEL > 4 )); then + pidwait -F "${_QEMU_PID}" & wait $! +else + tail --pid "$(cat ${_QEMU_PID})" --follow /dev/null & wait $! +fi From fb98cfc6501ef1368741ab0eadc7f78fe2f33cbd Mon Sep 17 00:00:00 2001 From: Kroese Date: Sat, 22 Apr 2023 18:34:57 +0200 Subject: [PATCH 5/5] Sync --- Dockerfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index c98f35f..0dda6ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,6 @@ RUN apt-get update && apt-get -y upgrade && \ iproute2 \ dnsmasq \ net-tools \ - bridge-utils \ ca-certificates \ isc-dhcp-client \ netcat-openbsd \ @@ -21,7 +20,6 @@ VOLUME /storage EXPOSE 22 -ENV ALLOCATE "Y" ENV CPU_CORES "1" ENV DISK_SIZE "16G" ENV RAM_SIZE "512M"