Commit 50a89476 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

Initial commit

Initial commit
parents dc145230 8e99a0b3
Loading
Loading
Loading
Loading
+1 −40
Original line number Diff line number Diff line
FROM golang:1.20 AS builder

COPY serial/ /src/serial/
WORKDIR /src/serial

RUN go get -d -v golang.org/x/net/html
RUN go get -d -v github.com/gorilla/mux
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /src/serial/main .

FROM debian:bookworm-20230320-slim

RUN apt-get update && apt-get -y upgrade && \
    apt-get --no-install-recommends -y install \
	jq \
	curl \
	cpio \
	wget \
	unzip \
	procps \
	dnsmasq \
	iptables \
	iproute2 \
	xz-utils \
	btrfs-progs \
	bridge-utils \
	netcat-openbsd \
	ca-certificates \
@@ -31,43 +15,20 @@ RUN apt-get update && apt-get -y upgrade && \
COPY run.sh /run/
COPY disk.sh /run/
COPY power.sh /run/
COPY serial.sh /run/
COPY server.sh /run/
COPY install.sh /run/
COPY network.sh /run/

COPY agent/agent.sh /agent/
COPY agent/service.sh /agent/

COPY --from=builder /src/serial/main /run/serial.bin

RUN ["chmod", "+x", "/run/run.sh"]
RUN ["chmod", "+x", "/run/disk.sh"]
RUN ["chmod", "+x", "/run/power.sh"]
RUN ["chmod", "+x", "/run/serial.sh"]
RUN ["chmod", "+x", "/run/server.sh"]
RUN ["chmod", "+x", "/run/install.sh"]
RUN ["chmod", "+x", "/run/network.sh"]
RUN ["chmod", "+x", "/run/serial.bin"]

COPY disks/template.img.xz /data/

VOLUME /storage

EXPOSE 22
EXPOSE 80
EXPOSE 139 
EXPOSE 443 
EXPOSE 445
EXPOSE 5000
EXPOSE 5001

ENV CPU_CORES 1
ENV DISK_SIZE 16G
ENV RAM_SIZE 512M

#ENV URL https://global.synologydownload.com/download/DSM/beta/7.2/64216/DSM_VirtualDSM_64216.pat
#ENV URL https://global.synologydownload.com/download/DSM/release/7.0.1/42218/DSM_VirtualDSM_42218.pat
ENV URL https://global.synologydownload.com/download/DSM/release/7.1.1/42962-1/DSM_VirtualDSM_42962.pat
ENV BOOT https://ftp.halifax.rwth-aachen.de/osdn/clonezilla/78259/clonezilla-live-3.0.3-22-amd64.iso

ENTRYPOINT ["/run/run.sh"]

agent/agent.sh

deleted100644 → 0
+0 −65
Original line number Diff line number Diff line
#!/usr/bin/env bash
set -u

declare nmi

function checkNMI {

  nmi=$(cat /proc/interrupts | grep NMI)
  nmi=$(echo "$nmi" | sed 's/[^0-9]*//g')
  nmi=$(echo "$nmi" | sed 's/^0*//')

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

    echo "Received shutdown request through NMI.." > /dev/ttyS0

    /usr/syno/sbin/synoshutdown -s > /dev/null
    exit 0

  fi

}

chmod 666 /dev/ttyS0
checkNMI

first_run=false

