mirror of
https://github.com/vdsm/virtual-dsm.git
synced 2026-08-29 04:07:02 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9a2a6966b | ||
|
|
498248f43e | ||
|
|
8963228b1c | ||
|
|
827d6d9e10 | ||
|
|
1c577abef4 | ||
|
|
20fd1a975c | ||
|
|
770774680c | ||
|
|
0c75aa81fe | ||
|
|
36ec57ac15 | ||
|
|
9c488c52b9 | ||
|
|
2ba756260f | ||
|
|
b306012743 | ||
|
|
61b3ee7e01 | ||
|
|
245036f844 | ||
|
|
68cadfe035 | ||
|
|
96102832b1 | ||
|
|
211a56bf73 | ||
|
|
506fb7b114 | ||
|
|
bcc3393b7f | ||
|
|
45c0f91d6e | ||
|
|
a1910546a6 | ||
|
|
637ed0e3a6 | ||
|
|
954a6076ad | ||
|
|
415aaa3038 | ||
|
|
c87e45719b | ||
|
|
102945ab48 | ||
|
|
1c3d490303 | ||
|
|
73fa70f52f | ||
|
|
7b095702fc | ||
|
|
f82b2769d3 | ||
|
|
cafe619028 | ||
|
|
e94a0c4a52 | ||
|
|
d6a353df0e | ||
|
|
050812e996 | ||
|
|
75280e9aa9 | ||
|
|
8dcc8ccee1 | ||
|
|
ba866e9e75 | ||
|
|
06e7a8edb9 | ||
|
|
495c67734a | ||
|
|
1557735331 | ||
|
|
f45b308c78 | ||
|
|
d5821cd782 | ||
|
|
312816b18e | ||
|
|
f42a3f1662 | ||
|
|
558e5022ca | ||
|
|
e03822f26e | ||
|
|
7b90ca30ba | ||
|
|
22c01bf2f1 | ||
|
|
1f601db8b0 | ||
|
|
e7e2c35cb7 | ||
|
|
50089112c0 | ||
|
|
f9538ce53b | ||
|
|
49d25a13d0 | ||
|
|
7c9d2b1fd5 | ||
|
|
b68d243b6a | ||
|
|
79e85f9fd1 | ||
|
|
476048a4ee | ||
|
|
f983dc3e46 | ||
|
|
38f9c49200 | ||
|
|
7157717a85 | ||
|
|
031674424d | ||
|
|
28e6f8da78 | ||
|
|
d5802607b5 | ||
|
|
7168f2f843 | ||
|
|
819dd29025 | ||
|
|
48ef47b85b | ||
|
|
10c902bc9b | ||
|
|
d6de10efe1 | ||
|
|
6d1d9d92b4 | ||
|
|
799649e78c | ||
|
|
12060c72b2 | ||
|
|
6eadd1b953 | ||
|
|
342ca4da03 | ||
|
|
b0b6190560 | ||
|
|
075410e4cf | ||
|
|
2a388b434b | ||
|
|
280c4037f3 | ||
|
|
007bdc735d | ||
|
|
41fc0a6a10 | ||
|
|
7753e4ac74 | ||
|
|
f74bf35202 | ||
|
|
aa472c96c6 | ||
|
|
0dcb880800 | ||
|
|
8daa6973b7 | ||
|
|
3884befa56 | ||
|
|
85b83f8eb1 | ||
|
|
f395dfaa66 | ||
|
|
b3124075ea | ||
|
|
aed909bdee | ||
|
|
d4cf5778e2 | ||
|
|
48de7dd00b | ||
|
|
686e64da3f | ||
|
|
7517dc8b3e | ||
|
|
660fe715e4 | ||
|
|
04ea29ec77 | ||
|
|
02c891cb16 | ||
|
|
0b5d21357e | ||
|
|
e0545b37d7 | ||
|
|
4161c21082 | ||
|
|
48d9a1771d | ||
|
|
471cdbb338 | ||
|
|
e77bca202b | ||
|
|
2e6c01e934 | ||
|
|
302c991c0c | ||
|
|
a89007ee03 | ||
|
|
8a89149d58 | ||
|
|
5e8bbc2868 | ||
|
|
4e48920309 | ||
|
|
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 |
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "dsm",
|
||||
"service": "dsm",
|
||||
"forwardPorts": [5000],
|
||||
"dockerComposeFile": ".github/codespaces.yml"
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
services:
|
||||
dsm:
|
||||
container_name: dsm
|
||||
image: vdsm/virtual-dsm
|
||||
image: ghcr.io/vdsm/virtual-dsm
|
||||
environment:
|
||||
RAM_SIZE: "max"
|
||||
RAM_SIZE: "half"
|
||||
DISK_SIZE: "max"
|
||||
CPU_CORES: "max"
|
||||
devices:
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "Virtual DSM",
|
||||
"service": "dsm",
|
||||
"forwardPorts": [5000],
|
||||
"portsAttributes": {
|
||||
"5000": {
|
||||
"label": "Web",
|
||||
"onAutoForward": "notify"
|
||||
}
|
||||
},
|
||||
"otherPortsAttributes": {
|
||||
"onAutoForward": "ignore"
|
||||
},
|
||||
"dockerComposeFile": "codespaces.yml"
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
.dockerignore
|
||||
.devcontainer
|
||||
.git
|
||||
.github
|
||||
.gitignore
|
||||
|
||||
@@ -22,13 +22,13 @@ jobs:
|
||||
steps:
|
||||
-
|
||||
name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@v7
|
||||
with:
|
||||
fetch-depth: 0
|
||||
-
|
||||
name: Docker metadata
|
||||
id: meta
|
||||
uses: docker/metadata-action@v5
|
||||
uses: docker/metadata-action@v6
|
||||
with:
|
||||
context: git
|
||||
images: |
|
||||
@@ -43,23 +43,23 @@ jobs:
|
||||
DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index
|
||||
-
|
||||
name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
uses: docker/setup-buildx-action@v4
|
||||
-
|
||||
name: Login into Docker Hub
|
||||
uses: docker/login-action@v3
|
||||
uses: docker/login-action@v4
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
-
|
||||
name: Login to GitHub Container Registry
|
||||
uses: docker/login-action@v3
|
||||
uses: docker/login-action@v4
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
-
|
||||
name: Build Docker image
|
||||
uses: docker/build-push-action@v6
|
||||
uses: docker/build-push-action@v7
|
||||
with:
|
||||
context: .
|
||||
push: true
|
||||
|
||||
@@ -7,9 +7,10 @@ jobs:
|
||||
name: shellcheck
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
-
|
||||
name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
-
|
||||
name: Checkout
|
||||
uses: actions/checkout@v7
|
||||
- parallel:
|
||||
-
|
||||
name: Run ShellCheck
|
||||
uses: ludeeus/action-shellcheck@master
|
||||
@@ -22,3 +23,8 @@ jobs:
|
||||
dockerfile: Dockerfile
|
||||
ignore: DL3008,DL3003,DL3006,DL3013
|
||||
failure-threshold: warning
|
||||
-
|
||||
name: Validate JSON and YML files
|
||||
uses: GrantBirki/json-yaml-validate@v5.0.0
|
||||
with:
|
||||
yaml_exclude_regex: ".*\\kubernetes\\.yml$"
|
||||
|
||||
+12
-10
@@ -12,13 +12,15 @@ jobs:
|
||||
dockerHubDescription:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
-
|
||||
name: Docker Hub Description
|
||||
uses: peter-evans/dockerhub-description@v5
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
repository: ${{ secrets.DOCKERHUB_REPO }}
|
||||
short-description: ${{ github.event.repository.description }}
|
||||
readme-filepath: ./readme.md
|
||||
-
|
||||
name: Checkout repo
|
||||
uses: actions/checkout@v7
|
||||
-
|
||||
name: Docker Hub Description
|
||||
uses: peter-evans/dockerhub-description@v5
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
repository: ${{ secrets.DOCKERHUB_REPO }}
|
||||
short-description: ${{ github.event.repository.description }}
|
||||
readme-filepath: ./readme.md
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
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@v7
|
||||
- parallel:
|
||||
-
|
||||
name: Spelling
|
||||
uses: reviewdog/action-misspell@v1
|
||||
with:
|
||||
locale: "US"
|
||||
level: warning
|
||||
pattern: |
|
||||
*.md
|
||||
*.sh
|
||||
reporter: github-pr-review
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
-
|
||||
name: Hadolint
|
||||
uses: reviewdog/action-hadolint@v1
|
||||
with:
|
||||
level: warning
|
||||
fail_level: error
|
||||
reporter: github-pr-review
|
||||
hadolint_ignore: DL3008 DL3003 DL3006 DL3013
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
-
|
||||
name: YamlLint
|
||||
uses: reviewdog/action-yamllint@v1
|
||||
with:
|
||||
level: warning
|
||||
reporter: github-pr-review
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
-
|
||||
name: ActionLint
|
||||
uses: reviewdog/action-actionlint@v1
|
||||
with:
|
||||
level: warning
|
||||
reporter: github-pr-review
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
-
|
||||
name: Shellformat
|
||||
uses: reviewdog/action-shfmt@v1
|
||||
if: false
|
||||
with:
|
||||
level: warning
|
||||
fail_on_error: "true"
|
||||
shfmt_flags: "-i 2 -ci -bn"
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
-
|
||||
name: Shellcheck
|
||||
uses: reviewdog/action-shellcheck@v1
|
||||
with:
|
||||
level: warning
|
||||
fail_level: error
|
||||
reporter: github-pr-review
|
||||
shellcheck_flags: -x -e SC1091 -e SC2001 -e SC2034 -e SC2064 -e SC2317 -e SC2153 -e SC2028
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
+59
-49
@@ -1,64 +1,74 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
FROM qemux/qemu-host:2.05 AS builder
|
||||
|
||||
# FROM golang as builder
|
||||
# WORKDIR /
|
||||
# RUN git clone https://github.com/qemus/qemu-host.git
|
||||
# WORKDIR /qemu-host/src
|
||||
# RUN go mod download
|
||||
# RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /qemu-host.bin .
|
||||
|
||||
FROM debian:trixie-slim
|
||||
|
||||
ARG TARGETARCH
|
||||
ARG TARGETPLATFORM
|
||||
ARG VERSION_ARG="0.0"
|
||||
ARG VERSION_PASST="2026_06_11"
|
||||
|
||||
ARG DEBCONF_NOWARNINGS="yes"
|
||||
ARG DEBIAN_FRONTEND="noninteractive"
|
||||
ARG DEBCONF_NONINTERACTIVE_SEEN="true"
|
||||
|
||||
RUN set -eu && \
|
||||
apt-get update && \
|
||||
apt-get --no-install-recommends -y install \
|
||||
jq \
|
||||
tini \
|
||||
curl \
|
||||
wget \
|
||||
fdisk \
|
||||
unzip \
|
||||
nginx \
|
||||
procps \
|
||||
ethtool \
|
||||
python3 \
|
||||
python3-pip \
|
||||
python3-msgpack \
|
||||
python3-pysodium \
|
||||
xz-utils \
|
||||
iptables \
|
||||
iproute2 \
|
||||
dnsmasq \
|
||||
fakeroot \
|
||||
apt-utils \
|
||||
net-tools \
|
||||
e2fsprogs \
|
||||
qemu-utils \
|
||||
websocketd \
|
||||
iputils-ping \
|
||||
inotify-tools \
|
||||
ca-certificates \
|
||||
netcat-openbsd \
|
||||
qemu-system-x86 && \
|
||||
wget "https://github.com/qemus/passt/releases/download/v2025_09_19/passt_2025_09_19_${TARGETARCH}.deb" -O /tmp/passt.deb -q && \
|
||||
dpkg -i /tmp/passt.deb && \
|
||||
apt-get clean && \
|
||||
pip3 install --no-cache-dir --break-system-packages --root-user-action=ignore dissect.cstruct && \
|
||||
mkdir -p /etc/qemu && \
|
||||
echo "allow br0" > /etc/qemu/bridge.conf && \
|
||||
unlink /etc/nginx/sites-enabled/default && \
|
||||
sed -i 's/^worker_processes.*/worker_processes 1;/' /etc/nginx/nginx.conf && \
|
||||
echo "$VERSION_ARG" > /run/version && \
|
||||
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
||||
RUN <<EOF
|
||||
set -eu
|
||||
|
||||
apt-get update
|
||||
apt-get --no-install-recommends -y install \
|
||||
jq \
|
||||
tini \
|
||||
curl \
|
||||
wget \
|
||||
fdisk \
|
||||
unzip \
|
||||
nginx \
|
||||
procps \
|
||||
ipcalc \
|
||||
ethtool \
|
||||
python3 \
|
||||
python3-pip \
|
||||
python3-msgpack \
|
||||
python3-pysodium \
|
||||
xz-utils \
|
||||
iptables \
|
||||
iproute2 \
|
||||
dnsmasq \
|
||||
fakeroot \
|
||||
apt-utils \
|
||||
net-tools \
|
||||
e2fsprogs \
|
||||
qemu-utils \
|
||||
websocketd \
|
||||
iputils-ping \
|
||||
inotify-tools \
|
||||
ca-certificates \
|
||||
netcat-openbsd \
|
||||
qemu-system-x86
|
||||
|
||||
# Install Passt package
|
||||
wget "https://github.com/qemus/passt/releases/download/v${VERSION_PASST}/passt_${VERSION_PASST}_${TARGETARCH}.deb" -O /tmp/passt.deb -q --timeout=10
|
||||
dpkg -i /tmp/passt.deb
|
||||
|
||||
apt-get clean
|
||||
|
||||
# Install Python dependencies
|
||||
pip3 install --no-cache-dir --break-system-packages --root-user-action=ignore dissect.cstruct
|
||||
|
||||
# Configure QEMU
|
||||
mkdir -p /etc/qemu
|
||||
echo "allow br0" > /etc/qemu/bridge.conf
|
||||
|
||||
# Configure nginx
|
||||
unlink /etc/nginx/sites-enabled/default
|
||||
sed -i 's/^worker_processes.*/worker_processes 1;/' /etc/nginx/nginx.conf
|
||||
|
||||
# Set version file
|
||||
echo "$VERSION_ARG" > /etc/version
|
||||
|
||||
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
||||
EOF
|
||||
|
||||
COPY --chmod=755 ./src /run/
|
||||
COPY --chmod=755 ./web /var/www/
|
||||
|
||||
@@ -16,13 +16,17 @@ Virtual DSM in a Docker container.
|
||||
|
||||
## Features ✨
|
||||
|
||||
- Multiple disks
|
||||
- KVM acceleration
|
||||
- Upgrades supported
|
||||
|
||||
- Runs Virtual DSM inside a Docker container
|
||||
- Automatic download of the installation files
|
||||
- Web-based access to the DSM desktop
|
||||
- Near-native performance with KVM acceleration
|
||||
- Customizable CPU, memory, and storage allocation
|
||||
- Supports multiple disks and physical disk passthrough
|
||||
- Supports bridge, user-mode, macvlan, and macvtap networking
|
||||
|
||||
## Usage 🐳
|
||||
|
||||
##### Via Docker Compose:
|
||||
##### Docker Compose:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
@@ -44,22 +48,31 @@ services:
|
||||
stop_grace_period: 2m
|
||||
```
|
||||
|
||||
##### Via Docker CLI:
|
||||
##### 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:
|
||||
##### Kubernetes:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://raw.githubusercontent.com/vdsm/virtual-dsm/refs/heads/master/kubernetes.yml
|
||||
```
|
||||
|
||||
##### Via Github Codespaces:
|
||||
##### GitHub Codespaces:
|
||||
|
||||
[](https://codespaces.new/vdsm/virtual-dsm)
|
||||
|
||||
## Requirements ⚙️
|
||||
|
||||
- A Linux host with KVM support, or Docker Desktop / Podman on Windows 11 with nested virtualization enabled.
|
||||
- At least 2 GB of RAM available.
|
||||
- At least 32 GB of free disk space.
|
||||
|
||||
> [!NOTE]
|
||||
> Docker Desktop on macOS and Windows 10 do not currently provide the required KVM support for this image.
|
||||
|
||||
## FAQ 💬
|
||||
|
||||
### How do I use it?
|
||||
@@ -70,7 +83,7 @@ kubectl apply -f https://raw.githubusercontent.com/vdsm/virtual-dsm/refs/heads/m
|
||||
|
||||
- Wait until DSM finishes its installation
|
||||
|
||||
- Choose an username and password, and you will be taken to the desktop.
|
||||
- Choose a username and password, and you will be taken to the desktop.
|
||||
|
||||
Enjoy your brand new NAS, and don't forget to star this repo!
|
||||
|
||||
@@ -95,7 +108,7 @@ kubectl apply -f https://raw.githubusercontent.com/vdsm/virtual-dsm/refs/heads/m
|
||||
```
|
||||
|
||||
> [!TIP]
|
||||
> This can also be used to resize the existing disk to a larger capacity without any data loss.
|
||||
> This can also be used to resize an existing disk to a larger capacity without any data loss.
|
||||
|
||||
### How do I add multiple disks?
|
||||
|
||||
@@ -110,9 +123,9 @@ kubectl apply -f https://raw.githubusercontent.com/vdsm/virtual-dsm/refs/heads/m
|
||||
- ./example3:/storage3
|
||||
```
|
||||
|
||||
### How do I pass-through a disk?
|
||||
### How do I pass through a disk?
|
||||
|
||||
It is possible to pass-through disk devices or partitions directly by adding them to your compose file in this way:
|
||||
You can pass through disk devices or partitions directly by adding them to your compose file in this way:
|
||||
|
||||
```yaml
|
||||
devices:
|
||||
@@ -124,7 +137,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:
|
||||
|
||||
@@ -158,7 +171,7 @@ kubectl apply -f https://raw.githubusercontent.com/vdsm/virtual-dsm/refs/heads/m
|
||||
|
||||
- you enabled "nested virtualization" if you are running the container inside a virtual machine.
|
||||
|
||||
- you are not using a cloud provider, as most of them do not allow nested virtualization for their VPS's.
|
||||
- you are not using a cloud provider, as most of them do not allow nested virtualization for their VPSs.
|
||||
|
||||
If you did not receive any error from `kvm-ok` but the container still complains about a missing KVM device, it could help to add `privileged: true` to your compose file (or `sudo` to your `docker` command) to rule out any permission issue.
|
||||
|
||||
@@ -214,9 +227,9 @@ kubectl apply -f https://raw.githubusercontent.com/vdsm/virtual-dsm/refs/heads/m
|
||||
- 'c *:* rwm'
|
||||
```
|
||||
|
||||
### How do I pass-through the GPU?
|
||||
### How do I pass through the GPU?
|
||||
|
||||
To pass-through your Intel GPU, add the following lines to your compose file:
|
||||
To pass through your Intel GPU, add the following lines to your compose file:
|
||||
|
||||
```yaml
|
||||
environment:
|
||||
@@ -254,12 +267,12 @@ kubectl apply -f https://raw.githubusercontent.com/vdsm/virtual-dsm/refs/heads/m
|
||||
|
||||
### Is this project legal?
|
||||
|
||||
Yes, this project contains only open-source code and does not distribute any copyrighted material. Neither does it try to circumvent any copyright protection measures. So under all applicable laws, this project will be considered legal.
|
||||
Yes, this project contains only open-source code and does not distribute any material owned by Synology. Neither does it try to circumvent any copyright protection measures.
|
||||
|
||||
However, by installing Synology's Virtual DSM, you must accept their end-user license agreement, which does not permit installation on non-Synology hardware. So only run this container on an official Synology NAS, as any other use will be a violation of their terms and conditions.
|
||||
|
||||
## Stars 🌟
|
||||
[](https://starchart.cc/vdsm/virtual-dsm)
|
||||
[](https://github.com/vdsm/virtual-dsm/stargazers)
|
||||
|
||||
## Disclaimer ⚖️
|
||||
|
||||
|
||||
+8
-5
@@ -1,24 +1,27 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
cd /run
|
||||
. utils.sh # Load functions
|
||||
|
||||
: "${DHCP:="N"}"
|
||||
: "${NETWORK:="Y"}"
|
||||
|
||||
[ -f "/run/shm/qemu.end" ] && echo "QEMU is shutting down.." && exit 1
|
||||
[ ! -s "/run/shm/qemu.pid" ] && echo "QEMU is not running yet.." && exit 0
|
||||
[[ "$NETWORK" == [Nn]* ]] && echo "Networking is disabled.." && exit 0
|
||||
[ -f "/run/shm/qemu.end" ] && echo "QEMU is shutting down..." && exit 1
|
||||
[ ! -s "/run/shm/qemu.pid" ] && echo "QEMU is not running yet..." && exit 0
|
||||
disabled "$NETWORK" && echo "Networking is disabled." && exit 0
|
||||
|
||||
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
|
||||
[ ! -s "$file" ] && echo "DSM has not enabled networking yet..." && exit 1
|
||||
|
||||
location=$(<"$file")
|
||||
|
||||
if ! curl -m 20 -ILfSs "http://$location/" > /dev/null; then
|
||||
|
||||
if [[ "$DHCP" == [Yy1]* ]]; then
|
||||
if enabled "$DHCP"; then
|
||||
ip=$(<"$address")
|
||||
echo "Failed to reach DSM at http://$location"
|
||||
else
|
||||
|
||||
+1
-28
@@ -3,6 +3,7 @@ set -Eeuo pipefail
|
||||
|
||||
DEF_OPTS="-nodefaults -boot strict=on"
|
||||
RAM_OPTS=$(echo "-m ${RAM_SIZE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
|
||||
MON_OPTS="-name $PROCESS,process=$PROCESS,debug-threads=on -pidfile $QEMU_PID"
|
||||
CPU_OPTS="-cpu $CPU_FLAGS -smp $CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1"
|
||||
MAC_OPTS="-machine type=q35,smm=off,usb=off,vmport=off,dump-guest-core=off,hpet=off${KVM_OPTS}"
|
||||
DEV_OPTS="-device virtio-balloon-pci,id=balloon0,bus=pcie.0,addr=0x4"
|
||||
@@ -12,32 +13,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
|
||||
|
||||
+346
-223
@@ -8,50 +8,56 @@ set -Eeuo pipefail
|
||||
: "${DISK_TYPE:=""}" # Device type to be used, "sata", "nvme", "blk" or "scsi"
|
||||
: "${DISK_FLAGS:=""}" # Specifies the options for use with the qcow2 disk format
|
||||
: "${DISK_CACHE:="none"}" # Caching mode, can be set to 'writeback' for better performance
|
||||
: "${DISK_DISCARD:="on"}" # Controls whether unmap (TRIM) commands are passed to the host.
|
||||
: "${DISK_DISCARD:="unmap"}" # Controls whether unmap (TRIM) commands are passed to the host.
|
||||
: "${DISK_ROTATION:="1"}" # Rotation rate, set to 1 for SSD storage and increase for HDD
|
||||
|
||||
# Sanitize all variables
|
||||
DISK_IO=$(strip "$DISK_IO")
|
||||
DISK_FMT=$(strip "$DISK_FMT")
|
||||
DISK_TYPE=$(strip "$DISK_TYPE")
|
||||
DISK_FLAGS=$(strip "$DISK_FLAGS")
|
||||
DISK_CACHE=$(strip "$DISK_CACHE")
|
||||
DISK_DISCARD=$(strip "$DISK_DISCARD")
|
||||
DISK_ROTATION=$(strip "$DISK_ROTATION")
|
||||
|
||||
BOOT="$STORAGE/$BASE.boot.img"
|
||||
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)
|
||||
echo "qcow2"
|
||||
;;
|
||||
raw)
|
||||
echo "img"
|
||||
;;
|
||||
*)
|
||||
error "Unrecognized disk format: $DISK_FMT" && exit 78
|
||||
;;
|
||||
qcow2) echo "qcow2" ;;
|
||||
raw) echo "img" ;;
|
||||
*) error "Unrecognized disk format: $DISK_FMT" && exit 78 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
ext2fmt() {
|
||||
local DISK_EXT=$1
|
||||
local DISK_EXT="$1"
|
||||
|
||||
case "${DISK_EXT,,}" in
|
||||
qcow2)
|
||||
echo "qcow2"
|
||||
;;
|
||||
img)
|
||||
echo "raw"
|
||||
;;
|
||||
*)
|
||||
error "Unrecognized file extension: .$DISK_EXT" && exit 78
|
||||
;;
|
||||
qcow2) echo "qcow2" ;;
|
||||
img) echo "raw" ;;
|
||||
*) error "Unrecognized file extension: .$DISK_EXT" && exit 78 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
getSize() {
|
||||
local DISK_FILE=$1
|
||||
local DISK_EXT DISK_FMT
|
||||
|
||||
local DISK_FILE="$1"
|
||||
local DISK_EXT DISK_FMT size
|
||||
|
||||
DISK_EXT=$(echo "${DISK_FILE//*./}" | sed 's/^.*\.//')
|
||||
DISK_FMT=$(ext2fmt "$DISK_EXT")
|
||||
@@ -60,17 +66,25 @@ getSize() {
|
||||
raw)
|
||||
stat -c%s "$DISK_FILE"
|
||||
;;
|
||||
|
||||
qcow2)
|
||||
qemu-img info "$DISK_FILE" -f "$DISK_FMT" | grep '^virtual size: ' | sed 's/.*(\(.*\) bytes)/\1/'
|
||||
size=$(qemu-img info --output=json -f "$DISK_FMT" "$DISK_FILE" | jq -r '."virtual-size" // empty')
|
||||
if [[ ! "$size" =~ ^[0-9]+$ ]]; then
|
||||
error "Failed to determine virtual size of $DISK_FILE"
|
||||
exit 78
|
||||
fi
|
||||
echo "$size"
|
||||
;;
|
||||
|
||||
*)
|
||||
error "Unrecognized disk format: $DISK_FMT" && exit 78
|
||||
error "Unrecognized disk format: $DISK_FMT"
|
||||
exit 78
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
isCow() {
|
||||
local FS=$1
|
||||
local FS="$1"
|
||||
|
||||
if [[ "${FS,,}" == "btrfs" ]]; then
|
||||
return 0
|
||||
@@ -80,7 +94,7 @@ isCow() {
|
||||
}
|
||||
|
||||
supportsDirect() {
|
||||
local FS=$1
|
||||
local FS="$1"
|
||||
|
||||
if [[ "${FS,,}" == "ecryptfs" || "${FS,,}" == "tmpfs" ]]; then
|
||||
return 1
|
||||
@@ -89,20 +103,96 @@ supportsDirect() {
|
||||
return 0
|
||||
}
|
||||
|
||||
allocateRaw() {
|
||||
|
||||
local DISK_FILE="$1"
|
||||
local DATA_SIZE="$2"
|
||||
|
||||
if disabled "$ALLOCATE"; then
|
||||
truncate -s "$DATA_SIZE" "$DISK_FILE"
|
||||
return $?
|
||||
fi
|
||||
|
||||
fallocate -l "$DATA_SIZE" "$DISK_FILE" &>/dev/null && return 0
|
||||
fallocate -l -x "$DATA_SIZE" "$DISK_FILE" && return 0
|
||||
truncate -s "$DATA_SIZE" "$DISK_FILE" || return 1
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
getDiskOptions() {
|
||||
|
||||
local FS="$1"
|
||||
local DISK_FMT="$2"
|
||||
local DISK_PARAM="$DISK_ALLOC"
|
||||
|
||||
isCow "$FS" && DISK_PARAM+=",nocow=on"
|
||||
|
||||
if [[ "${DISK_FMT,,}" != "raw" ]]; then
|
||||
[ -n "$DISK_FLAGS" ] && DISK_PARAM+=",$DISK_FLAGS"
|
||||
fi
|
||||
|
||||
echo "$DISK_PARAM"
|
||||
return 0
|
||||
}
|
||||
|
||||
normalizeSize() {
|
||||
|
||||
local DISK_SPACE="$1"
|
||||
local DISK_DESC="$2"
|
||||
local DIR="$3"
|
||||
local SPACE FREE GB DATA_SIZE
|
||||
|
||||
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="256G"
|
||||
[ -z "${SPACE//[0-9. ]}" ] && SPACE="${SPACE}G"
|
||||
SPACE=$(echo "${SPACE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
|
||||
|
||||
if ! numfmt --from=iec "$SPACE" &>/dev/null; then
|
||||
error "Invalid value for ${DISK_DESC^^}_SIZE: $DISK_SPACE" && exit 73
|
||||
fi
|
||||
|
||||
DATA_SIZE=$(numfmt --from=iec "$SPACE")
|
||||
|
||||
if (( DATA_SIZE < 6442450944 )); then
|
||||
error "Please increase the ${DISK_DESC^^}_SIZE variable to at least 6 GB." && exit 73
|
||||
fi
|
||||
|
||||
echo "$SPACE"
|
||||
return 0
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
rm -f "$DISK_FILE"
|
||||
|
||||
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
||||
|
||||
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
||||
if ! disabled "$ALLOCATE"; then
|
||||
|
||||
# Check free diskspace
|
||||
DIR=$(dirname "$DISK_FILE")
|
||||
@@ -113,6 +203,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..."
|
||||
@@ -130,33 +221,15 @@ createDisk() {
|
||||
{ chattr +C "$DISK_FILE"; } || :
|
||||
fi
|
||||
|
||||
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
||||
|
||||
# Create an empty file
|
||||
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
||||
rm -f "$DISK_FILE"
|
||||
error "$FAIL" && exit 77
|
||||
fi
|
||||
|
||||
else
|
||||
|
||||
# Create an empty file
|
||||
if ! fallocate -l "$DATA_SIZE" "$DISK_FILE" &>/dev/null; then
|
||||
if ! fallocate -l -x "$DATA_SIZE" "$DISK_FILE"; then
|
||||
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
||||
rm -f "$DISK_FILE"
|
||||
error "$FAIL" && exit 77
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if ! allocateRaw "$DISK_FILE" "$DATA_SIZE"; then
|
||||
rm -f "$DISK_FILE"
|
||||
error "$FAIL" && exit 77
|
||||
fi
|
||||
;;
|
||||
qcow2)
|
||||
|
||||
local DISK_PARAM="$DISK_ALLOC"
|
||||
isCow "$FS" && DISK_PARAM+=",nocow=on"
|
||||
[ -n "$DISK_FLAGS" ] && DISK_PARAM+=",$DISK_FLAGS"
|
||||
local DISK_PARAM
|
||||
DISK_PARAM=$(getDiskOptions "$FS" "$DISK_FMT")
|
||||
|
||||
if ! qemu-img create -f "$DISK_FMT" -o "$DISK_PARAM" -- "$DISK_FILE" "$DATA_SIZE" ; then
|
||||
rm -f "$DISK_FILE"
|
||||
@@ -177,19 +250,19 @@ 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")
|
||||
CUR_SIZE=$(getSize "$DISK_FILE") || exit 71
|
||||
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
|
||||
if ! disabled "$ALLOCATE"; then
|
||||
|
||||
# Check free diskspace
|
||||
DIR=$(dirname "$DISK_FILE")
|
||||
@@ -200,6 +273,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")
|
||||
@@ -211,24 +285,8 @@ resizeDisk() {
|
||||
case "${DISK_FMT,,}" in
|
||||
raw)
|
||||
|
||||
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
||||
|
||||
# Resize file by changing its length
|
||||
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
||||
error "$FAIL" && exit 75
|
||||
fi
|
||||
|
||||
else
|
||||
|
||||
# Resize file by allocating more space
|
||||
if ! fallocate -l "$DATA_SIZE" "$DISK_FILE" &>/dev/null; then
|
||||
if ! fallocate -l -x "$DATA_SIZE" "$DISK_FILE"; then
|
||||
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
||||
error "$FAIL" && exit 75
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if ! allocateRaw "$DISK_FILE" "$DATA_SIZE"; then
|
||||
error "$FAIL" && exit 75
|
||||
fi
|
||||
;;
|
||||
qcow2)
|
||||
@@ -245,27 +303,29 @@ 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
|
||||
[ ! -f "$SOURCE_FILE" ] && error "Conversion failed, source file $SOURCE_FILE does not exist?" && exit 79
|
||||
|
||||
local TMP_FILE="$DISK_BASE.tmp"
|
||||
rm -f "$TMP_FILE"
|
||||
|
||||
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
||||
local DIR FA
|
||||
DIR=$(dirname "$TMP_FILE")
|
||||
|
||||
local DIR CUR_SIZE SPACE GB
|
||||
if ! disabled "$ALLOCATE"; then
|
||||
|
||||
local CUR_SIZE SPACE GB
|
||||
|
||||
# Check free diskspace
|
||||
DIR=$(dirname "$TMP_FILE")
|
||||
CUR_SIZE=$(getSize "$SOURCE_FILE")
|
||||
CUR_SIZE=$(getSize "$SOURCE_FILE") || exit 79
|
||||
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
||||
|
||||
if (( CUR_SIZE > SPACE )); then
|
||||
@@ -273,6 +333,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"
|
||||
@@ -280,14 +341,13 @@ convertDisk() {
|
||||
info "$msg, please wait until completed..."
|
||||
|
||||
local CONV_FLAGS="-p"
|
||||
local DISK_PARAM="$DISK_ALLOC"
|
||||
isCow "$FS" && DISK_PARAM+=",nocow=on"
|
||||
local DISK_PARAM
|
||||
DISK_PARAM=$(getDiskOptions "$FS" "$DST_FMT")
|
||||
|
||||
if [[ "$DST_FMT" != "raw" ]]; then
|
||||
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
||||
if disabled "$ALLOCATE"; then
|
||||
CONV_FLAGS+=" -c"
|
||||
fi
|
||||
[ -n "$DISK_FLAGS" ] && DISK_PARAM+=",$DISK_FLAGS"
|
||||
fi
|
||||
|
||||
# shellcheck disable=SC2086
|
||||
@@ -297,7 +357,7 @@ convertDisk() {
|
||||
fi
|
||||
|
||||
if [[ "$DST_FMT" == "raw" ]]; then
|
||||
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
||||
if ! disabled "$ALLOCATE"; then
|
||||
# Work around qemu-img bug
|
||||
CUR_SIZE=$(stat -c%s "$TMP_FILE")
|
||||
if ! fallocate -l "$CUR_SIZE" "$TMP_FILE" &>/dev/null; then
|
||||
@@ -308,8 +368,17 @@ convertDisk() {
|
||||
fi
|
||||
fi
|
||||
|
||||
rm -f "$SOURCE_FILE"
|
||||
mv "$TMP_FILE" "$DST_FILE"
|
||||
if ! rm -f "$SOURCE_FILE"; then
|
||||
rm -f "$TMP_FILE"
|
||||
error "Failed to remove old $DISK_DESC image $SOURCE_FILE."
|
||||
exit 79
|
||||
fi
|
||||
|
||||
if ! mv "$TMP_FILE" "$DST_FILE"; then
|
||||
rm -f "$TMP_FILE"
|
||||
error "Failed to move converted $DISK_DESC image to $DST_FILE."
|
||||
exit 79
|
||||
fi
|
||||
|
||||
if isCow "$FS"; then
|
||||
FA=$(lsattr "$DST_FILE")
|
||||
@@ -327,31 +396,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"* && "${ENGINE,,}" == "docker" ]]; 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 +430,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=""
|
||||
@@ -413,18 +482,32 @@ createDevice () {
|
||||
return 0
|
||||
}
|
||||
|
||||
finishDisks () {
|
||||
|
||||
case "${DISK_TYPE,,}" in
|
||||
"blk" | "scsi" | "virtio-blk" | "virtio-scsi" )
|
||||
DISK_OPTS+=" -object iothread,id=io2" ;;
|
||||
esac
|
||||
|
||||
if ! enabled "$DISK_DISABLE"; then
|
||||
html "Initialized disks successfully..."
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
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 GB 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 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,31 +515,9 @@ addDisk () {
|
||||
DIR=$(dirname "$DISK_FILE")
|
||||
[ ! -d "$DIR" ] && return 0
|
||||
|
||||
if [[ "${DISK_SPACE,,}" == "max" ]]; then
|
||||
|
||||
local SPARE=536870912
|
||||
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
||||
(( SPACE < SPARE )) && SPACE="$SPARE" || SPACE=$((SPACE-SPARE))
|
||||
GB=$(( SPACE/1073741825 ))
|
||||
DISK_SPACE="${GB}G"
|
||||
|
||||
fi
|
||||
|
||||
SPACE="${DISK_SPACE// /}"
|
||||
[ -z "$SPACE" ] && SPACE="16G"
|
||||
[ -z "${SPACE//[0-9. ]}" ] && SPACE="${SPACE}G"
|
||||
SPACE=$(echo "${SPACE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
|
||||
|
||||
if ! numfmt --from=iec "$SPACE" &>/dev/null; then
|
||||
error "Invalid value for ${DISK_DESC^^}_SIZE: $DISK_SPACE" && exit 73
|
||||
fi
|
||||
|
||||
SPACE=$(normalizeSize "$DISK_SPACE" "$DISK_DESC" "$DIR")
|
||||
DATA_SIZE=$(numfmt --from=iec "$SPACE")
|
||||
|
||||
if (( DATA_SIZE < 6442450944 )); then
|
||||
error "Please increase ${DISK_DESC^^}_SIZE to at least 6 GB." && exit 73
|
||||
fi
|
||||
|
||||
FS=$(stat -f -c %T "$DIR")
|
||||
checkFS "$FS" "$DISK_FILE" "$DISK_DESC" || exit $?
|
||||
|
||||
@@ -465,7 +526,7 @@ addDisk () {
|
||||
DISK_CACHE="writeback"
|
||||
fi
|
||||
|
||||
if ! [ -s "$DISK_FILE" ] ; then
|
||||
if [ ! -s "$DISK_FILE" ] ; then
|
||||
|
||||
if [[ "${DISK_FMT,,}" != "raw" ]]; then
|
||||
PREV_FMT="raw"
|
||||
@@ -478,14 +539,26 @@ 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
|
||||
|
||||
CUR_SIZE=$(getSize "$DISK_FILE")
|
||||
CUR_SIZE=$(getSize "$DISK_FILE") || exit 71
|
||||
|
||||
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
|
||||
@@ -494,6 +567,40 @@ addDisk () {
|
||||
|
||||
fi
|
||||
|
||||
if [ -f "$DISK_FILE" ] && disabled "$ALLOCATE"; then
|
||||
|
||||
CUR_SIZE=$(getSize "$DISK_FILE") || exit 73
|
||||
USED=$(du -sB 1 "$DISK_FILE" | cut -f1)
|
||||
FREE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
||||
LEFT=$(( CUR_SIZE - USED - FREE ))
|
||||
(( LEFT < 0 )) && LEFT=0
|
||||
|
||||
if (( LEFT > 0 )); then
|
||||
|
||||
local GB
|
||||
GB=$(formatBytes "$FREE")
|
||||
LEFT=$(formatBytes "$LEFT")
|
||||
CUR_SIZE=$(formatBytes "$CUR_SIZE")
|
||||
msg="The virtual size of the ${DISK_DESC,,} is $CUR_SIZE"
|
||||
|
||||
if [ -n "$USED" ] && [[ "$USED" != "0" ]]; then
|
||||
USED=$(formatBytes "$USED")
|
||||
msg+=" (of which $USED is used)"
|
||||
fi
|
||||
|
||||
info "$msg, but there is only $GB of free space remaining in $DIR now."
|
||||
info "Please consider making at least $LEFT more space available in $DIR for future expansions."
|
||||
|
||||
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
|
||||
@@ -501,27 +608,38 @@ 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
|
||||
|
||||
local sectors=""
|
||||
local result logical physical
|
||||
result=$(fdisk -l "$DISK_DEV" | grep -m 1 -o "(logical/physical): .*" | cut -c 21-)
|
||||
logical="${result%% *}"
|
||||
physical=$(echo "$result" | grep -m 1 -o "/ .*" | cut -c 3-)
|
||||
physical="${physical%% *}"
|
||||
local dev_type=""
|
||||
dev_type=$(lsblk -no TYPE "$DISK_DEV" 2>/dev/null | head -n1)
|
||||
|
||||
if [ -n "$physical" ]; then
|
||||
if [[ "$physical" != "512" ]]; then
|
||||
# Only detect and apply sector sizes for partitions, not whole disks
|
||||
# Whole disk passthrough with explicit sector sizes causes DSM not to recognize the disk
|
||||
if [[ "$dev_type" == "part" ]]; then
|
||||
local result=""
|
||||
local logical=""
|
||||
local physical=""
|
||||
|
||||
result=$(fdisk -l "$DISK_DEV" 2>/dev/null | grep -m 1 -o "(logical/physical): .*" | cut -c 21- || true)
|
||||
|
||||
if [ -n "$result" ]; then
|
||||
logical="${result%% *}"
|
||||
physical=$(echo "$result" | grep -m 1 -o "/ .*" | cut -c 3- || true)
|
||||
physical="${physical%% *}"
|
||||
fi
|
||||
|
||||
if [ -z "$logical" ] || [ -z "$physical" ]; then
|
||||
warn "Failed to determine the sector size for $DISK_DEV"
|
||||
elif [[ "$physical" != "512" ]]; then
|
||||
sectors=",logical_block_size=$logical,physical_block_size=$physical"
|
||||
fi
|
||||
else
|
||||
warn "Failed to determine the sector size for $DISK_DEV"
|
||||
fi
|
||||
|
||||
DISK_OPTS+=$(createDevice "$DISK_DEV" "$DISK_TYPE" "$DISK_INDEX" "$DISK_ADDRESS" "raw" "$DISK_IO" "$DISK_CACHE" "" "$sectors")
|
||||
@@ -529,24 +647,61 @@ addDevice () {
|
||||
return 0
|
||||
}
|
||||
|
||||
msg="Initializing disks..."
|
||||
html "$msg"
|
||||
[[ "$DEBUG" == [Yy1]* ]] && echo "$msg"
|
||||
|
||||
[ -z "${DISK_OPTS:-}" ] && DISK_OPTS=""
|
||||
[ -z "${DISK_TYPE:-}" ] && DISK_TYPE="scsi"
|
||||
[ -z "${DISK_NAME:-}" ] && DISK_NAME="data"
|
||||
[ -z "${DISK_DISABLE:-}" ] && DISK_DISABLE=""
|
||||
|
||||
if ! enabled "$DISK_DISABLE"; then
|
||||
msg="Initializing disks..."
|
||||
html "$msg"
|
||||
enabled "$DEBUG" && echo "$msg"
|
||||
fi
|
||||
|
||||
if [[ "${DISK_IO,,}" == "native" && "${DISK_CACHE,,}" != "none" && "${DISK_CACHE,,}" != "directsync" ]]; then
|
||||
warn "DISK_IO=native requires direct I/O caching, using DISK_IO=threads with DISK_CACHE=$DISK_CACHE."
|
||||
DISK_IO="threads"
|
||||
fi
|
||||
|
||||
case "${DISK_DISCARD,,}" in
|
||||
"y" | "yes" | "true" | "1" | "on" | "unmap" )
|
||||
DISK_DISCARD="unmap" ;;
|
||||
|
||||
"n" | "no" | "false" | "0" | "off" | "ignore" )
|
||||
DISK_DISCARD="ignore" ;;
|
||||
|
||||
* )
|
||||
warn "Invalid DISK_DISCARD value '$DISK_DISCARD', using 'unmap'."
|
||||
DISK_DISCARD="unmap" ;;
|
||||
esac
|
||||
|
||||
if [[ ! "$DISK_ROTATION" =~ ^[0-9]+$ ]]; then
|
||||
warn "Invalid DISK_ROTATION value '$DISK_ROTATION', using 1."
|
||||
DISK_ROTATION="1"
|
||||
fi
|
||||
|
||||
DISK_FMT="${DISK_FMT,,}"
|
||||
|
||||
case "$DISK_FMT" in
|
||||
"raw" | "qcow2" ) ;;
|
||||
* ) error "Invalid DISK_FMT specified, value \"$DISK_FMT\" is not recognized!" && exit 78 ;;
|
||||
esac
|
||||
|
||||
case "${DISK_TYPE,,}" in
|
||||
"ide" | "sata" | "nvme" | "usb" | "scsi" | "blk" | "auto" | "none" ) ;;
|
||||
"ide" | "sata" | "nvme" | "usb" | "scsi" | "blk" | "virtio-blk" | "virtio-scsi" | "auto" | "none" ) ;;
|
||||
* ) error "Invalid DISK_TYPE specified, value \"$DISK_TYPE\" is not recognized!" && exit 80 ;;
|
||||
esac
|
||||
|
||||
if [[ "$DISK_FLAGS" =~ [[:space:]] ]]; then
|
||||
error "Invalid DISK_FLAGS value '$DISK_FLAGS', spaces are not allowed."
|
||||
exit 78
|
||||
fi
|
||||
|
||||
if [ -z "$ALLOCATE" ]; then
|
||||
ALLOCATE="N"
|
||||
fi
|
||||
|
||||
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
||||
if disabled "$ALLOCATE"; then
|
||||
DISK_STYLE="growable"
|
||||
DISK_ALLOC="preallocation=off"
|
||||
else
|
||||
@@ -557,38 +712,14 @@ fi
|
||||
DISK_OPTS+=$(createDevice "$BOOT" "$DISK_TYPE" "1" "0xa" "raw" "$DISK_IO" "$DISK_CACHE" "" "")
|
||||
DISK_OPTS+=$(createDevice "$SYSTEM" "$DISK_TYPE" "2" "0xb" "raw" "$DISK_IO" "$DISK_CACHE" "" "")
|
||||
|
||||
if enabled "$DISK_DISABLE"; then
|
||||
finishDisks
|
||||
return 0
|
||||
fi
|
||||
|
||||
DISK1_FILE="$STORAGE/${DISK_NAME}"
|
||||
if [ ! -f "$DISK1_FILE.img" ] && [ -f "$STORAGE/data${DISK_SIZE}.img" ]; then
|
||||
# Fallback for legacy installs
|
||||
mv "$STORAGE/data${DISK_SIZE}.img" "$DISK1_FILE.img"
|
||||
fi
|
||||
|
||||
DISK2_FILE="/storage2/${DISK_NAME}2"
|
||||
if [ ! -f "$DISK2_FILE.img" ]; then
|
||||
# Fallback for legacy installs
|
||||
FALLBACK="/storage2/data.img"
|
||||
if [[ -f "$DISK1_FILE.img" && -f "$FALLBACK" ]]; then
|
||||
SIZE1=$(stat -c%s "$FALLBACK")
|
||||
SIZE2=$(stat -c%s "$DISK1_FILE.img")
|
||||
if [[ SIZE1 -ne SIZE2 ]]; then
|
||||
mv "$FALLBACK" "$DISK2_FILE.img"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
DISK3_FILE="/storage3/${DISK_NAME}3"
|
||||
if [ ! -f "$DISK3_FILE.img" ]; then
|
||||
# Fallback for legacy installs
|
||||
FALLBACK="/storage3/data.img"
|
||||
if [[ -f "$DISK1_FILE.img" && -f "$FALLBACK" ]]; then
|
||||
SIZE1=$(stat -c%s "$FALLBACK")
|
||||
SIZE2=$(stat -c%s "$DISK1_FILE.img")
|
||||
if [[ SIZE1 -ne SIZE2 ]]; then
|
||||
mv "$FALLBACK" "$DISK3_FILE.img"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
DISK4_FILE="/storage4/${DISK_NAME}4"
|
||||
|
||||
: "${DISK2_SIZE:=""}"
|
||||
@@ -611,31 +742,23 @@ DISK4_FILE="/storage4/${DISK_NAME}4"
|
||||
[ -z "$DEVICE3" ] && [ -b "/dev/disk3" ] && DEVICE3="/dev/disk3"
|
||||
[ -z "$DEVICE4" ] && [ -b "/dev/disk4" ] && DEVICE4="/dev/disk4"
|
||||
|
||||
if [ -n "$DEVICE" ]; then
|
||||
addDevice "$DEVICE" "$DISK_TYPE" "3" "0xc" || exit $?
|
||||
else
|
||||
addDisk "$DISK1_FILE" "$DISK_TYPE" "disk" "$DISK_SIZE" "3" "0xc" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" || exit $?
|
||||
fi
|
||||
DISK_FILES=( "$DISK1_FILE" "$DISK2_FILE" "$DISK3_FILE" "$DISK4_FILE" )
|
||||
DISK_DESCS=( "disk" "disk2" "disk3" "disk4" )
|
||||
DISK_SIZES=( "$DISK_SIZE" "$DISK2_SIZE" "$DISK3_SIZE" "$DISK4_SIZE" )
|
||||
DISK_DEVICES=( "$DEVICE" "$DEVICE2" "$DEVICE3" "$DEVICE4" )
|
||||
DISK_INDEXES=( "3" "4" "5" "6" )
|
||||
DISK_ADDRESSES=( "0xc" "0xd" "0xe" "0xf" )
|
||||
|
||||
if [ -n "$DEVICE2" ]; then
|
||||
addDevice "$DEVICE2" "$DISK_TYPE" "4" "0xd" || exit $?
|
||||
else
|
||||
addDisk "$DISK2_FILE" "$DISK_TYPE" "disk2" "$DISK2_SIZE" "4" "0xd" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" || exit $?
|
||||
fi
|
||||
for i in "${!DISK_FILES[@]}"; do
|
||||
|
||||
if [ -n "$DEVICE3" ]; then
|
||||
addDevice "$DEVICE3" "$DISK_TYPE" "5" "0xe" || exit $?
|
||||
else
|
||||
addDisk "$DISK3_FILE" "$DISK_TYPE" "disk3" "$DISK3_SIZE" "5" "0xe" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" || exit $?
|
||||
fi
|
||||
if [ -n "${DISK_DEVICES[i]}" ]; then
|
||||
addDevice "${DISK_DEVICES[i]}" "$DISK_TYPE" "${DISK_INDEXES[i]}" "${DISK_ADDRESSES[i]}" || exit $?
|
||||
else
|
||||
addDisk "${DISK_FILES[i]}" "$DISK_TYPE" "${DISK_DESCS[i]}" "${DISK_SIZES[i]}" "${DISK_INDEXES[i]}" "${DISK_ADDRESSES[i]}" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" || exit $?
|
||||
fi
|
||||
|
||||
if [ -n "$DEVICE4" ]; then
|
||||
addDevice "$DEVICE4" "$DISK_TYPE" "6" "0xf" || exit $?
|
||||
else
|
||||
addDisk "$DISK4_FILE" "$DISK_TYPE" "disk4" "$DISK4_SIZE" "6" "0xf" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" || exit $?
|
||||
fi
|
||||
done
|
||||
|
||||
DISK_OPTS+=" -object iothread,id=io2"
|
||||
finishDisks
|
||||
|
||||
html "Initialized disks successfully..."
|
||||
return 0
|
||||
|
||||
+7
-2
@@ -8,9 +8,14 @@ set -Eeuo pipefail
|
||||
: "${DISPLAY:="none"}" # Display type
|
||||
: "${RENDERNODE:="/dev/dri/renderD128"}" # Render node
|
||||
|
||||
# Sanitize variables
|
||||
VGA=$(strip "$VGA")
|
||||
DISPLAY=$(strip "$DISPLAY")
|
||||
RENDERNODE=$(strip "$RENDERNODE")
|
||||
|
||||
CPU_VENDOR=$(lscpu | awk '/Vendor ID/{print $3}')
|
||||
|
||||
if [[ "$GPU" != [Yy1]* || "$CPU_VENDOR" != "GenuineIntel" || "$ARCH" != "amd64" ]]; then
|
||||
if ! enabled "$GPU" || [[ "$CPU_VENDOR" != "GenuineIntel" || "$ARCH" != "amd64" ]]; then
|
||||
|
||||
[[ "${DISPLAY,,}" == "none" ]] && VGA="none"
|
||||
DISPLAY_OPTS="-display $DISPLAY -vga $VGA"
|
||||
@@ -20,7 +25,7 @@ fi
|
||||
|
||||
msg="Configuring display drivers..."
|
||||
html "$msg"
|
||||
[[ "$DEBUG" == [Yy1]* ]] && echo "$msg"
|
||||
enabled "$DEBUG" && echo "$msg"
|
||||
|
||||
DISPLAY_OPTS="-display egl-headless,rendernode=$RENDERNODE"
|
||||
DISPLAY_OPTS+=" -vga $VGA"
|
||||
|
||||
+15
-9
@@ -18,23 +18,29 @@ 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
|
||||
|
||||
version=$(qemu-system-x86_64 --version | head -n 1 | cut -d '(' -f 1 | awk '{ print $NF }')
|
||||
cmd=(qemu-system-x86_64)
|
||||
version=$("${cmd[@]}" --version | awk 'NR==1 { print $4 }')
|
||||
info "Booting $APP using QEMU v$version..."
|
||||
|
||||
if [[ "$CONSOLE" == [Yy]* ]]; then
|
||||
exec qemu-system-x86_64 ${ARGS:+ $ARGS}
|
||||
if ! enabled "$SHUTDOWN"; then
|
||||
exec "${cmd[@]}" ${ARGS:+ $ARGS}
|
||||
fi
|
||||
|
||||
{ qemu-system-x86_64 ${ARGS:+ $ARGS} >"$QEMU_OUT" 2>"$QEMU_LOG"; rc=$?; } || :
|
||||
(( rc != 0 )) && error "$(<"$QEMU_LOG")" && exit 15
|
||||
if [ ! -t 1 ] || [ ! -c /dev/tty ]; then
|
||||
"${cmd[@]}" ${ARGS:+ $ARGS} &
|
||||
else
|
||||
"${cmd[@]}" ${ARGS:+ $ARGS} </dev/tty >/dev/tty &
|
||||
fi
|
||||
|
||||
terminal
|
||||
tail -fn +0 "$QEMU_LOG" --pid=$$ 2>/dev/null &
|
||||
cat "$QEMU_TERM" 2>/dev/null & wait $! || :
|
||||
rc=0
|
||||
wait $! || rc=$?
|
||||
[ -f "$QEMU_END" ] && exit "$rc"
|
||||
|
||||
sleep 1 & wait $!
|
||||
[ ! -f "$QEMU_END" ] && finish 0
|
||||
finish "$rc"
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
if enabled "$DEBUG"; then
|
||||
printf "QEMU arguments:\n\n%s\n\n" "${ARGS// -/$'\n-'}"
|
||||
fi
|
||||
|
||||
return 0
|
||||
+32
-14
@@ -27,11 +27,12 @@ FILE=$(find / -maxdepth 1 -type f -iname "$FN" -print -quit)
|
||||
[ ! -s "$FILE" ] && FILE=$(find "$STORAGE" -maxdepth 1 -type f -iname "$FN" -print -quit)
|
||||
[ -s "$FILE" ] && BASE="DSM_VirtualDSM" && URL="file://$FILE"
|
||||
|
||||
URL=$(strip "$URL")
|
||||
|
||||
if [ -n "$URL" ] && [ ! -s "$FILE" ] && [ ! -d "$DIR" ]; then
|
||||
BASE=$(basename "$URL" .pat)
|
||||
if [ ! -s "$STORAGE/$BASE.system.img" ]; then
|
||||
BASE=$(basename "${URL%%\?*}" .pat)
|
||||
BASE="${BASE//+/ }"
|
||||
printf -v BASE '%b' "${BASE//%/\\x}"
|
||||
BASE="${BASE//[!A-Za-z0-9._-]/_}"
|
||||
fi
|
||||
@@ -66,7 +67,6 @@ fi
|
||||
|
||||
if [ ! -s "$FILE" ]; then
|
||||
BASE=$(basename "${URL%%\?*}" .pat)
|
||||
BASE="${BASE//+/ }"
|
||||
printf -v BASE '%b' "${BASE//%/\\x}"
|
||||
BASE="${BASE//[!A-Za-z0-9._-]/_}"
|
||||
fi
|
||||
@@ -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"* && "${ENGINE,,}" == "docker" ]]; 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)
|
||||
@@ -202,11 +205,12 @@ fi
|
||||
MSG="Preparing system partition..."
|
||||
info "Install: $MSG" && html "$MSG"
|
||||
|
||||
BOOT=$(find "$TMP" -name "*.bin.zip")
|
||||
[ ! -s "$BOOT" ] && error "The PAT file contains no boot image." && exit 67
|
||||
BOOT=$(find "$TMP" -name "*.bin.zip" -print -quit)
|
||||
[ -z "$BOOT" ] && error "The PAT file contains no boot image." && exit 67
|
||||
[ ! -s "$BOOT" ] && error "The PAT boot image archive is empty." && exit 67
|
||||
|
||||
BOOT=$(echo "$BOOT" | head -c -5)
|
||||
unzip -q -o "$BOOT".zip -d "$TMP"
|
||||
unzip -q -o "$BOOT" -d "$TMP"
|
||||
BOOT="${BOOT%.zip}"
|
||||
|
||||
SYSTEM="$STORAGE/$BASE.system.img"
|
||||
rm -f "$SYSTEM"
|
||||
@@ -224,6 +228,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 +262,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"
|
||||
@@ -272,8 +282,9 @@ mv "$HDA.tgz" "$HDA.txz"
|
||||
[ -d "$PKG" ] && mv "$PKG/" "$MOUNT/.SynoUpgradePackages/"
|
||||
rm -f "$MOUNT/.SynoUpgradePackages/ActiveInsight-"*
|
||||
|
||||
INDEX_DB="$MOUNT/usr/syno/synoman/indexdb"
|
||||
|
||||
if [ -s "$IDB.txz" ]; then
|
||||
INDEX_DB="$MOUNT/usr/syno/synoman/indexdb"
|
||||
mkdir -p "$INDEX_DB"
|
||||
fi
|
||||
|
||||
@@ -291,6 +302,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,7 +310,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"
|
||||
|
||||
return 0
|
||||
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
msg="Checking memory..."
|
||||
enabled "$DEBUG" && echo "$msg"
|
||||
|
||||
RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}')
|
||||
AVAIL_MEM=$(formatBytes "$RAM_AVAIL")
|
||||
|
||||
checkConfiguredMemory() {
|
||||
|
||||
local wanted msg
|
||||
|
||||
if disabled "$RAM_CHECK" || [[ "${RAM_SIZE,,}" == "max" || "${RAM_SIZE,,}" == "half" ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
wanted=$(numfmt --from=iec "$RAM_SIZE")
|
||||
|
||||
if (( (wanted + RAM_SPARE) > RAM_AVAIL )); then
|
||||
msg="Your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is too high for the $AVAIL_MEM of free 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 (( (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 free 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
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
configureHalfMemory() {
|
||||
|
||||
local wanted
|
||||
|
||||
if [[ "${RAM_SIZE,,}" != "half" ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
if (( (RAM_AVAIL / 2) > RAM_SPARE )); then
|
||||
wanted=$(( (RAM_AVAIL / 2) / 1048577 ))
|
||||
RAM_SIZE="${wanted}M"
|
||||
info "Allocated $wanted MB of RAM for the virtual machine."
|
||||
else
|
||||
RAM_SIZE="max"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
configureMaxMemory() {
|
||||
|
||||
local wanted
|
||||
|
||||
if [[ "${RAM_SIZE,,}" != "max" ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
if (( RAM_AVAIL < (RAM_SPARE * 2) )); then
|
||||
|
||||
wanted=$(( RAM_AVAIL / 2 ))
|
||||
|
||||
else
|
||||
|
||||
wanted=$(( RAM_AVAIL - (RAM_SPARE * 3) ))
|
||||
|
||||
if (( wanted < (RAM_SPARE * 6) )); then
|
||||
wanted=$(( RAM_AVAIL - RAM_SPARE ))
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
wanted=$(( wanted / 1048577 ))
|
||||
RAM_SIZE="${wanted}M"
|
||||
|
||||
info "Allocated $wanted MB of RAM for the virtual machine."
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
checkMinimumMemory() {
|
||||
|
||||
local wanted
|
||||
|
||||
wanted=$(numfmt --from=iec "$RAM_SIZE")
|
||||
|
||||
if [ "$wanted" -lt "$RAM_MINIMUM" ]; then
|
||||
wanted=$(( wanted / 1048577 ))
|
||||
error "Not enough memory available, there is only $wanted MB left!"
|
||||
exit 16
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
checkConfiguredMemory
|
||||
configureHalfMemory
|
||||
configureMaxMemory
|
||||
checkMinimumMemory
|
||||
|
||||
return 0
|
||||
+1113
-424
File diff suppressed because it is too large
Load Diff
+192
-120
@@ -1,135 +1,131 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
: "${API_TIMEOUT:="50"}" # API Call timeout
|
||||
: "${QEMU_TIMEOUT:="50"}" # QEMU Termination timeout
|
||||
: "${SHUTDOWN:="Y"}" # Graceful ACPI shutdown
|
||||
: "${TIMEOUT:="115"}" # QEMU termination timeout
|
||||
: "${API_TIMEOUT:="90"}" # External API call timeout
|
||||
|
||||
# Configure QEMU for graceful shutdown
|
||||
|
||||
API_CMD=6
|
||||
API_HOST="127.0.0.1:$COM_PORT"
|
||||
|
||||
QEMU_TERM=""
|
||||
QEMU_DIR="/run/shm"
|
||||
QEMU_PID="$QEMU_DIR/qemu.pid"
|
||||
QEMU_LOG="$QEMU_DIR/qemu.log"
|
||||
QEMU_OUT="$QEMU_DIR/qemu.out"
|
||||
# Configure QEMU for graceful shutdown
|
||||
|
||||
QEMU_END="$QEMU_DIR/qemu.end"
|
||||
|
||||
if [[ "$KVM" == [Nn]* ]]; then
|
||||
API_TIMEOUT=$(( API_TIMEOUT*2 ))
|
||||
QEMU_TIMEOUT=$(( QEMU_TIMEOUT*2 ))
|
||||
fi
|
||||
|
||||
touch "$QEMU_LOG"
|
||||
|
||||
_trap() {
|
||||
func="$1" ; shift
|
||||
for sig ; do
|
||||
|
||||
local func="$1"; shift
|
||||
local sig
|
||||
TRAP_PID=$BASHPID
|
||||
|
||||
for sig; do
|
||||
trap "$func $sig" "$sig"
|
||||
done
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
signalCode() {
|
||||
|
||||
local sig="$1"
|
||||
|
||||
case "$sig" in
|
||||
SIGHUP) echo 129 ;;
|
||||
SIGINT) echo 130 ;;
|
||||
SIGQUIT) echo 131 ;;
|
||||
SIGABRT) echo 134 ;;
|
||||
SIGTERM) echo 143 ;;
|
||||
*) echo 0 ;;
|
||||
esac
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
displayReason() {
|
||||
|
||||
local reason="$1"
|
||||
|
||||
case "$reason" in
|
||||
129 ) echo "SIGHUP" ;;
|
||||
130 ) echo "SIGINT" ;;
|
||||
131 ) echo "SIGQUIT" ;;
|
||||
134 ) echo "SIGABRT" ;;
|
||||
143 ) echo "SIGTERM" ;;
|
||||
* ) echo "$reason" ;;
|
||||
esac
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
readQemuPid() {
|
||||
|
||||
local -n _pid="$1"
|
||||
|
||||
if [ ! -s "$QEMU_PID" ] || ! read -r _pid <"$QEMU_PID"; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
forceKillQemu() {
|
||||
|
||||
local reason="$1"
|
||||
local pid=""
|
||||
local display
|
||||
|
||||
! readQemuPid pid && return 0
|
||||
! isAlive "$pid" && return 0
|
||||
|
||||
display=$(displayReason "$reason")
|
||||
error "Forcefully terminating $(app), reason: $display..."
|
||||
{ disown "$pid" || :; kill -9 -- "$pid" || :; } 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
cleanupHelpers() {
|
||||
|
||||
local pids=( "${HOST_PID:-}" "${WSD_PID:-}" \
|
||||
"${WEB_PID:-}" "${PASST_PID:-}" "${DNSMASQ_PID:-}" )
|
||||
|
||||
mKill "${pids[@]}"
|
||||
fKill "print.sh"
|
||||
|
||||
closeNetwork
|
||||
return 0
|
||||
}
|
||||
|
||||
finish() {
|
||||
|
||||
local pid
|
||||
local reason=$1
|
||||
|
||||
touch "$QEMU_END"
|
||||
|
||||
if [ -s "$QEMU_PID" ]; then
|
||||
forceKillQemu "$reason"
|
||||
cleanupHelpers
|
||||
|
||||
pid=$(<"$QEMU_PID")
|
||||
echo && error "Forcefully terminating QEMU process, reason: $reason..."
|
||||
{ kill -15 "$pid" || true; } 2>/dev/null
|
||||
|
||||
while isAlive "$pid"; do
|
||||
sleep 1
|
||||
# Workaround for zombie pid
|
||||
[ ! -s "$QEMU_PID" ] && break
|
||||
done
|
||||
if ! waitPidFile "$QEMU_PID" 10; then
|
||||
warn "Timed out while waiting for $(app) to exit!"
|
||||
fi
|
||||
|
||||
fKill "print.sh"
|
||||
fKill "host.bin"
|
||||
|
||||
closeNetwork
|
||||
|
||||
sleep 1
|
||||
echo && echo "❯ Shutdown completed!"
|
||||
(( reason != 1 )) && echo && echo "❯ Shutdown completed!"
|
||||
|
||||
exit "$reason"
|
||||
}
|
||||
|
||||
terminal() {
|
||||
sendGuestShutdown() {
|
||||
|
||||
local dev=""
|
||||
|
||||
if [ -s "$QEMU_OUT" ]; then
|
||||
|
||||
local msg
|
||||
msg=$(<"$QEMU_OUT")
|
||||
|
||||
if [ -n "$msg" ]; then
|
||||
|
||||
if [[ "${msg,,}" != "char"* || "$msg" != *"serial0)" ]]; then
|
||||
echo "$msg"
|
||||
fi
|
||||
|
||||
dev="${msg#*/dev/p}"
|
||||
dev="/dev/p${dev%% *}"
|
||||
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ! -c "$dev" ]; then
|
||||
dev=$(echo 'info chardev' | nc -q 1 -w 1 localhost "$MON_PORT" | tr -d '\000')
|
||||
dev="${dev#*serial0}"
|
||||
dev="${dev#*pty:}"
|
||||
dev="${dev%%$'\n'*}"
|
||||
dev="${dev%%$'\r'*}"
|
||||
fi
|
||||
|
||||
if [ ! -c "$dev" ]; then
|
||||
error "Device '$dev' not found!"
|
||||
finish 34 && return 34
|
||||
fi
|
||||
|
||||
QEMU_TERM="$dev"
|
||||
return 0
|
||||
}
|
||||
|
||||
_graceful_shutdown() {
|
||||
|
||||
local code=$?
|
||||
local pid url response
|
||||
|
||||
set +e
|
||||
|
||||
if [ -f "$QEMU_END" ]; then
|
||||
echo && info "Received $1 signal while already shutting down..."
|
||||
return
|
||||
fi
|
||||
|
||||
touch "$QEMU_END"
|
||||
echo && info "Received $1 signal, sending shutdown command..."
|
||||
|
||||
if [ ! -s "$QEMU_PID" ]; then
|
||||
echo && error "QEMU PID file does not exist?"
|
||||
finish "$code" && return "$code"
|
||||
fi
|
||||
|
||||
pid=$(<"$QEMU_PID")
|
||||
|
||||
if ! isAlive "$pid"; then
|
||||
echo && error "QEMU process does not exist?"
|
||||
finish "$code" && return "$code"
|
||||
fi
|
||||
local pid="$1"
|
||||
local response
|
||||
local url
|
||||
|
||||
# Don't send the powerdown signal because vDSM ignores ACPI signals
|
||||
# echo 'system_powerdown' | nc -q 1 -w 1 localhost "$MON_PORT" > /dev/null
|
||||
# nc -q 1 -w 1 -U "$QEMU_DIR/monitor.sock" &> /dev/null <<<'system_powerdown' || :
|
||||
|
||||
# Send shutdown command to guest agent via serial port
|
||||
API_TIMEOUT=$(strip "$API_TIMEOUT")
|
||||
url="http://$API_HOST/read?command=$API_CMD&timeout=$API_TIMEOUT"
|
||||
response=$(curl -sk -m "$(( API_TIMEOUT+2 ))" -S "$url" 2>&1)
|
||||
|
||||
@@ -141,45 +137,121 @@ _graceful_shutdown() {
|
||||
|
||||
response="${response#*message\"\: \"}"
|
||||
[ -z "$response" ] && response="second signal"
|
||||
|
||||
echo && error "Forcefully terminating because of: ${response%%\"*}"
|
||||
{ kill -15 "$pid" || true; } 2>/dev/null
|
||||
kill -15 -- "$pid" 2>/dev/null || :
|
||||
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
normalizeTimeout() {
|
||||
|
||||
local term_grace=3 # seconds before loop ends to send SIGTERM
|
||||
local cleanup_grace=3 # seconds reserved after the loop for cleanup
|
||||
local elapsed
|
||||
local timeout_left
|
||||
local min
|
||||
|
||||
TIMEOUT=$(strip "$TIMEOUT")
|
||||
if [[ ! "$TIMEOUT" =~ ^[0-9]+$ ]]; then
|
||||
TIMEOUT=115
|
||||
fi
|
||||
|
||||
if (( TIMEOUT >= 30 )); then
|
||||
term_grace=5
|
||||
cleanup_grace=5
|
||||
elif (( TIMEOUT >= 15 )); then
|
||||
term_grace=4
|
||||
cleanup_grace=4
|
||||
fi
|
||||
|
||||
elapsed=$((SECONDS - start))
|
||||
timeout_left=$((TIMEOUT - elapsed))
|
||||
|
||||
min=$((term_grace + cleanup_grace + 1))
|
||||
(( timeout_left < min )) && timeout_left=$min
|
||||
|
||||
wait_until=$((timeout_left - cleanup_grace))
|
||||
sigterm_at=$((wait_until - term_grace))
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
waitForShutdown() {
|
||||
|
||||
local cnt=0
|
||||
local pid="$1"
|
||||
local name="$APP"
|
||||
local slp
|
||||
|
||||
while [ "$cnt" -lt "$QEMU_TIMEOUT" ]; do
|
||||
while (( cnt <= wait_until )); do
|
||||
|
||||
sleep 1 &
|
||||
slp=$!
|
||||
|
||||
# Stop waiting if the process has exited
|
||||
! isAlive "$pid" && break
|
||||
|
||||
sleep 1
|
||||
cnt=$((cnt+1))
|
||||
|
||||
[[ "$DEBUG" == [Yy1]* ]] && info "Shutting down, waiting... ($cnt/$QEMU_TIMEOUT)"
|
||||
|
||||
# Workaround for zombie pid
|
||||
# Workaround for stale/zombie QEMU pid file
|
||||
[ ! -s "$QEMU_PID" ] && break
|
||||
|
||||
if (( cnt == sigterm_at )); then
|
||||
info "${name^} is still running, sending SIGTERM... ($cnt/$wait_until)"
|
||||
kill -15 -- "$pid" 2>/dev/null || :
|
||||
elif (( cnt > 0 )) && enabled "${DEBUG:-}"; then
|
||||
info "Waiting for $name to shut down... ($cnt/$wait_until)"
|
||||
fi
|
||||
|
||||
wait "$slp"
|
||||
(( cnt++ ))
|
||||
|
||||
done
|
||||
|
||||
if [ "$cnt" -ge "$QEMU_TIMEOUT" ]; then
|
||||
echo && error "Shutdown timeout reached, aborting..."
|
||||
fi
|
||||
|
||||
finish "$code" && return "$code"
|
||||
return 0
|
||||
}
|
||||
|
||||
MON_OPTS="\
|
||||
-pidfile $QEMU_PID \
|
||||
-name $PROCESS,process=$PROCESS,debug-threads=on \
|
||||
-monitor telnet:localhost:$MON_PORT,server,nowait,nodelay"
|
||||
graceful_shutdown() {
|
||||
|
||||
if [[ "$CONSOLE" != [Yy]* ]]; then
|
||||
local sig="$1"
|
||||
local pid=""
|
||||
local code=0
|
||||
|
||||
MON_OPTS+=" -daemonize -D $QEMU_LOG"
|
||||
[[ $BASHPID != "$TRAP_PID" ]] && return
|
||||
|
||||
_trap _graceful_shutdown SIGTERM SIGHUP SIGINT SIGABRT SIGQUIT
|
||||
code=$(signalCode "$sig")
|
||||
|
||||
fi
|
||||
if [ -f "$QEMU_END" ]; then
|
||||
echo && info "Received $1 signal while already shutting down..."
|
||||
return
|
||||
fi
|
||||
|
||||
set +e
|
||||
start=$SECONDS
|
||||
touch "$QEMU_END"
|
||||
echo && info "Received $1 signal, sending shutdown command..."
|
||||
|
||||
if ! readQemuPid pid; then
|
||||
warn "QEMU PID file ($QEMU_PID) does not exist?"
|
||||
finish "$code"
|
||||
fi
|
||||
|
||||
if ! isAlive "$pid"; then
|
||||
warn "QEMU process with PID $pid does not exist?"
|
||||
finish "$code"
|
||||
fi
|
||||
|
||||
sendGuestShutdown "$pid"
|
||||
normalizeTimeout
|
||||
waitForShutdown "$pid"
|
||||
|
||||
finish "$code"
|
||||
}
|
||||
|
||||
! enabled "$SHUTDOWN" && return 0
|
||||
[ -n "${QEMU_TIMEOUT:-}" ] && TIMEOUT="$QEMU_TIMEOUT"
|
||||
|
||||
_trap graceful_shutdown SIGTERM SIGHUP SIGABRT SIGQUIT
|
||||
|
||||
return 0
|
||||
|
||||
+146
-47
@@ -1,16 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
# shellcheck disable=SC2329
|
||||
set -Eeuo pipefail
|
||||
|
||||
: "${DHCP:="N"}"
|
||||
: "${NETWORK:="Y"}"
|
||||
|
||||
[[ "$NETWORK" == [Nn]* ]] && exit 0
|
||||
cd /run
|
||||
. utils.sh # Load functions
|
||||
|
||||
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; }
|
||||
|
||||
disabled "$NETWORK" && exit 0
|
||||
|
||||
file="/run/shm/dsm.url"
|
||||
info="/run/shm/msg.html"
|
||||
msgs="/run/shm/msg.html"
|
||||
driver="/run/shm/qemu.nic"
|
||||
page="/run/shm/index.html"
|
||||
address="/run/shm/qemu.ip"
|
||||
@@ -22,68 +26,144 @@ resp_err="Guest returned an invalid response:"
|
||||
curl_err="Failed to connect to guest: curl error"
|
||||
jq_err="Failed to parse response from guest: jq error"
|
||||
|
||||
while [ ! -s "$file" ]
|
||||
do
|
||||
|
||||
# Check if not shutting down
|
||||
[ -f "$shutdown" ] && exit 1
|
||||
|
||||
sleep 3
|
||||
exitIfShuttingDown() {
|
||||
|
||||
[ -f "$shutdown" ] && exit 1
|
||||
[ -s "$file" ] && break
|
||||
|
||||
# Retrieve network info from guest VM
|
||||
return 0
|
||||
}
|
||||
|
||||
queryGuest() {
|
||||
|
||||
local rc
|
||||
|
||||
{ json=$(curl -m 20 -sk "$url"); rc=$?; } || :
|
||||
|
||||
[ -f "$shutdown" ] && exit 1
|
||||
(( rc != 0 )) && error "$curl_err $rc" && continue
|
||||
exitIfShuttingDown
|
||||
|
||||
{ result=$(echo "$json" | jq -r '.status'); rc=$?; } || :
|
||||
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
|
||||
[[ "$result" == "null" ]] && error "$resp_err $json" && continue
|
||||
|
||||
if [[ "$result" != "success" ]] ; then
|
||||
{ msg=$(echo "$json" | jq -r '.message'); rc=$?; } || :
|
||||
error "Guest replied $result: $msg" && continue
|
||||
if (( rc != 0 )); then
|
||||
error "$curl_err $rc"
|
||||
return 1
|
||||
fi
|
||||
|
||||
{ port=$(echo "$json" | jq -r '.data.data.dsm_setting.data.http_port'); rc=$?; } || :
|
||||
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
|
||||
[[ "$port" == "null" ]] && error "$resp_err $json" && continue
|
||||
[ -z "$port" ] && continue
|
||||
return 0
|
||||
}
|
||||
|
||||
{ ip=$(echo "$json" | jq -r '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip'); rc=$?; } || :
|
||||
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
|
||||
[[ "$ip" == "null" ]] && error "$resp_err $json" && continue
|
||||
readJsonField() {
|
||||
|
||||
[ -z "$ip" ] && continue
|
||||
echo "$ip:$port" > $file
|
||||
local query="$1"
|
||||
local result
|
||||
local rc
|
||||
|
||||
done
|
||||
{ result=$(jq -r "$query" <<< "$json"); rc=$?; } || :
|
||||
|
||||
[ -f "$shutdown" ] && exit 1
|
||||
if (( rc != 0 )); then
|
||||
error "$jq_err $rc ( $json )"
|
||||
return 1
|
||||
fi
|
||||
|
||||
location=$(<"$file")
|
||||
if [[ "$result" == "null" ]]; then
|
||||
error "$resp_err $json"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [[ "$DHCP" == [Yy1]* ]]; then
|
||||
printf '%s\n' "$result"
|
||||
return 0
|
||||
}
|
||||
|
||||
readGuestStatus() {
|
||||
|
||||
local result msg rc
|
||||
|
||||
result=$(readJsonField '.status') || return 1
|
||||
|
||||
if [[ "$result" != "success" ]]; then
|
||||
{ msg=$(jq -r '.message // empty' <<< "$json"); rc=$?; } || :
|
||||
|
||||
if (( rc != 0 )); then
|
||||
error "$jq_err $rc ( $json )"
|
||||
return 1
|
||||
fi
|
||||
|
||||
error "Guest replied $result: $msg"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
readGuestPort() {
|
||||
|
||||
port=$(readJsonField '.data.data.dsm_setting.data.http_port') || return 1
|
||||
[ -z "$port" ] && return 1
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
readGuestIp() {
|
||||
|
||||
ip=$(readJsonField '.data.data.ip.data[] | select(.name=="eth0" and has("ip")) | .ip | select(test("^[0-9]+\\."))') || return 1
|
||||
[ -z "$ip" ] && return 1
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
writeDsmLocation() {
|
||||
|
||||
echo "$ip:$port" > "$file"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
pollGuestLocation() {
|
||||
|
||||
while [ ! -s "$file" ]; do
|
||||
|
||||
# Check if not shutting down
|
||||
exitIfShuttingDown
|
||||
|
||||
sleep 3
|
||||
|
||||
exitIfShuttingDown
|
||||
[ -s "$file" ] && break
|
||||
|
||||
# Retrieve network info from guest VM
|
||||
queryGuest || continue
|
||||
readGuestStatus || continue
|
||||
readGuestPort || continue
|
||||
readGuestIp || continue
|
||||
writeDsmLocation
|
||||
|
||||
done
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
writeDhcpPage() {
|
||||
|
||||
local title body script html
|
||||
|
||||
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\]/}"
|
||||
html=$(<"$template")
|
||||
html="${html/\[1\]/$title}"
|
||||
html="${html/\[2\]/$script}"
|
||||
html="${html/\[3\]/$body}"
|
||||
html="${html/\[4\]/}"
|
||||
html="${html/\[5\]/}"
|
||||
|
||||
echo "$HTML" > "$page"
|
||||
echo "$body" > "$info"
|
||||
echo "$html" > "$page"
|
||||
echo "$body" > "$msgs"
|
||||
|
||||
else
|
||||
return 0
|
||||
}
|
||||
|
||||
buildStaticMessage() {
|
||||
|
||||
local nic ip port
|
||||
|
||||
nic=$(<"$driver")
|
||||
ip=$(<"$address")
|
||||
@@ -95,12 +175,31 @@ else
|
||||
msg="http://$ip:$port"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
printLoginMessage() {
|
||||
|
||||
echo "" >&2
|
||||
info "-----------------------------------------------------------"
|
||||
info " You can now login to DSM at $msg"
|
||||
info "-----------------------------------------------------------"
|
||||
echo "" >&2
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
pollGuestLocation
|
||||
exitIfShuttingDown
|
||||
|
||||
location=$(<"$file")
|
||||
|
||||
if enabled "$DHCP"; then
|
||||
writeDhcpPage
|
||||
else
|
||||
buildStaticMessage
|
||||
fi
|
||||
|
||||
echo "" >&2
|
||||
info "-----------------------------------------------------------"
|
||||
info " You can now login to DSM at $msg"
|
||||
info "-----------------------------------------------------------"
|
||||
echo "" >&2
|
||||
printLoginMessage
|
||||
|
||||
exit 0
|
||||
|
||||
+149
-72
@@ -7,15 +7,31 @@ set -Eeuo pipefail
|
||||
: "${CPU_FLAGS:=""}"
|
||||
: "${CPU_MODEL:=""}"
|
||||
|
||||
CLOCKSOURCE="tsc"
|
||||
[[ "${ARCH,,}" == "arm64" ]] && CLOCKSOURCE="arch_sys_counter"
|
||||
CLOCK="/sys/devices/system/clocksource/clocksource0/current_clocksource"
|
||||
HOST_CPU=$(strip "$HOST_CPU")
|
||||
CPU_FLAGS=$(strip "$CPU_FLAGS")
|
||||
CPU_MODEL=$(strip "$CPU_MODEL")
|
||||
|
||||
selectClocksource() {
|
||||
|
||||
CLOCKSOURCE="tsc"
|
||||
[[ "${ARCH,,}" == "arm64" ]] && CLOCKSOURCE="arch_sys_counter"
|
||||
CLOCK="/sys/devices/system/clocksource/clocksource0/current_clocksource"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
checkClocksource() {
|
||||
|
||||
local result
|
||||
|
||||
if [ ! -f "$CLOCK" ]; then
|
||||
warn "file \"$CLOCK\" cannot be found?"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [ ! -f "$CLOCK" ]; then
|
||||
warn "file \"$CLOCK\" cannot not found?"
|
||||
else
|
||||
result=$(<"$CLOCK")
|
||||
result="${result//[![:print:]]/}"
|
||||
|
||||
case "${result,,}" in
|
||||
"${CLOCKSOURCE,,}" ) ;;
|
||||
"kvm-clock" ) info "Nested KVM virtualization detected.." ;;
|
||||
@@ -23,30 +39,38 @@ else
|
||||
"hpet" ) warn "unsupported clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE'." ;;
|
||||
*) warn "unexpected clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE'." ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
flags=$(sed -ne '/^flags/s/^.*: //p' /proc/cpuinfo)
|
||||
return 0
|
||||
}
|
||||
|
||||
if [[ "$KVM" != [Nn]* ]]; then
|
||||
checkSse42() {
|
||||
|
||||
if ! grep -qw "sse4_2" <<< "$flags"; then
|
||||
error "Your CPU does not have the SSE4 instruction set that Virtual DSM requires!"
|
||||
! enabled "$DEBUG" && exit 88
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
configureKvmCpuModel() {
|
||||
|
||||
CPU_FEATURES="kvm=on,l3-cache=on,+hypervisor"
|
||||
KVM_OPTS=",accel=kvm -enable-kvm -global kvm-pit.lost_tick_policy=discard"
|
||||
|
||||
if ! grep -qw "sse4_2" <<< "$flags"; then
|
||||
info "Your CPU does not have the SSE4 instruction set that Virtual DSM requires, it will be emulated..."
|
||||
[ -z "$CPU_MODEL" ] && CPU_MODEL="qemu64"
|
||||
CPU_FEATURES+=",+ssse3,+sse4.1,+sse4.2"
|
||||
fi
|
||||
|
||||
if [ -z "$CPU_MODEL" ]; then
|
||||
CPU_MODEL="host"
|
||||
CPU_FEATURES+=",migratable=no"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
appendKvmInvtscFeature() {
|
||||
|
||||
if grep -qw "svm" <<< "$flags"; then
|
||||
|
||||
# AMD processor
|
||||
|
||||
if grep -qw "tsc_scale" <<< "$flags"; then
|
||||
CPU_FEATURES+=",+invtsc"
|
||||
fi
|
||||
@@ -54,7 +78,7 @@ if [[ "$KVM" != [Nn]* ]]; then
|
||||
else
|
||||
|
||||
# Intel processor
|
||||
|
||||
local vmx
|
||||
vmx=$(sed -ne '/^vmx flags/s/^.*: //p' /proc/cpuinfo)
|
||||
|
||||
if grep -qw "tsc_scaling" <<< "$vmx"; then
|
||||
@@ -63,7 +87,35 @@ if [[ "$KVM" != [Nn]* ]]; then
|
||||
|
||||
fi
|
||||
|
||||
else
|
||||
return 0
|
||||
}
|
||||
|
||||
configureKvm() {
|
||||
|
||||
configureKvmCpuModel
|
||||
checkSse42
|
||||
appendKvmInvtscFeature
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
configureTcgCpuModel() {
|
||||
|
||||
if [ -n "$CPU_MODEL" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ "$ARCH" == "amd64" ]]; then
|
||||
CPU_MODEL="max"
|
||||
CPU_FEATURES+=",migratable=no"
|
||||
else
|
||||
CPU_MODEL="qemu64"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
configureTcg() {
|
||||
|
||||
KVM_OPTS=""
|
||||
CPU_FEATURES="l3-cache=on,+hypervisor"
|
||||
@@ -72,70 +124,95 @@ else
|
||||
KVM_OPTS=" -accel tcg,thread=multi"
|
||||
fi
|
||||
|
||||
if [ -z "$CPU_MODEL" ]; then
|
||||
if [[ "$ARCH" == "amd64" ]]; then
|
||||
CPU_MODEL="max"
|
||||
CPU_FEATURES+=",migratable=no"
|
||||
configureTcgCpuModel
|
||||
CPU_FEATURES+=",+ssse3,+sse4.1,+sse4.2"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
extractHostCpuArgument() {
|
||||
|
||||
local args prefix suffix param
|
||||
|
||||
if [[ "$ARGUMENTS" == *"-cpu host,"* ]]; then
|
||||
|
||||
args="${ARGUMENTS} "
|
||||
prefix="${args/-cpu host,*/}"
|
||||
suffix="${args/*-cpu host,/}"
|
||||
param="${suffix%% *}"
|
||||
suffix="${suffix#* }"
|
||||
args="${prefix}${suffix}"
|
||||
ARGUMENTS="${args::-1}"
|
||||
|
||||
if [ -z "$CPU_FLAGS" ]; then
|
||||
CPU_FLAGS="$param"
|
||||
else
|
||||
CPU_MODEL="qemu64"
|
||||
CPU_FLAGS+=",$param"
|
||||
fi
|
||||
|
||||
else
|
||||
|
||||
if [[ "$ARGUMENTS" == *"-cpu host"* ]]; then
|
||||
ARGUMENTS="${ARGUMENTS//-cpu host/}"
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
composeCpuFlags() {
|
||||
|
||||
if [ -z "$CPU_FLAGS" ]; then
|
||||
if [ -z "$CPU_FEATURES" ]; then
|
||||
CPU_FLAGS="$CPU_MODEL"
|
||||
else
|
||||
CPU_FLAGS="$CPU_MODEL,$CPU_FEATURES"
|
||||
fi
|
||||
else
|
||||
if [ -z "$CPU_FEATURES" ]; then
|
||||
CPU_FLAGS="$CPU_MODEL,$CPU_FLAGS"
|
||||
else
|
||||
CPU_FLAGS="$CPU_MODEL,$CPU_FEATURES,$CPU_FLAGS"
|
||||
fi
|
||||
fi
|
||||
|
||||
CPU_FEATURES+=",+ssse3,+sse4.1,+sse4.2"
|
||||
return 0
|
||||
}
|
||||
|
||||
fi
|
||||
configureHostCpuName() {
|
||||
|
||||
if [[ "$ARGUMENTS" == *"-cpu host,"* ]]; then
|
||||
|
||||
args="${ARGUMENTS} "
|
||||
prefix="${args/-cpu host,*/}"
|
||||
suffix="${args/*-cpu host,/}"
|
||||
param="${suffix%% *}"
|
||||
suffix="${suffix#* }"
|
||||
args="${prefix}${suffix}"
|
||||
ARGUMENTS="${args::-1}"
|
||||
|
||||
if [ -z "$CPU_FLAGS" ]; then
|
||||
CPU_FLAGS="$param"
|
||||
else
|
||||
CPU_FLAGS+=",$param"
|
||||
if [ -z "$HOST_CPU" ]; then
|
||||
[[ "${CPU,,}" != "unknown" ]] && HOST_CPU="$CPU"
|
||||
fi
|
||||
|
||||
if [ -n "$HOST_CPU" ]; then
|
||||
HOST_CPU="${HOST_CPU%%,*},,"
|
||||
else
|
||||
HOST_CPU="QEMU, Virtual CPU,"
|
||||
if [ "$ARCH" == "amd64" ]; then
|
||||
HOST_CPU+=" X86_64"
|
||||
else
|
||||
HOST_CPU+=" $ARCH"
|
||||
fi
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
selectClocksource
|
||||
checkClocksource
|
||||
|
||||
flags=$(sed -ne '/^flags/s/^.*: //p' /proc/cpuinfo)
|
||||
|
||||
if ! disabled "$KVM"; then
|
||||
configureKvm
|
||||
else
|
||||
|
||||
if [[ "$ARGUMENTS" == *"-cpu host"* ]]; then
|
||||
ARGUMENTS="${ARGUMENTS//-cpu host/}"
|
||||
fi
|
||||
|
||||
configureTcg
|
||||
fi
|
||||
|
||||
if [ -z "$CPU_FLAGS" ]; then
|
||||
if [ -z "$CPU_FEATURES" ]; then
|
||||
CPU_FLAGS="$CPU_MODEL"
|
||||
else
|
||||
CPU_FLAGS="$CPU_MODEL,$CPU_FEATURES"
|
||||
fi
|
||||
else
|
||||
if [ -z "$CPU_FEATURES" ]; then
|
||||
CPU_FLAGS="$CPU_MODEL,$CPU_FLAGS"
|
||||
else
|
||||
CPU_FLAGS="$CPU_MODEL,$CPU_FEATURES,$CPU_FLAGS"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$HOST_CPU" ]; then
|
||||
[[ "${CPU,,}" != "unknown" ]] && HOST_CPU="$CPU"
|
||||
fi
|
||||
|
||||
if [ -n "$HOST_CPU" ]; then
|
||||
HOST_CPU="${HOST_CPU%%,*},,"
|
||||
else
|
||||
HOST_CPU="QEMU, Virtual CPU,"
|
||||
if [ "$ARCH" == "amd64" ]; then
|
||||
HOST_CPU+=" X86_64"
|
||||
else
|
||||
HOST_CPU+=" $ARCH"
|
||||
fi
|
||||
fi
|
||||
extractHostCpuArgument
|
||||
composeCpuFlags
|
||||
configureHostCpuName
|
||||
|
||||
return 0
|
||||
|
||||
+21
-14
@@ -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" ]] || [ "$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
|
||||
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
|
||||
|
||||
+223
-144
@@ -2,7 +2,7 @@
|
||||
set -Eeuo pipefail
|
||||
|
||||
trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR
|
||||
[[ "${TRACE:-}" == [Yy1]* ]] && set -o functrace && trap 'echo "# $BASH_COMMAND" >&2' DEBUG
|
||||
enabled "${TRACE:-}" && set -o functrace && trap 'echo "# $BASH_COMMAND" >&2' DEBUG
|
||||
|
||||
[ ! -f "/run/entry.sh" ] && error "Script must be run inside the container!" && exit 11
|
||||
[ "$(id -u)" -ne "0" ] && error "Script must be executed with root privileges." && exit 12
|
||||
@@ -13,7 +13,6 @@ trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR
|
||||
: "${KVM:="Y"}" # KVM acceleration
|
||||
: "${DEBUG:="N"}" # Disable debugging mode
|
||||
: "${COUNTRY:=""}" # Country code for mirror
|
||||
: "${CONSOLE:="N"}" # Disable console mode
|
||||
: "${ALLOCATE:=""}" # Preallocate diskspace
|
||||
: "${ARGUMENTS:=""}" # Extra QEMU parameters
|
||||
: "${CPU_CORES:="2"}" # Amount of CPU cores
|
||||
@@ -22,103 +21,247 @@ trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR
|
||||
: "${DISK_SIZE:="16G"}" # Initial data disk size
|
||||
: "${STORAGE:="/storage"}" # Storage folder location
|
||||
|
||||
# Helper variables
|
||||
detectEngine() {
|
||||
|
||||
PODMAN="N"
|
||||
if [ -f "/run/.containerenv" ]; then
|
||||
ENGINE="${container:-}"
|
||||
if [[ "${ENGINE,,}" == *"podman"* ]]; then
|
||||
ROOTLESS="Y"
|
||||
ENGINE="Podman"
|
||||
else
|
||||
[ -z "$ENGINE" ] && ENGINE="Kubernetes"
|
||||
fi
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
checkPrivileged() {
|
||||
|
||||
local cap_bnd
|
||||
local last_cap
|
||||
local max_cap
|
||||
|
||||
# Get the capability bounding set
|
||||
cap_bnd=$(grep '^CapBnd:' /proc/$$/status | awk '{print $2}')
|
||||
cap_bnd=$(printf "%d" "0x${cap_bnd}")
|
||||
|
||||
# Get the last capability number
|
||||
last_cap=$(cat /proc/sys/kernel/cap_last_cap)
|
||||
|
||||
# Calculate the maximum capability value
|
||||
max_cap=$(((1 << (last_cap + 1)) - 1))
|
||||
|
||||
if [ "$cap_bnd" -eq "$max_cap" ]; then
|
||||
ROOTLESS="N"
|
||||
PRIVILEGED="Y"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
normalizeCpuCores() {
|
||||
|
||||
CPU_CORES=$(strip "$CPU_CORES")
|
||||
[ -z "$CPU_CORES" ] && CPU_CORES=2
|
||||
[[ "${CPU_CORES,,}" == "max" ]] && CPU_CORES="$CORES"
|
||||
[[ "${CPU_CORES,,}" == "half" ]] && CPU_CORES=$(( CORES / 2 ))
|
||||
[ -z "${CPU_CORES##*[!0-9]*}" ] && error "Invalid amount of CPU_CORES: $CPU_CORES" && exit 15
|
||||
|
||||
[ "$CPU_CORES" -lt "1" ] && CPU_CORES=1
|
||||
if [ "$CPU_CORES" -gt "$CORES" ]; then
|
||||
warn "The amount for CPU_CORES (${CPU_CORES}) exceeds the amount of logical cores available (${CORES}) and will be limited."
|
||||
CPU_CORES="$CORES"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
checkStorage() {
|
||||
|
||||
# Check system
|
||||
|
||||
QEMU_DIR="/run/shm"
|
||||
|
||||
if [ ! -d "/dev/shm" ]; then
|
||||
error "Directory /dev/shm not found!" && exit 14
|
||||
else
|
||||
[ ! -d "$QEMU_DIR" ] && ln -s /dev/shm "$QEMU_DIR"
|
||||
fi
|
||||
|
||||
QEMU_PID="$QEMU_DIR/qemu.pid"
|
||||
|
||||
# Check folder
|
||||
|
||||
if [[ "${STORAGE,,}" != "/storage" ]]; then
|
||||
mkdir -p "$STORAGE"
|
||||
fi
|
||||
|
||||
if [ ! -d "$STORAGE" ]; then
|
||||
error "Storage folder ($STORAGE) not found!" && exit 13
|
||||
fi
|
||||
|
||||
if [ ! -w "$STORAGE" ]; then
|
||||
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
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
checkFilesystem() {
|
||||
|
||||
# Check filesystem
|
||||
FS=$(stat -f -c %T "$STORAGE")
|
||||
|
||||
if [[ "${FS,,}" == "ecryptfs" || "${FS,,}" == "tmpfs" ]]; then
|
||||
DISK_IO="threads"
|
||||
DISK_CACHE="writeback"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
normalizeRamSize() {
|
||||
|
||||
# Read memory
|
||||
RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}')
|
||||
RAM_TOTAL=$(free -b | grep -m 1 Mem: | awk '{print $2}')
|
||||
|
||||
RAM_SPARE=500000000
|
||||
RAM_MINIMUM=136314880
|
||||
|
||||
RAM_SIZE=$(strip "$RAM_SIZE")
|
||||
RAM_SIZE="${RAM_SIZE// /}"
|
||||
[ -z "$RAM_SIZE" ] && RAM_SIZE="2G"
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
wanted=$(numfmt --from=iec "$RAM_SIZE")
|
||||
[ "$wanted" -lt "$RAM_MINIMUM" ] && error "RAM_SIZE is too low: $RAM_SIZE" && exit 16
|
||||
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
checkKvm() {
|
||||
|
||||
# Check KVM support
|
||||
|
||||
if [[ "${PLATFORM,,}" == "x64" ]]; then
|
||||
TARGET="amd64"
|
||||
else
|
||||
TARGET="arm64"
|
||||
fi
|
||||
|
||||
if disabled "$KVM"; 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 ! disabled "$KVM"; 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
|
||||
if ! grep -qw "sse4_2" <<< "$flags"; then
|
||||
error "Your CPU does not have the SSE4 instruction set that Virtual DSM requires!"
|
||||
! enabled "$DEBUG" && exit 88
|
||||
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
|
||||
! enabled "$DEBUG" && exit 88
|
||||
fi
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Sanitize variables
|
||||
TZ=$(strip "$TZ")
|
||||
STORAGE=$(strip "$STORAGE")
|
||||
COUNTRY=$(strip "$COUNTRY")
|
||||
DISK_SIZE=$(strip "$DISK_SIZE")
|
||||
|
||||
# Helper variables
|
||||
ROOTLESS="N"
|
||||
PRIVILEGED="N"
|
||||
ENGINE="Docker"
|
||||
PROCESS="${APP,,}"
|
||||
PROCESS="${PROCESS// /-}"
|
||||
|
||||
if [ -f "/run/.containerenv" ]; then
|
||||
PODMAN="Y"
|
||||
ENGINE="Podman"
|
||||
fi
|
||||
detectEngine
|
||||
|
||||
echo "❯ Starting $APP for $ENGINE v$(</run/version)..."
|
||||
echo "❯ Starting $APP for $ENGINE v$(</etc/version)..."
|
||||
echo "❯ For support visit $SUPPORT"
|
||||
|
||||
checkPrivileged
|
||||
|
||||
INFO="/run/shm/msg.html"
|
||||
PAGE="/run/shm/index.html"
|
||||
TEMPLATE="/var/www/index.html"
|
||||
FOOTER1="$APP for $ENGINE v$(</run/version)"
|
||||
FOOTER1="$APP for $ENGINE v$(</etc/version)"
|
||||
FOOTER2="<a href='$SUPPORT'>$SUPPORT</a>"
|
||||
|
||||
SOCKETS=1
|
||||
CPU=$(cpu)
|
||||
SYS=$(uname -r)
|
||||
HOST=$(hostname -s)
|
||||
KERNEL=$(echo "$SYS" | cut -b 1)
|
||||
MINOR=$(echo "$SYS" | cut -d '.' -f2)
|
||||
ARCH=$(dpkg --print-architecture)
|
||||
CORES=$(grep -c '^processor' /proc/cpuinfo)
|
||||
|
||||
if ! grep -qi "socket(s)" <<< "$(lscpu)"; then
|
||||
SOCKETS=1
|
||||
else
|
||||
SOCKETS=$(lscpu | grep -m 1 -i 'socket(s)' | awk '{print $(2)}')
|
||||
if grep -qi "socket(s)" <<< "$(lscpu)"; then
|
||||
SOCKETS=$(lscpu | grep -m 1 -i 'socket(s)' | awk '{print $2}')
|
||||
[ -z "${SOCKETS##*[!0-9]*}" ] && SOCKETS=1
|
||||
[ "$SOCKETS" -lt "1" ] && SOCKETS=1
|
||||
fi
|
||||
|
||||
CPU_CORES="${CPU_CORES// /}"
|
||||
[[ "${CPU_CORES,,}" == "max" ]] && CPU_CORES="$CORES"
|
||||
[ -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 logical cores available, so will be limited to ${CORES}."
|
||||
CPU_CORES="$CORES"
|
||||
fi
|
||||
|
||||
# Check system
|
||||
|
||||
if [ ! -d "/dev/shm" ]; then
|
||||
error "Directory /dev/shm not found!" && exit 14
|
||||
else
|
||||
[ ! -d "/run/shm" ] && ln -s /dev/shm /run/shm
|
||||
fi
|
||||
|
||||
# Check folder
|
||||
|
||||
if [[ "${COMMIT:-}" == [Yy1]* ]]; then
|
||||
STORAGE="/local"
|
||||
mkdir -p "$STORAGE"
|
||||
fi
|
||||
|
||||
if [ ! -d "$STORAGE" ]; then
|
||||
error "Storage folder ($STORAGE) not found!" && exit 13
|
||||
fi
|
||||
|
||||
if [ ! -w "$STORAGE" ]; then
|
||||
error "Storage folder ($STORAGE) is not writeable!" && exit 13
|
||||
fi
|
||||
|
||||
# Check filesystem
|
||||
FS=$(stat -f -c %T "$STORAGE")
|
||||
|
||||
if [[ "${FS,,}" == "ecryptfs" || "${FS,,}" == "tmpfs" ]]; then
|
||||
DISK_IO="threads"
|
||||
DISK_CACHE="writeback"
|
||||
fi
|
||||
|
||||
# Read memory
|
||||
RAM_SPARE=500000000
|
||||
RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}')
|
||||
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 [ -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
|
||||
normalizeCpuCores
|
||||
checkStorage
|
||||
checkFilesystem
|
||||
normalizeRamSize
|
||||
|
||||
# Print system info
|
||||
SYS="${SYS/-generic/}"
|
||||
@@ -133,75 +276,11 @@ 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
|
||||
|
||||
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
|
||||
|
||||
# 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
|
||||
|
||||
fi
|
||||
checkKvm
|
||||
|
||||
# Cleanup files
|
||||
rm -f /run/shm/qemu.*
|
||||
rm -f /run/shm/dsm.url
|
||||
rm -f "$QEMU_DIR"/dsm.url
|
||||
rm -f "$QEMU_DIR"/qemu.* "$QEMU_DIR"/*.pid "$QEMU_DIR"/*.sock
|
||||
|
||||
# Cleanup dirs
|
||||
rm -rf /tmp/dsm
|
||||
|
||||
+72
-38
@@ -5,11 +5,22 @@ set -Eeuo pipefail
|
||||
|
||||
: "${HOST_MAC:=""}"
|
||||
: "${HOST_DEBUG:=""}"
|
||||
: "${HOST_SERIAL:=""}"
|
||||
: "${HOST_MODEL:=""}"
|
||||
: "${HOST_SERIAL:=""}"
|
||||
: "${GUEST_SERIAL:=""}"
|
||||
|
||||
if [ -n "$HOST_MAC" ]; then
|
||||
# Sanitize variables
|
||||
HOST_MAC=$(strip "$HOST_MAC")
|
||||
HOST_MODEL=$(strip "$HOST_MODEL")
|
||||
HOST_SERIAL=$(strip "$HOST_SERIAL")
|
||||
GUEST_SERIAL=$(strip "$GUEST_SERIAL")
|
||||
|
||||
validateHostMac() {
|
||||
local m
|
||||
|
||||
if [ -z "$HOST_MAC" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
HOST_MAC="${HOST_MAC//-/:}"
|
||||
|
||||
@@ -22,54 +33,77 @@ if [ -n "$HOST_MAC" ]; then
|
||||
error "Invalid HOST_MAC address: '$HOST_MAC', should be 12 or 17 digits long!" && exit 28
|
||||
fi
|
||||
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
HOST_ARGS=()
|
||||
HOST_ARGS+=("-cpu=$CPU_CORES")
|
||||
HOST_ARGS+=("-cpu_arch=$HOST_CPU")
|
||||
buildHostArguments() {
|
||||
|
||||
[ -n "$HOST_MAC" ] && HOST_ARGS+=("-mac=$HOST_MAC")
|
||||
[ -n "$HOST_MODEL" ] && HOST_ARGS+=("-model=$HOST_MODEL")
|
||||
[ -n "$HOST_SERIAL" ] && HOST_ARGS+=("-hostsn=$HOST_SERIAL")
|
||||
[ -n "$GUEST_SERIAL" ] && HOST_ARGS+=("-guestsn=$GUEST_SERIAL")
|
||||
HOST_ARGS=()
|
||||
HOST_ARGS+=("-cpu=$CPU_CORES")
|
||||
HOST_ARGS+=("-cpu_arch=$HOST_CPU")
|
||||
|
||||
if [[ "$HOST_DEBUG" == [Yy1]* ]]; then
|
||||
set -x
|
||||
./host.bin "${HOST_ARGS[@]}" &
|
||||
{ set +x; } 2>/dev/null
|
||||
echo
|
||||
else
|
||||
./host.bin "${HOST_ARGS[@]}" >/dev/null &
|
||||
fi
|
||||
[ -n "$HOST_MAC" ] && HOST_ARGS+=("-mac=$HOST_MAC")
|
||||
[ -n "$HOST_MODEL" ] && HOST_ARGS+=("-model=$HOST_MODEL")
|
||||
[ -n "$HOST_SERIAL" ] && HOST_ARGS+=("-hostsn=$HOST_SERIAL")
|
||||
[ -n "$GUEST_SERIAL" ] && HOST_ARGS+=("-guestsn=$GUEST_SERIAL")
|
||||
|
||||
cnt=0
|
||||
sleep 0.2
|
||||
return 0
|
||||
}
|
||||
|
||||
while ! nc -z -w2 127.0.0.1 "$COM_PORT" > /dev/null 2>&1; do
|
||||
sleep 0.1
|
||||
cnt=$((cnt + 1))
|
||||
(( cnt > 50 )) && error "Failed to connect to qemu-host.." && exit 58
|
||||
done
|
||||
startHostBinary() {
|
||||
|
||||
cnt=0
|
||||
if enabled "$HOST_DEBUG"; then
|
||||
set -x
|
||||
./host.bin "${HOST_ARGS[@]}" &
|
||||
{ set +x; } 2>/dev/null
|
||||
echo "$!" > "$HOST_PID"
|
||||
echo
|
||||
else
|
||||
./host.bin "${HOST_ARGS[@]}" >/dev/null &
|
||||
echo "$!" > "$HOST_PID"
|
||||
fi
|
||||
|
||||
while ! nc -z -w2 127.0.0.1 "$CHR_PORT" > /dev/null 2>&1; do
|
||||
sleep 0.1
|
||||
cnt=$((cnt + 1))
|
||||
(( cnt > 50 )) && error "Failed to connect to qemu-host.." && exit 59
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
# Configure serial ports
|
||||
waitForPort() {
|
||||
|
||||
if [[ "$CONSOLE" != [Yy]* ]]; then
|
||||
SERIAL_OPTS="-serial pty"
|
||||
else
|
||||
SERIAL_OPTS="-serial mon:stdio"
|
||||
fi
|
||||
local port="$1"
|
||||
local exit_code="$2"
|
||||
local cnt=0
|
||||
|
||||
SERIAL_OPTS+=" \
|
||||
while ! nc -z -w2 127.0.0.1 "$port" > /dev/null 2>&1; do
|
||||
sleep 0.1
|
||||
cnt=$((cnt + 1))
|
||||
(( cnt > 50 )) && error "Failed to connect to qemu-host.." && exit "$exit_code"
|
||||
done
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
configureSerialPorts() {
|
||||
|
||||
# Configure serial ports
|
||||
SERIAL_OPTS="-serial mon:stdio \
|
||||
-device virtio-serial-pci,id=virtio-serial0,bus=pcie.0,addr=0x3 \
|
||||
-chardev socket,id=charchannel0,host=127.0.0.1,port=$CHR_PORT,reconnect=10 \
|
||||
-device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=vchannel"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
validateHostMac
|
||||
|
||||
HOST_PID="$QEMU_DIR/host.pid"
|
||||
|
||||
buildHostArguments
|
||||
startHostBinary
|
||||
|
||||
sleep 0.2
|
||||
|
||||
waitForPort "$COM_PORT" 58
|
||||
waitForPort "$CHR_PORT" 59
|
||||
|
||||
configureSerialPorts
|
||||
|
||||
return 0
|
||||
|
||||
+50
-13
@@ -2,38 +2,75 @@
|
||||
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
|
||||
# Sanitize port variables
|
||||
COM_PORT=$(strip "$COM_PORT")
|
||||
WEB_PORT=$(strip "$WEB_PORT")
|
||||
CHR_PORT=$(strip "$CHR_PORT")
|
||||
WSD_PORT=$(strip "$WSD_PORT")
|
||||
|
||||
html "Starting $APP for $ENGINE..."
|
||||
WEB_PID="/run/nginx.pid"
|
||||
WSD_PID="$QEMU_DIR/websocketd.pid"
|
||||
|
||||
if [[ "${WEB:-}" != [Nn]* ]]; then
|
||||
prepareWebFiles() {
|
||||
cp -r /var/www/* "$QEMU_DIR"
|
||||
rm -f "$WSD_PID" "$WEB_PID"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
configureWebPorts() {
|
||||
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
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
configureIpv6Listen() {
|
||||
|
||||
# shellcheck disable=SC2143
|
||||
if [ -f /proc/net/if_inet6 ] && [[ "$(cat /proc/sys/net/ipv6/conf/all/disable_ipv6 2>/dev/null)" != "1" ]] && [ -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
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
configureWebServer() {
|
||||
|
||||
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
|
||||
configureWebPorts
|
||||
configureIpv6Listen
|
||||
|
||||
# shellcheck disable=SC2143
|
||||
if [ -f /proc/net/if_inet6 ] && [ -n "$(ifconfig -a | grep inet6)" ]; then
|
||||
return 0
|
||||
}
|
||||
|
||||
sed -i "s/listen $WEB_PORT default_server;/listen [::]:$WEB_PORT default_server ipv6only=off;/g" /etc/nginx/sites-enabled/web.conf
|
||||
|
||||
fi
|
||||
startWebServer() {
|
||||
|
||||
# Start webserver
|
||||
nginx -e stderr
|
||||
}
|
||||
|
||||
startWebsocketServer() {
|
||||
|
||||
# Start websocket server
|
||||
websocketd --address 127.0.0.1 --port="$WSD_PORT" /run/socket.sh >/var/log/websocketd.log &
|
||||
echo "$!" > /var/run/websocketd.pid
|
||||
echo "$!" > "$WSD_PID"
|
||||
}
|
||||
|
||||
prepareWebFiles
|
||||
|
||||
html "Starting $APP for $ENGINE..."
|
||||
|
||||
if ! disabled "${WEB:-}"; then
|
||||
configureWebServer
|
||||
startWebServer
|
||||
startWebsocketServer
|
||||
fi
|
||||
|
||||
return 0
|
||||
|
||||
+19
-4
@@ -1,16 +1,31 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
lastmsg=""
|
||||
path="/run/shm/msg.html"
|
||||
|
||||
if [ -f "$path" ] && [ -s "$path" ]; then
|
||||
echo -n "s: " && cat "$path"
|
||||
fi
|
||||
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"* ) echo -n "s: " && cat "$path" ;;
|
||||
"modify"* ) refresh ;;
|
||||
"delete_self" ) echo "c: vnc" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
+156
-9
@@ -7,6 +7,40 @@ info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "${1:-}" "\E[0m\n"; }
|
||||
error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: ${1:-}" "\E[0m\n" >&2; }
|
||||
warn () { printf "%b%s%b" "\E[1;31m❯ " "Warning: ${1:-}" "\E[0m\n" >&2; }
|
||||
|
||||
strip() {
|
||||
local value="${1:-}"
|
||||
|
||||
# Remove surrounding whitespace
|
||||
value="${value#"${value%%[![:space:]]*}"}"
|
||||
value="${value%"${value##*[![:space:]]}"}"
|
||||
|
||||
# Remove leading/trailing single/double quotes
|
||||
value="${value%\"}"
|
||||
value="${value#\"}"
|
||||
value="${value%\'}"
|
||||
value="${value#\'}"
|
||||
|
||||
# Remove surrounding whitespace again
|
||||
value="${value#"${value%%[![:space:]]*}"}"
|
||||
value="${value%"${value##*[![:space:]]}"}"
|
||||
|
||||
printf '%s' "$value"
|
||||
}
|
||||
|
||||
enabled() {
|
||||
case "$(strip "${1:-}")" in
|
||||
Y|y|YES|Yes|yes|TRUE|True|true|1|ON|On|on) return 0 ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
disabled() {
|
||||
case "$(strip "${1:-}")" in
|
||||
N|n|NO|No|no|FALSE|False|false|0|OFF|Off|off) return 0 ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
formatBytes() {
|
||||
local result
|
||||
result=$(numfmt --to=iec --suffix=B "$1" | sed -r 's/([A-Z])/ \1/' | sed 's/ B/ bytes/g;')
|
||||
@@ -28,6 +62,7 @@ formatBytes() {
|
||||
|
||||
isAlive() {
|
||||
local pid="$1"
|
||||
[ -z "$pid" ] && return 1
|
||||
|
||||
if kill -0 "$pid" 2>/dev/null; then
|
||||
return 0
|
||||
@@ -36,23 +71,67 @@ isAlive() {
|
||||
return 1
|
||||
}
|
||||
|
||||
pKill() {
|
||||
waitPid() {
|
||||
local i=0
|
||||
local pid="$1"
|
||||
local timeout="${2:-10}"
|
||||
|
||||
{ kill -15 "$pid" || true; } 2>/dev/null
|
||||
|
||||
while isAlive "$pid"; do
|
||||
while [ -n "$pid" ] && isAlive "$pid"; do
|
||||
sleep 0.2
|
||||
i=$((i + 1))
|
||||
(( i >= timeout * 5 )) && return 1
|
||||
done
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
waitPidFile() {
|
||||
local i=0
|
||||
local pid=""
|
||||
local file="$1"
|
||||
local timeout="${2:-10}"
|
||||
|
||||
[ ! -s "$file" ] && return 0
|
||||
! read -r pid <"$file" && return 0
|
||||
[ -z "$pid" ] && return 0
|
||||
|
||||
while [ -s "$file" ] && isAlive "$pid"; do
|
||||
sleep 0.2
|
||||
i=$((i + 1))
|
||||
(( i >= timeout * 5 )) && return 1
|
||||
done
|
||||
|
||||
rm -f -- "$file"
|
||||
return 0
|
||||
}
|
||||
|
||||
pKill() {
|
||||
local pid="$1"
|
||||
local timeout="${2:-10}"
|
||||
|
||||
{ kill -15 -- "$pid" || :; } 2>/dev/null
|
||||
|
||||
if ! waitPid "$pid" "$timeout"; then
|
||||
warn "Timed out while waiting for PID $pid"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
fWait() {
|
||||
local i=0
|
||||
local name="$1"
|
||||
local timeout="${2:-10}"
|
||||
|
||||
[ -z "$name" ] && return 0
|
||||
|
||||
while pgrep -f -l "$name" >/dev/null; do
|
||||
sleep 0.2
|
||||
i=$((i + 1))
|
||||
if (( i >= timeout * 5 )); then
|
||||
warn "Timed out while waiting for process: $name"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
return 0
|
||||
@@ -60,9 +139,75 @@ fWait() {
|
||||
|
||||
fKill() {
|
||||
local name="$1"
|
||||
local timeout="${2:-10}"
|
||||
|
||||
{ pkill -f "$name" || true; } 2>/dev/null
|
||||
fWait "$name"
|
||||
[ -z "$name" ] && return 0
|
||||
|
||||
{ pkill -f "$name" || :; } 2>/dev/null
|
||||
fWait "$name" "$timeout"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
sKill() {
|
||||
local pid=""
|
||||
local file="$1"
|
||||
|
||||
[ ! -s "$file" ] && return 0
|
||||
! read -r pid <"$file" && return 0
|
||||
[ -z "$pid" ] && return 0
|
||||
|
||||
if isAlive "$pid"; then
|
||||
{ kill -15 -- "$pid" || :; } 2>/dev/null
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
mKill() {
|
||||
local timeout=10
|
||||
local files=("$@")
|
||||
|
||||
for file in "${files[@]}"; do
|
||||
sKill "$file"
|
||||
done
|
||||
|
||||
for file in "${files[@]}"; do
|
||||
if ! waitPidFile "$file" "$timeout"; then
|
||||
warn "Timed out while waiting for PID file: $file"
|
||||
fi
|
||||
done
|
||||
|
||||
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
|
||||
}
|
||||
@@ -123,11 +268,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/}"
|
||||
@@ -147,6 +292,8 @@ cpu() {
|
||||
|
||||
hasDisk() {
|
||||
|
||||
enabled "${DISK_DISABLE:-}" && return 1
|
||||
|
||||
[ -b "/disk" ] && return 0
|
||||
[ -b "/disk1" ] && return 0
|
||||
[ -b "/dev/disk1" ] && return 0
|
||||
|
||||
+5
-1
@@ -7,10 +7,14 @@ function getInfo() {
|
||||
var url = "msg.html";
|
||||
|
||||
try {
|
||||
if (request) {
|
||||
request.abort();
|
||||
}
|
||||
|
||||
if (window.XMLHttpRequest) {
|
||||
request = new XMLHttpRequest();
|
||||
} else {
|
||||
throw "XMLHttpRequest not available!";
|
||||
throw new Error("XMLHttpRequest not available!");
|
||||
}
|
||||
|
||||
request.onreadystatechange = processInfo;
|
||||
|
||||
Reference in New Issue
Block a user