Commit 1d42955d authored by Kroese's avatar Kroese Committed by GitHub
Browse files

Use colorcodes in log output

Use colorcodes in log output
parents da0d32ba fbf8a335
Loading
Loading
Loading
Loading
+15 −12
Original line number Diff line number Diff line
@@ -6,9 +6,12 @@ HEADER="VirtualDSM Agent"

# Functions

error () { echo -e "\E[1;31m❯ ERROR: $1\E[0m" ; }
info () { echo -e "\E[1;34m❯\E[1;36m $1\E[0m" ; }

finish() {

  echo "$HEADER: Shutting down.."
  echo "$HEADER: Shutting down.."
  exit

}
@@ -20,7 +23,7 @@ function checkNMI {

  if [ "$nmi" != "" ]; then

    echo "$HEADER: Received shutdown request through NMI.."
    info "Received shutdown request through NMI.."

    /usr/syno/sbin/synoshutdown -s > /dev/null
    finish
@@ -48,27 +51,27 @@ function downloadUpdate {
  [[ remote_size -eq local_size ]] && return

  if ! curl -sfk -m 10 -o "${TMP}" "${URL}"; then
    echo "$HEADER: curl error ($?)" && return
    error "$HEADER: curl error ($?)" && return
  fi

  if [ ! -f "${TMP}" ]; then
    echo "$HEADER: update error, file not found.." && return
    error "$HEADER: update error, file not found.." && return
  fi

  line=$(head -1 "${TMP}")

  if [[ "$line" != "#!/usr/bin/env bash" ]]; then
    echo "$HEADER: update error, invalid header: $line" && return
    error "$HEADER: update error, invalid header: $line" && return
  fi

  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

  mv -f "${TMP}" "${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

      echo "$HEADER: Installing package ${BASE}.."
      info "Installing package ${BASE}.."

      /usr/syno/bin/synopkg install "$filename" > /dev/null
      /usr/syno/bin/synopkg start "$BASE" > /dev/null &
@@ -97,7 +100,7 @@ function installPackages {
trap finish SIGINT SIGTERM

ts=$(date +%s%N)
echo "$HEADER v$VERSION"
echo "❯ Started $HEADER v$VERSION..."

checkNMI

@@ -140,9 +143,9 @@ else
  MSG="http://${IP}:5000"
fi

echo "--------------------------------------------------------"
echo " You can now login to DSM at ${MSG}"
echo "--------------------------------------------------------"
info "--------------------------------------------------------"
info " You can now login to DSM at ${MSG}"
info "--------------------------------------------------------"

# Wait for NMI interrupt as a shutdown signal

+7 −4
Original line number Diff line number Diff line
@@ -3,6 +3,9 @@
PIDFILE="/var/run/agent.pid"
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() {

  if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")"; then
@@ -25,13 +28,14 @@ start() {

  if [ ! -f "$SCRIPT" ]; then

    echo 'ERROR: Agent script not found!' > /dev/ttyS0

    URL="https://raw.githubusercontent.com/kroese/virtual-dsm/master/agent/agent.sh"

    if ! curl -sfk -m 10 -o "${SCRIPT}" "${URL}"; then
      error 'Failed to download agent script.' > /dev/ttyS0
      rm -f "${SCRIPT}"
      return 1
    else
      info 'Agent script was missing?' > /dev/ttyS0
    fi

    chmod 755 "${SCRIPT}"
@@ -54,7 +58,7 @@ stop() {
  echo 'Stopping agent service...'

  chmod 666 /dev/ttyS0
  echo 'Stopping agent service...' > /dev/ttyS0
  info 'Stopping agent service...' > /dev/ttyS0

  kill -15 "$(cat "$PIDFILE")" && rm -f "$PIDFILE"
  rm -f /var/lock/subsys/agent.sh
@@ -81,4 +85,3 @@ case "$1" in
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
+17 −17
Original line number Diff line number Diff line
@@ -11,8 +11,8 @@ set -Eeuo pipefail
BOOT="$STORAGE/$BASE.boot.img"
SYSTEM="$STORAGE/$BASE.system.img"

[ ! -f "$BOOT" ] && echo "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 "$BOOT" ] && error "Virtual DSM boot-image does not exist ($BOOT)" && exit 81
[ ! -f "$SYSTEM" ] && error "Virtual DSM system-image does not exist ($SYSTEM)" && exit 82

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}")

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

if [ -f "${DATA}" ]; then
@@ -34,7 +34,7 @@ if [ -f "${DATA}" ]; 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

@@ -49,20 +49,20 @@ if [ -f "${DATA}" ]; then
      SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)

      if (( REQ > SPACE )); then
        echo "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 "Not enough free space to resize virtual disk to ${DISK_SIZE}."
        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 virtual disk." && exit 85
        error "Could not allocate a file for the virtual disk." && exit 85
      fi

      if [[ "${ALLOCATE}" == [Zz]* ]]; then

        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

      fi
@@ -71,8 +71,8 @@ if [ -f "${DATA}" ]; then

  if [ "$DATA_SIZE" -lt "$OLD_SIZE" ]; then

    echo "INFO: Shrinking existing disks is not supported yet!"
    echo "INFO: Creating backup of old drive in storage folder..."
    info "Shrinking existing disks is not supported yet!"
    info "Creating backup of old drive in storage folder..."

    mv -f "${DATA}" "${DATA}.bak"

@@ -92,19 +92,19 @@ if [ ! -f "${DATA}" ]; then
    SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)

    if (( DATA_SIZE > SPACE )); then
      echo "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 "Not enough free space to create a virtual disk of ${DISK_SIZE}."
      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 virtual disk." && exit 87
      error "Could not allocate a file for the virtual disk." && exit 87
    fi

    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

    fi
@@ -112,7 +112,7 @@ if [ ! -f "${DATA}" ]; then

  # Check if file exists
  if [ ! -f "${DATA}" ]; then
    echo "ERROR: Virtual disk does not exist ($DATA)" && exit 88
    error "Virtual disk does not exist ($DATA)" && exit 88
  fi

  # Format as BTRFS filesystem
@@ -124,14 +124,14 @@ fi
SIZE=$(stat -c%s "${DATA}")

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

AGENT="${STORAGE}/${BASE}.agent"
[ -f "$AGENT" ] && AGENT_VERSION=$(cat "${AGENT}") || AGENT_VERSION=1

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

DISK_OPTS="\
+22 −22
Original line number Diff line number Diff line
@@ -33,16 +33,16 @@ rm -rf "$TMP" && mkdir -p "$TMP"
if [ ! -f "${RDC}" ]; then

  RD="$TMP/rd.gz"
  echo "Install: Downloading installer..." 
  info "Install: Downloading installer..." 
  LOC="$DL/release/7.0.1/42218/DSM_VirtualDSM_42218.pat"

  { 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 " ")

  if [ "$SUM" != "ab399db750f88ac7aa88f608f2b8651c" ]; then
    echo "ERROR: Invalid download location (checksum $SUM)" && exit 61
    error "Invalid download location (checksum $SUM)" && exit 61
  fi

  cp "$RD" "$RDC"
@@ -64,7 +64,7 @@ chmod +x /run/extract/syno_extract_system_patch

rm -rf "$TMP" && mkdir -p "$TMP"

echo "Install: Downloading $(basename "$URL")..."
info "Install: Downloading $(basename "$URL")..."

PAT="/$BASE.pat"
rm -f "$PAT"
@@ -77,24 +77,24 @@ else
fi

{ 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")

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

echo "Install: Extracting downloaded image..."
info "Install: Extracting downloaded image..."

if { tar tf "$PAT"; } >/dev/null 2>&1; then
   tar xpf "$PAT" -C "$TMP/."
else
   export LD_LIBRARY_PATH="/run/extract"
   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
   export LD_LIBRARY_PATH=""
fi
@@ -104,19 +104,19 @@ IDB="$TMP/indexdb"
PKG="$TMP/packages"
HDP="$TMP/synohdpack_img"

[ ! -f "$HDA.tgz" ] && echo "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 "$IDB.txz" ] && echo "ERROR: Invalid PAT file: contains no IndexDB." && exit 66
[ ! -d "$PKG" ] && echo "ERROR: Invalid PAT file: contains no packages." && exit 68
[ ! -f "$HDA.tgz" ] && error "Invalid PAT file: contains no OS image." && exit 64
[ ! -f "$HDP.txz" ] && error "Invalid PAT file: contains no HD pack." && exit 65
[ ! -f "$IDB.txz" ] && error "Invalid PAT file: contains no IndexDB." && exit 66
[ ! -d "$PKG" ] && error "Invalid PAT file: contains no packages." && exit 68

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)
unzip -q -o "$BOOT".zip -d "$TMP"

[[ "${ALLOCATE}" == [Zz]* ]] && echo "Install: Allocating diskspace..."
[[ "${ALLOCATE}" == [Zz]* ]] && info "Install: Allocating diskspace..."

SYSTEM="$TMP/sys.img"
SYSTEM_SIZE=4954537983
@@ -125,22 +125,22 @@ SYSTEM_SIZE=4954537983
SPACE=$(df --output=avail -B 1 "$TMP" | tail -n 1)

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

if ! fallocate -l "${SYSTEM_SIZE}" "${SYSTEM}"; then
  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

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
fi

# Check if file exists
if [ ! -f "${SYSTEM}" ]; then
    echo "ERROR: System disk does not exist ($SYSTEM)" && exit 89
    error "System disk does not exist ($SYSTEM)" && exit 89
fi

# Check the filesize
@@ -148,7 +148,7 @@ SIZE=$(stat -c%s "${SYSTEM}")

if [[ SIZE -ne SYSTEM_SIZE ]]; then
  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

PART="$TMP/partition.fdisk"
@@ -165,7 +165,7 @@ PART="$TMP/partition.fdisk"

sfdisk -q "$SYSTEM" < "$PART"

echo "Install: Extracting system partition..."
info "Install: Extracting system partition..."

MOUNT="$TMP/system"

@@ -196,7 +196,7 @@ chmod 755 "$LOC/agent.sh"
# Store agent version
echo "7" > "$STORAGE"/"$BASE".agent

echo "Install: Installing system partition..."
info "Install: Installing system partition..."

LABEL="1.44.1-42218"
OFFSET="1048576" # 2048 * 512
+20 −20
Original line number Diff line number Diff line
@@ -33,8 +33,8 @@ configureDHCP() {
  { ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge ; rc=$?; } || :

  if (( rc != 0 )); then
    echo "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 "Cannot create macvlan interface. Please make sure the network type is 'macvlan' and not 'ipvlan',"
    error "and that the NET_ADMIN capability has been added to the container config: --cap-add NET_ADMIN" && exit 15
  fi

  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=$?; } || :

  if (( rc != 0 )); then
    echo "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 "Capability NET_ADMIN has not been set most likely. Please add the "
    error "following docker setting to your container: --cap-add NET_ADMIN" && exit 16
  fi

  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)

  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

  [[ ! -e "${TAP_PATH}" ]] && [[ -e "/dev0/${TAP_PATH##*/}" ]] && ln -s "/dev0/${TAP_PATH##*/}" "${TAP_PATH}"

  if [[ ! -e "${TAP_PATH}" ]]; then
    { 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

  { exec 30>>"$TAP_PATH"; rc=$?; } || :

  if (( rc != 0 )); then
    echo "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 "Cannot create TAP interface ($rc). Please add the following docker settings to your "
    error "container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21
  fi

  { exec 40>>/dev/vhost-net; rc=$?; } || :

  if (( rc != 0 )); then
    echo "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 "VHOST can not be found ($rc). Please add the following "
    error "docker setting to your container: --device=/dev/vhost-net" && exit 22
  fi

  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=$?; } || :

  if (( rc != 0 )); then
    echo "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 "Capability NET_ADMIN has not been set most likely. Please add the "
    error "following docker setting to your container: --cap-add NET_ADMIN" && exit 23
  fi

  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
    { sysctl -w net.ipv4.ip_forward=1 ; rc=$?; } || :
    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

@@ -191,7 +191,7 @@ if [ ! -c /dev/net/tun ]; then
  chmod 666 /dev/net/tun
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
if [ ! -c /dev/vhost-net ]; then
@@ -208,15 +208,15 @@ GATEWAY=$(ip r | grep default | awk '{print $3}')
if [[ "${DEBUG}" == [Yy1]* ]]; 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

if [[ "${DHCP}" == [Yy1]* ]]; then

  if [[ "$GATEWAY" == "172."* ]]; then
    echo -n "ERROR: You cannot enable DHCP while the container is "
    echo "in a bridge network, only on a macvlan network!" && exit 86
    error "You cannot enable DHCP while the container is "
    error "in a bridge network, only on a macvlan network!" && exit 86
  fi

  # 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"

[[ "${DEBUG}" == [Yy1]* ]] && echo && echo "Finished network setup.." && echo
[[ "${DEBUG}" == [Yy1]* ]] && echo && info "Finished network setup.." && echo

return 0
Loading