🖥️ FHEM in CHROOT installieren, FHEM auf OpenWRT Gerät wie RPi/PC laufen lassen

Begonnen von Torxgewinde, 21 August 2023, 19:06:28

Vorheriges Thema - Nächstes Thema

Torxgewinde

Hallo,
Hier ein Skript um ein Debian Bookworm mit FHEM auf einem OpenWRT oder einem Ubuntu hochzuziehen. Bei einem Raspberry-Pi 3 oder 4 muss man arm64 wählen, auf einem x86 nimmt man amd64 und auf alten RPis "armhf" oder für RPi-1b "armel".

#!/bin/sh

THIS_BASE="/mnt/USB"
THIS_CHROOT="$THIS_BASE/debian"
THIS_CACHE="$THIS_BASE/cache"

#ARCH="arm64"
#ARCH="armhf"
ARCH="amd64"
#ARCH="armel"

#check if curl is installed
echo "checking if curl is installed:"
which curl || exit 1

echo "changing to directory: $THIS_BASE"
mkdir -p "$THIS_CHROOT" || exit 1
mkdir -p "$THIS_CACHE" || exit 1
cd "$THIS_BASE" || exit 1

# Extract the most recent version from the index page using curl and grep, the GET parameters sort it for us
LATEST_PACKAGE=$(curl -s "http://ftp.debian.org/debian/pool/main/c/cdebootstrap/?C=M;O=D" | grep "$ARCH"| grep -o "cdebootstrap-static_[^\"]*deb" | head -n1)
if [ -z "$LATEST_PACKAGE" ]; then
  echo "Failed to extract the latest package name."
  exit 1
fi
DOWNLOAD_URL="http://ftp.debian.org/debian/pool/main/c/cdebootstrap/$LATEST_PACKAGE"

echo "Downloading cdebootstrap-static: $DOWNLOAD_URL"
while true; do
    wget -O "$THIS_CACHE/cdebootstrap-static.deb" "$DOWNLOAD_URL" && break
    sleep 5
done

echo "unpacking cdebootstrap-static:"
cd "$THIS_CACHE"
ar x cdebootstrap-static.deb || exit 1
tar xvf data.tar.xz || exit 1

echo "Executing cdebootstrap-static:"
./usr/bin/cdebootstrap-static --arch $ARCH --configdir=./usr/share/cdebootstrap-static --helperdir=./usr/share/cdebootstrap-static/ stable "$THIS_CHROOT" || exit 1

#fhem package needs mount points, or it fails:
mount -t proc proc "$THIS_CHROOT/proc/" || exit 1
mount -t sysfs sys "$THIS_CHROOT/sys/" || exit 1
mount -o bind /dev "$THIS_CHROOT/dev/" || exit 1
mount -o bind /dev/pts "$THIS_CHROOT/dev/pts" || exit 1

echo "Installing additional packages in chroot:"
chroot "$THIS_CHROOT" apt update || exit 1
chroot "$THIS_CHROOT" apt -y install vim wget curl htop bash-completion gpg locales mpc \
                                     libxml-libxml-perl sqlite3 libdbi-perl \
                                     libdbd-sqlite3-perl libdigest-sha-perl \
                                     libmime-base64-urlsafe-perl libmime-base64-perl \
                                     libcrypt-cbc-perl libio-socket-ssl-perl libsoap-lite-perl \
                                     libhttp-request-ascgi-perl liblwp-protocol-https-perl \
                                     libhtml-parser-perl libwww-perl libdigest-sha-perl \
                                     libtest-lwp-useragent-perl || exit 1

echo "Setting timezone:"
chroot "$THIS_CHROOT" bash -c "ln -fs /usr/share/zoneinfo/Europe/Berlin /etc/localtime && \
                               dpkg-reconfigure --frontend noninteractive tzdata" || exit 1
                              
echo "configuring German as locale:"
chroot "$THIS_CHROOT" bash -c "echo \
'LANG=de_DE.UTF-8
LC_MESSAGES=de_DE.UTF-8
LC_COLLATE=de_DE.UTF-8
LC_TIME=de_DE.UTF-8' > /etc/default/locale" || exit 1
chroot "$THIS_CHROOT" bash -c "echo 'en_US.UTF-8 UTF-8' | tee -a /etc/locale.gen" || exit 1
chroot "$THIS_CHROOT" bash -c "echo 'de_DE.UTF-8 UTF-8' | tee -a /etc/locale.gen" || exit 1
chroot "$THIS_CHROOT" bash -c "locale-gen" || exit 1
chroot "$THIS_CHROOT" bash -c "update-locale LANG=de_DE.UTF-8" || exit 1

echo "Installing FHEM in chroot:"
chroot "$THIS_CHROOT" /bin/bash -c 'wget -O- https://debian.fhem.de/archive.key | gpg --dearmor > /usr/share/keyrings/debianfhemde-archive-keyring.gpg' || exit 1
chroot "$THIS_CHROOT" /bin/bash -c 'echo "deb [signed-by=/usr/share/keyrings/debianfhemde-archive-keyring.gpg] https://debian.fhem.de/nightly/ /" >> /etc/apt/sources.list' || exit 1
chroot "$THIS_CHROOT" apt update || exit 1
chroot "$THIS_CHROOT" apt -y install fhem || exit 1

## install custom CA to the chroot to be able to establish outgoing TLS connections to such servers:
# mkdir -p "$THIS_CHROOT/usr/local/share/ca-certificates/Torxgewinde/" || exit 1
# cp /etc/config/Torxgewinde.crt "$THIS_CHROOT/usr/local/share/ca-certificates/Torxgewinde/" || exit 1
# chroot "$THIS_CHROOT" update-ca-certificates || exit 1

##Set ownership to user "fhem" and group "dialout"
chroot "$THIS_CHROOT" /bin/bash -c "chown -R fhem:dialout /opt/fhem" || exit 1

#cleaning up
rm -rf "$THIS_CACHE" || exit 1
umount "$THIS_CHROOT/dev/pts" || exit 1
umount "$THIS_CHROOT/proc/" || exit 1
umount "$THIS_CHROOT/sys/" || exit 1
umount -l "$THIS_CHROOT/dev/" || exit 1

echo "$0: all done!"
exit 0

Als User "fhem" das Perl Skript "fhem.pl" ausführen mit:
# Check if everything is mounted
mount | grep "$THIS_CHROOT" > /dev/null
if [ $? -ne 0 ]; then
    mount -t proc proc "$THIS_CHROOT/proc/"
    mount -t sysfs sys "$THIS_CHROOT/sys/"
    mount -o bind /dev "$THIS_CHROOT/dev/"
    mount -o bind /dev/pts "$THIS_CHROOT/dev/pts"
fi

# Run FHEM within the chroot environment
chroot "$THIS_CHROOT" /bin/bash -c "su -l fhem -s /bin/bash -c 'cd /opt/fhem && perl fhem.pl fhem.cfg'"