mirror of
https://github.com/vdsm/virtual-dsm.git
synced 2025-11-06 10:04:52 +08:00
Compare commits
45 Commits
fd5ec52226
...
v7.48
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b145924b9 | ||
|
|
a0328e1e9c | ||
|
|
b7f5214a7b | ||
|
|
b0e4c4ac5f | ||
|
|
bbb67aac93 | ||
|
|
433c83b393 | ||
|
|
5577178eeb | ||
|
|
221b0242fa | ||
|
|
c05623f8af | ||
|
|
eb9884cc96 | ||
|
|
8e2490e6bc | ||
|
|
399243886e | ||
|
|
b0dbfcb805 | ||
|
|
dec2dc9230 | ||
|
|
2c44669d91 | ||
|
|
175d90aad7 | ||
|
|
e1e5f8f91d | ||
|
|
dff8abbe1e | ||
|
|
0f97091e61 | ||
|
|
ea0d7ee6cd | ||
|
|
75daa31d36 | ||
|
|
8c44319c45 | ||
|
|
dac574d933 | ||
|
|
fd4c5001e8 | ||
|
|
dfe0b23995 | ||
|
|
6158372eaa | ||
|
|
b1a778d7b2 | ||
|
|
bc0defd813 | ||
|
|
48e7a9fff0 | ||
|
|
c2fa58ef27 | ||
|
|
ea49cb144b | ||
|
|
b8e778a79d | ||
|
|
c70e12f0a2 | ||
|
|
6281205912 | ||
|
|
1ffc5c55b2 | ||
|
|
c3302e1720 | ||
|
|
25227944b5 | ||
|
|
06650e916a | ||
|
|
2e34dffed5 | ||
|
|
3db91f077f | ||
|
|
fae14f4dd9 | ||
|
|
d190b3ba87 | ||
|
|
554f3295cb | ||
|
|
cdc4689d6a | ||
|
|
21d18fd439 |
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "dsm",
|
||||
"service": "dsm",
|
||||
"forwardPorts": [5000],
|
||||
"dockerComposeFile": "compose.yml"
|
||||
}
|
||||
19
.devcontainer/codespaces.yml
Normal file
19
.devcontainer/codespaces.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
services:
|
||||
dsm:
|
||||
container_name: dsm
|
||||
image: ghcr.io/vdsm/virtual-dsm
|
||||
environment:
|
||||
RAM_SIZE: "half"
|
||||
DISK_SIZE: "max"
|
||||
CPU_CORES: "max"
|
||||
devices:
|
||||
- /dev/kvm
|
||||
- /dev/net/tun
|
||||
cap_add:
|
||||
- NET_ADMIN
|
||||
ports:
|
||||
- 5000:5000
|
||||
volumes:
|
||||
- ./dsm:/storage
|
||||
restart: on-failure
|
||||
stop_grace_period: 2m
|
||||
17
.devcontainer/devcontainer.json
Normal file
17
.devcontainer/devcontainer.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "Virtual DSM",
|
||||
"service": "vdsm",
|
||||
"forwardPorts": [5000],
|
||||
"portsAttributes": {
|
||||
"5000": {
|
||||
"label": "Web",
|
||||
"onAutoForward": "notify"
|
||||
}
|
||||
},
|
||||
"otherPortsAttributes": {
|
||||
"onAutoForward": "ignore"
|
||||
},
|
||||
"dockerComposeFile": "codespaces.yml",
|
||||
"workspaceFolder": "/workspaces/vdsm",
|
||||
"initializeCommand": "docker system prune --all --force"
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
.dockerignore
|
||||
.devcontainer
|
||||
.git
|
||||
.github
|
||||
.gitignore
|
||||
|
||||
5
.github/workflows/check.yml
vendored
5
.github/workflows/check.yml
vendored
@@ -22,3 +22,8 @@ jobs:
|
||||
dockerfile: Dockerfile
|
||||
ignore: DL3008,DL3003,DL3006,DL3013
|
||||
failure-threshold: warning
|
||||
-
|
||||
name: Validate JSON and YML files
|
||||
uses: GrantBirki/json-yaml-validate@v4
|
||||
with:
|
||||
yaml_exclude_regex: ".*\\kubernetes\\.yml$"
|
||||
|
||||
66
.github/workflows/review.yml
vendored
Normal file
66
.github/workflows/review.yml
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
on:
|
||||
pull_request:
|
||||
|
||||
name: "Review"
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: write
|
||||
checks: write
|
||||
|
||||
jobs:
|
||||
review:
|
||||
name: review
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
-
|
||||
name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
-
|
||||
name: Spelling
|
||||
uses: reviewdog/action-misspell@v1
|
||||
with:
|
||||
locale: "US"
|
||||
level: warning
|
||||
pattern: |
|
||||
*.md
|
||||
*.sh
|
||||
reporter: github-pr-review
|
||||
github_token: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||
-
|
||||
name: Hadolint
|
||||
uses: reviewdog/action-hadolint@v1
|
||||
with:
|
||||
level: warning
|
||||
reporter: github-pr-review
|
||||
hadolint_ignore: DL3008 DL3003 DL3006 DL3013
|
||||
github_token: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||
-
|
||||
name: YamlLint
|
||||
uses: reviewdog/action-yamllint@v1
|
||||
with:
|
||||
level: warning
|
||||
reporter: github-pr-review
|
||||
github_token: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||
-
|
||||
name: ActionLint
|
||||
uses: reviewdog/action-actionlint@v1
|
||||
with:
|
||||
level: warning
|
||||
reporter: github-pr-review
|
||||
github_token: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||
-
|
||||
name: Shellformat
|
||||
uses: reviewdog/action-shfmt@v1
|
||||
with:
|
||||
level: warning
|
||||
shfmt_flags: "-i 2 -ci -bn"
|
||||
github_token: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||
-
|
||||
name: Shellcheck
|
||||
uses: reviewdog/action-shellcheck@v1
|
||||
with:
|
||||
level: warning
|
||||
reporter: github-pr-review
|
||||
shellcheck_flags: -x -e SC2001 -e SC2034 -e SC2064 -e SC2317 -e SC2153 -e SC2028
|
||||
github_token: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||
5
.github/workflows/test.yml
vendored
5
.github/workflows/test.yml
vendored
@@ -1,11 +1,6 @@
|
||||
on:
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
paths:
|
||||
- '**/*.sh'
|
||||
- 'Dockerfile'
|
||||
- '.github/workflows/test.yml'
|
||||
- '.github/workflows/check.yml'
|
||||
|
||||
name: "Test"
|
||||
permissions: {}
|
||||
|
||||
@@ -37,13 +37,15 @@ RUN set -eu && \
|
||||
xz-utils \
|
||||
iptables \
|
||||
iproute2 \
|
||||
apt-utils \
|
||||
dnsmasq \
|
||||
fakeroot \
|
||||
apt-utils \
|
||||
net-tools \
|
||||
e2fsprogs \
|
||||
qemu-utils \
|
||||
websocketd \
|
||||
iputils-ping \
|
||||
inotify-tools \
|
||||
ca-certificates \
|
||||
netcat-openbsd \
|
||||
qemu-system-x86 && \
|
||||
|
||||
@@ -47,7 +47,7 @@ services:
|
||||
##### Via Docker CLI:
|
||||
|
||||
```bash
|
||||
docker run -it --rm --name dsm -e "DISK_SIZE=256G" -p 5000:5000 --device=/dev/kvm --device=/dev/net/tun --cap-add NET_ADMIN -v "${PWD:-.}/dsm:/storage" --stop-timeout 120 vdsm/virtual-dsm
|
||||
docker run -it --rm --name dsm -e "DISK_SIZE=256G" -p 5000:5000 --device=/dev/kvm --device=/dev/net/tun --cap-add NET_ADMIN -v "${PWD:-.}/dsm:/storage" --stop-timeout 120 docker.io/vdsm/virtual-dsm
|
||||
```
|
||||
|
||||
##### Via Kubernetes:
|
||||
@@ -124,7 +124,7 @@ kubectl apply -f https://raw.githubusercontent.com/vdsm/virtual-dsm/refs/heads/m
|
||||
|
||||
### How do I change the amount of CPU or RAM?
|
||||
|
||||
By default, the container will be allowed to use a maximum of 2 CPU cores and 2 GB of RAM.
|
||||
By default, Virtual DSM will be allowed to use 2 CPU cores and 2 GB of RAM.
|
||||
|
||||
If you want to adjust this, you can specify the desired amount using the following environment variables:
|
||||
|
||||
|
||||
12
src/check.sh
12
src/check.sh
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
: "${DHCP:="N"}"
|
||||
: "${NETWORK:="Y"}"
|
||||
|
||||
[ -f "/run/shm/qemu.end" ] && echo "QEMU is shutting down.." && exit 1
|
||||
@@ -9,6 +10,7 @@ set -Eeuo pipefail
|
||||
|
||||
file="/run/shm/dsm.url"
|
||||
address="/run/shm/qemu.ip"
|
||||
gateway="/run/shm/qemu.gw"
|
||||
|
||||
[ ! -s "$file" ] && echo "DSM has not enabled networking yet.." && exit 1
|
||||
|
||||
@@ -16,13 +18,13 @@ location=$(<"$file")
|
||||
|
||||
if ! curl -m 20 -ILfSs "http://$location/" > /dev/null; then
|
||||
|
||||
if [[ "$location" == "20.20"* ]]; then
|
||||
ip="20.20.20.1"
|
||||
if [[ "$DHCP" == [Yy1]* ]]; then
|
||||
ip=$(<"$address")
|
||||
echo "Failed to reach DSM at http://$location"
|
||||
else
|
||||
ip=$(<"$gateway")
|
||||
port="${location##*:}"
|
||||
echo "Failed to reach DSM at port $port"
|
||||
else
|
||||
echo "Failed to reach DSM at http://$location"
|
||||
ip=$(<"$address")
|
||||
fi
|
||||
|
||||
echo "You might need to whitelist IP $ip in the DSM firewall." && exit 1
|
||||
|
||||
@@ -12,32 +12,4 @@ DEV_OPTS+=" -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0,addr=0x1c"
|
||||
ARGS="$DEF_OPTS $CPU_OPTS $RAM_OPTS $MAC_OPTS $DISPLAY_OPTS $MON_OPTS $SERIAL_OPTS $NET_OPTS $DISK_OPTS $DEV_OPTS $ARGUMENTS"
|
||||
ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ')
|
||||
|
||||
# Check available memory as the very last step
|
||||
|
||||
if [[ "$RAM_CHECK" != [Nn]* ]]; then
|
||||
|
||||
RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}')
|
||||
AVAIL_MEM=$(formatBytes "$RAM_AVAIL")
|
||||
|
||||
if (( (RAM_WANTED + RAM_SPARE) > RAM_AVAIL )); then
|
||||
msg="Your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is too high for the $AVAIL_MEM of memory available, please set a lower value."
|
||||
[[ "${FS,,}" != "zfs" ]] && error "$msg" && exit 17
|
||||
info "$msg"
|
||||
else
|
||||
if (( (RAM_WANTED + (RAM_SPARE * 3)) > RAM_AVAIL )); then
|
||||
msg="your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is very close to the $AVAIL_MEM of memory available, please consider a lower value."
|
||||
if [[ "${FS,,}" != "zfs" ]]; then
|
||||
warn "$msg"
|
||||
else
|
||||
info "$msg"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
if [[ "$DEBUG" == [Yy1]* ]]; then
|
||||
printf "QEMU arguments:\n\n%s\n\n" "${ARGS// -/$'\n-'}"
|
||||
fi
|
||||
|
||||
return 0
|
||||
|
||||
193
src/disk.sh
193
src/disk.sh
@@ -17,8 +17,16 @@ SYSTEM="$STORAGE/$BASE.system.img"
|
||||
[ ! -s "$BOOT" ] && error "Virtual DSM boot-image does not exist ($BOOT)" && exit 81
|
||||
[ ! -s "$SYSTEM" ] && error "Virtual DSM system-image does not exist ($SYSTEM)" && exit 82
|
||||
|
||||
if ! setOwner "$BOOT"; then
|
||||
error "Failed to set the owner for \"$BOOT\" !"
|
||||
fi
|
||||
|
||||
if ! setOwner "$SYSTEM"; then
|
||||
error "Failed to set the owner for \"$SYSTEM\" !"
|
||||
fi
|
||||
|
||||
fmt2ext() {
|
||||
local DISK_FMT=$1
|
||||
local DISK_FMT="$1"
|
||||
|
||||
case "${DISK_FMT,,}" in
|
||||
qcow2)
|
||||
@@ -34,7 +42,7 @@ fmt2ext() {
|
||||
}
|
||||
|
||||
ext2fmt() {
|
||||
local DISK_EXT=$1
|
||||
local DISK_EXT="$1"
|
||||
|
||||
case "${DISK_EXT,,}" in
|
||||
qcow2)
|
||||
@@ -50,7 +58,7 @@ ext2fmt() {
|
||||
}
|
||||
|
||||
getSize() {
|
||||
local DISK_FILE=$1
|
||||
local DISK_FILE="$1"
|
||||
local DISK_EXT DISK_FMT
|
||||
|
||||
DISK_EXT=$(echo "${DISK_FILE//*./}" | sed 's/^.*\.//')
|
||||
@@ -70,7 +78,7 @@ getSize() {
|
||||
}
|
||||
|
||||
isCow() {
|
||||
local FS=$1
|
||||
local FS="$1"
|
||||
|
||||
if [[ "${FS,,}" == "btrfs" ]]; then
|
||||
return 0
|
||||
@@ -80,7 +88,7 @@ isCow() {
|
||||
}
|
||||
|
||||
supportsDirect() {
|
||||
local FS=$1
|
||||
local FS="$1"
|
||||
|
||||
if [[ "${FS,,}" == "ecryptfs" || "${FS,,}" == "tmpfs" ]]; then
|
||||
return 1
|
||||
@@ -91,17 +99,17 @@ supportsDirect() {
|
||||
|
||||
createDisk() {
|
||||
|
||||
local DISK_FILE=$1
|
||||
local DISK_SPACE=$2
|
||||
local DISK_DESC=$3
|
||||
local DISK_FMT=$4
|
||||
local FS=$5
|
||||
local DISK_FILE="$1"
|
||||
local DISK_SPACE="$2"
|
||||
local DISK_DESC="$3"
|
||||
local DISK_FMT="$4"
|
||||
local FS="$5"
|
||||
local DATA_SIZE DIR SPACE GB FA
|
||||
|
||||
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
||||
|
||||
rm -f "$DISK_FILE"
|
||||
|
||||
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
||||
|
||||
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
||||
|
||||
# Check free diskspace
|
||||
@@ -113,6 +121,7 @@ createDisk() {
|
||||
error "Not enough free space to create a $DISK_DESC of ${DISK_SPACE/G/ GB} in $DIR, it has only $GB available..."
|
||||
error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 76
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
html "Creating a $DISK_DESC image..."
|
||||
@@ -177,16 +186,16 @@ createDisk() {
|
||||
|
||||
resizeDisk() {
|
||||
|
||||
local DISK_FILE=$1
|
||||
local DISK_SPACE=$2
|
||||
local DISK_DESC=$3
|
||||
local DISK_FMT=$4
|
||||
local FS=$5
|
||||
local DISK_FILE="$1"
|
||||
local DISK_SPACE="$2"
|
||||
local DISK_DESC="$3"
|
||||
local DISK_FMT="$4"
|
||||
local FS="$5"
|
||||
local CUR_SIZE DATA_SIZE DIR SPACE GB
|
||||
|
||||
CUR_SIZE=$(getSize "$DISK_FILE")
|
||||
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
||||
local REQ=$((DATA_SIZE-CUR_SIZE))
|
||||
local REQ=$(( DATA_SIZE - CUR_SIZE ))
|
||||
(( REQ < 1 )) && error "Shrinking disks is not supported yet, please increase ${DISK_DESC^^}_SIZE." && exit 71
|
||||
|
||||
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
||||
@@ -200,6 +209,7 @@ resizeDisk() {
|
||||
error "Not enough free space to resize $DISK_DESC to ${DISK_SPACE/G/ GB} in $DIR, it has only $GB available.."
|
||||
error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 74
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
GB=$(formatBytes "$CUR_SIZE")
|
||||
@@ -245,13 +255,13 @@ resizeDisk() {
|
||||
|
||||
convertDisk() {
|
||||
|
||||
local SOURCE_FILE=$1
|
||||
local SOURCE_FMT=$2
|
||||
local DST_FILE=$3
|
||||
local DST_FMT=$4
|
||||
local DISK_BASE=$5
|
||||
local DISK_DESC=$6
|
||||
local FS=$7
|
||||
local SOURCE_FILE="$1"
|
||||
local SOURCE_FMT="$2"
|
||||
local DST_FILE="$3"
|
||||
local DST_FMT="$4"
|
||||
local DISK_BASE="$5"
|
||||
local DISK_DESC="$6"
|
||||
local FS="$7"
|
||||
|
||||
[ -f "$DST_FILE" ] && error "Conversion failed, destination file $DST_FILE already exists?" && exit 79
|
||||
[ ! -f "$SOURCE_FILE" ] && error "Conversion failed, source file $SOURCE_FILE does not exists?" && exit 79
|
||||
@@ -273,6 +283,7 @@ convertDisk() {
|
||||
error "Not enough free space to convert $DISK_DESC to $DST_FMT in $DIR, it has only $GB available..."
|
||||
error "Please free up some disk space or disable preallocation by setting ALLOCATE=N." && exit 76
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
local msg="Converting $DISK_DESC to $DST_FMT"
|
||||
@@ -327,31 +338,31 @@ convertDisk() {
|
||||
|
||||
checkFS () {
|
||||
|
||||
local FS=$1
|
||||
local DISK_FILE=$2
|
||||
local DISK_DESC=$3
|
||||
local FS="$1"
|
||||
local DISK_FILE="$2"
|
||||
local DISK_DESC="$3"
|
||||
local DIR FA
|
||||
|
||||
DIR=$(dirname "$DISK_FILE")
|
||||
[ ! -d "$DIR" ] && return 0
|
||||
|
||||
if [[ "${FS,,}" == "overlay"* ]]; then
|
||||
info "Warning: the filesystem of $DIR is OverlayFS, this usually means it was binded to an invalid path!"
|
||||
if [[ "${FS,,}" == "overlay"* && "$PODMAN" != [Yy1]* ]]; then
|
||||
warn "the filesystem of $DIR is OverlayFS, this usually means it was binded to an invalid path!"
|
||||
fi
|
||||
|
||||
if [[ "${FS,,}" == "fuse"* ]]; then
|
||||
info "Warning: the filesystem of $DIR is FUSE, this extra layer will negatively affect performance!"
|
||||
warn "the filesystem of $DIR is FUSE, this extra layer will negatively affect performance!"
|
||||
fi
|
||||
|
||||
if ! supportsDirect "$FS"; then
|
||||
info "Warning: the filesystem of $DIR is $FS, which does not support O_DIRECT mode, adjusting settings..."
|
||||
warn "the filesystem of $DIR is $FS, which does not support O_DIRECT mode, adjusting settings..."
|
||||
fi
|
||||
|
||||
if isCow "$FS"; then
|
||||
if [ -f "$DISK_FILE" ]; then
|
||||
FA=$(lsattr "$DISK_FILE")
|
||||
if [[ "$FA" != *"C"* ]]; then
|
||||
info "Warning: COW (copy on write) is not disabled for $DISK_DESC image file $DISK_FILE, this is recommended on ${FS^^} filesystems!"
|
||||
warn "COW (copy on write) is not disabled for $DISK_DESC image file $DISK_FILE, this is recommended on ${FS^^} filesystems!"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
@@ -361,15 +372,15 @@ checkFS () {
|
||||
|
||||
createDevice () {
|
||||
|
||||
local DISK_FILE=$1
|
||||
local DISK_TYPE=$2
|
||||
local DISK_INDEX=$3
|
||||
local DISK_ADDRESS=$4
|
||||
local DISK_FMT=$5
|
||||
local DISK_IO=$6
|
||||
local DISK_CACHE=$7
|
||||
local DISK_SERIAL=$8
|
||||
local DISK_SECTORS=$9
|
||||
local DISK_FILE="$1"
|
||||
local DISK_TYPE="$2"
|
||||
local DISK_INDEX="$3"
|
||||
local DISK_ADDRESS="$4"
|
||||
local DISK_FMT="$5"
|
||||
local DISK_IO="$6"
|
||||
local DISK_CACHE="$7"
|
||||
local DISK_SERIAL="$8"
|
||||
local DISK_SECTORS="$9"
|
||||
local DISK_ID="data$DISK_INDEX"
|
||||
|
||||
local index=""
|
||||
@@ -415,16 +426,16 @@ createDevice () {
|
||||
|
||||
addDisk () {
|
||||
|
||||
local DISK_BASE=$1
|
||||
local DISK_TYPE=$2
|
||||
local DISK_DESC=$3
|
||||
local DISK_SPACE=$4
|
||||
local DISK_INDEX=$5
|
||||
local DISK_ADDRESS=$6
|
||||
local DISK_FMT=$7
|
||||
local DISK_IO=$8
|
||||
local DISK_CACHE=$9
|
||||
local DISK_EXT DIR SPACE DATA_SIZE FS PREV_FMT PREV_EXT CUR_SIZE
|
||||
local DISK_BASE="$1"
|
||||
local DISK_TYPE="$2"
|
||||
local DISK_DESC="$3"
|
||||
local DISK_SPACE="$4"
|
||||
local DISK_INDEX="$5"
|
||||
local DISK_ADDRESS="$6"
|
||||
local DISK_FMT="$7"
|
||||
local DISK_IO="$8"
|
||||
local DISK_CACHE="$9"
|
||||
local DISK_EXT DIR SPACE GB DATA_SIZE FS PREV_FMT PREV_EXT CUR_SIZE LEFT FREE USED
|
||||
|
||||
DISK_EXT=$(fmt2ext "$DISK_FMT")
|
||||
local DISK_FILE="$DISK_BASE.$DISK_EXT"
|
||||
@@ -432,8 +443,25 @@ addDisk () {
|
||||
DIR=$(dirname "$DISK_FILE")
|
||||
[ ! -d "$DIR" ] && return 0
|
||||
|
||||
if [[ "${DISK_SPACE,,}" == "max" || "${DISK_SPACE,,}" == "half" ]]; then
|
||||
|
||||
local SPARE=1073741824
|
||||
FREE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
||||
|
||||
if [[ "${DISK_SPACE,,}" == "max" ]]; then
|
||||
FREE=$(( FREE - SPARE ))
|
||||
else
|
||||
FREE=$(( FREE / 2 ))
|
||||
fi
|
||||
|
||||
(( FREE < SPARE )) && FREE="$SPARE"
|
||||
GB=$(( FREE / 1073741825 ))
|
||||
DISK_SPACE="${GB}G"
|
||||
|
||||
fi
|
||||
|
||||
SPACE="${DISK_SPACE// /}"
|
||||
[ -z "$SPACE" ] && SPACE="16G"
|
||||
[ -z "$SPACE" ] && SPACE="256G"
|
||||
[ -z "${SPACE//[0-9. ]}" ] && SPACE="${SPACE}G"
|
||||
SPACE=$(echo "${SPACE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
|
||||
|
||||
@@ -444,7 +472,7 @@ addDisk () {
|
||||
DATA_SIZE=$(numfmt --from=iec "$SPACE")
|
||||
|
||||
if (( DATA_SIZE < 6442450944 )); then
|
||||
error "Please increase ${DISK_DESC^^}_SIZE to at least 6 GB." && exit 73
|
||||
error "Please increase the ${DISK_DESC^^}_SIZE variable to at least 6 GB." && exit 73
|
||||
fi
|
||||
|
||||
FS=$(stat -f -c %T "$DIR")
|
||||
@@ -455,7 +483,7 @@ addDisk () {
|
||||
DISK_CACHE="writeback"
|
||||
fi
|
||||
|
||||
if ! [ -s "$DISK_FILE" ] ; then
|
||||
if [ ! -s "$DISK_FILE" ] ; then
|
||||
|
||||
if [[ "${DISK_FMT,,}" != "raw" ]]; then
|
||||
PREV_FMT="raw"
|
||||
@@ -468,6 +496,7 @@ addDisk () {
|
||||
if [ -s "$DISK_BASE.$PREV_EXT" ] ; then
|
||||
convertDisk "$DISK_BASE.$PREV_EXT" "$PREV_FMT" "$DISK_FILE" "$DISK_FMT" "$DISK_BASE" "$DISK_DESC" "$FS" || exit $?
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
if [ -s "$DISK_FILE" ]; then
|
||||
@@ -475,7 +504,18 @@ addDisk () {
|
||||
CUR_SIZE=$(getSize "$DISK_FILE")
|
||||
|
||||
if (( DATA_SIZE > CUR_SIZE )); then
|
||||
|
||||
resizeDisk "$DISK_FILE" "$SPACE" "$DISK_DESC" "$DISK_FMT" "$FS" || exit $?
|
||||
|
||||
else
|
||||
|
||||
if (( DATA_SIZE < CUR_SIZE )); then
|
||||
|
||||
if [[ "${DISK_SPACE,,}" != "max" && "${DISK_SPACE,,}" != "half" ]]; then
|
||||
info "You decreased the ${DISK_DESC^^}_SIZE variable to ${DISK_SPACE/G/ GB} but shrinking disks is not supported, will be ignored..."
|
||||
fi
|
||||
|
||||
fi
|
||||
fi
|
||||
|
||||
else
|
||||
@@ -484,6 +524,39 @@ addDisk () {
|
||||
|
||||
fi
|
||||
|
||||
if [ -f "$DISK_FILE" ] && [[ "$ALLOCATE" == [Nn]* ]]; then
|
||||
|
||||
CUR_SIZE=$(getSize "$DISK_FILE")
|
||||
USED=$(du -sB 1 "$DISK_FILE" | cut -f1)
|
||||
FREE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
||||
LEFT=$(( CUR_SIZE - USED - FREE ))
|
||||
|
||||
if (( LEFT > 0 )); then
|
||||
|
||||
GB=$(formatBytes "$FREE")
|
||||
LEFT=$(formatBytes "$LEFT")
|
||||
CUR_SIZE=$(formatBytes "$CUR_SIZE")
|
||||
msg="the virtual size of the ${DISK_DESC,,} is $CUR_SIZE"
|
||||
|
||||
if [[ "$USED" == "0" ]]; then
|
||||
msg+=","
|
||||
else
|
||||
USED=$(formatBytes "$USED")
|
||||
msg+=" (of which $USED is used),"
|
||||
fi
|
||||
|
||||
warn "$msg but there is only $GB of free space left in $DIR, make at least $LEFT more room available!"
|
||||
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
if [ -f "$DISK_FILE" ]; then
|
||||
if ! setOwner "$DISK_FILE"; then
|
||||
error "Failed to set the owner for \"$DISK_FILE\" !"
|
||||
fi
|
||||
fi
|
||||
|
||||
DISK_OPTS+=$(createDevice "$DISK_FILE" "$DISK_TYPE" "$DISK_INDEX" "$DISK_ADDRESS" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" "" "")
|
||||
|
||||
return 0
|
||||
@@ -491,10 +564,10 @@ addDisk () {
|
||||
|
||||
addDevice () {
|
||||
|
||||
local DISK_DEV=$1
|
||||
local DISK_TYPE=$2
|
||||
local DISK_INDEX=$3
|
||||
local DISK_ADDRESS=$4
|
||||
local DISK_DEV="$1"
|
||||
local DISK_TYPE="$2"
|
||||
local DISK_INDEX="$3"
|
||||
local DISK_ADDRESS="$4"
|
||||
|
||||
[ -z "$DISK_DEV" ] && return 0
|
||||
[ ! -b "$DISK_DEV" ] && error "Device $DISK_DEV cannot be found! Please add it to the 'devices' section of your compose file." && exit 55
|
||||
@@ -506,7 +579,7 @@ addDevice () {
|
||||
physical=$(echo "$result" | grep -m 1 -o "/ .*" | cut -c 3-)
|
||||
physical="${physical%% *}"
|
||||
|
||||
if [ -n "$physical" ]; then
|
||||
if [ -n "$physical" ]; then
|
||||
if [[ "$physical" != "512" ]]; then
|
||||
sectors=",logical_block_size=$logical,physical_block_size=$physical"
|
||||
fi
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
: "${PLATFORM:="x64"}"
|
||||
: "${APP:="Virtual DSM"}"
|
||||
: "${SUPPORT:="https://github.com/vdsm/virtual-dsm"}"
|
||||
|
||||
cd /run
|
||||
|
||||
. start.sh # Placeholder
|
||||
. start.sh # Startup hook
|
||||
. utils.sh # Load functions
|
||||
. reset.sh # Initialize system
|
||||
. server.sh # Start webserver
|
||||
. install.sh # Run installation
|
||||
. disk.sh # Initialize disks
|
||||
. display.sh # Initialize graphics
|
||||
@@ -16,7 +18,9 @@ cd /run
|
||||
. proc.sh # Initialize processor
|
||||
. serial.sh # Initialize serialport
|
||||
. power.sh # Configure shutdown
|
||||
. memory.sh # Check available memory
|
||||
. config.sh # Configure arguments
|
||||
. finish.sh # Finish initialization
|
||||
|
||||
trap - ERR
|
||||
|
||||
@@ -31,7 +35,7 @@ fi
|
||||
(( rc != 0 )) && error "$(<"$QEMU_LOG")" && exit 15
|
||||
|
||||
terminal
|
||||
tail -fn +0 "$QEMU_LOG" 2>/dev/null &
|
||||
tail -fn +0 "$QEMU_LOG" --pid=$$ 2>/dev/null &
|
||||
cat "$QEMU_TERM" 2>/dev/null & wait $! || :
|
||||
|
||||
sleep 1 & wait $!
|
||||
|
||||
8
src/finish.sh
Normal file
8
src/finish.sh
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
if [[ "$DEBUG" == [Yy1]* ]]; then
|
||||
printf "QEMU arguments:\n\n%s\n\n" "${ARGS// -/$'\n-'}"
|
||||
fi
|
||||
|
||||
return 0
|
||||
@@ -82,16 +82,16 @@ rm -f "$STORAGE/$BASE.system.img"
|
||||
# Check filesystem
|
||||
FS=$(stat -f -c %T "$STORAGE")
|
||||
|
||||
if [[ "${FS,,}" == "overlay"* ]]; then
|
||||
info "Warning: the filesystem of $STORAGE is OverlayFS, this usually means it was binded to an invalid path!"
|
||||
if [[ "${FS,,}" == "overlay"* && "$PODMAN" != [Yy1]* ]]; then
|
||||
warn "the filesystem of $STORAGE is OverlayFS, this usually means it was binded to an invalid path!"
|
||||
fi
|
||||
|
||||
if [[ "${FS,,}" == "fuse"* ]]; then
|
||||
info "Warning: the filesystem of $STORAGE is FUSE, this extra layer will negatively affect performance!"
|
||||
warn "the filesystem of $STORAGE is FUSE, this extra layer will negatively affect performance!"
|
||||
fi
|
||||
|
||||
if [[ "${FS,,}" == "ecryptfs" || "${FS,,}" == "tmpfs" ]]; then
|
||||
info "Warning: the filesystem of $STORAGE is $FS, which does not support O_DIRECT mode, adjusting settings..."
|
||||
warn "the filesystem of $STORAGE is $FS, which does not support O_DIRECT mode, adjusting settings..."
|
||||
fi
|
||||
|
||||
if [[ "${FS,,}" == "fat"* || "${FS,,}" == "vfat"* || "${FS,,}" == "msdos"* ]]; then
|
||||
@@ -100,6 +100,10 @@ fi
|
||||
|
||||
if [[ "${FS,,}" != "exfat"* && "${FS,,}" != "ntfs"* && "${FS,,}" != "unknown"* ]]; then
|
||||
TMP="$STORAGE/tmp"
|
||||
rm -rf "$TMP"
|
||||
if ! makeDir "$TMP"; then
|
||||
error "Failed to create directory \"$TMP\" !" && exit 93
|
||||
fi
|
||||
else
|
||||
TMP="/tmp/dsm"
|
||||
TMP_SPACE=2147483648
|
||||
@@ -108,10 +112,9 @@ else
|
||||
if (( TMP_SPACE > SPACE )); then
|
||||
error "Not enough free space inside the container, have $SPACE_MB available but need at least 2 GB." && exit 93
|
||||
fi
|
||||
rm -rf "$TMP" && mkdir -p "$TMP"
|
||||
fi
|
||||
|
||||
rm -rf "$TMP" && mkdir -p "$TMP"
|
||||
|
||||
# Check free diskspace
|
||||
ROOT_SPACE=536870912
|
||||
SPACE=$(df --output=avail -B 1 / | tail -n 1)
|
||||
@@ -224,6 +227,8 @@ if ! touch "$SYSTEM"; then
|
||||
error "Could not create file $SYSTEM for the system disk." && exit 98
|
||||
fi
|
||||
|
||||
! setOwner "$SYSTEM" && error "Failed to set the owner for \"$SYSTEM\" !"
|
||||
|
||||
if [[ "${FS,,}" == "btrfs" ]]; then
|
||||
{ chattr +C "$SYSTEM"; } || :
|
||||
FA=$(lsattr "$SYSTEM")
|
||||
@@ -256,7 +261,11 @@ PART="$TMP/partition.fdisk"
|
||||
sfdisk -q "$SYSTEM" < "$PART"
|
||||
|
||||
MOUNT="$TMP/system"
|
||||
rm -rf "$MOUNT" && mkdir -p "$MOUNT"
|
||||
rm -rf "$MOUNT"
|
||||
|
||||
if ! makeDir "$MOUNT"; then
|
||||
error "Failed to create directory \"$MOUNT\" !" && exit 93
|
||||
fi
|
||||
|
||||
MSG="Extracting system partition..."
|
||||
info "Install: $MSG" && html "$MSG"
|
||||
@@ -291,6 +300,7 @@ fakeroot -- bash -c "set -Eeu;\
|
||||
|
||||
rm -rf "$MOUNT"
|
||||
echo "$BASE" > "$STORAGE/dsm.ver"
|
||||
! setOwner "$STORAGE/dsm.ver" && error "Failed to set the owner for \"$STORAGE/dsm.ver\" !"
|
||||
|
||||
if [[ "$URL" == "file://$STORAGE/$BASE.pat" ]]; then
|
||||
rm -f "$PAT"
|
||||
@@ -298,10 +308,13 @@ else
|
||||
mv -f "$PAT" "$STORAGE/$BASE.pat"
|
||||
fi
|
||||
|
||||
if [ -f "$STORAGE/$BASE.pat" ]; then
|
||||
! setOwner "$STORAGE/$BASE.pat" && error "Failed to set the owner for \"$STORAGE/$BASE.pat\" !"
|
||||
fi
|
||||
|
||||
mv -f "$BOOT" "$STORAGE/$BASE.boot.img"
|
||||
! setOwner "$STORAGE/$BASE.boot.img" && error "Failed to set the owner for \"$STORAGE/$BASE.boot.img\" !"
|
||||
|
||||
rm -rf "$TMP"
|
||||
|
||||
html "Booting DSM instance..."
|
||||
sleep 1.2
|
||||
|
||||
return 0
|
||||
|
||||
86
src/memory.sh
Normal file
86
src/memory.sh
Normal file
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}')
|
||||
|
||||
if [[ "$RAM_CHECK" != [Nn]* && "${RAM_SIZE,,}" != "max" && "${RAM_SIZE,,}" != "half" ]]; then
|
||||
|
||||
AVAIL_MEM=$(formatBytes "$RAM_AVAIL")
|
||||
|
||||
if (( (RAM_WANTED + RAM_SPARE) > RAM_AVAIL )); then
|
||||
msg="Your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is too high for the $AVAIL_MEM of memory available,"
|
||||
if [[ "${FS,,}" == "zfs" ]]; then
|
||||
info "$msg but since ZFS is active this will be ignored."
|
||||
else
|
||||
RAM_SIZE="max"
|
||||
warn "$msg it will automatically be adjusted to a lower amount."
|
||||
fi
|
||||
else
|
||||
if (( (RAM_WANTED + (RAM_SPARE * 3)) > RAM_AVAIL )); then
|
||||
msg="your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is very close to the $AVAIL_MEM of memory available,"
|
||||
if [[ "${FS,,}" == "zfs" ]]; then
|
||||
info "$msg but since ZFS is active this will be ignored."
|
||||
else
|
||||
warn "$msg please consider a lower amount."
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
if [[ "${RAM_SIZE,,}" == "half" ]]; then
|
||||
|
||||
RAM_WANTED=$(( RAM_AVAIL / 2 ))
|
||||
RAM_WANTED=$(( RAM_WANTED / 1073741825 ))
|
||||
|
||||
if (( "$RAM_WANTED" < 1 )); then
|
||||
RAM_WANTED=$(( RAM_AVAIL / 2 ))
|
||||
RAM_WANTED=$(( RAM_WANTED / 1048577 ))
|
||||
RAM_SIZE="${RAM_WANTED}M"
|
||||
else
|
||||
RAM_SIZE="${RAM_WANTED}G"
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
if [[ "${RAM_SIZE,,}" == "max" ]]; then
|
||||
|
||||
RAM_WANTED=$(( RAM_AVAIL - (RAM_SPARE * 3) ))
|
||||
RAM_WANTED=$(( RAM_WANTED / 1073741825 ))
|
||||
|
||||
if (( "$RAM_WANTED" < 1 )); then
|
||||
|
||||
RAM_WANTED=$(( RAM_AVAIL - (RAM_SPARE * 2) ))
|
||||
RAM_WANTED=$(( RAM_WANTED / 1073741825 ))
|
||||
|
||||
if (( "$RAM_WANTED" < 1 )); then
|
||||
|
||||
RAM_WANTED=$(( RAM_AVAIL - RAM_SPARE ))
|
||||
RAM_WANTED=$(( RAM_WANTED / 1073741825 ))
|
||||
|
||||
if (( "$RAM_WANTED" < 1 )); then
|
||||
|
||||
RAM_WANTED=$(( RAM_AVAIL - RAM_SPARE ))
|
||||
RAM_WANTED=$(( RAM_WANTED / 1048577 ))
|
||||
|
||||
if (( "$RAM_WANTED" < 1 )); then
|
||||
|
||||
RAM_WANTED=$(( RAM_AVAIL ))
|
||||
RAM_WANTED=$(( RAM_WANTED / 1048577 ))
|
||||
|
||||
fi
|
||||
|
||||
RAM_SIZE="${RAM_WANTED}M"
|
||||
else
|
||||
RAM_SIZE="${RAM_WANTED}G"
|
||||
fi
|
||||
else
|
||||
RAM_SIZE="${RAM_WANTED}G"
|
||||
fi
|
||||
else
|
||||
RAM_SIZE="${RAM_WANTED}G"
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
return 0
|
||||
283
src/network.sh
283
src/network.sh
@@ -20,6 +20,7 @@ set -Eeuo pipefail
|
||||
: "${VM_NET_MASK:="255.255.255.0"}"
|
||||
|
||||
: "${PASST:="passt"}"
|
||||
: "${PASST_MTU:=""}"
|
||||
: "${PASST_OPTS:=""}"
|
||||
: "${PASST_DEBUG:=""}"
|
||||
|
||||
@@ -121,6 +122,8 @@ configureDNS() {
|
||||
local mask="$5"
|
||||
local gateway="$6"
|
||||
|
||||
echo "$gateway" > /run/shm/qemu.gw
|
||||
|
||||
[[ "${DNSMASQ_DISABLE:-}" == [Yy1]* ]] && return 0
|
||||
[[ "$DEBUG" == [Yy1]* ]] && echo "Starting dnsmasq daemon..."
|
||||
|
||||
@@ -128,7 +131,7 @@ configureDNS() {
|
||||
rm -f "$log"
|
||||
|
||||
case "${NETWORK,,}" in
|
||||
"nat" | "tap" | "tun" | "tuntap" | "y" )
|
||||
"tap" | "tun" | "tuntap" | "y" )
|
||||
|
||||
# Create lease file for faster resolve
|
||||
echo "0 $mac $ip $host 01:$mac" > /var/lib/misc/dnsmasq.leases
|
||||
@@ -174,70 +177,104 @@ configureDNS() {
|
||||
fi
|
||||
|
||||
if [[ "$DNSMASQ_DEBUG" == [Yy1]* ]]; then
|
||||
tail -fn +0 "$log" &
|
||||
tail -fn +0 "$log" --pid=$$ &
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
getUserPorts() {
|
||||
|
||||
local args=""
|
||||
local list=$1
|
||||
local ssh="22"
|
||||
local dsm="5000"
|
||||
|
||||
[ -z "$list" ] && list="$ssh,$dsm" || list+=",$ssh,$dsm"
|
||||
|
||||
list="${list//,/ }"
|
||||
list="${list## }"
|
||||
list="${list%% }"
|
||||
|
||||
for port in $list; do
|
||||
proto="tcp"
|
||||
num="$port"
|
||||
|
||||
if [[ "$port" == */udp ]]; then
|
||||
proto="udp"
|
||||
num="${port%/udp}"
|
||||
elif [[ "$port" == */tcp ]]; then
|
||||
proto="tcp"
|
||||
num="${port%/tcp}"
|
||||
fi
|
||||
|
||||
args+="hostfwd=$proto::$num-$VM_NET_IP:$num,"
|
||||
done
|
||||
|
||||
echo "${args%?}"
|
||||
return 0
|
||||
}
|
||||
|
||||
getHostPorts() {
|
||||
|
||||
local list="$1"
|
||||
list=$(echo "${list// /}" | sed 's/,*$//g')
|
||||
local list=""
|
||||
list+="$MON_PORT,"
|
||||
list+="${HOST_PORTS// /},"
|
||||
|
||||
[ -z "$list" ] && list="$MON_PORT" || list+=",$MON_PORT"
|
||||
|
||||
if [[ "${NETWORK,,}" == "passt" ]]; then
|
||||
|
||||
local DNS_PORT="53"
|
||||
|
||||
if [[ "${DNSMASQ_DISABLE:-}" != [Yy1]* ]]; then
|
||||
[ -z "$list" ] && list="$DNS_PORT" || list+=",$DNS_PORT"
|
||||
fi
|
||||
|
||||
[ -z "$list" ] && list="$COM_PORT" || list+=",$COM_PORT"
|
||||
[ -z "$list" ] && list="$CHR_PORT" || list+=",$CHR_PORT"
|
||||
|
||||
fi
|
||||
# Remove duplicates
|
||||
list=$(echo "${list//,,/,}," | awk 'BEGIN{RS=ORS=","} !seen[$0]++' | sed 's/,*$//g')
|
||||
|
||||
echo "$list"
|
||||
return 0
|
||||
}
|
||||
|
||||
getUserPorts() {
|
||||
|
||||
local ssh="22"
|
||||
local dsm="5000,5001"
|
||||
|
||||
local list="$ssh,$dsm,"
|
||||
list+="${USER_PORTS// /},"
|
||||
|
||||
local exclude
|
||||
exclude=$(getHostPorts)
|
||||
|
||||
local ports=""
|
||||
local userport=""
|
||||
local hostport=""
|
||||
|
||||
for userport in ${list//,/ }; do
|
||||
|
||||
local num="${userport///tcp}"
|
||||
num="${num///udp}"
|
||||
|
||||
for hostport in ${exclude//,/ }; do
|
||||
|
||||
local port="${hostport///tcp}"
|
||||
port="${port///udp}"
|
||||
|
||||
if [[ "$num" == "$port" ]]; then
|
||||
num=""
|
||||
if [[ "$port" != "$WEB_PORT" ]]; then
|
||||
warn "Could not assign port $port to \"USER_PORTS\" because it is already in \"HOST_PORTS\"!"
|
||||
fi
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
[ -n "$num" ] && ports+="$userport,"
|
||||
|
||||
done
|
||||
|
||||
# Remove duplicates
|
||||
ports=$(echo "${ports//,,/,}," | awk 'BEGIN{RS=ORS=","} !seen[$0]++' | sed 's/,*$//g')
|
||||
|
||||
echo "$ports"
|
||||
return 0
|
||||
}
|
||||
|
||||
getSlirp() {
|
||||
|
||||
local args=""
|
||||
local list=""
|
||||
|
||||
list=$(getUserPorts)
|
||||
|
||||
for port in ${list//,/ }; do
|
||||
|
||||
local proto="tcp"
|
||||
local num="${port%/tcp}"
|
||||
[ -z "$num" ] && continue
|
||||
|
||||
if [[ "$port" == *"/udp" ]]; then
|
||||
proto="udp"
|
||||
num="${port%/udp}"
|
||||
elif [[ "$port" != *"/tcp" ]]; then
|
||||
args+="hostfwd=$proto::$num-$VM_NET_IP:$num,"
|
||||
proto="udp"
|
||||
num="${port%/udp}"
|
||||
fi
|
||||
|
||||
args+="hostfwd=$proto::$num-$VM_NET_IP:$num,"
|
||||
done
|
||||
|
||||
args=$(echo "$args" | sed 's/,*$//g')
|
||||
|
||||
echo "${args%?}"
|
||||
return 0
|
||||
}
|
||||
|
||||
configureSlirp() {
|
||||
|
||||
NETWORK="slirp"
|
||||
[[ "$DEBUG" == [Yy1]* ]] && echo "Configuring slirp networking..."
|
||||
|
||||
local ip="$IP"
|
||||
@@ -251,14 +288,16 @@ configureSlirp() {
|
||||
|
||||
NET_OPTS="-netdev user,id=hostnet0,ipv4=on,host=$gateway,net=${gateway%.*}.0/24,dhcpstart=$ip,${ipv6}hostname=$VM_NET_HOST"
|
||||
|
||||
local forward
|
||||
forward=$(getUserPorts "${USER_PORTS:-}")
|
||||
local forward=""
|
||||
forward=$(getSlirp)
|
||||
[ -n "$forward" ] && NET_OPTS+=",$forward"
|
||||
|
||||
if [[ "${DNSMASQ_DISABLE:-}" != [Yy1]* ]]; then
|
||||
cp /etc/resolv.conf /etc/resolv.dnsmasq
|
||||
echo -e "nameserver 127.0.0.1\nsearch .\noptions ndots:0" >/etc/resolv.conf
|
||||
if [[ "${DNSMASQ_DISABLE:-}" == [Yy1]* ]]; then
|
||||
echo "$gateway" > /run/shm/qemu.gw
|
||||
else
|
||||
[ ! -f /etc/resolv.dnsmasq ] && cp /etc/resolv.conf /etc/resolv.dnsmasq
|
||||
configureDNS "lo" "$ip" "$VM_NET_MAC" "$VM_NET_HOST" "$VM_NET_MASK" "$gateway" || return 1
|
||||
echo -e "nameserver 127.0.0.1\nsearch .\noptions ndots:0" >/etc/resolv.conf
|
||||
fi
|
||||
|
||||
VM_NET_IP="$ip"
|
||||
@@ -267,6 +306,7 @@ configureSlirp() {
|
||||
|
||||
configurePasst() {
|
||||
|
||||
NETWORK="passt"
|
||||
[[ "$DEBUG" == [Yy1]* ]] && echo "Configuring user-mode networking..."
|
||||
|
||||
local log="/var/log/passt.log"
|
||||
@@ -291,25 +331,33 @@ configurePasst() {
|
||||
PASST_OPTS+=" -a $ip"
|
||||
PASST_OPTS+=" -g $gateway"
|
||||
PASST_OPTS+=" -n $VM_NET_MASK"
|
||||
[ -n "$PASST_MTU" ] && PASST_OPTS+=" -m $PASST_MTU"
|
||||
|
||||
exclude=$(getHostPorts "$HOST_PORTS")
|
||||
local forward=""
|
||||
forward=$(getUserPorts)
|
||||
forward="${forward///tcp}"
|
||||
forward="${forward///udp}"
|
||||
|
||||
if [ -z "$exclude" ]; then
|
||||
exclude="all"
|
||||
else
|
||||
exclude="~${exclude//,/,~}"
|
||||
if [ -n "$forward" ]; then
|
||||
forward="%${VM_NET_DEV}/$forward"
|
||||
PASST_OPTS+=" -t $forward"
|
||||
PASST_OPTS+=" -u $forward"
|
||||
fi
|
||||
|
||||
PASST_OPTS+=" -t $exclude"
|
||||
PASST_OPTS+=" -u $exclude"
|
||||
PASST_OPTS+=" -H $VM_NET_HOST"
|
||||
PASST_OPTS+=" -M $GATEWAY_MAC"
|
||||
|
||||
local uid gid
|
||||
uid=$(id -u)
|
||||
gid=$(id -g)
|
||||
PASST_OPTS+=" --runas $uid:$gid"
|
||||
|
||||
PASST_OPTS+=" -P /var/run/passt.pid"
|
||||
PASST_OPTS+=" -l $log"
|
||||
PASST_OPTS+=" -q"
|
||||
|
||||
if [[ "${DNSMASQ_DISABLE:-}" != [Yy1]* ]]; then
|
||||
cp /etc/resolv.conf /etc/resolv.dnsmasq
|
||||
[ ! -f /etc/resolv.dnsmasq ] && cp /etc/resolv.conf /etc/resolv.dnsmasq
|
||||
echo -e "nameserver 127.0.0.1\nsearch .\noptions ndots:0" >/etc/resolv.conf
|
||||
fi
|
||||
|
||||
@@ -317,14 +365,21 @@ configurePasst() {
|
||||
[[ "$DEBUG" == [Yy1]* ]] && printf "Passt arguments:\n\n%s\n\n" "${PASST_OPTS// -/$'\n-'}"
|
||||
|
||||
if ! $PASST ${PASST_OPTS:+ $PASST_OPTS} >/dev/null 2>&1; then
|
||||
local msg="Failed to start passt, reason: $?"
|
||||
[ -f "$log" ] && cat "$log"
|
||||
error "$msg"
|
||||
return 1
|
||||
|
||||
rm -f "$log"
|
||||
PASST_OPTS="${PASST_OPTS/ -q/}"
|
||||
{ $PASST ${PASST_OPTS:+ $PASST_OPTS}; rc=$?; } || :
|
||||
|
||||
if (( rc != 0 )); then
|
||||
[ -f "$log" ] && cat "$log"
|
||||
warn "failed to start passt ($rc), falling back to slirp networking!"
|
||||
configureSlirp && return 0 || return 1
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
if [[ "$PASST_DEBUG" == [Yy1]* ]]; then
|
||||
tail -fn +0 "$log" &
|
||||
tail -fn +0 "$log" --pid=$$ &
|
||||
else
|
||||
if [[ "$DEBUG" == [Yy1]* ]]; then
|
||||
[ -f "$log" ] && cat "$log" && echo ""
|
||||
@@ -368,7 +423,14 @@ configureNAT() {
|
||||
fi
|
||||
fi
|
||||
|
||||
local ip="172.30.0.2"
|
||||
local ip base
|
||||
base=$(echo "$IP" | sed -r 's/([^.]*.){2}//')
|
||||
if [[ "$IP" != "172.30."* ]]; then
|
||||
ip="172.30.$base"
|
||||
else
|
||||
ip="172.31.$base"
|
||||
fi
|
||||
|
||||
[ -n "$VM_NET_IP" ] && ip="$VM_NET_IP"
|
||||
|
||||
local gateway=""
|
||||
@@ -426,7 +488,7 @@ configureNAT() {
|
||||
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null
|
||||
fi
|
||||
|
||||
exclude=$(getHostPorts "$HOST_PORTS")
|
||||
exclude=$(getHostPorts)
|
||||
|
||||
if [ -n "$exclude" ]; then
|
||||
if [[ "$exclude" != *","* ]]; then
|
||||
@@ -492,14 +554,24 @@ closeBridge() {
|
||||
return 0
|
||||
}
|
||||
|
||||
closeWeb() {
|
||||
|
||||
# Shutdown nginx
|
||||
nginx -s stop 2> /dev/null
|
||||
fWait "nginx"
|
||||
|
||||
# Shutdown websocket
|
||||
local pid="/var/run/websocketd.pid"
|
||||
[ -s "$pid" ] && pKill "$(<"$pid")"
|
||||
rm -f "$pid"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
closeNetwork() {
|
||||
|
||||
if [[ "${WEB:-}" != [Nn]* && "$DHCP" == [Yy1]* ]]; then
|
||||
|
||||
# Shutdown nginx
|
||||
nginx -s stop 2> /dev/null
|
||||
fWait "nginx"
|
||||
|
||||
closeWeb
|
||||
fi
|
||||
|
||||
[[ "$NETWORK" == [Nn]* ]] && return 0
|
||||
@@ -542,8 +614,8 @@ checkOS() {
|
||||
local if="macvlan"
|
||||
kernel=$(uname -a)
|
||||
|
||||
[[ "${kernel,,}" == *"darwin"* ]] && os="Docker Desktop for macOS"
|
||||
[[ "${kernel,,}" == *"microsoft"* ]] && os="Docker Desktop for Windows"
|
||||
[[ "${kernel,,}" == *"darwin"* ]] && os="$ENGINE Desktop for macOS"
|
||||
[[ "${kernel,,}" == *"microsoft"* ]] && os="$ENGINE Desktop for Windows"
|
||||
|
||||
if [[ "$DHCP" == [Yy1]* ]]; then
|
||||
if="macvtap"
|
||||
@@ -576,12 +648,17 @@ getInfo() {
|
||||
fi
|
||||
|
||||
GATEWAY=$(ip route list dev "$VM_NET_DEV" | awk ' /^default/ {print $3}' | head -n 1)
|
||||
IP=$(ip address show dev "$VM_NET_DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/ | head -n 1)
|
||||
IP6=""
|
||||
{ IP=$(ip address show dev "$VM_NET_DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/ | head -n 1); rc=$?; } 2>/dev/null || :
|
||||
|
||||
if (( rc != 0 )); then
|
||||
error "Could not determine container IP address!" && exit 26
|
||||
fi
|
||||
|
||||
IP6=""
|
||||
# shellcheck disable=SC2143
|
||||
if [ -f /proc/net/if_inet6 ] && [ -n "$(ifconfig -a | grep inet6)" ]; then
|
||||
IP6=$(ip -6 addr show dev "$VM_NET_DEV" scope global up)
|
||||
{ IP6=$(ip -6 addr show dev "$VM_NET_DEV" scope global up); rc=$?; } 2>/dev/null || :
|
||||
(( rc != 0 )) && IP6=""
|
||||
[ -n "$IP6" ] && IP6=$(echo "$IP6" | sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d' | head -n 1)
|
||||
fi
|
||||
|
||||
@@ -628,13 +705,8 @@ getInfo() {
|
||||
[ -z "$MTU" ] && MTU="$mtu"
|
||||
[ -z "$MTU" ] && MTU="0"
|
||||
|
||||
if [ "$MTU" -gt "1500" ]; then
|
||||
[[ "$DEBUG" == [Yy1]* ]] && echo "MTU size is too large: $MTU, ignoring..."
|
||||
MTU="0"
|
||||
fi
|
||||
|
||||
if [[ "${ADAPTER,,}" != "virtio-net-pci" ]]; then
|
||||
if [[ "$MTU" != "0" && "$MTU" != "1500" ]]; then
|
||||
if [[ "$MTU" != "0" ]] && [ "$MTU" -lt "1500" ]; then
|
||||
warn "MTU size is $MTU, but cannot be set for $ADAPTER adapters!" && MTU="0"
|
||||
fi
|
||||
fi
|
||||
@@ -647,6 +719,7 @@ getInfo() {
|
||||
# Generate MAC address based on Docker container ID in hostname
|
||||
VM_NET_MAC=$(echo "$HOST" | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:11:32:\3:\4:\5/')
|
||||
echo "${VM_NET_MAC^^}" > "$file"
|
||||
! setOwner "$file" && error "Failed to set the owner for \"$file\" !"
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -683,6 +756,8 @@ getInfo() {
|
||||
fi
|
||||
|
||||
echo "$IP" > /run/shm/qemu.ip
|
||||
echo "$nic" > /run/shm/qemu.nic
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -706,27 +781,24 @@ if [[ "$IP" == "172.17."* ]]; then
|
||||
warn "your container IP starts with 172.17.* which will cause conflicts when you install the Container Manager package inside DSM!"
|
||||
fi
|
||||
|
||||
MSG="Booting DSM instance..."
|
||||
html "$MSG"
|
||||
|
||||
if [[ "$DHCP" == [Yy1]* ]]; then
|
||||
|
||||
# Configure for macvtap interface
|
||||
configureDHCP || exit 20
|
||||
|
||||
MSG="Booting DSM instance..."
|
||||
html "$MSG"
|
||||
|
||||
else
|
||||
|
||||
if [[ "${WEB:-}" != [Nn]* ]]; then
|
||||
|
||||
# Shutdown nginx
|
||||
nginx -s stop 2> /dev/null
|
||||
fWait "nginx"
|
||||
|
||||
sleep 1.2
|
||||
closeWeb
|
||||
fi
|
||||
|
||||
case "${NETWORK,,}" in
|
||||
"user"* | "passt" | "slirp" ) ;;
|
||||
"nat" | "tap" | "tun" | "tuntap" | "y" )
|
||||
"passt" | "slirp" | "user"* ) ;;
|
||||
"tap" | "tun" | "tuntap" | "y" )
|
||||
|
||||
# Configure tap interface
|
||||
if ! configureNAT; then
|
||||
@@ -740,11 +812,9 @@ else
|
||||
|
||||
esac
|
||||
|
||||
[[ "${NETWORK,,}" == "user"* ]] && NETWORK="passt"
|
||||
|
||||
case "${NETWORK,,}" in
|
||||
"nat" | "tap" | "tun" | "tuntap" | "y" ) ;;
|
||||
"passt" )
|
||||
"tap" | "tun" | "tuntap" | "y" ) ;;
|
||||
"passt" | "user"* )
|
||||
|
||||
# Configure for user-mode networking (passt)
|
||||
if ! configurePasst; then
|
||||
@@ -758,16 +828,21 @@ else
|
||||
if ! configureSlirp; then
|
||||
error "Failed to configure user-mode networking!"
|
||||
exit 24
|
||||
fi
|
||||
|
||||
if [ -z "$USER_PORTS" ]; then
|
||||
info "Notice: slirp networking is active, so when you want to expose ports, you will need to map them using this variable: \"USER_PORTS=5000,5001\"."
|
||||
fi ;;
|
||||
|
||||
*)
|
||||
error "Unrecognized NETWORK value: \"$NETWORK\"" && exit 24 ;;
|
||||
esac
|
||||
|
||||
case "${NETWORK,,}" in
|
||||
"passt" | "slirp" )
|
||||
|
||||
if [ -z "$USER_PORTS" ]; then
|
||||
info "Notice: because user-mode networking is active, when you need to forward custom ports to DSM, add them to the \"USER_PORTS\" variable."
|
||||
fi ;;
|
||||
|
||||
esac
|
||||
|
||||
fi
|
||||
|
||||
NET_OPTS+=" -device $ADAPTER,id=net0,netdev=hostnet0,romfile=,mac=$VM_NET_MAC"
|
||||
|
||||
13
src/power.sh
13
src/power.sh
@@ -33,6 +33,7 @@ _trap() {
|
||||
finish() {
|
||||
|
||||
local pid
|
||||
local cnt=0
|
||||
local reason=$1
|
||||
|
||||
touch "$QEMU_END"
|
||||
@@ -40,14 +41,24 @@ finish() {
|
||||
if [ -s "$QEMU_PID" ]; then
|
||||
|
||||
pid=$(<"$QEMU_PID")
|
||||
echo && error "Forcefully terminating QEMU process, reason: $reason..."
|
||||
echo && error "Forcefully terminating Virtual DSM, reason: $reason..."
|
||||
{ kill -15 "$pid" || true; } 2>/dev/null
|
||||
|
||||
while isAlive "$pid"; do
|
||||
|
||||
sleep 1
|
||||
cnt=$((cnt+1))
|
||||
|
||||
# Workaround for zombie pid
|
||||
[ ! -s "$QEMU_PID" ] && break
|
||||
|
||||
if [ "$cnt" == "5" ]; then
|
||||
echo && error "QEMU did not terminate itself, forcefully killing process..."
|
||||
{ kill -9 "$pid" || true; } 2>/dev/null
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
fi
|
||||
|
||||
fKill "print.sh"
|
||||
|
||||
14
src/print.sh
14
src/print.sh
@@ -11,11 +11,12 @@ error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; }
|
||||
|
||||
file="/run/shm/dsm.url"
|
||||
info="/run/shm/msg.html"
|
||||
driver="/run/shm/qemu.nic"
|
||||
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:$COM_PORT/read?command=10"
|
||||
url="http://127.0.0.1:2210/read?command=10"
|
||||
|
||||
resp_err="Guest returned an invalid response:"
|
||||
curl_err="Failed to connect to guest: curl error"
|
||||
@@ -56,11 +57,7 @@ do
|
||||
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
|
||||
[[ "$ip" == "null" ]] && error "$resp_err $json" && continue
|
||||
|
||||
if [ -z "$ip" ]; then
|
||||
[[ "$DHCP" == [Yy1]* ]] && continue
|
||||
ip="20.20.20.21"
|
||||
fi
|
||||
|
||||
[ -z "$ip" ] && continue
|
||||
echo "$ip:$port" > $file
|
||||
|
||||
done
|
||||
@@ -69,7 +66,7 @@ done
|
||||
|
||||
location=$(<"$file")
|
||||
|
||||
if [[ "$location" != "20.20"* ]]; then
|
||||
if [[ "$DHCP" == [Yy1]* ]]; then
|
||||
|
||||
msg="http://$location"
|
||||
title="<title>Virtual DSM</title>"
|
||||
@@ -88,10 +85,11 @@ if [[ "$location" != "20.20"* ]]; then
|
||||
|
||||
else
|
||||
|
||||
nic=$(<"$driver")
|
||||
ip=$(<"$address")
|
||||
port="${location##*:}"
|
||||
|
||||
if [[ "$ip" == "172."* ]]; then
|
||||
if [[ "${nic,,}" != "macvlan" ]]; then
|
||||
msg="port $port"
|
||||
else
|
||||
msg="http://$ip:$port"
|
||||
|
||||
48
src/proc.sh
48
src/proc.sh
@@ -3,7 +3,6 @@ set -Eeuo pipefail
|
||||
|
||||
# Docker environment variables
|
||||
|
||||
: "${KVM:="Y"}"
|
||||
: "${HOST_CPU:=""}"
|
||||
: "${CPU_FLAGS:=""}"
|
||||
: "${CPU_MODEL:=""}"
|
||||
@@ -26,52 +25,7 @@ else
|
||||
esac
|
||||
fi
|
||||
|
||||
if [[ "$KVM" == [Nn]* ]]; then
|
||||
warn "KVM acceleration is disabled, this will cause the machine to run about 10 times slower!"
|
||||
else
|
||||
if [[ "${ARCH,,}" != "amd64" ]]; then
|
||||
KVM="N"
|
||||
warn "your CPU architecture is ${ARCH^^} and cannot provide KVM acceleration for x64 instructions, so the machine will run about 10 times slower."
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ "$KVM" != [Nn]* ]]; then
|
||||
|
||||
KVM_ERR=""
|
||||
|
||||
if [ ! -e /dev/kvm ]; then
|
||||
KVM_ERR="(/dev/kvm is missing)"
|
||||
else
|
||||
if ! sh -c 'echo -n > /dev/kvm' &> /dev/null; then
|
||||
KVM_ERR="(/dev/kvm is unwriteable)"
|
||||
else
|
||||
flags=$(sed -ne '/^flags/s/^.*: //p' /proc/cpuinfo)
|
||||
if ! grep -qw "vmx\|svm" <<< "$flags"; then
|
||||
KVM_ERR="(not enabled in BIOS)"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -n "$KVM_ERR" ]; then
|
||||
KVM="N"
|
||||
if [[ "$OSTYPE" =~ ^darwin ]]; then
|
||||
warn "you are using macOS which has no KVM support, so the machine will run about 10 times slower."
|
||||
else
|
||||
kernel=$(uname -a)
|
||||
case "${kernel,,}" in
|
||||
*"microsoft"* )
|
||||
error "Please bind '/dev/kvm' as a volume in the optional container settings when using Docker Desktop." ;;
|
||||
*"synology"* )
|
||||
error "Please make sure that Synology VMM (Virtual Machine Manager) is installed and that '/dev/kvm' is binded to this container." ;;
|
||||
*)
|
||||
error "KVM acceleration is not available $KVM_ERR, this will cause the machine to run about 10 times slower."
|
||||
error "See the FAQ for possible causes, or disable acceleration by adding the \"KVM=N\" variable (not recommended)." ;;
|
||||
esac
|
||||
[[ "$DEBUG" != [Yy1]* ]] && exit 88
|
||||
fi
|
||||
fi
|
||||
|
||||
fi
|
||||
flags=$(sed -ne '/^flags/s/^.*: //p' /proc/cpuinfo)
|
||||
|
||||
if [[ "$KVM" != [Nn]* ]]; then
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
info="/run/shm/msg.html"
|
||||
|
||||
escape () {
|
||||
local s
|
||||
s=${1//&/\&}
|
||||
@@ -11,28 +13,33 @@ escape () {
|
||||
return 0
|
||||
}
|
||||
|
||||
file="$1"
|
||||
path="$1"
|
||||
total="$2"
|
||||
body=$(escape "$3")
|
||||
info="/run/shm/msg.html"
|
||||
|
||||
if [[ "$body" == *"..." ]]; then
|
||||
body="<p class=\"loading\">${body/.../}</p>"
|
||||
body="<p class=\"loading\">${body::-3}</p>"
|
||||
fi
|
||||
|
||||
while true
|
||||
do
|
||||
if [ -s "$file" ]; then
|
||||
bytes=$(du -sb "$file" | cut -f1)
|
||||
if (( bytes > 1000 )); then
|
||||
if [ -z "$total" ] || [[ "$total" == "0" ]]; then
|
||||
size=$(numfmt --to=iec --suffix=B "$bytes" | sed -r 's/([A-Z])/ \1/')
|
||||
else
|
||||
size="$(echo "$bytes" "$total" | awk '{printf "%.1f", $1 * 100 / $2}')"
|
||||
size="$size%"
|
||||
fi
|
||||
echo "${body//(\[P\])/($size)}"> "$info"
|
||||
fi
|
||||
|
||||
if [ ! -s "$path" ] && [ ! -d "$path" ]; then
|
||||
bytes="0"
|
||||
else
|
||||
bytes=$(du -sb "$path" | cut -f1)
|
||||
fi
|
||||
|
||||
if (( bytes > 4096 )); then
|
||||
if [ -z "$total" ] || [[ "$total" == "0" ]] || [ "$bytes" -gt "$total" ]; then
|
||||
size=$(numfmt --to=iec --suffix=B "$bytes" | sed -r 's/([A-Z])/ \1/')
|
||||
else
|
||||
size="$(echo "$bytes" "$total" | awk '{printf "%.1f", $1 * 100 / $2}')"
|
||||
size="$size%"
|
||||
fi
|
||||
[[ "$size" != "0.0%" ]] && echo "${body//(\[P\])/($size)}"> "$info"
|
||||
fi
|
||||
|
||||
sleep 1 & wait $!
|
||||
|
||||
done
|
||||
|
||||
184
src/reset.sh
184
src/reset.sh
@@ -9,7 +9,8 @@ trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR
|
||||
|
||||
# Docker environment variables
|
||||
|
||||
: "${TZ:=""}" # System local timezone
|
||||
: "${TZ:=""}" # System timezone
|
||||
: "${KVM:="Y"}" # KVM acceleration
|
||||
: "${DEBUG:="N"}" # Disable debugging mode
|
||||
: "${COUNTRY:=""}" # Country code for mirror
|
||||
: "${CONSOLE:="N"}" # Disable console mode
|
||||
@@ -58,10 +59,12 @@ fi
|
||||
|
||||
CPU_CORES="${CPU_CORES// /}"
|
||||
[[ "${CPU_CORES,,}" == "max" ]] && CPU_CORES="$CORES"
|
||||
[[ "${CPU_CORES,,}" == "half" ]] && CPU_CORES=$(( CORES / 2 ))
|
||||
[[ "${CPU_CORES,,}" == "0" ]] && CPU_CORES="1"
|
||||
[ -n "${CPU_CORES//[0-9 ]}" ] && error "Invalid amount of CPU_CORES: $CPU_CORES" && exit 15
|
||||
|
||||
if [ "$CPU_CORES" -gt "$CORES" ]; then
|
||||
warn "The amount for CPU_CORES (${CPU_CORES}) exceeds the amount of physical cores, so will be limited to ${CORES}."
|
||||
warn "The amount for CPU_CORES (${CPU_CORES}) exceeds the amount of logical cores available, so will be limited to ${CORES}."
|
||||
CPU_CORES="$CORES"
|
||||
fi
|
||||
|
||||
@@ -75,8 +78,7 @@ fi
|
||||
|
||||
# Check folder
|
||||
|
||||
if [[ "${COMMIT:-}" == [Yy1]* ]]; then
|
||||
STORAGE="/local"
|
||||
if [[ "${STORAGE,,}" != "/storage" ]]; then
|
||||
mkdir -p "$STORAGE"
|
||||
fi
|
||||
|
||||
@@ -85,7 +87,9 @@ if [ ! -d "$STORAGE" ]; then
|
||||
fi
|
||||
|
||||
if [ ! -w "$STORAGE" ]; then
|
||||
error "Storage folder ($STORAGE) is not writeable!" && exit 13
|
||||
msg="Storage folder ($STORAGE) is not writeable!"
|
||||
msg+=" If SELinux is active, you need to add the \":Z\" flag to the bind mount."
|
||||
error "$msg" && exit 13
|
||||
fi
|
||||
|
||||
# Check filesystem
|
||||
@@ -104,20 +108,18 @@ RAM_TOTAL=$(free -b | grep -m 1 Mem: | awk '{print $2}')
|
||||
RAM_SIZE="${RAM_SIZE// /}"
|
||||
[ -z "$RAM_SIZE" ] && error "RAM_SIZE not specified!" && exit 16
|
||||
|
||||
if [[ "${RAM_SIZE,,}" == "max" ]]; then
|
||||
RAM_WANTED=$(( RAM_AVAIL - RAM_SPARE - RAM_SPARE ))
|
||||
RAM_WANTED=$(( RAM_WANTED / 1073741825 ))
|
||||
RAM_SIZE="${RAM_WANTED}G"
|
||||
fi
|
||||
if [[ "${RAM_SIZE,,}" != "max" && "${RAM_SIZE,,}" != "half" ]]; then
|
||||
|
||||
if [ -z "${RAM_SIZE//[0-9. ]}" ]; then
|
||||
[ "${RAM_SIZE%%.*}" -lt "130" ] && RAM_SIZE="${RAM_SIZE}G" || RAM_SIZE="${RAM_SIZE}M"
|
||||
fi
|
||||
if [ -z "${RAM_SIZE//[0-9. ]}" ]; then
|
||||
[ "${RAM_SIZE%%.*}" -lt "130" ] && RAM_SIZE="${RAM_SIZE}G" || RAM_SIZE="${RAM_SIZE}M"
|
||||
fi
|
||||
|
||||
RAM_SIZE=$(echo "${RAM_SIZE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
|
||||
! numfmt --from=iec "$RAM_SIZE" &>/dev/null && error "Invalid RAM_SIZE: $RAM_SIZE" && exit 16
|
||||
RAM_WANTED=$(numfmt --from=iec "$RAM_SIZE")
|
||||
[ "$RAM_WANTED" -lt "136314880 " ] && error "RAM_SIZE is too low: $RAM_SIZE" && exit 16
|
||||
RAM_SIZE=$(echo "${RAM_SIZE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
|
||||
! numfmt --from=iec "$RAM_SIZE" &>/dev/null && error "Invalid RAM_SIZE: $RAM_SIZE" && exit 16
|
||||
RAM_WANTED=$(numfmt --from=iec "$RAM_SIZE")
|
||||
[ "$RAM_WANTED" -lt "136314880 " ] && error "RAM_SIZE is too low: $RAM_SIZE" && exit 16
|
||||
|
||||
fi
|
||||
|
||||
# Print system info
|
||||
SYS="${SYS/-generic/}"
|
||||
@@ -132,13 +134,61 @@ TOTAL_MEM=$(formatBytes "$RAM_TOTAL" "up")
|
||||
echo "❯ CPU: ${CPU} | RAM: ${AVAIL_MEM/ GB/}/$TOTAL_MEM | DISK: $SPACE_GB (${FS}) | KERNEL: ${SYS}..."
|
||||
echo
|
||||
|
||||
# Check available memory
|
||||
# Check KVM support
|
||||
|
||||
if [[ "${PLATFORM,,}" == "x64" ]]; then
|
||||
TARGET="amd64"
|
||||
else
|
||||
TARGET="arm64"
|
||||
fi
|
||||
|
||||
if [[ "$KVM" == [Nn]* ]]; then
|
||||
warn "KVM acceleration is disabled, this will cause the machine to run about 10 times slower!"
|
||||
else
|
||||
if [[ "${ARCH,,}" != "$TARGET" ]]; then
|
||||
KVM="N"
|
||||
warn "your CPU architecture is ${ARCH^^} and cannot provide KVM acceleration for ${PLATFORM^^} instructions, so the machine will run about 10 times slower."
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ "$KVM" != [Nn]* ]]; then
|
||||
|
||||
KVM_ERR=""
|
||||
|
||||
if [ ! -e /dev/kvm ]; then
|
||||
KVM_ERR="(/dev/kvm is missing)"
|
||||
else
|
||||
if ! sh -c 'echo -n > /dev/kvm' &> /dev/null; then
|
||||
KVM_ERR="(/dev/kvm is unwriteable)"
|
||||
else
|
||||
if [[ "${PLATFORM,,}" == "x64" ]]; then
|
||||
flags=$(sed -ne '/^flags/s/^.*: //p' /proc/cpuinfo)
|
||||
if ! grep -qw "vmx\|svm" <<< "$flags"; then
|
||||
KVM_ERR="(not enabled in BIOS)"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -n "$KVM_ERR" ]; then
|
||||
KVM="N"
|
||||
if [[ "$OSTYPE" =~ ^darwin ]]; then
|
||||
warn "you are using macOS which has no KVM support, so the machine will run about 10 times slower."
|
||||
else
|
||||
kernel=$(uname -a)
|
||||
case "${kernel,,}" in
|
||||
*"microsoft"* )
|
||||
error "Please bind '/dev/kvm' as a volume in the optional container settings when using Docker Desktop." ;;
|
||||
*"synology"* )
|
||||
error "Please make sure that Synology VMM (Virtual Machine Manager) is installed and that '/dev/kvm' is binded to this container." ;;
|
||||
*)
|
||||
error "KVM acceleration is not available $KVM_ERR, this will cause the machine to run about 10 times slower."
|
||||
error "See the FAQ for possible causes, or disable acceleration by adding the \"KVM=N\" variable (not recommended)." ;;
|
||||
esac
|
||||
[[ "$DEBUG" != [Yy1]* ]] && exit 88
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ "$RAM_CHECK" != [Nn]* ]] && (( (RAM_WANTED + RAM_SPARE) > RAM_AVAIL )); then
|
||||
AVAIL_MEM=$(formatBytes "$RAM_AVAIL")
|
||||
msg="Your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is too high for the $AVAIL_MEM of memory available, please set a lower value."
|
||||
[[ "${FS,,}" != "zfs" ]] && error "$msg" && exit 17
|
||||
info "$msg"
|
||||
fi
|
||||
|
||||
# Cleanup files
|
||||
@@ -149,92 +199,4 @@ rm -f /run/shm/dsm.url
|
||||
rm -rf /tmp/dsm
|
||||
rm -rf "$STORAGE/tmp"
|
||||
|
||||
getCountry() {
|
||||
local url=$1
|
||||
local query=$2
|
||||
local rc json result
|
||||
|
||||
{ json=$(curl -m 5 -H "Accept: application/json" -sfk "$url"); rc=$?; } || :
|
||||
(( rc != 0 )) && return 0
|
||||
|
||||
{ result=$(echo "$json" | jq -r "$query" 2> /dev/null); rc=$?; } || :
|
||||
(( rc != 0 )) && return 0
|
||||
|
||||
[[ ${#result} -ne 2 ]] && return 0
|
||||
[[ "${result^^}" == "XX" ]] && return 0
|
||||
|
||||
COUNTRY="${result^^}"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
setCountry() {
|
||||
|
||||
[[ "${TZ,,}" == "asia/harbin" ]] && COUNTRY="CN"
|
||||
[[ "${TZ,,}" == "asia/beijing" ]] && COUNTRY="CN"
|
||||
[[ "${TZ,,}" == "asia/urumqi" ]] && COUNTRY="CN"
|
||||
[[ "${TZ,,}" == "asia/kashgar" ]] && COUNTRY="CN"
|
||||
[[ "${TZ,,}" == "asia/shanghai" ]] && COUNTRY="CN"
|
||||
[[ "${TZ,,}" == "asia/chongqing" ]] && COUNTRY="CN"
|
||||
|
||||
[ -z "$COUNTRY" ] && getCountry "https://api.ipapi.is" ".location.country_code"
|
||||
[ -z "$COUNTRY" ] && getCountry "https://ifconfig.co/json" ".country_iso"
|
||||
[ -z "$COUNTRY" ] && getCountry "https://api.ip2location.io" ".country_code"
|
||||
[ -z "$COUNTRY" ] && getCountry "https://ipinfo.io/json" ".country"
|
||||
[ -z "$COUNTRY" ] && getCountry "https://api.ipquery.io/?format=json" ".location.country_code"
|
||||
[ -z "$COUNTRY" ] && getCountry "https://api.myip.com" ".cc"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
addPackage() {
|
||||
local pkg=$1
|
||||
local desc=$2
|
||||
|
||||
if apt-mark showinstall | grep -qx "$pkg"; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
MSG="Installing $desc..."
|
||||
info "$MSG" && html "$MSG"
|
||||
|
||||
[ -z "$COUNTRY" ] && setCountry
|
||||
|
||||
if [[ "${COUNTRY^^}" == "CN" ]]; then
|
||||
sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources
|
||||
fi
|
||||
|
||||
DEBIAN_FRONTEND=noninteractive apt-get -qq update
|
||||
DEBIAN_FRONTEND=noninteractive apt-get -qq --no-install-recommends -y install "$pkg" > /dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
: "${COM_PORT:="2210"}" # Comm port
|
||||
: "${MON_PORT:="7100"}" # Monitor port
|
||||
: "${WEB_PORT:="5000"}" # Webserver port
|
||||
: "${CHR_PORT:="12345"}" # Character port
|
||||
|
||||
cp -r /var/www/* /run/shm
|
||||
html "Starting $APP for $ENGINE..."
|
||||
|
||||
if [[ "${WEB:-}" != [Nn]* ]]; then
|
||||
|
||||
mkdir -p /etc/nginx/sites-enabled
|
||||
cp /etc/nginx/default.conf /etc/nginx/sites-enabled/web.conf
|
||||
|
||||
sed -i "s/listen 5000 default_server;/listen $WEB_PORT default_server;/g" /etc/nginx/sites-enabled/web.conf
|
||||
|
||||
# shellcheck disable=SC2143
|
||||
if [ -f /proc/net/if_inet6 ] && [ -n "$(ifconfig -a | grep inet6)" ]; then
|
||||
|
||||
sed -i "s/listen $WEB_PORT default_server;/listen [::]:$WEB_PORT default_server ipv6only=off;/g" /etc/nginx/sites-enabled/web.conf
|
||||
|
||||
fi
|
||||
|
||||
# Start webserver
|
||||
nginx -e stderr
|
||||
|
||||
fi
|
||||
|
||||
return 0
|
||||
|
||||
39
src/server.sh
Normal file
39
src/server.sh
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
: "${COM_PORT:="2210"}" # Comm port
|
||||
: "${MON_PORT:="7100"}" # Monitor port
|
||||
: "${WEB_PORT:="5000"}" # Webserver port
|
||||
: "${CHR_PORT:="12345"}" # Character port
|
||||
: "${WSD_PORT:="8004"}" # Websockets port
|
||||
|
||||
cp -r /var/www/* /run/shm
|
||||
rm -f /var/run/websocketd.pid
|
||||
|
||||
html "Starting $APP for $ENGINE..."
|
||||
|
||||
if [[ "${WEB:-}" != [Nn]* ]]; then
|
||||
|
||||
mkdir -p /etc/nginx/sites-enabled
|
||||
cp /etc/nginx/default.conf /etc/nginx/sites-enabled/web.conf
|
||||
|
||||
sed -i "s/listen 5000 default_server;/listen $WEB_PORT default_server;/g" /etc/nginx/sites-enabled/web.conf
|
||||
sed -i "s/proxy_pass http:\/\/127.0.0.1:8004\/;/proxy_pass http:\/\/127.0.0.1:$WSD_PORT\/;/g" /etc/nginx/sites-enabled/web.conf
|
||||
|
||||
# shellcheck disable=SC2143
|
||||
if [ -f /proc/net/if_inet6 ] && [ -n "$(ifconfig -a | grep inet6)" ]; then
|
||||
|
||||
sed -i "s/listen $WEB_PORT default_server;/listen [::]:$WEB_PORT default_server ipv6only=off;/g" /etc/nginx/sites-enabled/web.conf
|
||||
|
||||
fi
|
||||
|
||||
# Start webserver
|
||||
nginx -e stderr
|
||||
|
||||
# Start websocket server
|
||||
websocketd --address 127.0.0.1 --port="$WSD_PORT" /run/socket.sh >/var/log/websocketd.log &
|
||||
echo "$!" > /var/run/websocketd.pid
|
||||
|
||||
fi
|
||||
|
||||
return 0
|
||||
31
src/socket.sh
Normal file
31
src/socket.sh
Normal file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
lastmsg=""
|
||||
path="/run/shm/msg.html"
|
||||
|
||||
refresh() {
|
||||
|
||||
[ ! -f "$path" ] && return 0
|
||||
[ ! -s "$path" ] && return 0
|
||||
|
||||
msg=$(< "$path")
|
||||
msg="${msg%$'\n'}"
|
||||
|
||||
[ -z "$msg" ] && return 0
|
||||
[[ "$msg" == "$lastmsg" ]] && return 0
|
||||
|
||||
lastmsg="$msg"
|
||||
echo "s: $msg"
|
||||
return 0
|
||||
}
|
||||
|
||||
refresh
|
||||
|
||||
inotifywait -m "$path" |
|
||||
while read -r fp event fn; do
|
||||
case "${event,,}" in
|
||||
"modify"* ) refresh ;;
|
||||
"delete_self" ) echo "c: vnc" ;;
|
||||
esac
|
||||
done
|
||||
@@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
# Override this placeholder file using a Docker bind to execute a script during startup!
|
||||
# You can override this hook to execute a script before startup!
|
||||
|
||||
return 0
|
||||
|
||||
98
src/utils.sh
98
src/utils.sh
@@ -67,6 +67,37 @@ fKill() {
|
||||
return 0
|
||||
}
|
||||
|
||||
setOwner() {
|
||||
local file="$1"
|
||||
local dir uid gid
|
||||
|
||||
[ ! -f "$file" ] && return 1
|
||||
|
||||
dir=$(dirname -- "$file")
|
||||
uid=$(stat -c '%u' "$dir")
|
||||
gid=$(stat -c '%g' "$dir")
|
||||
|
||||
! chown "$uid:$gid" "$file" && return 1
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
makeDir() {
|
||||
local path="$1"
|
||||
local dir uid gid
|
||||
|
||||
[ -d "$path" ] && return 0
|
||||
! mkdir -p "$path" && return 1
|
||||
|
||||
dir=$(dirname -- "$path")
|
||||
uid=$(stat -c '%u' "$dir")
|
||||
gid=$(stat -c '%g' "$dir")
|
||||
|
||||
! chown "$uid:$gid" "$path" && return 1
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
escape () {
|
||||
local s
|
||||
s=${1//&/\&}
|
||||
@@ -123,11 +154,11 @@ cpu() {
|
||||
fi
|
||||
|
||||
cpu="${cpu// CPU/}"
|
||||
cpu="${cpu// [0-9] Core}"
|
||||
cpu="${cpu// [0-9][0-9] Core}"
|
||||
cpu="${cpu// [0-9][0-9][0-9] Core}"
|
||||
cpu="${cpu//[0-9]th Gen }"
|
||||
cpu="${cpu// [0-9][0-9] Core}"
|
||||
cpu="${cpu// [0-9] Core}"
|
||||
cpu="${cpu//[0-9][0-9]th Gen }"
|
||||
cpu="${cpu//[0-9]th Gen }"
|
||||
cpu="${cpu// Processor/}"
|
||||
cpu="${cpu// Quad core/}"
|
||||
cpu="${cpu// Dual core/}"
|
||||
@@ -159,4 +190,65 @@ hasDisk() {
|
||||
return 1
|
||||
}
|
||||
|
||||
getCountry() {
|
||||
local url=$1
|
||||
local query=$2
|
||||
local rc json result
|
||||
|
||||
{ json=$(curl -m 5 -H "Accept: application/json" -sfk "$url"); rc=$?; } || :
|
||||
(( rc != 0 )) && return 0
|
||||
|
||||
{ result=$(echo "$json" | jq -r "$query" 2> /dev/null); rc=$?; } || :
|
||||
(( rc != 0 )) && return 0
|
||||
|
||||
[[ ${#result} -ne 2 ]] && return 0
|
||||
[[ "${result^^}" == "XX" ]] && return 0
|
||||
|
||||
COUNTRY="${result^^}"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
setCountry() {
|
||||
|
||||
[[ "${TZ,,}" == "asia/harbin" ]] && COUNTRY="CN"
|
||||
[[ "${TZ,,}" == "asia/beijing" ]] && COUNTRY="CN"
|
||||
[[ "${TZ,,}" == "asia/urumqi" ]] && COUNTRY="CN"
|
||||
[[ "${TZ,,}" == "asia/kashgar" ]] && COUNTRY="CN"
|
||||
[[ "${TZ,,}" == "asia/shanghai" ]] && COUNTRY="CN"
|
||||
[[ "${TZ,,}" == "asia/chongqing" ]] && COUNTRY="CN"
|
||||
|
||||
[ -z "$COUNTRY" ] && getCountry "https://api.ipapi.is" ".location.country_code"
|
||||
[ -z "$COUNTRY" ] && getCountry "https://ifconfig.co/json" ".country_iso"
|
||||
[ -z "$COUNTRY" ] && getCountry "https://api.ip2location.io" ".country_code"
|
||||
[ -z "$COUNTRY" ] && getCountry "https://ipinfo.io/json" ".country"
|
||||
[ -z "$COUNTRY" ] && getCountry "https://api.ipquery.io/?format=json" ".location.country_code"
|
||||
[ -z "$COUNTRY" ] && getCountry "https://api.myip.com" ".cc"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
addPackage() {
|
||||
local pkg=$1
|
||||
local desc=$2
|
||||
|
||||
if apt-mark showinstall | grep -qx "$pkg"; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
MSG="Installing $desc..."
|
||||
info "$MSG" && html "$MSG"
|
||||
|
||||
[ -z "$COUNTRY" ] && setCountry
|
||||
|
||||
if [[ "${COUNTRY^^}" == "CN" ]]; then
|
||||
sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources
|
||||
fi
|
||||
|
||||
DEBIAN_FRONTEND=noninteractive apt-get -qq update
|
||||
DEBIAN_FRONTEND=noninteractive apt-get -qq --no-install-recommends -y install "$pkg" > /dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
return 0
|
||||
|
||||
@@ -27,4 +27,18 @@ server {
|
||||
index index.html;
|
||||
|
||||
}
|
||||
|
||||
location /status {
|
||||
|
||||
proxy_http_version 1.1;
|
||||
|
||||
proxy_set_header Connection 'upgrade';
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
|
||||
proxy_buffering off;
|
||||
proxy_read_timeout 3600s;
|
||||
proxy_send_timeout 3600s;
|
||||
|
||||
proxy_pass http://127.0.0.1:8004/;
|
||||
}
|
||||
}
|
||||
|
||||
138
web/js/script.js
138
web/js/script.js
@@ -1,4 +1,5 @@
|
||||
var request;
|
||||
var booting = false;
|
||||
var interval = 1000;
|
||||
|
||||
function getInfo() {
|
||||
@@ -6,7 +7,6 @@ function getInfo() {
|
||||
var url = "msg.html";
|
||||
|
||||
try {
|
||||
|
||||
if (window.XMLHttpRequest) {
|
||||
request = new XMLHttpRequest();
|
||||
} else {
|
||||
@@ -18,22 +18,49 @@ function getInfo() {
|
||||
request.send();
|
||||
|
||||
} catch (e) {
|
||||
var err = "Error: " + e.message;
|
||||
console.log(err);
|
||||
setError(err);
|
||||
setError("Error: " + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
function getURL() {
|
||||
|
||||
var protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
|
||||
var path = window.location.pathname.replace(/[^/]*$/, '').replace(/\/$/, '');
|
||||
|
||||
return protocol + "//" + window.location.host + path;
|
||||
}
|
||||
|
||||
function processMsg(msg) {
|
||||
|
||||
if (msg.toLowerCase().indexOf("href=") !== -1) {
|
||||
var div = document.createElement("div");
|
||||
div.innerHTML = msg;
|
||||
var url = div.querySelector("a").href;
|
||||
setTimeout(() => {
|
||||
window.location.assign(url);
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
setInfo(msg);
|
||||
return true;
|
||||
}
|
||||
|
||||
function processInfo() {
|
||||
try {
|
||||
|
||||
if (request.readyState != 4) {
|
||||
return true;
|
||||
}
|
||||
|
||||
var msg = request.responseText;
|
||||
if (msg == null || msg.length == 0) {
|
||||
setInfo("Booting DSM instance", true);
|
||||
schedule();
|
||||
|
||||
if (booting) {
|
||||
schedule();
|
||||
return true;
|
||||
}
|
||||
|
||||
window.location.reload();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -43,20 +70,11 @@ function processInfo() {
|
||||
if (msg.toLowerCase().indexOf("<html>") !== -1) {
|
||||
notFound = true;
|
||||
} else {
|
||||
if (msg.toLowerCase().indexOf("href=") !== -1) {
|
||||
var div = document.createElement("div");
|
||||
div.innerHTML = msg;
|
||||
var url = div.querySelector("a").href;
|
||||
setTimeout(() => {
|
||||
window.location.assign(url);
|
||||
}, 3000);
|
||||
setInfo(msg);
|
||||
return true;
|
||||
} else {
|
||||
setInfo(msg);
|
||||
processMsg(msg);
|
||||
if (msg.toLowerCase().indexOf("href=") == -1) {
|
||||
schedule();
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,44 +85,60 @@ function processInfo() {
|
||||
}
|
||||
|
||||
setError("Error: Received statuscode " + request.status);
|
||||
schedule();
|
||||
return false;
|
||||
|
||||
} catch (e) {
|
||||
var err = "Error: " + e.message;
|
||||
console.log(err);
|
||||
setError(err);
|
||||
setError("Error: " + e.message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function setInfo(msg, loading, error) {
|
||||
function extractContent(s) {
|
||||
var span = document.createElement('span');
|
||||
span.innerHTML = s;
|
||||
return span.textContent || span.innerText;
|
||||
};
|
||||
|
||||
function setInfo(msg, loading, error) {
|
||||
try {
|
||||
|
||||
if (msg == null || msg.length == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var el = document.getElementById("spinner");
|
||||
if (msg.includes("Booting ")) {
|
||||
booting = true;
|
||||
}
|
||||
|
||||
var el = document.getElementById("info");
|
||||
|
||||
if (el.innerText == msg || el.innerHTML == msg) {
|
||||
return true;
|
||||
}
|
||||
|
||||
var spin = document.getElementById("spinner");
|
||||
|
||||
error = !!error;
|
||||
if (!error) {
|
||||
el.style.visibility = 'visible';
|
||||
spin.style.visibility = 'visible';
|
||||
} else {
|
||||
el.style.visibility = 'hidden';
|
||||
spin.style.visibility = 'hidden';
|
||||
}
|
||||
|
||||
var p = "<p class=\"loading\">";
|
||||
loading = !!loading;
|
||||
if (loading) {
|
||||
msg = "<p class=\"loading\">" + msg + "</p>";
|
||||
msg = p + msg + "</p>";
|
||||
}
|
||||
|
||||
el = document.getElementById("info");
|
||||
|
||||
if (el.innerHTML != msg) {
|
||||
el.innerHTML = msg;
|
||||
if (msg.includes(p)) {
|
||||
if (el.innerHTML.includes(p)) {
|
||||
el.getElementsByClassName('loading')[0].textContent = extractContent(msg);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
el.innerHTML = msg;
|
||||
return true;
|
||||
|
||||
} catch (e) {
|
||||
@@ -114,6 +148,7 @@ function setInfo(msg, loading, error) {
|
||||
}
|
||||
|
||||
function setError(text) {
|
||||
console.warn(text);
|
||||
return setInfo(text, false, true);
|
||||
}
|
||||
|
||||
@@ -123,8 +158,47 @@ function schedule() {
|
||||
|
||||
function reload() {
|
||||
setTimeout(() => {
|
||||
document.location.reload();
|
||||
window.location.reload();
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
function connect() {
|
||||
|
||||
var wsUrl = getURL() + "/status";
|
||||
var ws = new WebSocket(wsUrl);
|
||||
|
||||
ws.onmessage = function(e) {
|
||||
|
||||
var pos = e.data.indexOf(":");
|
||||
var cmd = e.data.substring(0, pos);
|
||||
var msg = e.data.substring(pos + 2);
|
||||
|
||||
switch (cmd) {
|
||||
case "s":
|
||||
processMsg(msg);
|
||||
break;
|
||||
case "e":
|
||||
setError(msg);
|
||||
break;
|
||||
default:
|
||||
console.warn("Unknown event: " + cmd);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
ws.onclose = function(e) {
|
||||
setTimeout(function() {
|
||||
connect();
|
||||
}, interval);
|
||||
};
|
||||
|
||||
ws.onerror = function(e) {
|
||||
ws.close();
|
||||
if (!booting) {
|
||||
window.location.reload();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
schedule();
|
||||
connect();
|
||||
|
||||
Reference in New Issue
Block a user