Add vhost support in NAT mode

Add vhost support in NAT mode
This commit is contained in:
Kroese 2023-05-06 15:18:10 +02:00 committed by GitHub
commit 24efe5585c
11 changed files with 54 additions and 43 deletions

View File

@ -72,7 +72,7 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- -
name: Create a release name: Create a release
uses: kroese/github-release@v6 uses: action-pack/github-release@v6
env: env:
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
with: with:
@ -81,7 +81,7 @@ jobs:
body: "Release v${{ vars.MAJOR }}.${{ vars.MINOR }}" body: "Release v${{ vars.MAJOR }}.${{ vars.MINOR }}"
- -
name: Increment minor version name: Increment minor version
uses: kroese/increment@v1 uses: action-pack/increment@v1
with: with:
name: 'MINOR' name: 'MINOR'
token: ${{ secrets.REPO_ACCESS_TOKEN }} token: ${{ secrets.REPO_ACCESS_TOKEN }}
@ -97,7 +97,7 @@ jobs:
fetch-depth: 0 fetch-depth: 0
- -
name: Push to Gitlab mirror name: Push to Gitlab mirror
uses: kroese/gitlab-sync@v2 uses: action-pack/gitlab-sync@v2
with: with:
url: ${{ secrets.GITLAB_URL }} url: ${{ secrets.GITLAB_URL }}
token: ${{ secrets.GITLAB_TOKEN }} token: ${{ secrets.GITLAB_TOKEN }}

View File

@ -9,6 +9,7 @@ services:
RAM_SIZE: "512M" RAM_SIZE: "512M"
devices: devices:
- /dev/kvm - /dev/kvm
- /dev/vhost-net
cap_add: cap_add:
- NET_ADMIN - NET_ADMIN
ports: ports:

View File

@ -40,6 +40,7 @@ services:
DISK_SIZE: "16G" DISK_SIZE: "16G"
devices: devices:
- /dev/kvm - /dev/kvm
- /dev/vhost-net
cap_add: cap_add:
- NET_ADMIN - NET_ADMIN
ports: ports:

View File

@ -5,7 +5,7 @@ set -u
: ${DHCP:='N'} : ${DHCP:='N'}
if [ "$DHCP" = "Y" ]; then if [[ "${DHCP}" == [Yy1]* ]]; then
PORT=5555 PORT=5555
IP="127.0.0.1" IP="127.0.0.1"
else else

View File

@ -36,7 +36,7 @@ if [ -f "${DATA}" ]; then
echo "INFO: Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.." echo "INFO: Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.."
if [ "$ALLOCATE" = "N" ]; then if [[ "${ALLOCATE}" == [Nn]* ]]; then
# Resize file by changing its length # Resize file by changing its length
truncate -s "${DATA_SIZE}" "${DATA}"; truncate -s "${DATA_SIZE}" "${DATA}";
@ -58,7 +58,7 @@ if [ -f "${DATA}" ]; then
echo "ERROR: Could not allocate a file for the virtual disk." && exit 85 echo "ERROR: Could not allocate a file for the virtual disk." && exit 85
fi fi
if [ "$ALLOCATE" = "Z" ]; then if [[ "${ALLOCATE}" == [Zz]* ]]; then
GB=$(( (REQ + 1073741823)/1073741824 )) GB=$(( (REQ + 1073741823)/1073741824 ))
@ -81,7 +81,7 @@ fi
if [ ! -f "${DATA}" ]; then if [ ! -f "${DATA}" ]; then
if [ "$ALLOCATE" = "N" ]; then if [[ "${ALLOCATE}" == [Nn]* ]]; then
# Create an empty file # Create an empty file
truncate -s "${DATA_SIZE}" "${DATA}" truncate -s "${DATA_SIZE}" "${DATA}"
@ -102,7 +102,7 @@ if [ ! -f "${DATA}" ]; then
echo "ERROR: Could not allocate a file for the virtual disk." && exit 87 echo "ERROR: Could not allocate a file for the virtual disk." && exit 87
fi fi
if [ "$ALLOCATE" = "Z" ]; then if [[ "${ALLOCATE}" == [Zz]* ]]; then
echo "INFO: Preallocating ${DISK_SIZE} of diskspace, please wait..." echo "INFO: Preallocating ${DISK_SIZE} of diskspace, please wait..."
dd if=/dev/urandom of="${DATA}" count="${DATA_SIZE}" bs=1M iflag=count_bytes status=none dd if=/dev/urandom of="${DATA}" count="${DATA_SIZE}" bs=1M iflag=count_bytes status=none

