Compare commits

...

4 Commits

Author SHA1 Message Date
Kroese
dec2dc9230 feat: Check free diskspace during startup (#1080) 2025-10-17 16:47:43 +02:00
Kroese
2c44669d91 feat: Improve Codespaces configuration (#1079) 2025-10-17 13:25:02 +02:00
Kroese
175d90aad7 fix: Move order in entry file (#1078) 2025-10-17 13:04:03 +02:00
Kroese
e1e5f8f91d fix: Validate status messages (#1077) 2025-10-17 12:07:13 +02:00
6 changed files with 128 additions and 72 deletions

View File

@@ -3,7 +3,8 @@ services:
container_name: dsm container_name: dsm
image: vdsm/virtual-dsm image: vdsm/virtual-dsm
environment: environment:
RAM_SIZE: "max" ALLOCATE: "Y"
RAM_SIZE: "half"
DISK_SIZE: "max" DISK_SIZE: "max"
CPU_CORES: "max" CPU_CORES: "max"
devices: devices:

View File

@@ -2,9 +2,7 @@
"name": "Virtual DSM", "name": "Virtual DSM",
"service": "dsm", "service": "dsm",
"containerEnv": { "containerEnv": {
"RAM_SIZE": "max", "ALLOCATE": "Y"
"DISK_SIZE": "max",
"CPU_CORES": "max"
}, },
"forwardPorts": [5000], "forwardPorts": [5000],
"portsAttributes": { "portsAttributes": {

View File

@@ -18,7 +18,7 @@ SYSTEM="$STORAGE/$BASE.system.img"
[ ! -s "$SYSTEM" ] && error "Virtual DSM system-image does not exist ($SYSTEM)" && exit 82 [ ! -s "$SYSTEM" ] && error "Virtual DSM system-image does not exist ($SYSTEM)" && exit 82
fmt2ext() { fmt2ext() {
local DISK_FMT=$1 local DISK_FMT="$1"
case "${DISK_FMT,,}" in case "${DISK_FMT,,}" in
qcow2) qcow2)
@@ -34,7 +34,7 @@ fmt2ext() {
} }
ext2fmt() { ext2fmt() {
local DISK_EXT=$1 local DISK_EXT="$1"
case "${DISK_EXT,,}" in case "${DISK_EXT,,}" in
qcow2) qcow2)
@@ -50,7 +50,7 @@ ext2fmt() {
} }
getSize() { getSize() {
local DISK_FILE=$1 local DISK_FILE="$1"
local DISK_EXT DISK_FMT local DISK_EXT DISK_FMT
DISK_EXT=$(echo "${DISK_FILE//*./}" | sed 's/^.*\.//') DISK_EXT=$(echo "${DISK_FILE//*./}" | sed 's/^.*\.//')
@@ -70,7 +70,7 @@ getSize() {
} }
isCow() { isCow() {
local FS=$1 local FS="$1"
if [[ "${FS,,}" == "btrfs" ]]; then if [[ "${FS,,}" == "btrfs" ]]; then
return 0 return 0
@@ -80,7 +80,7 @@ isCow() {
} }
supportsDirect() { supportsDirect() {
local FS=$1 local FS="$1"
if [[ "${FS,,}" == "ecryptfs" || "${FS,,}" == "tmpfs" ]]; then if [[ "${FS,,}" == "ecryptfs" || "${FS,,}" == "tmpfs" ]]; then
return 1 return 1
@@ -91,11 +91,11 @@ supportsDirect() {
createDisk() { createDisk() {
local DISK_FILE=$1 local DISK_FILE="$1"
local DISK_SPACE=$2 local DISK_SPACE="$2"
local DISK_DESC=$3 local DISK_DESC="$3"
local DISK_FMT=$4 local DISK_FMT="$4"
local FS=$5 local FS="$5"
local DATA_SIZE DIR SPACE GB FA local DATA_SIZE DIR SPACE GB FA
rm -f "$DISK_FILE" rm -f "$DISK_FILE"
@@ -113,6 +113,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 "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 error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 76
fi fi
fi fi
html "Creating a $DISK_DESC image..." html "Creating a $DISK_DESC image..."
@@ -177,16 +178,16 @@ createDisk() {
resizeDisk() { resizeDisk() {
local DISK_FILE=$1 local DISK_FILE="$1"
local DISK_SPACE=$2 local DISK_SPACE="$2"
local DISK_DESC=$3 local DISK_DESC="$3"
local DISK_FMT=$4 local DISK_FMT="$4"
local FS=$5 local FS="$5"
local CUR_SIZE DATA_SIZE DIR SPACE GB local CUR_SIZE DATA_SIZE DIR SPACE GB
CUR_SIZE=$(getSize "$DISK_FILE") CUR_SIZE=$(getSize "$DISK_FILE")
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE") 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 (( REQ < 1 )) && error "Shrinking disks is not supported yet, please increase ${DISK_DESC^^}_SIZE." && exit 71
if [[ "$ALLOCATE" != [Nn]* ]]; then if [[ "$ALLOCATE" != [Nn]* ]]; then
@@ -200,6 +201,7 @@ resizeDisk() {
error "Not enough free space to resize $DISK_DESC to ${DISK_SPACE/G/ GB} in $DIR, it has only $GB available.." 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 error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 74
fi fi
fi fi
GB=$(formatBytes "$CUR_SIZE") GB=$(formatBytes "$CUR_SIZE")
@@ -245,13 +247,13 @@ resizeDisk() {
convertDisk() { convertDisk() {
local SOURCE_FILE=$1 local SOURCE_FILE="$1"
local SOURCE_FMT=$2 local SOURCE_FMT="$2"
local DST_FILE=$3 local DST_FILE="$3"
local DST_FMT=$4 local DST_FMT="$4"
local DISK_BASE=$5 local DISK_BASE="$5"
local DISK_DESC=$6 local DISK_DESC="$6"
local FS=$7 local FS="$7"
[ -f "$DST_FILE" ] && error "Conversion failed, destination file $DST_FILE already exists?" && exit 79 [ -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 exists?" && exit 79
@@ -273,6 +275,7 @@ convertDisk() {
error "Not enough free space to convert $DISK_DESC to $DST_FMT in $DIR, it has only $GB available..." 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 error "Please free up some disk space or disable preallocation by setting ALLOCATE=N." && exit 76
fi fi
fi fi
local msg="Converting $DISK_DESC to $DST_FMT" local msg="Converting $DISK_DESC to $DST_FMT"
@@ -327,9 +330,9 @@ convertDisk() {
checkFS () { checkFS () {
local FS=$1 local FS="$1"
local DISK_FILE=$2 local DISK_FILE="$2"
local DISK_DESC=$3 local DISK_DESC="$3"
local DIR FA local DIR FA
DIR=$(dirname "$DISK_FILE") DIR=$(dirname "$DISK_FILE")
@@ -361,15 +364,15 @@ checkFS () {
createDevice () { createDevice () {
local DISK_FILE=$1 local DISK_FILE="$1"
local DISK_TYPE=$2 local DISK_TYPE="$2"
local DISK_INDEX=$3 local DISK_INDEX="$3"
local DISK_ADDRESS=$4 local DISK_ADDRESS="$4"
local DISK_FMT=$5 local DISK_FMT="$5"
local DISK_IO=$6 local DISK_IO="$6"
local DISK_CACHE=$7 local DISK_CACHE="$7"
local DISK_SERIAL=$8 local DISK_SERIAL="$8"
local DISK_SECTORS=$9 local DISK_SECTORS="$9"
local DISK_ID="data$DISK_INDEX" local DISK_ID="data$DISK_INDEX"
local index="" local index=""
@@ -415,16 +418,16 @@ createDevice () {
addDisk () { addDisk () {
local DISK_BASE=$1 local DISK_BASE="$1"
local DISK_TYPE=$2 local DISK_TYPE="$2"
local DISK_DESC=$3 local DISK_DESC="$3"
local DISK_SPACE=$4 local DISK_SPACE="$4"
local DISK_INDEX=$5 local DISK_INDEX="$5"
local DISK_ADDRESS=$6 local DISK_ADDRESS="$6"
local DISK_FMT=$7 local DISK_FMT="$7"
local DISK_IO=$8 local DISK_IO="$8"
local DISK_CACHE=$9 local DISK_CACHE="$9"
local DISK_EXT DIR SPACE GB DATA_SIZE FS PREV_FMT PREV_EXT CUR_SIZE local DISK_EXT DIR SPACE GB DATA_SIZE FS PREV_FMT PREV_EXT CUR_SIZE LEFT FREE USED
DISK_EXT=$(fmt2ext "$DISK_FMT") DISK_EXT=$(fmt2ext "$DISK_FMT")
local DISK_FILE="$DISK_BASE.$DISK_EXT" local DISK_FILE="$DISK_BASE.$DISK_EXT"
@@ -435,22 +438,22 @@ addDisk () {
if [[ "${DISK_SPACE,,}" == "max" || "${DISK_SPACE,,}" == "half" ]]; then if [[ "${DISK_SPACE,,}" == "max" || "${DISK_SPACE,,}" == "half" ]]; then
local SPARE=2147483648 local SPARE=2147483648
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1) FREE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
if [[ "${DISK_SPACE,,}" == "max" ]]; then if [[ "${DISK_SPACE,,}" == "max" ]]; then
SPACE=$((SPACE-SPARE)) FREE=$(( FREE - SPARE ))
else else
SPACE=$(( SPACE / 2 )) FREE=$(( FREE / 2 ))
fi fi
(( SPACE < SPARE )) && SPACE="$SPARE" (( FREE < SPARE )) && FREE="$SPARE"
GB=$(( SPACE/1073741825 )) GB=$(( FREE / 1073741825 ))
DISK_SPACE="${GB}G" DISK_SPACE="${GB}G"
fi fi
SPACE="${DISK_SPACE// /}" SPACE="${DISK_SPACE// /}"
[ -z "$SPACE" ] && SPACE="16G" [ -z "$SPACE" ] && SPACE="256G"
[ -z "${SPACE//[0-9. ]}" ] && SPACE="${SPACE}G" [ -z "${SPACE//[0-9. ]}" ] && SPACE="${SPACE}G"
SPACE=$(echo "${SPACE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') SPACE=$(echo "${SPACE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
@@ -461,7 +464,7 @@ addDisk () {
DATA_SIZE=$(numfmt --from=iec "$SPACE") DATA_SIZE=$(numfmt --from=iec "$SPACE")
if (( DATA_SIZE < 6442450944 )); then if (( DATA_SIZE < 6442450944 )); then
error "Please increase ${DISK_DESC^^}_SIZE to at least 6 GB." && exit 73 error "Please increase the ${DISK_DESC^^}_SIZE variable to at least 6 GB." && exit 73
fi fi
FS=$(stat -f -c %T "$DIR") FS=$(stat -f -c %T "$DIR")
@@ -472,7 +475,7 @@ addDisk () {
DISK_CACHE="writeback" DISK_CACHE="writeback"
fi fi
if ! [ -s "$DISK_FILE" ] ; then if [ ! -s "$DISK_FILE" ] ; then
if [[ "${DISK_FMT,,}" != "raw" ]]; then if [[ "${DISK_FMT,,}" != "raw" ]]; then
PREV_FMT="raw" PREV_FMT="raw"
@@ -485,6 +488,7 @@ addDisk () {
if [ -s "$DISK_BASE.$PREV_EXT" ] ; then if [ -s "$DISK_BASE.$PREV_EXT" ] ; then
convertDisk "$DISK_BASE.$PREV_EXT" "$PREV_FMT" "$DISK_FILE" "$DISK_FMT" "$DISK_BASE" "$DISK_DESC" "$FS" || exit $? convertDisk "$DISK_BASE.$PREV_EXT" "$PREV_FMT" "$DISK_FILE" "$DISK_FMT" "$DISK_BASE" "$DISK_DESC" "$FS" || exit $?
fi fi
fi fi
if [ -s "$DISK_FILE" ]; then if [ -s "$DISK_FILE" ]; then
@@ -492,7 +496,18 @@ addDisk () {
CUR_SIZE=$(getSize "$DISK_FILE") CUR_SIZE=$(getSize "$DISK_FILE")
if (( DATA_SIZE > CUR_SIZE )); then if (( DATA_SIZE > CUR_SIZE )); then
resizeDisk "$DISK_FILE" "$SPACE" "$DISK_DESC" "$DISK_FMT" "$FS" || exit $? 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 fi
else else
@@ -501,6 +516,33 @@ addDisk () {
fi fi
if [ -f "$DISK_FILE" ] && [[ "$ALLOCATE" == [Nn]* ]]; then
CUR_SIZE=$(getSize "$DISK_FILE")
USED=$(du -sB 1 "$DISK_FILE" | cut -f1)
FREE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
LEFT=$(( CUR_SIZE - USED - FREE ))
if (( LEFT > 0 )); then
GB=$(formatBytes "$FREE")
LEFT=$(formatBytes "$LEFT")
CUR_SIZE=$(formatBytes "$CUR_SIZE")
msg="the virtual size of the ${DISK_DESC,,} is $CUR_SIZE"
if [[ "$USED" == "0" ]]; then
msg+=","
else
USED=$(formatBytes "$USED")
msg+=" (of which $USED is used),"
fi
warn "$msg but there is only $GB of free space left in $DIR, make at least $LEFT more room available!"
fi
fi
DISK_OPTS+=$(createDevice "$DISK_FILE" "$DISK_TYPE" "$DISK_INDEX" "$DISK_ADDRESS" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" "" "") DISK_OPTS+=$(createDevice "$DISK_FILE" "$DISK_TYPE" "$DISK_INDEX" "$DISK_ADDRESS" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" "" "")
return 0 return 0
@@ -508,10 +550,10 @@ addDisk () {
addDevice () { addDevice () {
local DISK_DEV=$1 local DISK_DEV="$1"
local DISK_TYPE=$2 local DISK_TYPE="$2"
local DISK_INDEX=$3 local DISK_INDEX="$3"
local DISK_ADDRESS=$4 local DISK_ADDRESS="$4"
[ -z "$DISK_DEV" ] && return 0 [ -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 [ ! -b "$DISK_DEV" ] && error "Device $DISK_DEV cannot be found! Please add it to the 'devices' section of your compose file." && exit 55
@@ -523,7 +565,7 @@ addDevice () {
physical=$(echo "$result" | grep -m 1 -o "/ .*" | cut -c 3-) physical=$(echo "$result" | grep -m 1 -o "/ .*" | cut -c 3-)
physical="${physical%% *}" physical="${physical%% *}"
if [ -n "$physical" ]; then if [ -n "$physical" ]; then
if [[ "$physical" != "512" ]]; then if [[ "$physical" != "512" ]]; then
sectors=",logical_block_size=$logical,physical_block_size=$physical" sectors=",logical_block_size=$logical,physical_block_size=$physical"
fi fi

View File

@@ -18,8 +18,8 @@ cd /run
. proc.sh # Initialize processor . proc.sh # Initialize processor
. serial.sh # Initialize serialport . serial.sh # Initialize serialport
. power.sh # Configure shutdown . power.sh # Configure shutdown
. config.sh # Configure arguments
. memory.sh # Check available memory . memory.sh # Check available memory
. config.sh # Configure arguments
. finish.sh # Finish initialization . finish.sh # Finish initialization
trap - ERR trap - ERR

View File

@@ -2,7 +2,7 @@
set -Eeuo pipefail set -Eeuo pipefail
RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}') RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}')
if [[ "$RAM_CHECK" != [Nn]* && "${RAM_SIZE,,}" != "max" && "${RAM_SIZE,,}" != "half" ]]; then if [[ "$RAM_CHECK" != [Nn]* && "${RAM_SIZE,,}" != "max" && "${RAM_SIZE,,}" != "half" ]]; then
AVAIL_MEM=$(formatBytes "$RAM_AVAIL") AVAIL_MEM=$(formatBytes "$RAM_AVAIL")
@@ -59,15 +59,15 @@ if [[ "${RAM_SIZE,,}" == "max" ]]; then
RAM_WANTED=$(( RAM_WANTED / 1073741825 )) RAM_WANTED=$(( RAM_WANTED / 1073741825 ))
if (( "$RAM_WANTED" < 1 )); then if (( "$RAM_WANTED" < 1 )); then
RAM_WANTED=$(( RAM_AVAIL - RAM_SPARE )) RAM_WANTED=$(( RAM_AVAIL - RAM_SPARE ))
RAM_WANTED=$(( RAM_WANTED / 1048577 )) RAM_WANTED=$(( RAM_WANTED / 1048577 ))
if (( "$RAM_WANTED" < 1 )); then if (( "$RAM_WANTED" < 1 )); then
RAM_WANTED=$(( RAM_AVAIL )) RAM_WANTED=$(( RAM_AVAIL ))
RAM_WANTED=$(( RAM_WANTED / 1048577 )) RAM_WANTED=$(( RAM_WANTED / 1048577 ))
fi fi
RAM_SIZE="${RAM_WANTED}M" RAM_SIZE="${RAM_WANTED}M"
@@ -80,7 +80,7 @@ if [[ "${RAM_SIZE,,}" == "max" ]]; then
else else
RAM_SIZE="${RAM_WANTED}G" RAM_SIZE="${RAM_WANTED}G"
fi fi
fi fi
return 0 return 0

View File

@@ -1,16 +1,31 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -Eeuo pipefail set -Eeuo pipefail
lastmsg=""
path="/run/shm/msg.html" path="/run/shm/msg.html"
if [ -f "$path" ] && [ -s "$path" ]; then refresh() {
echo -n "s: " && cat "$path"
fi [ ! -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" | inotifywait -m "$path" |
while read -r fp event fn; do while read -r fp event fn; do
case "${event,,}" in case "${event,,}" in
"modify"* ) echo -n "s: " && cat "$path" ;; "modify"* ) refresh ;;
"delete_self" ) echo "c: vnc" ;; "delete_self" ) echo "c: vnc" ;;
esac esac
done done