Skip to content

Commit

Permalink
Use colorcodes in log output
Browse files Browse the repository at this point in the history
Use colorcodes in log output
  • Loading branch information
kroese committed May 11, 2023
2 parents 14212a5 + d262d42 commit 90308d9
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 86 deletions.
27 changes: 15 additions & 12 deletions agent/agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
Expand All @@ -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
Expand Down Expand Up @@ -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..."

}

Expand All @@ -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 &
Expand All @@ -97,7 +100,7 @@ function installPackages {
trap finish SIGINT SIGTERM

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

checkNMI

Expand Down Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions agent/service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand All @@ -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
Expand All @@ -81,4 +85,3 @@ case "$1" in
echo "Usage: $0 {start|stop|restart}"
exit 1
esac

34 changes: 17 additions & 17 deletions run/disk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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"

Expand All @@ -92,27 +92,27 @@ 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
fi

# 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
Expand All @@ -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="\
Expand Down
44 changes: 22 additions & 22 deletions run/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -125,30 +125,30 @@ 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
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"
Expand All @@ -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"

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 90308d9

Please sign in to comment.