Sync
This commit is contained in:
Kroese 2023-04-22 18:37:02 +02:00 committed by GitHub
commit e8d2421dcb
5 changed files with 44 additions and 28 deletions

View file

@ -11,4 +11,4 @@ jobs:
- name: Run ShellCheck - name: Run ShellCheck
uses: ludeeus/action-shellcheck@master uses: ludeeus/action-shellcheck@master
env: 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

View file

@ -7,7 +7,6 @@ RUN apt-get update && apt-get -y upgrade && \
iproute2 \ iproute2 \
dnsmasq \ dnsmasq \
net-tools \ net-tools \
bridge-utils \
ca-certificates \ ca-certificates \
isc-dhcp-client \ isc-dhcp-client \
netcat-openbsd \ netcat-openbsd \
@ -21,7 +20,6 @@ VOLUME /storage
EXPOSE 22 EXPOSE 22
ENV ALLOCATE "Y"
ENV CPU_CORES "1" ENV CPU_CORES "1"
ENV DISK_SIZE "16G" ENV DISK_SIZE "16G"
ENV RAM_SIZE "512M" ENV RAM_SIZE "512M"

View file

@ -24,6 +24,7 @@ if [ -f "${DATA}" ]; then
if [ "$ALLOCATE" = "N" ]; then if [ "$ALLOCATE" = "N" ]; then
# Resize file by changing its length
truncate -s "${DATA_SIZE}" "${DATA}"; truncate -s "${DATA_SIZE}" "${DATA}";
else else
@ -38,6 +39,7 @@ if [ -f "${DATA}" ]; then
echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 84 echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 84
fi fi
# Resize file by allocating more space
if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
echo "ERROR: Could not allocate a file for the data disk." && exit 85 echo "ERROR: Could not allocate a file for the data disk." && exit 85
fi fi
@ -65,10 +67,9 @@ fi
if [ ! -f "${DATA}" ]; then if [ ! -f "${DATA}" ]; then
# Create an empty file
if [ "$ALLOCATE" = "N" ]; then if [ "$ALLOCATE" = "N" ]; then
# Create an empty file
truncate -s "${DATA_SIZE}" "${DATA}" truncate -s "${DATA_SIZE}" "${DATA}"
else else
@ -81,6 +82,7 @@ if [ ! -f "${DATA}" ]; then
echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 86 echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 86
fi fi
# Create an empty file
if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
rm -f "${DATA}" rm -f "${DATA}"
echo "ERROR: Could not allocate a file for the data disk." && exit 87 echo "ERROR: Could not allocate a file for the data disk." && exit 87

View file