View File

@ -33,7 +33,7 @@ TMP="$STORAGE/tmp"
RD="$TMP/rd.gz" RD="$TMP/rd.gz"
rm -rf "$TMP" && mkdir -p "$TMP" rm -rf "$TMP" && mkdir -p "$TMP"
[ "$DEBUG" = "Y" ] && set -x [[ "${DEBUG}" == [Yy1]* ]] && set -x
LOC="$DL/release/7.0.1/42218/DSM_VirtualDSM_42218.pat" LOC="$DL/release/7.0.1/42218/DSM_VirtualDSM_42218.pat"
@ -113,7 +113,7 @@ BOOT=$(find "$TMP" -name "*.bin.zip")
BOOT=$(echo "$BOOT" | head -c -5) BOOT=$(echo "$BOOT" | head -c -5)
unzip -q -o "$BOOT".zip -d "$TMP" unzip -q -o "$BOOT".zip -d "$TMP"
[ "$ALLOCATE" != "Z" ] && echo "Install: Allocating diskspace..." [[ "${ALLOCATE}" == [Zz]* ]] && echo "Install: Allocating diskspace..."
SYSTEM="$TMP/sys.img" SYSTEM="$TMP/sys.img"
SYSTEM_SIZE=4954537983 SYSTEM_SIZE=4954537983
@ -130,7 +130,7 @@ if ! fallocate -l "${SYSTEM_SIZE}" "${SYSTEM}"; then
echo "ERROR: Could not allocate a file for the system disk." && exit 88 echo "ERROR: Could not allocate a file for the system disk." && exit 88
fi fi
if [ "$ALLOCATE" = "Z" ]; then if [[ "${ALLOCATE}" == [Zz]* ]]; then
echo "Install: Preallocating 4 GB of diskspace..." echo "Install: Preallocating 4 GB of diskspace..."
dd if=/dev/urandom of="${SYSTEM}" count="${SYSTEM_SIZE}" bs=1M iflag=count_bytes status=none dd if=/dev/urandom of="${SYSTEM}" count="${SYSTEM_SIZE}" bs=1M iflag=count_bytes status=none
fi fi
@ -211,6 +211,6 @@ mv -f "$SYSTEM" "$STORAGE"/"$BASE".system.img
rm -rf "$TMP" rm -rf "$TMP"
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
[ "$DEBUG" = "Y" ] && echo [[ "${DEBUG}" == [Yy1]* ]] && echo
return 0 return 0

View File

@ -25,7 +25,7 @@ configureDHCP() {
NETWORK=$(ip -o route | grep "${VM_NET_DEV}" | grep -v default | awk '{print $1}') NETWORK=$(ip -o route | grep "${VM_NET_DEV}" | grep -v default | awk '{print $1}')
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/)
[ "$DEBUG" = "Y" ] && set -x [[ "${DEBUG}" == [Yy1]* ]] && set -x
{ ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge 2> /dev/null ; rc=$?; } || : { ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge 2> /dev/null ; rc=$?; } || :
if (( rc != 0 )); then if (( rc != 0 )); then
@ -80,12 +80,6 @@ configureDHCP() {
echo "container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21 echo "container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && 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
{ exec 40>>/dev/vhost-net; rc=$?; } || : { exec 40>>/dev/vhost-net; rc=$?; } || :
if (( rc != 0 )); then if (( rc != 0 )); then
@ -99,7 +93,7 @@ configureDHCP() {
configureNAT () { configureNAT () {
VM_NET_IP='20.20.20.21' VM_NET_IP='20.20.20.21'
[ "$DEBUG" = "Y" ] && set -x [[ "${DEBUG}" == [Yy1]* ]] && set -x
#Create bridge with static IP for the VM guest #Create bridge with static IP for the VM guest
@ -129,7 +123,7 @@ configureNAT () {
fi fi
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
[ "$DEBUG" = "Y" ] && echo [[ "${DEBUG}" == [Yy1]* ]] && echo
#Check port forwarding flag #Check port forwarding flag
if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then
@ -149,9 +143,15 @@ 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"
{ exec 40>>/dev/vhost-net; rc=$?; } || :
if (( rc == 0 )); then
NET_OPTS="$NET_OPTS,vhost=on,vhostfd=40"
fi
# Build DNS options from container /etc/resolv.conf # Build DNS options from container /etc/resolv.conf
if [ "$DEBUG" = "Y" ]; then if [[ "${DEBUG}" == [Yy1]* ]]; then
echo "/etc/resolv.conf:" && echo && cat /etc/resolv.conf && echo echo "/etc/resolv.conf:" && echo && cat /etc/resolv.conf && echo
fi fi
@ -178,7 +178,7 @@ configureNAT () {
DNSMASQ_OPTS=$(echo "$DNSMASQ_OPTS" | sed 's/\t/ /g' | tr -s ' ' | sed 's/^ *//') DNSMASQ_OPTS=$(echo "$DNSMASQ_OPTS" | sed 's/\t/ /g' | tr -s ' ' | sed 's/^ *//')
[ "$DEBUG" = "Y" ] && set -x [[ "${DEBUG}" == [Yy1]* ]] && set -x
$DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS} $DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}
@ -198,13 +198,19 @@ fi
[ ! -c /dev/net/tun ] && echo "ERROR: TUN network interface not available..." && exit 85 [ ! -c /dev/net/tun ] && echo "ERROR: TUN network interface not available..." && exit 85
# Create the necessary file structure for /dev/vhost-net
if [ ! -c /dev/vhost-net ]; then
mknod /dev/vhost-net c 10 238
chmod 660 /dev/vhost-net
fi
update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null
VM_NET_MAC="${VM_NET_MAC//-/:}" VM_NET_MAC="${VM_NET_MAC//-/:}"
GATEWAY=$(ip r | grep default | awk '{print $3}') GATEWAY=$(ip r | grep default | awk '{print $3}')
if [ "$DEBUG" = "Y" ]; then if [[ "${DEBUG}" == [Yy1]* ]]; 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 echo "INFO: Container IP is ${IP} with gateway ${GATEWAY}" && echo
@ -213,12 +219,7 @@ if [ "$DEBUG" = "Y" ]; then
fi fi
if [ "$DHCP" != "Y" ]; then if [[ "${DHCP}" == [Yy1]* ]]; then
# Configuration for static IP
configureNAT
else
if [[ "$GATEWAY" == "172."* ]]; then if [[ "$GATEWAY" == "172."* ]]; then
echo -n "ERROR: You cannot enable DHCP while the container is " echo -n "ERROR: You cannot enable DHCP while the container is "
@ -229,22 +230,29 @@ else
configureDHCP configureDHCP
# Display the received IP on port 5000 # Display the received IP on port 5000
HTML="DSM is using another IP address.<br><br>(Check the logfile to see which one was assigned.)" HTML="DSM is using another IP address.<br><br>Check the Docker logfile to see which one was<br> assigned, or download the\
<a href='https://global.synologydownload.com/download/Utility/Assistant/7.0.4-50051/Windows/synology-assistant-7.0.4-50051.exe'>\
Synology Assistant</a> utility."
pkill -f server.sh || true pkill -f server.sh || true
/run/server.sh 80 "${HTML}" > /dev/null & /run/server.sh 80 "${HTML}" > /dev/null &
/run/server.sh 5000 "${HTML}" > /dev/null & /run/server.sh 5000 "${HTML}" > /dev/null &
else
# Configuration for static IP
configureNAT
fi fi
NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0" NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0"
if [ "$DHCP" = "Y" ]; then if [[ "${DHCP}" == [Yy1]* ]]; then
# Add extra LAN interface for Docker Healthcheck script # Add extra LAN interface for Docker Healthcheck script
NET_OPTS="${NET_OPTS} -netdev user,id=hostnet1,restrict=y,hostfwd=tcp::5555-:5000" NET_OPTS="${NET_OPTS} -netdev user,id=hostnet1,restrict=y,hostfwd=tcp::5555-:5000"
NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet1,id=net1" NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet1,id=net1"
fi fi
[ "$DEBUG" = "Y" ] && echo && echo "Finished network setup.." && echo [[ "${DEBUG}" == [Yy1]* ]] && echo && echo "Finished network setup.." && echo
return 0 return 0

View File

@ -32,7 +32,7 @@ _graceful_shutdown() {
# Don't send the powerdown signal because vDSM ignores ACPI signals # Don't send the powerdown signal because vDSM ignores ACPI signals
# echo 'system_powerdown' | nc -q 1 -w 1 localhost "${QEMU_MONPORT}" > /dev/null # echo 'system_powerdown' | nc -q 1 -w 1 localhost "${QEMU_MONPORT}" > /dev/null
# Send shutdown command to host via serial port # Send shutdown command to guest agent via serial port
RESPONSE=$(curl -s -m 2 -S http://127.0.0.1:2210/write?command=6 2>&1) RESPONSE=$(curl -s -m 2 -S http://127.0.0.1:2210/write?command=6 2>&1)
if [[ ! "${RESPONSE}" =~ "\"success\"" ]] ; then if [[ ! "${RESPONSE}" =~ "\"success\"" ]] ; then

View File

@ -11,14 +11,15 @@ set -Eeuo pipefail
: ${RAM_SIZE:='512M'} # Maximum RAM amount : ${RAM_SIZE:='512M'} # Maximum RAM amount
echo "Starting Virtual DSM for Docker v${VERSION}..." echo "Starting Virtual DSM for Docker v${VERSION}..."
trap 'echo >&2 "Error status $? for: ${BASH_COMMAND} (line $LINENO/$BASH_LINENO)"' ERR
[ ! -f "/run/run.sh" ] && echo "ERROR: Script must run inside Docker container!" && exit 11
[ "$(id -u)" -ne "0" ] && echo "ERROR: Script must be executed with root privileges." && exit 12
STORAGE="/storage" STORAGE="/storage"
KERNEL=$(uname -r | cut -b 1) KERNEL=$(uname -r | cut -b 1)
trap 'echo >&2 "Error status $? for: ${BASH_COMMAND} (line $LINENO/$BASH_LINENO)"' ERR [ ! -d "$STORAGE" ] && echo "ERROR: Storage folder (${STORAGE}) not found!" && exit 13
[ ! -d "$STORAGE" ] && echo "Storage folder (${STORAGE}) not found!" && exit 69
[ ! -f "/run/run.sh" ] && echo "Script must run inside Docker container!" && exit 60
if [ -f "$STORAGE"/dsm.ver ]; then if [ -f "$STORAGE"/dsm.ver ]; then
BASE=$(cat "${STORAGE}/dsm.ver") BASE=$(cat "${STORAGE}/dsm.ver")
@ -58,7 +59,7 @@ fi
if [ -n "${KVM_ERR}" ]; then if [ -n "${KVM_ERR}" ]; then
echo "ERROR: KVM acceleration not detected ${KVM_ERR}, please enable it." echo "ERROR: KVM acceleration not detected ${KVM_ERR}, please enable it."
[ "$DEBUG" != "Y" ] && exit 88 [[ "${DEBUG}" == [Yy1]* ]] && exit 88
else else
KVM_OPTS=",accel=kvm -enable-kvm -cpu host" KVM_OPTS=",accel=kvm -enable-kvm -cpu host"
fi fi
@ -78,7 +79,7 @@ trap - ERR
set -m set -m
( (
[ "$DEBUG" = "Y" ] && set -x [[ "${DEBUG}" == [Yy1]* ]] && set -x
qemu-system-x86_64 ${ARGS:+ $ARGS} & echo $! > "${_QEMU_PID}" qemu-system-x86_64 ${ARGS:+ $ARGS} & echo $! > "${_QEMU_PID}"
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
) )

View File

@ -31,7 +31,7 @@ HOST_ARGS+=("-cpu_arch=${HOST_CPU}")
[ -n "$HOST_VERSION" ] && HOST_ARGS+=("-version=${HOST_VERSION}") [ -n "$HOST_VERSION" ] && HOST_ARGS+=("-version=${HOST_VERSION}")
[ -n "$HOST_TIMESTAMP" ] && HOST_ARGS+=("-ts=${HOST_TIMESTAMP}") [ -n "$HOST_TIMESTAMP" ] && HOST_ARGS+=("-ts=${HOST_TIMESTAMP}")
if [ "$HOST_DEBUG" = "Y" ]; then if [[ "${HOST_DEBUG}" == [Yy1]* ]]; then
set -x set -x
./run/host.bin "${HOST_ARGS[@]}" & ./run/host.bin "${HOST_ARGS[@]}" &
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null

View File

@ -5,8 +5,8 @@ trap 'kill 0' EXIT
trap exit SIGINT SIGTERM trap exit SIGINT SIGTERM
# Serve the page # Serve the page
HTML="<HTML><HEAD><STYLE>body { color: white; background-color: #00BFFF; }</STYLE>\ HTML="<!DOCTYPE html><HTML><HEAD><TITLE>VirtualDSM</TITLE><STYLE>body { color: white; background-color: #125bdb; font-family: Verdana,\
</HEAD><BODY><BR><BR><H1><CENTER>$2</CENTER></H1></BODY></HTML>" Arial,sans-serif; } a, a:hover, a:active, a:visited { color: white; }</STYLE></HEAD><BODY><BR><BR><H1><CENTER>$2</CENTER></H1></BODY></HTML>"
LENGTH="${#HTML}" LENGTH="${#HTML}"