Use colorcodes in log output

Use colorcodes in log output
This commit is contained in:
Kroese 2023-05-11 05:19:58 +02:00 committed by GitHub
commit 1d42955d14
7 changed files with 95 additions and 86 deletions

View File

@ -6,9 +6,12 @@ HEADER="VirtualDSM Agent"
# Functions # Functions
error () { echo -e "\E[1;31m ERROR: $1\E[0m" ; }
info () { echo -e "\E[1;34m\E[1;36m $1\E[0m" ; }
finish() { finish() {
echo "$HEADER: Shutting down.." echo " $HEADER: Shutting down.."
exit exit
} }
@ -20,7 +23,7 @@ function checkNMI {
if [ "$nmi" != "" ]; then if [ "$nmi" != "" ]; then
echo "$HEADER: Received shutdown request through NMI.." info "Received shutdown request through NMI.."
/usr/syno/sbin/synoshutdown -s > /dev/null /usr/syno/sbin/synoshutdown -s > /dev/null
finish finish
@ -48,27 +51,27 @@ function downloadUpdate {
[[ remote_size -eq local_size ]] && return [[ remote_size -eq local_size ]] && return
if ! curl -sfk -m 10 -o "${TMP}" "${URL}"; then if ! curl -sfk -m 10 -o "${TMP}" "${URL}"; then
echo "$HEADER: curl error ($?)" && return error "$HEADER: curl error ($?)" && return
fi fi
if [ ! -f "${TMP}" ]; then if [ ! -f "${TMP}" ]; then
echo "$HEADER: update error, file not found.." && return error "$HEADER: update error, file not found.." && return
fi fi
line=$(head -1 "${TMP}") line=$(head -1 "${TMP}")
if [[ "$line" != "#!/usr/bin/env bash" ]]; then if [[ "$line" != "#!/usr/bin/env bash" ]]; then
echo "$HEADER: update error, invalid header: $line" && return error "$HEADER: update error, invalid header: $line" && return
fi fi
if cmp --silent -- "${TMP}" "${SCRIPT}"; then if cmp --silent -- "${TMP}" "${SCRIPT}"; then
echo "$HEADER: update file is already equal? (${local_size} / ${remote_size})" && return error "$HEADER: update file is already equal? (${local_size} / ${remote_size})" && return
fi fi
mv -f "${TMP}" "${SCRIPT}" mv -f "${TMP}" "${SCRIPT}"
chmod 755 "${SCRIPT}" chmod 755 "${SCRIPT}"
echo "$HEADER: succesfully installed update, please reboot." info "$HEADER: succesfully installed update..."
} }
@ -82,7 +85,7 @@ function installPackages {
[[ $BASE == "ActiveInsight" ]] && continue [[ $BASE == "ActiveInsight" ]] && continue
echo "$HEADER: Installing package ${BASE}.." info "Installing package ${BASE}.."
/usr/syno/bin/synopkg install "$filename" > /dev/null /usr/syno/bin/synopkg install "$filename" > /dev/null
/usr/syno/bin/synopkg start "$BASE" > /dev/null & /usr/syno/bin/synopkg start "$BASE" > /dev/null &
@ -97,7 +100,7 @@ function installPackages {
trap finish SIGINT SIGTERM trap finish SIGINT SIGTERM
ts=$(date +%s%N) ts=$(date +%s%N)
echo "$HEADER v$VERSION" echo " Started $HEADER v$VERSION..."
checkNMI checkNMI
@ -140,9 +143,9 @@ else
MSG="http://${IP}:5000" MSG="http://${IP}:5000"
fi fi
echo "--------------------------------------------------------" info "--------------------------------------------------------"
echo " You can now login to DSM at ${MSG}" info " You can now login to DSM at ${MSG}"
echo "--------------------------------------------------------" info "--------------------------------------------------------"
# Wait for NMI interrupt as a shutdown signal # Wait for NMI interrupt as a shutdown signal

View File

@ -3,6 +3,9 @@
PIDFILE="/var/run/agent.pid" PIDFILE="/var/run/agent.pid"
SCRIPT="/usr/local/bin/agent.sh" SCRIPT="/usr/local/bin/agent.sh"
error () { echo -e "\E[1;31m ERROR: $1\E[0m" ; }
info () { echo -e "\E[1;34m\E[1;36m $1\E[0m" ; }
status() { status() {
if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")"; then if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")"; then
@ -25,13 +28,14 @@ start() {
if [ ! -f "$SCRIPT" ]; then if [ ! -f "$SCRIPT" ]; then
echo 'ERROR: Agent script not found!' > /dev/ttyS0
URL="https://raw.githubusercontent.com/kroese/virtual-dsm/master/agent/agent.sh" URL="https://raw.githubusercontent.com/kroese/virtual-dsm/master/agent/agent.sh"
if ! curl -sfk -m 10 -o "${SCRIPT}" "${URL}"; then if ! curl -sfk -m 10 -o "${SCRIPT}" "${URL}"; then
error 'Failed to download agent script.' > /dev/ttyS0
rm -f "${SCRIPT}" rm -f "${SCRIPT}"
return 1 return 1
else
info 'Agent script was missing?' > /dev/ttyS0
fi fi
chmod 755 "${SCRIPT}" chmod 755 "${SCRIPT}"
@ -54,7 +58,7 @@ stop() {
echo 'Stopping agent service...' echo 'Stopping agent service...'
chmod 666 /dev/ttyS0 chmod 666 /dev/ttyS0
echo 'Stopping agent service...' > /dev/ttyS0 info 'Stopping agent service...' > /dev/ttyS0
kill -15 "$(cat "$PIDFILE")" && rm -f "$PIDFILE" kill -15 "$(cat "$PIDFILE")" && rm -f "$PIDFILE"
rm -f /var/lock/subsys/agent.sh rm -f /var/lock/subsys/agent.sh
@ -81,4 +85,3 @@ case "$1" in
echo "Usage: $0 {start|stop|restart}" echo "Usage: $0 {start|stop|restart}"
exit 1 exit 1
esac esac

View File

@ -11,8 +11,8 @@ set -Eeuo pipefail
BOOT="$STORAGE/$BASE.boot.img" BOOT="$STORAGE/$BASE.boot.img"
SYSTEM="$STORAGE/$BASE.system.img" SYSTEM="$STORAGE/$BASE.system.img"
[ ! -f "$BOOT" ] && echo "ERROR: Virtual DSM boot-image does not exist ($BOOT)" && exit 81 [ ! -f "$BOOT" ] && error "Virtual DSM boot-image does not exist ($BOOT)" && exit 81
[ ! -f "$SYSTEM" ] && echo "ERROR: Virtual DSM system-image does not exist ($SYSTEM)" && exit 82 [ ! -f "$SYSTEM" ] && error "Virtual DSM system-image does not exist ($SYSTEM)" && exit 82
DATA="${STORAGE}/data.img" DATA="${STORAGE}/data.img"
@ -25,7 +25,7 @@ DISK_SIZE=$(echo "${DISK_SIZE}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
DATA_SIZE=$(numfmt --from=iec "${DISK_SIZE}") DATA_SIZE=$(numfmt --from=iec "${DISK_SIZE}")
if (( DATA_SIZE < 6442450944 )); then if (( DATA_SIZE < 6442450944 )); then
echo "ERROR: Please increase DISK_SIZE to at least 6 GB." && exit 83 error "Please increase DISK_SIZE to at least 6 GB." && exit 83
fi fi
if [ -f "${DATA}" ]; then if [ -f "${DATA}" ]; then
@ -34,7 +34,7 @@ if [ -f "${DATA}" ]; then
if [ "$DATA_SIZE" -gt "$OLD_SIZE" ]; then if [ "$DATA_SIZE" -gt "$OLD_SIZE" ]; then
echo "INFO: Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.." info "Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.."
if [[ "${ALLOCATE}" == [Nn]* ]]; then if [[ "${ALLOCATE}" == [Nn]* ]]; then
@ -49,20 +49,20 @@ if [ -f "${DATA}" ]; then
SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1) SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)
if (( REQ > SPACE )); then if (( REQ > SPACE )); then
echo "ERROR: Not enough free space to resize virtual disk to ${DISK_SIZE}." error "Not enough free space to resize virtual disk to ${DISK_SIZE}."
echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 84 error "Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 84
fi fi
# Resize file by allocating more space # 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 virtual disk." && exit 85 error "Could not allocate a file for the virtual disk." && exit 85
fi fi
if [[ "${ALLOCATE}" == [Zz]* ]]; then if [[ "${ALLOCATE}" == [Zz]* ]]; then
GB=$(( (REQ + 1073741823)/1073741824 )) GB=$(( (REQ + 1073741823)/1073741824 ))
echo "INFO: Preallocating ${GB} GB of diskspace, please wait..." info "Preallocating ${GB} GB of diskspace, please wait..."
dd if=/dev/urandom of="${DATA}" seek="${OLD_SIZE}" count="${REQ}" bs=1M iflag=count_bytes oflag=seek_bytes status=none dd if=/dev/urandom of="${DATA}" seek="${OLD_SIZE}" count="${REQ}" bs=1M iflag=count_bytes oflag=seek_bytes status=none
fi fi
@ -71,8 +71,8 @@ if [ -f "${DATA}" ]; then
if [ "$DATA_SIZE" -lt "$OLD_SIZE" ]; then if [ "$DATA_SIZE" -lt "$OLD_SIZE" ]; then
echo "INFO: Shrinking existing disks is not supported yet!" info "Shrinking existing disks is not supported yet!"
echo "INFO: Creating backup of old drive in storage folder..." info "Creating backup of old drive in storage folder..."
mv -f "${DATA}" "${DATA}.bak" mv -f "${DATA}" "${DATA}.bak"
@ -92,19 +92,19 @@ if [ ! -f "${DATA}" ]; then
SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1) SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)
if (( DATA_SIZE > SPACE )); then if (( DATA_SIZE > SPACE )); then
echo "ERROR: Not enough free space to create a virtual disk of ${DISK_SIZE}." error "Not enough free space to create a virtual disk of ${DISK_SIZE}."
echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 86 error "Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 86
fi fi
# Create an empty file # 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 virtual disk." && exit 87 error "Could not allocate a file for the virtual disk." && exit 87
fi fi
if [[ "${ALLOCATE}" == [Zz]* ]]; then if [[ "${ALLOCATE}" == [Zz]* ]]; then
echo "INFO: Preallocating ${DISK_SIZE} of diskspace, please wait..." 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
fi fi
@ -112,7 +112,7 @@ if [ ! -f "${DATA}" ]; then
# Check if file exists # Check if file exists
if [ ! -f "${DATA}" ]; then if [ ! -f "${DATA}" ]; then
echo "ERROR: Virtual disk does not exist ($DATA)" && exit 88 error "Virtual disk does not exist ($DATA)" && exit 88
fi fi
# Format as BTRFS filesystem # Format as BTRFS filesystem
@ -124,14 +124,14 @@ fi
SIZE=$(stat -c%s "${DATA}") SIZE=$(stat -c%s "${DATA}")
if [[ SIZE -ne DATA_SIZE ]]; then if [[ SIZE -ne DATA_SIZE ]]; then
echo "ERROR: Virtual disk has the wrong size: ${SIZE}" && exit 89 error "Virtual disk has the wrong size: ${SIZE}" && exit 89
fi fi
AGENT="${STORAGE}/${BASE}.agent" AGENT="${STORAGE}/${BASE}.agent"
[ -f "$AGENT" ] && AGENT_VERSION=$(cat "${AGENT}") || AGENT_VERSION=1 [ -f "$AGENT" ] && AGENT_VERSION=$(cat "${AGENT}") || AGENT_VERSION=1
if ((AGENT_VERSION < 5)); then if ((AGENT_VERSION < 5)); then
echo "INFO: The installed VirtualDSM Agent v${AGENT_VERSION} is an outdated version, please upgrade it." info "The installed VirtualDSM Agent v${AGENT_VERSION} is an outdated version, please upgrade it."
fi fi
DISK_OPTS="\ DISK_OPTS="\

View File

@ -33,16 +33,16 @@ rm -rf "$TMP" && mkdir -p "$TMP"
if [ ! -f "${RDC}" ]; then if [ ! -f "${RDC}" ]; then
RD="$TMP/rd.gz" RD="$TMP/rd.gz"
echo "Install: Downloading installer..." info "Install: Downloading installer..."
LOC="$DL/release/7.0.1/42218/DSM_VirtualDSM_42218.pat" LOC="$DL/release/7.0.1/42218/DSM_VirtualDSM_42218.pat"
{ curl -r 65627648-71021836 -sfk -o "$RD" "$LOC"; rc=$?; } || : { curl -r 65627648-71021836 -sfk -o "$RD" "$LOC"; rc=$?; } || :
(( rc != 0 )) && echo "ERROR: Failed to download $LOC, reason: $rc" && exit 60 (( rc != 0 )) && error "Failed to download $LOC, reason: $rc" && exit 60
SUM=$(md5sum "$RD" | cut -f 1 -d " ") SUM=$(md5sum "$RD" | cut -f 1 -d " ")
if [ "$SUM" != "ab399db750f88ac7aa88f608f2b8651c" ]; then if [ "$SUM" != "ab399db750f88ac7aa88f608f2b8651c" ]; then
echo "ERROR: Invalid download location (checksum $SUM)" && exit 61 error "Invalid download location (checksum $SUM)" && exit 61
fi fi
cp "$RD" "$RDC" cp "$RD" "$RDC"
@ -64,7 +64,7 @@ chmod +x /run/extract/syno_extract_system_patch
rm -rf "$TMP" && mkdir -p "$TMP" rm -rf "$TMP" && mkdir -p "$TMP"
echo "Install: Downloading $(basename "$URL")..." info "Install: Downloading $(basename "$URL")..."
PAT="/$BASE.pat" PAT="/$BASE.pat"
rm -f "$PAT" rm -f "$PAT"
@ -77,24 +77,24 @@ else
fi fi
{ wget "$URL" -O "$PAT" -q --no-check-certificate --show-progress "$PROGRESS"; rc=$?; } || : { wget "$URL" -O "$PAT" -q --no-check-certificate --show-progress "$PROGRESS"; rc=$?; } || :
(( rc != 0 )) && echo "ERROR: Failed to download $URL, reason: $rc" && exit 69 (( rc != 0 )) && error "Failed to download $URL, reason: $rc" && exit 69
[ ! -f "$PAT" ] && echo "ERROR: Failed to download $URL" && exit 69 [ ! -f "$PAT" ] && error "Failed to download $URL" && exit 69
SIZE=$(stat -c%s "$PAT") SIZE=$(stat -c%s "$PAT")
if ((SIZE<250000000)); then if ((SIZE<250000000)); then
echo "ERROR: Invalid PAT file: File is an update pack which contains no OS image." && exit 62 error "Invalid PAT file: File is an update pack which contains no OS image." && exit 62
fi fi
echo "Install: Extracting downloaded image..." info "Install: Extracting downloaded image..."
if { tar tf "$PAT"; } >/dev/null 2>&1; then if { tar tf "$PAT"; } >/dev/null 2>&1; then
tar xpf "$PAT" -C "$TMP/." tar xpf "$PAT" -C "$TMP/."
else else
export LD_LIBRARY_PATH="/run/extract" export LD_LIBRARY_PATH="/run/extract"
if ! /run/extract/syno_extract_system_patch "$PAT" "$TMP/." ; then if ! /run/extract/syno_extract_system_patch "$PAT" "$TMP/." ; then
echo "ERROR: Invalid PAT file: File is an update pack which contains no OS image." && exit 63 error "Invalid PAT file: File is an update pack which contains no OS image." && exit 63
fi fi
export LD_LIBRARY_PATH="" export LD_LIBRARY_PATH=""
fi fi
@ -104,19 +104,19 @@ IDB="$TMP/indexdb"
PKG="$TMP/packages" PKG="$TMP/packages"
HDP="$TMP/synohdpack_img" HDP="$TMP/synohdpack_img"
[ ! -f "$HDA.tgz" ] && echo "ERROR: Invalid PAT file: contains no OS image." && exit 64 [ ! -f "$HDA.tgz" ] && error "Invalid PAT file: contains no OS image." && exit 64
[ ! -f "$HDP.txz" ] && echo "ERROR: Invalid PAT file: contains no HD pack." && exit 65 [ ! -f "$HDP.txz" ] && error "Invalid PAT file: contains no HD pack." && exit 65
[ ! -f "$IDB.txz" ] && echo "ERROR: Invalid PAT file: contains no IndexDB." && exit 66 [ ! -f "$IDB.txz" ] && error "Invalid PAT file: contains no IndexDB." && exit 66
[ ! -d "$PKG" ] && echo "ERROR: Invalid PAT file: contains no packages." && exit 68 [ ! -d "$PKG" ] && error "Invalid PAT file: contains no packages." && exit 68
BOOT=$(find "$TMP" -name "*.bin.zip") BOOT=$(find "$TMP" -name "*.bin.zip")
[ ! -f "$BOOT" ] && echo "ERROR: Invalid PAT file: contains no boot file." && exit 67 [ ! -f "$BOOT" ] && error "Invalid PAT file: contains no boot file." && exit 67
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}" == [Zz]* ]] && echo "Install: Allocating diskspace..." [[ "${ALLOCATE}" == [Zz]* ]] && info "Install: Allocating diskspace..."
SYSTEM="$TMP/sys.img" SYSTEM="$TMP/sys.img"
SYSTEM_SIZE=4954537983 SYSTEM_SIZE=4954537983
@ -125,22 +125,22 @@ SYSTEM_SIZE=4954537983
SPACE=$(df --output=avail -B 1 "$TMP" | tail -n 1) SPACE=$(df --output=avail -B 1 "$TMP" | tail -n 1)
if (( SYSTEM_SIZE > SPACE )); then if (( SYSTEM_SIZE > SPACE )); then
echo "ERROR: Not enough free space to create a 4 GB system disk." && exit 87 error "Not enough free space to create a 4 GB system disk." && exit 87
fi fi
if ! fallocate -l "${SYSTEM_SIZE}" "${SYSTEM}"; then if ! fallocate -l "${SYSTEM_SIZE}" "${SYSTEM}"; then
rm -f "${SYSTEM}" rm -f "${SYSTEM}"
echo "ERROR: Could not allocate a file for the system disk." && exit 88 error "Could not allocate a file for the system disk." && exit 88
fi fi
if [[ "${ALLOCATE}" == [Zz]* ]]; then if [[ "${ALLOCATE}" == [Zz]* ]]; then
echo "Install: Preallocating 4 GB of diskspace..." info "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
# Check if file exists # Check if file exists
if [ ! -f "${SYSTEM}" ]; then if [ ! -f "${SYSTEM}" ]; then
echo "ERROR: System disk does not exist ($SYSTEM)" && exit 89 error "System disk does not exist ($SYSTEM)" && exit 89
fi fi
# Check the filesize # Check the filesize
@ -148,7 +148,7 @@ SIZE=$(stat -c%s "${SYSTEM}")
if [[ SIZE -ne SYSTEM_SIZE ]]; then if [[ SIZE -ne SYSTEM_SIZE ]]; then
rm -f "${SYSTEM}" rm -f "${SYSTEM}"
echo "ERROR: System disk has the wrong size: ${SIZE}" && exit 90 error "System disk has the wrong size: ${SIZE}" && exit 90
fi fi
PART="$TMP/partition.fdisk" PART="$TMP/partition.fdisk"
@ -165,7 +165,7 @@ PART="$TMP/partition.fdisk"
sfdisk -q "$SYSTEM" < "$PART" sfdisk -q "$SYSTEM" < "$PART"
echo "Install: Extracting system partition..." info "Install: Extracting system partition..."
MOUNT="$TMP/system" MOUNT="$TMP/system"
@ -196,7 +196,7 @@ chmod 755 "$LOC/agent.sh"
# Store agent version # Store agent version
echo "7" > "$STORAGE"/"$BASE".agent echo "7" > "$STORAGE"/"$BASE".agent
echo "Install: Installing system partition..." info "Install: Installing system partition..."
LABEL="1.44.1-42218" LABEL="1.44.1-42218"
OFFSET="1048576" # 2048 * 512 OFFSET="1048576" # 2048 * 512

View File

@ -33,8 +33,8 @@ configureDHCP() {
{ ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge ; rc=$?; } || : { ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge ; rc=$?; } || :
if (( rc != 0 )); then if (( rc != 0 )); then
echo "ERROR: Cannot create macvlan interface. Please make sure the network type is 'macvlan' and not 'ipvlan'," error "Cannot create macvlan interface. Please make sure the network type is 'macvlan' and not 'ipvlan',"
echo "ERROR: and that the NET_ADMIN capability has been added to the container config: --cap-add NET_ADMIN" && exit 15 error "and that the NET_ADMIN capability has been added to the container config: --cap-add NET_ADMIN" && exit 15
fi fi
ip address add "${IP}" dev "${VM_NET_VLAN}" ip address add "${IP}" dev "${VM_NET_VLAN}"
@ -48,8 +48,8 @@ configureDHCP() {
{ ip link add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge ; rc=$?; } || : { ip link add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge ; rc=$?; } || :
if (( rc != 0 )); then if (( rc != 0 )); then
echo "ERROR: Capability NET_ADMIN has not been set most likely. Please add the " error "Capability NET_ADMIN has not been set most likely. Please add the "
echo "ERROR: following docker setting to your container: --cap-add NET_ADMIN" && exit 16 error "following docker setting to your container: --cap-add NET_ADMIN" && exit 16
fi fi
ip link set "${VM_NET_TAP}" up ip link set "${VM_NET_TAP}" up
@ -63,28 +63,28 @@ configureDHCP() {
IFS=: read -r MAJOR MINOR < <(cat /sys/devices/virtual/net/"${VM_NET_TAP}"/tap*/dev) IFS=: read -r MAJOR MINOR < <(cat /sys/devices/virtual/net/"${VM_NET_TAP}"/tap*/dev)
if (( MAJOR < 1)); then if (( MAJOR < 1)); then
echo "ERROR: Cannot find: sys/devices/virtual/net/${VM_NET_TAP}" && exit 18 error "Cannot find: sys/devices/virtual/net/${VM_NET_TAP}" && exit 18
fi fi
[[ ! -e "${TAP_PATH}" ]] && [[ -e "/dev0/${TAP_PATH##*/}" ]] && ln -s "/dev0/${TAP_PATH##*/}" "${TAP_PATH}" [[ ! -e "${TAP_PATH}" ]] && [[ -e "/dev0/${TAP_PATH##*/}" ]] && ln -s "/dev0/${TAP_PATH##*/}" "${TAP_PATH}"
if [[ ! -e "${TAP_PATH}" ]]; then if [[ ! -e "${TAP_PATH}" ]]; then
{ mknod "${TAP_PATH}" c "$MAJOR" "$MINOR" ; rc=$?; } || : { mknod "${TAP_PATH}" c "$MAJOR" "$MINOR" ; rc=$?; } || :
(( rc != 0 )) && echo "ERROR: Cannot mknod: ${TAP_PATH} ($rc)" && exit 20 (( rc != 0 )) && error "Cannot mknod: ${TAP_PATH} ($rc)" && exit 20
fi fi
{ exec 30>>"$TAP_PATH"; rc=$?; } || : { exec 30>>"$TAP_PATH"; rc=$?; } || :
if (( rc != 0 )); then if (( rc != 0 )); then
echo "ERROR: Cannot create TAP interface ($rc). Please add the following docker settings to your " error "Cannot create TAP interface ($rc). Please add the following docker settings to your "
echo "ERROR: container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21 error "container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21
fi fi
{ exec 40>>/dev/vhost-net; rc=$?; } || : { exec 40>>/dev/vhost-net; rc=$?; } || :
if (( rc != 0 )); then if (( rc != 0 )); then
echo "ERROR: VHOST can not be found ($rc). Please add the following " error "VHOST can not be found ($rc). Please add the following "
echo "ERROR: docker setting to your container: --device=/dev/vhost-net" && exit 22 error "docker setting to your container: --device=/dev/vhost-net" && exit 22
fi fi
NET_OPTS="-netdev tap,id=hostnet0,vhost=on,vhostfd=40,fd=30" NET_OPTS="-netdev tap,id=hostnet0,vhost=on,vhostfd=40,fd=30"
@ -99,8 +99,8 @@ configureNAT () {
{ ip link add dev dockerbridge type bridge ; rc=$?; } || : { ip link add dev dockerbridge type bridge ; rc=$?; } || :
if (( rc != 0 )); then if (( rc != 0 )); then
echo "ERROR: Capability NET_ADMIN has not been set most likely. Please add the " error "Capability NET_ADMIN has not been set most likely. Please add the "
echo "ERROR: following docker setting to your container: --cap-add NET_ADMIN" && exit 23 error "following docker setting to your container: --cap-add NET_ADMIN" && exit 23
fi fi
ip address add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge ip address add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge
@ -128,7 +128,7 @@ configureNAT () {
if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then
{ sysctl -w net.ipv4.ip_forward=1 ; rc=$?; } || : { sysctl -w net.ipv4.ip_forward=1 ; rc=$?; } || :
if (( rc != 0 )); then if (( rc != 0 )); then
echo "ERROR: Please add the following docker setting to your container: --sysctl net.ipv4.ip_forward=1" && exit 24 error "Please add the following docker setting to your container: --sysctl net.ipv4.ip_forward=1" && exit 24
fi fi
fi fi
@ -191,7 +191,7 @@ if [ ! -c /dev/net/tun ]; then
chmod 666 /dev/net/tun chmod 666 /dev/net/tun
fi fi
[ ! -c /dev/net/tun ] && echo "ERROR: TUN network interface not available..." && exit 85 [ ! -c /dev/net/tun ] && error "TUN network interface not available..." && exit 85
# Create the necessary file structure for /dev/vhost-net # Create the necessary file structure for /dev/vhost-net
if [ ! -c /dev/vhost-net ]; then if [ ! -c /dev/vhost-net ]; then
@ -208,15 +208,15 @@ GATEWAY=$(ip r | grep default | awk '{print $3}')
if [[ "${DEBUG}" == [Yy1]* ]]; 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 info "Container IP is ${IP} with gateway ${GATEWAY}" && echo
fi fi
if [[ "${DHCP}" == [Yy1]* ]]; then if [[ "${DHCP}" == [Yy1]* ]]; then
if [[ "$GATEWAY" == "172."* ]]; then if [[ "$GATEWAY" == "172."* ]]; then
echo -n "ERROR: You cannot enable DHCP while the container is " error "You cannot enable DHCP while the container is "
echo "in a bridge network, only on a macvlan network!" && exit 86 error "in a bridge network, only on a macvlan network!" && exit 86
fi fi
# Configuration for DHCP IP # Configuration for DHCP IP
@ -236,6 +236,6 @@ 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"
[[ "${DEBUG}" == [Yy1]* ]] && echo && echo "Finished network setup.." && echo [[ "${DEBUG}" == [Yy1]* ]] && echo && info "Finished network setup.." && echo
return 0 return 0

View File

@ -26,7 +26,7 @@ _graceful_shutdown() {
[ ! -f "${_QEMU_PID}" ] && return [ ! -f "${_QEMU_PID}" ] && return
[ -f "${_QEMU_SHUTDOWN_COUNTER}" ] && return [ -f "${_QEMU_SHUTDOWN_COUNTER}" ] && return
echo && echo "Received $1 signal, shutting down..." echo && info "Received $1 signal, shutting down..."
echo 0 > "${_QEMU_SHUTDOWN_COUNTER}" echo 0 > "${_QEMU_SHUTDOWN_COUNTER}"
# Don't send the powerdown signal because vDSM ignores ACPI signals # Don't send the powerdown signal because vDSM ignores ACPI signals
@ -37,7 +37,7 @@ _graceful_shutdown() {
if [[ ! "${RESPONSE}" =~ "\"success\"" ]] ; then if [[ ! "${RESPONSE}" =~ "\"success\"" ]] ; then
echo && echo "ERROR: Could not send shutdown command to the guest ($RESPONSE)" echo && error "Could not send shutdown command to the guest ($RESPONSE)"
# If we cannot shutdown the usual way, fallback to the NMI method # If we cannot shutdown the usual way, fallback to the NMI method
@ -55,7 +55,7 @@ _graceful_shutdown() {
if ((AGENT_VERSION < 2)); then if ((AGENT_VERSION < 2)); then
echo && echo "Please update the VirtualDSM Agent to allow for gracefull shutdowns..." echo && info "Please update the VirtualDSM Agent to allow for gracefull shutdowns..."
kill -15 "$(cat "${_QEMU_PID}")" kill -15 "$(cat "${_QEMU_PID}")"
pkill -f qemu-system-x86_64 || true pkill -f qemu-system-x86_64 || true
@ -72,13 +72,13 @@ _graceful_shutdown() {
if echo 'info version'| nc -q 1 -w 1 localhost "${QEMU_MONPORT}" >/dev/null 2>&1 ; then if echo 'info version'| nc -q 1 -w 1 localhost "${QEMU_MONPORT}" >/dev/null 2>&1 ; then
sleep 1 sleep 1
#echo "Shutting down, waiting... ($(cat ${_QEMU_SHUTDOWN_COUNTER})/${QEMU_POWERDOWN_TIMEOUT})" #info "Shutting down, waiting... ($(cat ${_QEMU_SHUTDOWN_COUNTER})/${QEMU_POWERDOWN_TIMEOUT})"
fi fi
done done
echo && echo "Quitting..." echo && info "Quitting..."
echo 'quit' | nc -q 1 -w 1 localhost "${QEMU_MONPORT}" >/dev/null 2>&1 || true echo 'quit' | nc -q 1 -w 1 localhost "${QEMU_MONPORT}" >/dev/null 2>&1 || true
return return

View File

@ -10,16 +10,19 @@ set -Eeuo pipefail
: ${DISK_SIZE:='16G'} # Initial data disk size : ${DISK_SIZE:='16G'} # Initial data disk size
: ${RAM_SIZE:='512M'} # Maximum RAM amount : ${RAM_SIZE:='512M'} # Maximum RAM amount
echo "Starting Virtual DSM for Docker v${VERSION}..." info () { echo -e "\E[1;34m \E[1;36m$1\E[0m" ; }
trap 'echo >&2 "Error status $? for: ${BASH_COMMAND} (line $LINENO/$BASH_LINENO)"' ERR error () { echo -e >&2 "\E[1;31m ERROR: $1\E[0m" ; }
[ ! -f "/run/run.sh" ] && echo "ERROR: Script must run inside Docker container!" && exit 11 echo " Starting Virtual DSM for Docker v${VERSION}..."
[ "$(id -u)" -ne "0" ] && echo "ERROR: Script must be executed with root privileges." && exit 12 trap 'error "Error status $? for: ${BASH_COMMAND} (line $LINENO/$BASH_LINENO)"' ERR
[ ! -f "/run/run.sh" ] && error "Script must run inside Docker container!" && exit 11
[ "$(id -u)" -ne "0" ] && 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)
[ ! -d "$STORAGE" ] && echo "ERROR: Storage folder (${STORAGE}) not found!" && exit 13 [ ! -d "$STORAGE" ] && error "Storage folder (${STORAGE}) not found!" && exit 13
if [ -f "$STORAGE"/dsm.ver ]; then if [ -f "$STORAGE"/dsm.ver ]; then
BASE=$(cat "${STORAGE}/dsm.ver") BASE=$(cat "${STORAGE}/dsm.ver")
@ -58,7 +61,7 @@ else
fi fi
if [ -n "${KVM_ERR}" ]; then if [ -n "${KVM_ERR}" ]; then
echo "ERROR: KVM acceleration not detected ${KVM_ERR}, please enable it." error "KVM acceleration not detected ${KVM_ERR}, please enable it."
[[ "${DEBUG}" == [Yy1]* ]] && exit 88 [[ "${DEBUG}" == [Yy1]* ]] && exit 88
else else
KVM_OPTS=",accel=kvm -enable-kvm -cpu host" KVM_OPTS=",accel=kvm -enable-kvm -cpu host"