@ -26,7 +26,6 @@ configureDHCP() {
IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/) 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 l add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge
ip address add "${IP}" dev "${VM_NET_VLAN}" ip address add "${IP}" dev "${VM_NET_VLAN}"
ip link set dev "${VM_NET_VLAN}" up ip link set dev "${VM_NET_VLAN}" up
@ -54,17 +53,6 @@ configureDHCP() {
ip a flush "${VM_NET_TAP}" 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=$(</sys/class/net/"${VM_NET_TAP}"/ifindex) TAP_NR=$(</sys/class/net/"${VM_NET_TAP}"/ifindex)
TAP_PATH="/dev/tap${TAP_NR}" TAP_PATH="/dev/tap${TAP_NR}"
@ -88,6 +76,12 @@ configureDHCP() {
echo "--device=/dev/vhost-net --device-cgroup-rule='c ${MAJOR}:* rwm'" && exit 21 echo "--device=/dev/vhost-net --device-cgroup-rule='c ${MAJOR}:* rwm'" && exit 21
fi fi
# Create /dev/vhost-net
if [ ! -c /dev/vhost-net ]; then
mknod /dev/vhost-net c 10 238
chmod 660 /dev/vhost-net
fi
if ! exec 40>>/dev/vhost-net; then if ! exec 40>>/dev/vhost-net; then
echo -n "ERROR: VHOST can not be found. Please add the following docker " echo -n "ERROR: VHOST can not be found. Please add the following docker "
echo "variable to your container: --device=/dev/vhost-net" && exit 22 echo "variable to your container: --device=/dev/vhost-net" && exit 22
@ -101,22 +95,24 @@ configureNAT () {
VM_NET_IP='20.20.20.21' VM_NET_IP='20.20.20.21'
#Create bridge with static IP for the VM guest #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 addr add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge
ip link set dockerbridge up ip link set dockerbridge up
#QEMU Works with taps, set tap to the bridge created #QEMU Works with taps, set tap to the bridge created
ip tuntap add dev "${VM_NET_TAP}" mode tap ip tuntap add dev "${VM_NET_TAP}" mode tap
ip link set "${VM_NET_TAP}" up promisc on 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 #Add internet connection to the VM
iptables -t nat -A POSTROUTING -o "${VM_NET_DEV}" -j MASQUERADE 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 tcp -j DNAT --to $VM_NET_IP
iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -p udp -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" if (( KERNEL > 4 )); then
iptables -A POSTROUTING -t mangle -p udp --dport bootpc -j CHECKSUM --checksum-fill || true # 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 #Enable port forwarding flag
[[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]] && sysctl -w net.ipv4.ip_forward=1 [[ $(< /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" NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0"
# Build DNS options from container /etc/resolv.conf # 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}') domainname=$(echo "$searchdomains" | awk -F"," '{print $1}')
for nameserver in "${nameservers[@]}"; do for nameserver in "${nameservers[@]}"; do
nameserver=$(echo "$nameserver" | sed 's/#.*//' )
if ! [[ "$nameserver" =~ .*:.* ]]; then if ! [[ "$nameserver" =~ .*:.* ]]; then
[[ -z "$DNS_SERVERS" ]] && DNS_SERVERS="$nameserver" || DNS_SERVERS="$DNS_SERVERS,$nameserver" [[ -z "$DNS_SERVERS" ]] && DNS_SERVERS="$nameserver" || DNS_SERVERS="$DNS_SERVERS,$nameserver"
fi fi
@ -151,7 +153,8 @@ configureNAT () {
[[ -z $(hostname -d) ]] || DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-name,$(hostname -d)" [[ -z $(hostname -d) ]] || DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-name,$(hostname -d)"
fi 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} $DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}
} }
@ -177,7 +180,7 @@ GATEWAY=$(ip r | grep default | awk '{print $3}')
if [ "$DEBUG" = "Y" ]; then if [ "$DEBUG" = "Y" ]; then
IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/) 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 fi

View file

@ -3,8 +3,8 @@ set -eu
# Docker environment variabeles # Docker environment variabeles
: ${BOOT:=''}. # URL of the ISO file : ${BOOT:=''} # URL of the ISO file
: ${DEBUG:=''}. # Enable debug mode : ${DEBUG:='N'} # Enable debug mode
: ${ALLOCATE:='Y'} # Preallocate diskspace : ${ALLOCATE:='Y'} # Preallocate diskspace
: ${CPU_CORES:='1'} # Amount of CPU cores : ${CPU_CORES:='1'} # Amount of CPU cores
: ${DISK_SIZE:='16G'} # Initial data disk size : ${DISK_SIZE:='16G'} # Initial data disk size
@ -13,6 +13,8 @@ set -eu
echo "Starting QEMU for Docker v${VERSION}..." echo "Starting QEMU for Docker v${VERSION}..."
STORAGE="/storage" STORAGE="/storage"
KERNEL=$(uname -r | cut -b 1)
[ ! -d "$STORAGE" ] && echo "Storage folder (${STORAGE}) not found!" && exit 69 [ ! -d "$STORAGE" ] && echo "Storage folder (${STORAGE}) not found!" && exit 69
[ ! -f "/run/run.sh" ] && echo "Script must run inside Docker container!" && exit 60 [ ! -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" 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" 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" 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="${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 set -m
( (
@ -56,4 +65,8 @@ set -m
) )
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