Skip to content

Commit

Permalink
feat: Display progress via web (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
kroese committed Jan 20, 2024
1 parent fc92b66 commit 2c7cea0
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 101 deletions.
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ RUN if [ "$TARGETPLATFORM" != "linux/amd64" ]; then extra="qemu-user"; fi \
wget \
fdisk \
unzip \
socat \
nginx \
procps \
xz-utils \
iptables \
Expand All @@ -39,11 +39,16 @@ RUN if [ "$TARGETPLATFORM" != "linux/amd64" ]; then extra="qemu-user"; fi \
qemu-system-x86 \
"$extra" \
&& apt-get clean \
&& unlink /etc/nginx/sites-enabled/default \
&& sed -i 's/^worker_processes.*/worker_processes 1;/' /etc/nginx/nginx.conf \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY ./src /run/
COPY ./web /var/www/
COPY --from=builder /qemu-host.bin /run/host.bin

RUN chmod +x /run/*.sh && chmod +x /run/*.bin
RUN mv /var/www/nginx.conf /etc/nginx/sites-enabled/web.conf

VOLUME /storage
EXPOSE 22 139 445 5000
Expand Down
25 changes: 17 additions & 8 deletions src/disk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ getSize() {
local DISK_FILE=$1
local DISK_EXT DISK_FMT

DISK_EXT="$(echo "${DISK_FILE//*./}" | sed 's/^.*\.//')"
DISK_FMT="$(ext2fmt "$DISK_EXT")"
DISK_EXT=$(echo "${DISK_FILE//*./}" | sed 's/^.*\.//')
DISK_FMT=$(ext2fmt "$DISK_EXT")

case "${DISK_FMT,,}" in
raw)
Expand Down Expand Up @@ -112,7 +112,9 @@ createDisk() {
fi
fi

info "Creating a $DISK_TYPE $DISK_DESC image in $DISK_FMT format with a size of $DISK_SPACE..."
MSG="Creating a $DISK_TYPE $DISK_DESC image in $DISK_FMT format with a size of $DISK_SPACE..."
info "$MSG" && html "$MSG"

local FAIL="Could not create a $DISK_TYPE $DISK_FMT $DISK_DESC image of $DISK_SPACE ($DISK_FILE)"

case "${DISK_FMT,,}" in
Expand Down Expand Up @@ -195,7 +197,9 @@ resizeDisk() {
fi

local GB=$(( (CUR_SIZE + 1073741823)/1073741824 ))
info "Resizing $DISK_DESC from ${GB}G to $DISK_SPACE..."
MSG="Resizing $DISK_DESC from ${GB}G to $DISK_SPACE..."
info "$MSG" && html "$MSG"

local FAIL="Could not resize the $DISK_TYPE $DISK_FMT $DISK_DESC image from ${GB}G to $DISK_SPACE ($DISK_FILE)"

case "${DISK_FMT,,}" in
Expand Down Expand Up @@ -262,7 +266,8 @@ convertDisk() {
fi
fi

info "Converting $DISK_DESC to $DST_FMT, please wait until completed..."
MSG="Converting $DISK_DESC to $DST_FMT, please wait until completed..."
info "$MSG" && html "$MSG"

local CONV_FLAGS="-p"
local DISK_PARAM="$DISK_ALLOC"
Expand Down Expand Up @@ -301,7 +306,8 @@ convertDisk() {
fi
fi

info "Conversion of $DISK_DESC to $DST_FMT completed succesfully!"
MSG="Conversion of $DISK_DESC to $DST_FMT completed succesfully!"
info "$MSG" && html "$MSG"

return 0
}
Expand Down Expand Up @@ -372,7 +378,7 @@ addDisk () {
else
PREV_FMT="qcow2"
fi
PREV_EXT="$(fmt2ext "$PREV_FMT")"
PREV_EXT=$(fmt2ext "$PREV_FMT")

if [ -f "$DISK_BASE.$PREV_EXT" ] ; then
convertDisk "$DISK_BASE.$PREV_EXT" "$PREV_FMT" "$DISK_FILE" "$DISK_FMT" "$DISK_BASE" "$DISK_DESC" "$FS" || exit $?
Expand Down Expand Up @@ -420,7 +426,9 @@ addDevice () {
return 0
}

DISK_EXT="$(fmt2ext "$DISK_FMT")" || exit $?
html "Initializing disks..."

DISK_EXT=$(fmt2ext "$DISK_FMT")

if [ -z "$ALLOCATE" ]; then
if [[ "${DISK_FMT,,}" == "raw" ]]; then
Expand Down Expand Up @@ -505,4 +513,5 @@ else
addDisk "userdata4" "$DISK4_FILE" "$DISK_EXT" "disk4" "$DISK4_SIZE" "6" "0xf" "$DISK_FMT" || exit $?
fi

html "Initialized disks successfully..."
return 0
5 changes: 2 additions & 3 deletions src/entry.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env bash
set -Eeuo pipefail

echo "❯ Starting Virtual DSM for Docker v$(</run/version)..."
echo "❯ For support visit https://github.com/vdsm/virtual-dsm"
echo
APP="Virtual DSM"
SUPPORT="https://github.com/vdsm/virtual-dsm"

cd /run

Expand Down
25 changes: 16 additions & 9 deletions src/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ if [[ -f "$STORAGE/$BASE.boot.img" ]] && [[ -f "$STORAGE/$BASE.system.img" ]]; t
return 0 # Previous installation found
fi

# Display wait message
/run/server.sh 5000 install &
html "Please wait while Virtual DSM is being installed..."

DL=""
DL_CHINA="https://cndl.synology.cn/download/DSM"
Expand Down Expand Up @@ -105,7 +104,8 @@ RDC="$STORAGE/dsm.rd"

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

info "Install: Downloading installer..."
MSG="Downloading installer..."
info "Install: $MSG" && html "$MSG"

RD="$TMP/rd.gz"
POS="65627648-71021835"
Expand Down Expand Up @@ -174,7 +174,8 @@ fi

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

info "Install: Downloading $BASE.pat..."
MSG="Downloading $BASE.pat..."
info "Install: $MSG" && html "$MSG"

PAT="/$BASE.pat"
rm -f "$PAT"
Expand All @@ -198,7 +199,8 @@ if ((SIZE<250000000)); then
error "The specified PAT file is probably an update pack as it's too small." && exit 62
fi

info "Install: Extracting downloaded image..."
MSG="Extracting downloaded image..."
info "Install: $MSG" && html "$MSG"

if { tar tf "$PAT"; } >/dev/null 2>&1; then

Expand All @@ -221,7 +223,9 @@ else
fi

rm -rf /run/extract
info "Install: Preparing system partition..."

MSG="Preparing system partition..."
info "Install: $MSG" && html "$MSG"

BOOT=$(find "$TMP" -name "*.bin.zip")
[ ! -f "$BOOT" ] && error "The PAT file contains no boot image." && exit 67
Expand Down Expand Up @@ -277,7 +281,8 @@ sfdisk -q "$SYSTEM" < "$PART"
MOUNT="$TMP/system"
rm -rf "$MOUNT" && mkdir -p "$MOUNT"

info "Install: Extracting system partition..."
MSG="Extracting system partition..."
info "Install: $MSG" && html "$MSG"

HDA="$TMP/hda1"
IDB="$TMP/indexdb"
Expand All @@ -301,20 +306,21 @@ fi
LABEL="1.44.1-42218"
OFFSET="1048576" # 2048 * 512
NUMBLOCKS="622560" # (4980480 * 512) / 4096
MSG="Installing system partition..."

if [[ "$ROOT" != [Nn]* ]]; then

tar xpfJ "$HDA.txz" --absolute-names --skip-old-files -C "$MOUNT/"

info "Install: Installing system partition..."
info "Install: $MSG" && html "$MSG"

mke2fs -q -t ext4 -b 4096 -d "$MOUNT/" -L "$LABEL" -F -E "offset=$OFFSET" "$SYSTEM" "$NUMBLOCKS"

else

fakeroot -- bash -c "set -Eeu;\
tar xpfJ $HDA.txz --absolute-names --skip-old-files -C $MOUNT/;\
printf '%b%s%b' '\E[1;34m❯ \E[1;36m' 'Install: Installing system partition...' '\E[0m\n';\
printf '%b%s%b' '\E[1;34m❯ \E[1;36m' 'Install: $MSG' '\E[0m\n';\
mke2fs -q -t ext4 -b 4096 -d $MOUNT/ -L $LABEL -F -E offset=$OFFSET $SYSTEM $NUMBLOCKS"

fi
Expand All @@ -334,4 +340,5 @@ rm -rf "$TMP"
{ set +x; } 2>/dev/null
[[ "$DEBUG" == [Yy1]* ]] && echo

html "Installation finished successfully..."
return 0
18 changes: 12 additions & 6 deletions src/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,17 @@ closeNetwork() {

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

fKill "server.sh"
# Shutdown nginx
nginx -s stop 2> /dev/null
fWait "nginx"

ip link set "$VM_NET_TAP" down || true
ip link delete "$VM_NET_TAP" || true

else

fKill "dnsmasq"
local pid="/var/run/dnsmasq.pid"
[ -f "$pid" ] && pKill "$(<"$pid")"

ip link set "$VM_NET_TAP" down promisc off || true
ip link delete "$VM_NET_TAP" || true
Expand Down Expand Up @@ -227,15 +230,14 @@ getInfo() {
# Configure Network
# ######################################

fKill "server.sh"

if [ ! -c /dev/vhost-net ]; then
if mknod /dev/vhost-net c 10 238; then
chmod 660 /dev/vhost-net
fi
fi

getInfo
html "Initializing network..."

if [[ "$DEBUG" == [Yy1]* ]]; then
info "Container IP is $IP with gateway $GATEWAY on interface $VM_NET_DEV" && echo
Expand All @@ -252,11 +254,15 @@ if [[ "$DHCP" == [Yy1]* ]]; then
# Configuration for DHCP IP
configureDHCP

# Display IP on port 80 and 5000
/run/server.sh 5000 /run/ip.sh &
MSG="Please wait while discovering IP..."
html "$MSG" "2000"

else

# Shutdown nginx
nginx -s stop 2> /dev/null
fWait "nginx"

# Configuration for static IP
configureNAT

Expand Down
14 changes: 14 additions & 0 deletions src/print.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "$1" "\E[0m\n" >&2; }
error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; }

file="/run/shm/dsm.url"
page="/run/shm/index.html"
address="/run/shm/qemu.ip"
shutdown="/run/shm/qemu.end"
template="/var/www/index.html"
url="http://127.0.0.1:2210/read?command=10"

resp_err="Guest returned an invalid response:"
Expand Down Expand Up @@ -66,6 +68,18 @@ location=$(<"$file")
if [[ "$location" != "20.20"* ]]; then

msg="http://$location"
title="<title>Virtual DSM</title>"
body="The location of DSM is <a href='http://$location'>http://$location</a>"
script="<script>setTimeout(function(){ window.location.assign('http://$location'); }, 3000);</script>"

HTML=$(<"$template")
HTML="${HTML/\[1\]/$title}"
HTML="${HTML/\[2\]/$script}"
HTML="${HTML/\[3\]/$body}"
HTML="${HTML/\[4\]/}"
HTML="${HTML/\[5\]/}"

echo "$HTML" > "$page"

else

Expand Down
Loading

0 comments on commit 2c7cea0

Please sign in to comment.