OpenMediaVault_installScript/install
Santiago Reig 96b4650086
correctly setup network interfaces
Add available network interfaces to OMV settings, so all interfaces are up after first reboot

Ethernet is added with "predictable naming" to keep connectivity after first reboot
If wpa_supplicant.conf is found:
-  Setup wifi country in crda file (without this, wifi can't be enabled)
- SSID and password are taken and added to OMV
2020-05-02 18:23:30 +02:00

507 lines
16 KiB
Bash
Executable File

#!/bin/bash
#
# shellcheck disable=SC1090,SC1091,SC1117,SC2016,SC2046,SC2086
#
# Copyright (c) 2015-2020 OpenMediaVault Plugin Developers
# Copyright (c) 2017-2020 Armbian Developers
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.
#
# Ideas/code used from:
# https://github.com/armbian/config/blob/master/debian-software
# https://forum.openmediavault.org/index.php/Thread/25062-Install-OMV5-on-Debian-10-Buster/
#
# version: 1.0.9
#
if [[ $(id -u) -ne 0 ]]; then
echo "This script must be executed as root or using sudo."
exit 99
fi
systemd="$(ps --no-headers -o comm 1)"
if [ ! "${systemd}" = "systemd" ]; then
echo "This system is not running systemd. Exiting..."
exit 100
fi
declare -l codename
declare -l omvCodename
declare -l omvInstall=""
declare -l omvextrasInstall=""
declare -i skipFlash=0
declare -i version
cpuFreqDef="/etc/default/cpufrequtils"
defaultGovSearch="^CONFIG_CPU_FREQ_DEFAULT_GOV_"
ioniceCron="/etc/cron.d/make_nas_processes_faster"
ioniceScript="/usr/sbin/omv-ionice"
keyserver="hkp://keyserver.ubuntu.com:80"
omvKey="/etc/apt/trusted.gpg.d/openmediavault-archive-keyring.asc"
omvRepo="http://packages.openmediavault.org/public"
omvSources="/etc/apt/sources.list.d/openmediavault.list"
smbOptions="min receivefile size = 16384\nwrite cache size = 524288\ngetwd cache = yes\nsocket options = TCP_NODELAY IPTOS_LOWDELAY"
url="https://github.com/OpenMediaVault-Plugin-Developers/packages/raw/master/"
export DEBIAN_FRONTEND=noninteractive
export APT_LISTCHANGES_FRONTEND=none
export LANG=C.UTF-8
if [ -f /etc/armbian-release ]; then
. /etc/armbian-release
fi
while getopts "fh" opt; do
echo "option ${opt}"
case "${opt}" in
f)
skipFlash=1
;;
h)
echo "Use the following flags:"
echo " -f"
echo " to skip the installation of the flashmemory plugin"
echo ""
echo "Examples:"
echo " install"
echo " install -f"
exit 100
;;
\?)
echo "Invalid option: -${OPTARG}"
;;
esac
done
# Fix permissions on / if wrong
echo "Current / permissions = $(stat -c %a /)"
chmod g-w,o-w /
echo "New / permissions = $(stat -c %a /)"
echo "Updating repos before installing..."
apt-get update
echo "Installing lsb_release..."
apt-get --yes --no-install-recommends --reinstall install lsb-release
arch="$(dpkg --print-architecture)"
codename="$(lsb_release --codename --short)"
distributor="$(lsb_release --id --short)"
case ${codename} in
stretch)
confCmd="omv-mkconf"
ntp="ntp"
omvCodename="arrakis"
phpfpm="php-fpm"
version=4
;;
buster|eoan)
confCmd="omv-salt deploy run"
ntp="chrony"
omvCodename="usul"
phpfpm="phpfpm"
version=5
;;
*)
echo "Unsupported version. Exiting..."
exit 1
;;
esac
echo "${omvCodename} :: ${version}"
hostname=$(</etc/hostname)
tz=$(</etc/timezone)
# Add Debian signing keys to raspbian to prevent apt-get update failures
# when OMV adds security and/or backports repos
if [[ "${distributor}" == "Raspbian" ]]; then
echo "Adding Debian signing keys..."
for key in AA8E81B4331F7F50 112695A0E562B32A 04EE7237B7D453EC 648ACFD622F3D138; do
apt-key adv --no-tty --keyserver ${keyserver} --recv-keys "${key}"
done
fi
echo "Install prerequisites..."
apt-get --yes --no-install-recommends install dirmngr gnupg
# install openmediavault if not installed already
omvInstall=$(dpkg -l | awk '$2 == "openmediavault" { print $1 }')
if [[ ! "${omvInstall}" == "ii" ]]; then
echo "Installing openmediavault required packages..."
if ! apt-get --yes --no-install-recommends install postfix; then
echo "failed installing postfix"
exit 2
fi
echo "Adding openmediavault repo and key..."
echo "deb ${omvRepo} ${omvCodename} main" > ${omvSources}
wget -O "${omvKey}" ${omvRepo}/archive.key
apt-key add "${omvKey}"
echo "Updating repos..."
if ! apt-get update; then
echo "failed to update apt repos."
exit 2
fi
echo "Install openmediavault-keyring..."
if ! apt-get --yes install openmediavault-keyring; then
echo "failed to install openmediavault-keyring package."
exit 2
fi
echo "Installing openmediavault..."
aptFlags="--yes --auto-remove --show-upgraded --allow-downgrades --allow-change-held-packages --no-install-recommends"
cmd="apt-get ${aptFlags} install openmediavault"
if ! ${cmd}; then
echo "failed to install openmediavault package."
exit 2
fi
if [ ${version} -gt 4 ]; then
omv-confdbadm populate
else
omv-initsystem
omv-mkconf interfaces
omv-mkconf issue
fi
fi
# check if openmediavault is install properly
omvInstall=$(dpkg -l | awk '$2 == "openmediavault" { print $1 }')
if [[ ! "${omvInstall}" == "ii" ]]; then
echo "openmediavault package failed to install or is in a bad state."
exit 3
fi
. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions
# remove backports from sources.list to avoid duplicate sources warning
sed -i "/\(stretch\|buster\)-backports/d" /etc/apt/sources.list
if [ "${codename}" = "eoan" ]; then
omv_set_default "OMV_APT_USE_KERNEL_BACKPORTS" false true
fi
# install omv-extras
echo "Downloading omv-extras.org plugin for openmediavault ${version}.x ..."
file="openmediavault-omvextrasorg_latest_all${version}.deb"
if [ -f "${file}" ]; then
rm ${file}
fi
wget ${url}/${file}
if [ -f "${file}" ]; then
if ! dpkg --install ${file}; then
echo "Installing other dependencies ..."
apt-get --yes --fix-broken install
omvextrasInstall=$(dpkg -l | awk '$2 == "openmediavault-omvextrasorg" { print $1 }')
if [[ ! "${omvextrasInstall}" == "ii" ]]; then
echo "omv-extras failed to install correctly. Trying to fix with ${confCmd} ..."
if ${confCmd} omvextras; then
echo "Trying to fix apt ..."
apt-get --yes --fix-broken install
else
echo "${confCmd} failed and openmediavault-omvextrasorg is in a bad state."
exit 3
fi
fi
omvextrasInstall=$(dpkg -l | awk '$2 == "openmediavault-omvextrasorg" { print $1 }')
if [[ ! "${omvextrasInstall}" == "ii" ]]; then
echo "openmediavault-omvextrasorg package failed to install or is in a bad state."
exit 3
fi
fi
echo "Updating repos ..."
apt-get update
else
echo "There was a problem downloading the package."
fi
# disable armbian log services if found
for service in log2ram armbian-ramlog armbian-zram-config; do
if systemctl list-units --full -all | grep ${service}; then
systemctl stop ${service}
systemctl disable ${service}
fi
done
rm -f /etc/cron.daily/armbian-ram-logging
if [ -f "/etc/default/armbian-ramlog" ]; then
sed -i "s/ENABLED=.*/ENABLED=false/g" /etc/default/armbian-ramlog
fi
if [ -f "/etc/default/armbian-zram-config" ]; then
sed -i "s/ENABLED=.*/ENABLED=false/g" /etc/default/armbian-zram-config
fi
if [ -f "/etc/systemd/system/logrotate.service" ]; then
rm -f /etc/systemd/system/logrotate.service
systemctl daemon-reload
fi
# install flashmemory plugin unless disabled
if [ ${skipFlash} -eq 1 ]; then
echo "Skipping installation of the flashmemory plugin."
else
echo "Install folder2ram..."
if apt-get --yes --fix-missing --no-install-recommends install folder2ram; then
echo "Installed folder2ram."
else
echo "Failed to install folder2ram."
fi
echo "Install flashmemory plugin..."
if apt-get --yes install openmediavault-flashmemory; then
echo "Installed flashmemory plugin."
else
echo "Failed to install flashmemory plugin."
${confCmd} flashmemory
apt-get --yes --fix-broken install
fi
fi
# change default OMV settings
xmlstarlet ed -L -u "/config/services/smb/extraoptions" -v "$(echo -e "${smbOptions}")" ${OMV_CONFIG_FILE}
xmlstarlet ed -L -u "/config/services/ssh/enable" -v "1" ${OMV_CONFIG_FILE}
xmlstarlet ed -L -u "/config/services/ssh/permitrootlogin" -v "1" ${OMV_CONFIG_FILE}
xmlstarlet ed -L -u "/config/system/time/ntp/enable" -v "1" ${OMV_CONFIG_FILE}
xmlstarlet ed -L -u "/config/system/time/timezone" -v "${tz}" ${OMV_CONFIG_FILE}
xmlstarlet ed -L -u "/config/system/network/dns/hostname" -v "${hostname}" ${OMV_CONFIG_FILE}
# disable monitoring and apply changes
/usr/sbin/omv-rpc -u admin "perfstats" "set" '{"enable":false}'
/usr/sbin/omv-rpc -u admin "config" "applyChanges" '{ "modules": ["monit","rrdcached","collectd"],"force": true }'
# set min and max frequency for RPi boards
if [[ "${distributor}" == "Raspbian" ]]; then
MIN_SPEED="$(</sys/devices/system/cpu/cpufreq/policy0/cpuinfo_min_freq)"
MAX_SPEED="$(</sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq)"
# Determine if RPi4 (for future use)
if [[ $(awk '$1 == "Revision" { print $3 }' /proc/cpuinfo) =~ [a-c]03111 ]]; then
BOARD="rpi4"
fi
cat << EOF > ${cpuFreqDef}
GOVERNOR="ondemand"
MIN_SPEED="${MIN_SPEED}"
MAX_SPEED="${MAX_SPEED}"
EOF
fi
if [ -f "${cpuFreqDef}" ]; then
. ${cpuFreqDef}
else
# set cpufreq settings if no defaults
if [ -f "/proc/config.gz" ]; then
defaultGov="$(zgrep "${defaultGovSearch}" /proc/config.gz | sed -e "s/${defaultGovSearch}\(.*\)=y/\1/")"
elif [ -f "/boot/config-$(uname -r)" ]; then
defaultGov="$(grep "${defaultGovSearch}" /boot/config-$(uname -r) | sed -e "s/${defaultGovSearch}\(.*\)=y/\1/")"
fi
if [ -z "${DEFAULT_GOV}" ]; then
defaultGov="ondemand"
fi
GOVERNOR=${defaultGov,,}
MIN_SPEED="0"
MAX_SPEED="0"
fi
# set defaults in /etc/default/openmediavault
omv_set_default "OMV_CPUFREQUTILS_GOVERNOR" "${GOVERNOR}"
omv_set_default "OMV_CPUFREQUTILS_MINSPEED" "${MIN_SPEED}"
omv_set_default "OMV_CPUFREQUTILS_MAXSPEED" "${MAX_SPEED}"
if [ ${version} -gt 4 ]; then
# update pillar default list - /srv/pillar/omv/default.sls
omv-salt stage run prepare
fi
# update config files
for service in nginx ${phpfpm} samba flashmemory ssh ${ntp} timezone monit rrdcached collectd cpufrequtils ; do
${confCmd} ${service}
done
# create php directories if they don't exist
modDir="/var/lib/php/modules"
if [ ! -d "${modDir}" ]; then
mkdir --parents --mode=0755 ${modDir}
fi
sessDir="/var/lib/php/sessions"
if [ ! -d "${sessDir}" ]; then
mkdir --parents --mode=1733 ${sessDir}
fi
if [[ "${arch}" == "amd64" ]] || [[ "${arch}" == "i386" ]]; then
# skip ionice on x86 boards
echo "Done."
exit 0
fi
# Add a cron job to make NAS processes more snappy and silence rsyslog
cat << EOF > /etc/rsyslog.d/omv-armbian.conf
:msg, contains, "omv-ionice" ~
:msg, contains, "action " ~
:msg, contains, "netsnmp_assert" ~
:msg, contains, "Failed to initiate sched scan" ~
EOF
systemctl restart rsyslog
# add taskset to ionice cronjob for biglittle boards
case ${BOARD} in
odroidxu4|bananapim3|nanopifire3|nanopct3plus|nanopim3)
taskset='; taskset -c -p 4-7 ${srv}'
;;
*rk3399*|*edge*|nanopct4|nanopim4|nanopineo4|renegade-elite|rockpi-4*|rockpro64)
taskset='; taskset -c -p 4-5 ${srv}'
;;
odroidn2)
taskset='; taskset -c -p 2-5 ${srv}'
;;
esac
# create ionice script
cat << EOF > ${ioniceScript}
#!/bin/sh
for srv in \$(pgrep "ftpd|nfsiod|smbd"); do
ionice -c1 -p \${srv} ${taskset};
done
EOF
chmod 755 ${ioniceScript}
# create ionice cronjob
cat << EOF > ${ioniceCron}
* * * * * root ${ioniceScript} >/dev/null 2>&1
EOF
chmod 600 ${ioniceCron}
if getent passwd pi > /dev/null; then
echo "Adding pi user to ssh group ..."
usermod -a -G ssh pi
fi
# remove networkmanager and dhcpcd5 then configure networkd
if [ ${version} -gt 4 ]; then
defLink="/etc/systemd/network/99-default.link"
if [ -e "${defLink}" ]; then
rm -f "${defLink}"
fi
nic="eth0"
if grep -qw "${nic}" /proc/net/dev; then
echo "Removing network-manager and dhcpcd5 ..."
apt-get -y --autoremove purge network-manager dhcpcd5
echo "Enable and start systemd-resolved ..."
systemctl enable systemd-resolved
systemctl start systemd-resolved
rm /etc/resolv.conf
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
echo "Configure ${nic} to use networkd ..."
mkdir -p /etc/netplan
# use preditable naming so doesn't break on reboot
nicMac=$(cat /sys/class/net/$nic/address | tr -d ':')
UUID=$(cat /proc/sys/kernel/random/uuid)
xmlstarlet ed -L \
-s "/config/system/network/interfaces" -t elem -n interfaceTMP \
-s //interfaceTMP -t elem -n uuid -v "$UUID" \
-s //interfaceTMP -t elem -n type -v "ethernet" \
-s //interfaceTMP -t elem -n devicename -v "enx$nicMac" \
-s //interfaceTMP -t elem -n method -v "dhcp" \
-s //interfaceTMP -t elem -n address \
-s //interfaceTMP -t elem -n netmask \
-s //interfaceTMP -t elem -n gateway \
-s //interfaceTMP -t elem -n method6 -v "dhcp" \
-s //interfaceTMP -t elem -n address6 \
-s //interfaceTMP -t elem -n netmask6 -v "64" \
-s //interfaceTMP -t elem -n gateway6 \
-s //interfaceTMP -t elem -n dnsnameservers \
-s //interfaceTMP -t elem -n dnssearch \
-s //interfaceTMP -t elem -n mtu -v "0" \
-s //interfaceTMP -t elem -n wol -v "0" \
-s //interfaceTMP -t elem -n comment \
-s //interfaceTMP -t elem -n slaves \
-s //interfaceTMP -t elem -n bondprimary \
-s //interfaceTMP -t elem -n bondmode -v "1" \
-s //interfaceTMP -t elem -n bondmiimon -v "0" \
-s //interfaceTMP -t elem -n bonddowndelay -v "0" \
-s //interfaceTMP -t elem -n bondupdelay -v "0" \
-s //interfaceTMP -t elem -n vlanid -v "1" \
-s //interfaceTMP -t elem -n vlanrawdevice \
-s //interfaceTMP -t elem -n wpassid \
-s //interfaceTMP -t elem -n wpapsk \
-r //interfaceTMP -v interface \
${OMV_CONFIG_FILE}
nic="wlan0"
if grep -qw "${nic}" /proc/net/dev; then
wpaConf="/etc/wpa_supplicant/wpa_supplicant.conf"
crda="/etc/default/crda"
if [ -f $wpaConf ]; then
country=$(awk -F'=' '/country=/{gsub(/["\r]/,""); print $NF}' $wpaConf)
wifiName=$(awk -F'=' '/ssid="/{st=index($0,"="); ssid=substr($0,st+1); gsub(/["\r]/,"",ssid); print ssid}' $wpaConf)
wifiPass=$(awk -F'=' '/psk="/{st=index($0,"="); pass=substr($0,st+1); gsub(/["\r]/,"",pass); print pass}' $wpaConf)
if [ -n "$country" ] && [ -n "$wifiName" ] && [ -n "$wifiPass" ]; then
if [ -f $crda ]; then
awk -i inplace -F'=' -v country="$country" '/REGDOMAIN=/{$0=$1"="country} {print $0}' $crda
fi
echo "Configure ${nic} to use networkd ..."
UUID=$(cat /proc/sys/kernel/random/uuid)
xmlstarlet ed -L \
-s "/config/system/network/interfaces" -t elem -n interfaceTMP \
-s //interfaceTMP -t elem -n uuid -v "$UUID" \
-s //interfaceTMP -t elem -n type -v "wireless" \
-s //interfaceTMP -t elem -n devicename -v "$nic" \
-s //interfaceTMP -t elem -n method -v "dhcp" \
-s //interfaceTMP -t elem -n address \
-s //interfaceTMP -t elem -n netmask \
-s //interfaceTMP -t elem -n gateway \
-s //interfaceTMP -t elem -n method6 -v "dhcp" \
-s //interfaceTMP -t elem -n address6 \
-s //interfaceTMP -t elem -n netmask6 -v "64" \
-s //interfaceTMP -t elem -n gateway6 \
-s //interfaceTMP -t elem -n dnsnameservers \
-s //interfaceTMP -t elem -n dnssearch \
-s //interfaceTMP -t elem -n mtu -v "0" \
-s //interfaceTMP -t elem -n wol -v "0" \
-s //interfaceTMP -t elem -n comment \
-s //interfaceTMP -t elem -n slaves \
-s //interfaceTMP -t elem -n bondprimary \
-s //interfaceTMP -t elem -n bondmode -v "1" \
-s //interfaceTMP -t elem -n bondmiimon -v "0" \
-s //interfaceTMP -t elem -n bonddowndelay -v "0" \
-s //interfaceTMP -t elem -n bondupdelay -v "0" \
-s //interfaceTMP -t elem -n vlanid -v "1" \
-s //interfaceTMP -t elem -n vlanrawdevice \
-s //interfaceTMP -t elem -n wpassid -v "$wifiName" \
-s //interfaceTMP -t elem -n wpapsk -v "$wifiPass" \
-r //interfaceTMP -v interface \
${OMV_CONFIG_FILE}
fi
fi
fi
echo "Applying network configuration ..."
omv-salt deploy run systemd-networkd
echo -e "\n"
echo -e "\e[7;49;92mNetwork interfaces have been configured with DHCP\e[0m"
echo -e "\e[7;49;92mFor more complex configurations, go to the OMV web interface BEFORE REBOOTING\e[0m"
echo ""
echo "OMV web interface currently available at:"
hostname -I | awk '{out="http://"$1; for(i=2;i<=NF;i++){out=out"\nhttp://"$i}; print out}'
echo ""
fi
fi
exit 0