for filename in /usr/local/packages/*.spk; do
  if [ -f "$filename" ]; then
    first_run=true
  fi
done

if [ "$first_run" = true ]; then
  for filename in /usr/local/packages/*.spk; do
    if [ -f "$filename" ]; then

      /usr/syno/bin/synopkg install "$filename" > /dev/null

      BASE=$(basename "$filename" .spk)
      BASE="${BASE%%-*}"

      /usr/syno/bin/synopkg start "$BASE" > /dev/null

      rm "$filename"

    fi
  done
else

  sleep 5

fi

echo "-------------------------------------------" > /dev/ttyS0
echo " You can now login to DSM at port 5000     " > /dev/ttyS0
echo "-------------------------------------------" > /dev/ttyS0

while true; do

  checkNMI
  sleep 1

done

agent/service.sh

deleted100644 → 0
+0 −49
Original line number Diff line number Diff line
#!/bin/bash

PIDFILE="/var/run/agent.pid"
LOGFILE="/var/log/agent.log"
SCRIPT="/usr/local/bin/agent.sh" 

status() {
  if [ -f "$PIDFILE" ]; then
    echo 'Service running' >&2
    return 1
  fi
}

start() {
  if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")"; then
    echo 'Service already running' >&2
    return 1
  fi
  printf 'Starting agent service...' >&2
  "$SCRIPT" &> "$LOGFILE" & echo $! > "$PIDFILE"
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 "$(cat "$PIDFILE")"; then
    echo 'Service not running' >&2
    return 1
  fi
  echo 'Stopping agent service' >&2
  kill -15 "$(cat "$PIDFILE")" && rm -f "$PIDFILE"
  echo 'Service stopped' >&2
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
esac
+3 −3
Original line number Diff line number Diff line
#!/usr/bin/env bash
set -e

docker build --tag dsm .
docker images dsm:latest --format "{{.Repository}}:{{.Tag}} -> {{.Size}}"
docker run --rm -it --name dsm --device="/dev/kvm" --cap-add NET_ADMIN -p 80:5000 -p 443:5001 -p 5000:5000 -p 5001:5001 docker.io/library/dsm
docker build --tag qemu .
docker images qemu:latest --format "{{.Repository}}:{{.Tag}} -> {{.Size}}"
docker run --rm -it --name qemu --device="/dev/kvm" --cap-add NET_ADMIN -p 80:80 docker.io/library/qemu
+11 −40
Original line number Diff line number Diff line
@@ -2,50 +2,21 @@
set -eu

IMG="/storage"
BASE=$(basename "$URL" .pat)

FILE="$IMG/$BASE.boot.img"
[ ! -f "$FILE" ] && echo "ERROR: Virtual DSM boot-image does not exist ($FILE)" && exit 81

FILE="$IMG/$BASE.system.img"
[ ! -f "$FILE" ] && echo "ERROR: Virtual DSM system-image does not exist ($FILE)" && exit 82
FILE="$IMG/boot.img"
[ ! -f "$FILE" ] && echo "ERROR: Boot image does not exist ($FILE)" && exit 81

DISK_SIZE=$(echo "${DISK_SIZE}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
NEW_SIZE=$(numfmt --from=iec "${DISK_SIZE}")

FILE="$IMG/data$DISK_SIZE.img"

if [ ! -f "$FILE" ]; then
    truncate -s "${NEW_SIZE}" "${FILE}"
    mkfs.btrfs -q -L data -d single -m single "${FILE}" > /dev/null
    #qemu-img convert -f raw -O qcow2 -o extended_l2=on,cluster_size=128k,compression_type=zstd,preallocation=metadata "$TMP" "$FILE"
fi

[ ! -f "$FILE" ] && echo "ERROR: Virtual DSM data-image does not exist ($FILE)" && exit 83

#OLD_SIZE=$(stat -c%s "${FILE}")
#
#if [ "$NEW_SIZE" -ne "$OLD_SIZE" ]; then
#  echo "Resizing data disk from $OLD_SIZE to $NEW_SIZE bytes"
#
#  if [ "$NEW_SIZE" -gt "$OLD_SIZE" ]; then
#    truncate -s "${NEW_SIZE}" "${FILE}"
#    btrfs filesystem resize "${NEW_SIZE}" "${FILE}"
#  fi
#
#  if [ "$NEW_SIZE" -lt "$OLD_SIZE" ]; then
#    btrfs filesystem resize "${NEW_SIZE}" "${FILE}"
#    truncate -s "${NEW_SIZE}" "${FILE}"
#  fi
#fi
FILE="$IMG/data${DISK_SIZE}.img"
[ ! -f "$FILE" ] && truncate -s "${NEW_SIZE}" "${FILE}"
[ ! -f "$FILE" ] && echo "ERROR: Data image does not exist ($FILE)" && exit 83

KVM_DISK_OPTS="\
    -device virtio-scsi-pci,id=hw-synoboot,bus=pcie.0,addr=0xa \
    -drive file=${IMG}/${BASE}.boot.img,if=none,id=drive-synoboot,format=raw,cache=none,aio=native,discard=on,detect-zeroes=on \
    -device scsi-hd,bus=hw-synoboot.0,channel=0,scsi-id=0,lun=0,drive=drive-synoboot,id=synoboot0,rotation_rate=1,bootindex=1 \
    -device virtio-scsi-pci,id=hw-synosys,bus=pcie.0,addr=0xb \
    -drive file=${IMG}/${BASE}.system.img,if=none,id=drive-synosys,format=raw,cache=none,aio=native,discard=on,detect-zeroes=on \
    -device scsi-hd,bus=hw-synosys.0,channel=0,scsi-id=0,lun=0,drive=drive-synosys,id=synosys0,rotation_rate=1,bootindex=2 \
    -device virtio-scsi-pci,id=hw-userdata,bus=pcie.0,addr=0xc \
    -device virtio-scsi-pci,id=hw-boot,bus=pcie.0,addr=0xa \
    -drive file=${IMG}/boot.img,if=none,id=drive-boot,format=raw,cache=none,aio=native,discard=on,detect-zeroes=on \
    -device scsi-hd,bus=hw-boot.0,channel=0,scsi-id=0,lun=0,drive=drive-boot,id=boot0,rotation_rate=1,bootindex=1 \
    -device virtio-scsi-pci,id=hw-userdata,bus=pcie.0,addr=0xb \
    -drive file=${IMG}/data${DISK_SIZE}.img,if=none,id=drive-userdata,format=raw,cache=none,aio=native,discard=on,detect-zeroes=on \
    -device scsi-hd,bus=hw-userdata.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata,id=userdata0,rotation_rate=1,bootindex=3"
    -device scsi-hd,bus=hw-userdata.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata,id=userdata0,rotation_rate=1,bootindex=2"
Loading