diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..848bfbc --- /dev/null +++ b/build.sh @@ -0,0 +1,3 @@ +docker build --tag dsm . +docker run --rm -it --name dsm --device=/dev/kvm:/dev/kvm --device=/dev/fuse:/dev/fuse --device=/dev/net/tun:/dev/net/tun --cap-add NET_ADMIN --cap-add SYS_ADMIN -p 80:5000 -p 443:5001 -p 5000:5000 -p 5001:5001 -v ${PWD}/img:/storage docker.io/library/dsm + diff --git a/install.sh b/install.sh index ab0c2ae..1fb1096 100644 --- a/install.sh +++ b/install.sh @@ -1,29 +1,22 @@ #!/usr/bin/env bash set -eu - -if [ ! -f "/run/server.sh" ]; then - echo "Script must run inside Docker container!" - exit 1 -fi - IMG="/storage" +[ ! -f "/run/server.sh" ] && echo "Script must run inside Docker container!" && exit 1 [ ! -f "$IMG/boot.img" ] && rm -f $IMG/system.img if [ ! -f "$IMG/system.img" ]; then - echo "Downloading Synology DSM from $URL..." + echo "Downloading $URL..." TMP="$IMG/tmp" FILE="$TMP/dsm.pat" - rm -rf $TMP - mkdir -p $TMP - + rm -rf $TMP && mkdir -p $TMP wget $URL -O $FILE -q --show-progress - echo "Extracting DSM boot image..." + echo "Extracting boot image..." if { tar tf "$FILE"; } >/dev/null 2>&1; then tar xpf $FILE -C $TMP/. @@ -41,14 +34,14 @@ if [ ! -f "$IMG/system.img" ]; then unzip -q $BOOT.zip -d $TMP rm $BOOT.zip - echo "Extracting DSM system image..." + echo "Extracting system image..." HDA="$TMP/hda1" mv $HDA.tgz $HDA.xz unxz $HDA.xz mv $HDA $HDA.tar - echo "Extracting DSM disk template..." + echo "Extracting data image..." SYSTEM="$TMP/temp.img" PLATE="/data/template.img" @@ -67,7 +60,7 @@ if [ ! -f "$IMG/system.img" ]; then echo -n "Installing system partition.." - tar xpf $HDA.tar --absolute-names --checkpoint=.5000 -C $MOUNT/ + tar xpf $HDA.tar --absolute-names --checkpoint=.6000 -C $MOUNT/ echo "" echo "Unmounting disk template..." @@ -83,16 +76,10 @@ if [ ! -f "$IMG/system.img" ]; then fi FILE="$IMG/boot.img" -if [ ! -f "$FILE" ]; then - echo "ERROR: Synology DSM boot-image does not exist ($FILE)" - exit 2 -fi +[ ! -f "$FILE" ] && echo "ERROR: Synology DSM boot-image does not exist ($FILE)" && exit 2 FILE="$IMG/system.img" -if [ ! -f "$FILE" ]; then - echo "ERROR: Synology DSM system-image does not exist ($FILE)" - exit 2 -fi +[ ! -f "$FILE" ] && echo "ERROR: Synology DSM system-image does not exist ($FILE)" && exit 2 FILE="$IMG/data.img" if [ ! -f "$FILE" ]; then @@ -100,9 +87,6 @@ if [ ! -f "$FILE" ]; then mkfs.ext4 -q $FILE fi -if [ ! -f "$FILE" ]; then - echo "ERROR: Synology DSM data-image does not exist ($FILE)" - exit 2 -fi +[ ! -f "$FILE" ] && echo "ERROR: Synology DSM data-image does not exist ($FILE)" && exit 2 exit 0 diff --git a/run.sh b/run.sh index 080037e..e0c9a2d 100755 --- a/run.sh +++ b/run.sh @@ -2,15 +2,14 @@ set -eu -/run/server.sh 5000 "

Please wait while Synology is installing...

" > /dev/null & +/run/server.sh 5000 > /dev/null & -if [ ! /run/install.sh ]; then - echo "Installation failed!" - exit 2 +if /run/install.sh; then + echo "Starting DSM for Docker..." +else + echo "Installation failed (code $?)" && exit 2 fi -echo "Booting Synology DSM for Docker..." - # A bridge of this name will be created to host the TAP interface created for # the VM QEMU_BRIDGE='qemubr0' @@ -75,8 +74,16 @@ GUEST_SERIAL=$(/run/serial.sh) # Stop the webserver pkill -f server.sh +[ ! -e /dev/fuse ] && echo "Error: FUSE interface not available..." && exit 2 +[ ! -e /dev/net/tun ] && echo "Error: TUN interface not available..." && exit 2 + +if [ -e /dev/kvm ] && sh -c 'echo -n > /dev/kvm' &> /dev/null; then + echo "Booting DSM image..." +else + echo "Error: KVM not available..." && exit 2 +fi + IMG="/storage" -echo "Booting OS..." # Configure QEMU for graceful shutdown diff --git a/serial.sh b/serial.sh index 1802efd..b275c44 100644 --- a/serial.sh +++ b/serial.sh @@ -1,7 +1,7 @@ #!/bin/bash -permanent="PSN" -serialstart="1960" +permanent="DSM" +serialstart="2000" serialnum="$(echo "$serialstart" | tr ' ' '\n' | sort -R | tail -1)$permanent"$(printf "%06d" $(($RANDOM % 30000 + 1))) echo $serialnum diff --git a/server.sh b/server.sh index 742d22d..9378eac 100644 --- a/server.sh +++ b/server.sh @@ -2,7 +2,8 @@ set -eu -RESPONSE="HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n${2:-"OK"}\r\n" +HTML="

Please wait while Synology is installing...

" +RESPONSE="HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n$HTML\r\n" while { echo -en "$RESPONSE"; } | nc -lN "${1:-8080}"; do echo "================================================"