virtual-dsm/src/install.sh

288 lines
7.6 KiB
Bash
Raw Normal View History

2023-03-28 07:24:17 +02:00
#!/usr/bin/env bash
2023-05-03 18:58:50 +02:00
set -Eeuo pipefail
STORAGE="/storage"
[ ! -d "$STORAGE" ] && error "Storage folder (${STORAGE}) not found!" && exit 13
if [ -f "$STORAGE"/dsm.ver ]; then
BASE=$(cat "${STORAGE}/dsm.ver")
else
# Fallback for old installs
BASE="DSM_VirtualDSM_42962"
fi
[ -n "$URL" ] && BASE=$(basename "$URL" .pat)
if [[ -f "$STORAGE/$BASE.boot.img" ]] && [[ -f "$STORAGE/$BASE.system.img" ]]; then
# Previous installation found
return 0
fi
2023-05-09 01:54:13 +02:00
# Display wait message
/run/server.sh 5000 install &
2023-04-21 21:57:05 +02:00
# Download the required files from the Synology website
2023-04-16 05:44:51 +02:00
DL="https://global.synologydownload.com/download/DSM"
2023-04-21 06:49:49 +02:00
if [ -z "$URL" ]; then
2023-04-14 16:12:14 +02:00
if [ "$ARCH" == "amd64" ]; then
2023-11-01 01:06:41 +01:00
URL="$DL/release/7.2.1/69057-1/DSM_VirtualDSM_69057.pat"
else
URL="$DL/release/7.0.1/42218/DSM_VirtualDSM_42218.pat"
fi
2023-04-14 16:12:14 +02:00
fi
2023-04-02 08:28:31 +02:00
2023-05-16 22:14:43 +02:00
# Check if output is to interactive TTY
2023-05-16 21:47:35 +02:00
if [ -t 1 ]; then
PROGRESS="--progress=bar:noscroll"
else
PROGRESS="--progress=dot:giga"
fi
2023-04-14 16:54:41 +02:00
BASE=$(basename "$URL" .pat)
2023-04-16 16:20:17 +02:00
rm -f "$STORAGE"/"$BASE".pat
rm -f "$STORAGE"/"$BASE".agent
rm -f "$STORAGE"/"$BASE".boot.img
rm -f "$STORAGE"/"$BASE".system.img
2023-04-14 17:08:55 +02:00
MIN_SPACE=6442450944
FS=$(stat -f -c %T "$STORAGE")
if [[ "$FS" != "fat"* && "$FS" != "vfat"* && "$FS" != "exfat"* && \
"$FS" != "ntfs"* && "$FS" != "fuse"* && "$FS" != "msdos"* ]]; then
TMP="$STORAGE/tmp"
else
TMP="/tmp/dsm"
SPACE=$(df --output=avail -B 1 /tmp | tail -n 1)
(( MIN_SPACE > SPACE )) && TMP="$STORAGE/tmp"
fi
rm -rf /tmp/dsm
rm -rf "$STORAGE/tmp"
rm -rf "$TMP" && mkdir -p "$TMP"
# Check free diskspace
SPACE=$(df --output=avail -B 1 "$TMP" | tail -n 1)
(( MIN_SPACE > SPACE )) && error "Not enough free space for installation, need at least 6 GB." && exit 95
2023-05-06 13:58:54 +02:00
[[ "${DEBUG}" == [Yy1]* ]] && set -x
2023-05-01 23:26:55 +02:00
2023-10-07 12:06:22 +02:00
RDC="$STORAGE/dsm.rd"
2023-05-25 13:36:13 +02:00
if [ ! -f "${RDC}" ]; then
2023-05-08 13:41:25 +02:00
2023-05-16 21:47:35 +02:00
info "Install: Downloading installer..."
2023-05-19 17:14:18 +02:00
RD="$TMP/rd.gz"
POS="65627648-71021835"
2023-05-19 17:32:13 +02:00
VERIFY="b4215a4b213ff5154db0488f92c87864"
2023-05-08 13:41:25 +02:00
LOC="$DL/release/7.0.1/42218/DSM_VirtualDSM_42218.pat"
2023-05-19 17:14:18 +02:00
{ curl -r "$POS" -sfk -o "$RD" "$LOC"; rc=$?; } || :
2023-05-11 03:53:48 +02:00
(( rc != 0 )) && error "Failed to download $LOC, reason: $rc" && exit 60
2023-04-17 15:11:14 +02:00
2023-05-08 13:41:25 +02:00
SUM=$(md5sum "$RD" | cut -f 1 -d " ")
2023-05-16 21:47:35 +02:00
if [ "$SUM" != "$VERIFY" ]; then
2023-05-16 22:14:43 +02:00
PAT="/install.pat"
2023-05-16 21:47:35 +02:00
rm "$RD"
rm -f "$PAT"
{ wget "$LOC" -O "$PAT" -q --no-check-certificate --show-progress "$PROGRESS"; rc=$?; } || :
(( rc != 0 )) && error "Failed to download $LOC, reason: $rc" && exit 60
2023-05-19 17:14:18 +02:00
tar --extract --file="$PAT" --directory="$(dirname "${RD}")"/. "$(basename "${RD}")"
2023-05-16 21:47:35 +02:00
rm "$PAT"
2023-05-08 13:41:25 +02:00
fi
2023-05-08 13:47:00 +02:00
2023-05-08 13:41:25 +02:00
cp "$RD" "$RDC"
2023-05-08 13:47:00 +02:00
2023-04-15 01:33:56 +02:00
fi
2023-05-25 13:36:13 +02:00
if [ -f "${RDC}" ]; then
2023-05-19 17:14:18 +02:00
{ xz -dc <"$RDC" >"$TMP/rd" 2>/dev/null; rc=$?; } || :
(( rc != 1 )) && error "Failed to unxz $RDC, reason $rc" && exit 91
2023-04-02 08:28:31 +02:00
{ (cd "$TMP" && cpio -idm <"$TMP/rd" 2>/dev/null); rc=$?; } || :
(( rc != 0 )) && error "Failed to cpio $RDC, reason $rc" && exit 92
2023-04-02 08:28:31 +02:00
mkdir -p /run/extract
for file in $TMP/usr/lib/libcurl.so.4 \
$TMP/usr/lib/libmbedcrypto.so.5 \
$TMP/usr/lib/libmbedtls.so.13 \
$TMP/usr/lib/libmbedx509.so.1 \
$TMP/usr/lib/libmsgpackc.so.2 \
$TMP/usr/lib/libsodium.so \
$TMP/usr/lib/libsynocodesign-ng-virtual-junior-wins.so.7 \
$TMP/usr/syno/bin/scemd; do
cp "$file" /run/extract/
done
2023-04-02 08:28:31 +02:00
2023-05-25 13:36:13 +02:00
if [ "$ARCH" != "amd64" ]; then
mkdir -p /lib64/
2023-05-25 13:38:46 +02:00
cp "$TMP/usr/lib/libc.so.6" /lib64/
cp "$TMP/usr/lib/libpthread.so.0" /lib64/
cp "$TMP/usr/lib/ld-linux-x86-64.so.2" /lib64/
2023-10-09 03:14:41 +02:00
fi
2023-05-25 13:36:13 +02:00
mv /run/extract/scemd /run/extract/syno_extract_system_patch
chmod +x /run/extract/syno_extract_system_patch
fi
2023-03-28 07:24:17 +02:00
rm -rf "$TMP" && mkdir -p "$TMP"
2023-05-11 03:53:48 +02:00
info "Install: Downloading $(basename "$URL")..."
2023-04-15 00:50:53 +02:00
PAT="/$BASE.pat"
rm -f "$PAT"
2023-04-30 00:36:37 +02:00
{ wget "$URL" -O "$PAT" -q --no-check-certificate --show-progress "$PROGRESS"; rc=$?; } || :
2023-04-21 20:49:39 +02:00
2023-05-20 01:53:46 +02:00
(( rc != 0 )) && error "Failed to download $URL, reason: $rc" && exit 69
2023-05-11 03:53:48 +02:00
[ ! -f "$PAT" ] && error "Failed to download $URL" && exit 69
2023-03-28 22:00:23 +02:00
2023-04-14 16:12:14 +02:00
SIZE=$(stat -c%s "$PAT")
2023-03-28 22:00:23 +02:00
2023-03-30 03:23:54 +02:00
if ((SIZE<250000000)); then
2023-05-17 14:18:56 +02:00
error "The specified PAT file is probably an update pack as it's too small." && exit 62
2023-03-30 03:23:54 +02:00
fi
2023-03-28 22:00:23 +02:00
2023-04-14 16:12:14 +02:00
if { tar tf "$PAT"; } >/dev/null 2>&1; then
2023-10-09 03:14:41 +02:00
info "Install: Extracting downloaded image..."
tar xpf "$PAT" -C "$TMP/."
2023-05-25 13:36:13 +02:00
2023-03-30 03:23:54 +02:00
else
2023-05-25 13:36:13 +02:00
if [ "$ARCH" != "amd64" ]; then
2023-10-09 03:14:41 +02:00
info "Install: Installing QEMU..."
2023-10-09 18:32:52 +02:00
export DEBCONF_NOWARNINGS="yes"
export DEBIAN_FRONTEND="noninteractive"
2023-05-25 13:36:13 +02:00
2023-10-09 18:32:52 +02:00
apt-get -qq update
apt-get -qq --no-install-recommends -y install qemu-user > /dev/null
2023-10-09 03:14:41 +02:00
fi
info "Install: Extracting downloaded image..."
export LD_LIBRARY_PATH="/run/extract"
2023-05-25 13:36:13 +02:00
if [ "$ARCH" == "amd64" ]; then
{ /run/extract/syno_extract_system_patch "$PAT" "$TMP/."; rc=$?; } || :
else
{ qemu-x86_64 /run/extract/syno_extract_system_patch "$PAT" "$TMP/."; rc=$?; } || :
fi
export LD_LIBRARY_PATH=""
2023-05-25 13:36:13 +02:00
(( rc != 0 )) && error "Failed to extract PAT file, reason $rc" && exit 63
2023-03-30 03:23:54 +02:00
fi
2023-03-28 07:24:17 +02:00
2023-03-30 03:23:54 +02:00
HDA="$TMP/hda1"
IDB="$TMP/indexdb"
PKG="$TMP/packages"
2023-03-30 03:23:54 +02:00
HDP="$TMP/synohdpack_img"
2023-03-28 22:00:23 +02:00
2023-05-17 14:18:56 +02:00
[ ! -f "$HDA.tgz" ] && error "The PAT file contains no OS image." && exit 64
2023-03-28 22:00:23 +02:00
2023-04-21 06:59:29 +02:00
BOOT=$(find "$TMP" -name "*.bin.zip")
2023-05-24 12:45:35 +02:00
[ ! -f "$BOOT" ] && error "The PAT file contains no boot image." && exit 67
2023-03-28 22:00:23 +02:00
2023-03-30 20:21:33 +02:00
BOOT=$(echo "$BOOT" | head -c -5)
2023-04-21 06:59:29 +02:00
unzip -q -o "$BOOT".zip -d "$TMP"
2023-03-28 07:24:17 +02:00
2023-05-11 03:53:48 +02:00
[[ "${ALLOCATE}" == [Zz]* ]] && info "Install: Allocating diskspace..."
2023-03-28 07:24:17 +02:00
2023-04-15 00:38:24 +02:00
SYSTEM="$TMP/sys.img"
SYSTEM_SIZE=4954537983
2023-04-15 02:15:18 +02:00
2023-04-18 18:16:41 +02:00
# Check free diskspace
SPACE=$(df --output=avail -B 1 "$TMP" | tail -n 1)
2023-05-19 17:39:37 +02:00
(( SYSTEM_SIZE > SPACE )) && error "Not enough free space to create a 4 GB system disk." && exit 87
2023-04-15 04:02:07 +02:00
2023-04-19 06:59:52 +02:00
if ! fallocate -l "${SYSTEM_SIZE}" "${SYSTEM}"; then
2023-07-03 11:26:10 +02:00
if ! truncate -s "${SYSTEM_SIZE}" "${SYSTEM}"; then
rm -f "${SYSTEM}" && error "Could not allocate a file for the system disk." && exit 88
fi
2023-04-19 06:59:52 +02:00
fi
2023-05-06 13:31:00 +02:00
if [[ "${ALLOCATE}" == [Zz]* ]]; then
2023-05-11 03:53:48 +02:00
info "Install: Preallocating 4 GB of diskspace..."
2023-04-19 07:05:20 +02:00
dd if=/dev/urandom of="${SYSTEM}" count="${SYSTEM_SIZE}" bs=1M iflag=count_bytes status=none
fi
2023-03-28 07:24:17 +02:00
# Check if file exists
2023-05-19 17:39:37 +02:00
[ ! -f "${SYSTEM}" ] && error "System disk does not exist ($SYSTEM)" && exit 89
# Check the filesize
2023-04-19 03:05:34 +02:00
SIZE=$(stat -c%s "${SYSTEM}")
2023-05-19 17:39:37 +02:00
[[ SIZE -ne SYSTEM_SIZE ]] && rm -f "${SYSTEM}" && error "System disk has the wrong size: ${SIZE}" && exit 90
2023-04-19 03:05:34 +02:00
2023-04-15 00:38:24 +02:00
PART="$TMP/partition.fdisk"
2023-03-28 07:24:17 +02:00
2023-10-09 18:32:52 +02:00
{ echo "label: dos"
echo "label-id: 0x6f9ee2e9"
echo "device: ${SYSTEM}"
echo "unit: sectors"
echo "sector-size: 512"
echo ""
echo "${SYSTEM}1 : start= 2048, size= 4980480, type=83"
echo "${SYSTEM}2 : start= 4982528, size= 4194304, type=82"
2023-04-21 07:14:43 +02:00
} > "$PART"
2023-04-21 06:59:29 +02:00
sfdisk -q "$SYSTEM" < "$PART"
2023-05-11 03:53:48 +02:00
info "Install: Extracting system partition..."
2023-03-28 07:24:17 +02:00
2023-04-15 00:38:24 +02:00
MOUNT="$TMP/system"
2023-04-21 06:59:29 +02:00
rm -rf "$MOUNT" && mkdir -p "$MOUNT"
2023-05-20 01:38:11 +02:00
mv "$HDA.tgz" "$HDA.txz"
2023-04-21 06:59:29 +02:00
tar xpfJ "$HDA.txz" --absolute-names -C "$MOUNT/"
2023-05-24 12:45:35 +02:00
[ -d "$PKG" ] && mv "$PKG/" "$MOUNT/.SynoUpgradePackages/"
2023-05-24 17:35:10 +02:00
rm -f "$MOUNT/.SynoUpgradePackages/ActiveInsight-"*
2023-05-24 12:45:35 +02:00
2023-05-24 17:35:10 +02:00
[ -f "$HDP.txz" ] && tar xpfJ "$HDP.txz" --absolute-names -C "$MOUNT/"
[ -f "$IDB.txz" ] && tar xpfJ "$IDB.txz" --absolute-names -C "$MOUNT/usr/syno/synoman/indexdb/"
2023-03-28 22:00:23 +02:00
2023-05-11 03:53:48 +02:00
info "Install: Installing system partition..."
2023-04-15 00:38:24 +02:00
LABEL="1.44.1-42218"
OFFSET="1048576" # 2048 * 512
NUMBLOCKS="622560" # (4980480 * 512) / 4096
2023-04-21 07:02:19 +02:00
mke2fs -q -t ext4 -b 4096 -d "$MOUNT/" -L "$LABEL" -F -E "offset=$OFFSET" "$SYSTEM" "$NUMBLOCKS"
2023-04-21 06:59:29 +02:00
rm -rf "$MOUNT"
2023-03-28 07:24:17 +02:00
2023-04-16 16:20:17 +02:00
echo "$BASE" > "$STORAGE"/dsm.ver
2023-10-05 15:47:17 +02:00
if [[ "$TMP" != "$STORAGE/tmp" ]]; then
# Check free diskspace
SPACE=$(df --output=avail -B 1 "$STORAGE" | tail -n 1)
(( MIN_SPACE > SPACE )) && error "Not enough free space in storage folder, need at least 6 GB." && exit 94
fi
2023-10-05 15:47:17 +02:00
2023-04-16 16:20:17 +02:00
mv -f "$PAT" "$STORAGE"/"$BASE".pat
mv -f "$BOOT" "$STORAGE"/"$BASE".boot.img
mv -f "$SYSTEM" "$STORAGE"/"$BASE".system.img
2023-03-28 07:24:17 +02:00
2023-04-21 06:59:29 +02:00
rm -rf "$TMP"
2023-05-01 23:26:55 +02:00
{ set +x; } 2>/dev/null
2023-05-06 13:58:54 +02:00
[[ "${DEBUG}" == [Yy1]* ]] && echo
2023-05-03 18:06:01 +02:00
return 0