Skip to content

Instantly share code, notes, and snippets.

@bcdonadio
Created August 26, 2019 02:18
Show Gist options
  • Save bcdonadio/e7fd34befb05dc2112fce5f7da6cf2af to your computer and use it in GitHub Desktop.
Save bcdonadio/e7fd34befb05dc2112fce5f7da6cf2af to your computer and use it in GitHub Desktop.
Install the NHS (https://www.nhs.com.br) UPS server software on Raspbian using qemu-user to emulate i386 arch on armv7l
#!/bin/bash
# Install the NHS (https://www.nhs.com.br) UPS server software on Raspbian
# using qemu-user to emulate i386 arch on armv7l
debRelease="stretch"
targetArch="i386"
serviceUser="nhsups"
zipURL="https://nhs.com.br/wp-content/uploads/2018/05/nhsups_3.1.36_x86_eGLIBC_2.11.zip"
installDir="/usr/local/nhs"
varDir="/var/lib/nhsups"
chrootName="${debRelease}_${targetArch}"
chrootDir="/${chrootName}"
serviceGroup="${serviceUser}"
apt install binfmt-support qemu qemu-user-static debootstrap schroot
qemu-debootstrap --arch="${targetArch}" "${debRelease}" "${chrootDir}"
temp=$(mktemp -d)
pushd $temp
mkdir "${chrootDir}${installDir}"
wget "${zipURL}" -O nhsups.zip
unzip -o nhsups.zip
rm -rf nhsups.zip
cp -Rf nhsups_*/nhsups_release/{nhsupsserver,web} "${chrootDir}${installDir}"
chmod +x "${chrootDir}${installDir}/nhsupsserver"
popd
rm -rf $temp
groupadd -r "${serviceGroup}" || true
useradd -r -g "${serviceGroup}" "${serviceUser}" || true
mkdir -p "${chrootDir}${varDir}"
cat >/etc/schroot/chroot.d/stretch_i386.conf <<EOF
[${chrootName}]
description=Debian ${debRelease} ${targetArch}
directory=${chrootDir}
root-users=root
users=root,${serviceUser}
type=directory
personality=linux32
EOF
cat >/etc/udev/rules.d/99-nhsups.rules <<EOF
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0925", ATTRS{idProduct}=="1241", MODE="0660", OWNER="${serviceUser}", GROUP="${serviceGroup}", SYMLINK+="nhsups"
EOF
udevadm control --reload-rules
udevadm trigger
cat >"${chrootDir}${varDir}/nhsupsserver.cfg" <<EOF
[com]
porta = /dev/nhsups
[smtp]
servidor = example.com
usar-ssl = 0
porta = 25
metodo-login = login
login = user@example.com
senha = password
remetente = noreply@example.com
[proxy]
porta = 80
[gsm]
porta = /dev/ttyS3
local = IP
contato = Nome
[timeout]
falha-rede = 30
desligamento = 60
[habilitar_deslig]
option = Y
mode = bateria
[autentic]
req = N
EOF
chown -R "${serviceUser}:${serviceGroup}" "${chrootDir}${varDir}"
cat >/etc/systemd/system/nhsups.service <<EOF
[Unit]
Description=NHS UPS monitor service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
ExecStart=/usr/bin/schroot -u ${serviceUser} -c chroot:${chrootName} -d ${installDir} nhsupsserver
Restart=always
RestartSec=1
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nhsups
sleep 2
systemctl status nhsups
echo "Service installed and running (hopefully) on port 2001!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment