diff --git a/install_yunohost b/install_yunohost index a9b687e..110676b 100755 --- a/install_yunohost +++ b/install_yunohost @@ -74,6 +74,20 @@ ensure_root() { return 0 } +ensure_pi_logout() { + who | grep pi > /dev/null && return 1 + return 0 +} + +is_raspbian() { + # On Raspbian image lsb_release is available + if [[ "$(lsb_release -i -s 2> /dev/null)" != "Raspbian" ]] ; + then + return 1 + fi + return 0 +} + apt_get_wrapper() { if [[ "$AUTOMODE" == "0" ]] ; then @@ -87,11 +101,22 @@ apt_get_wrapper() { } upgrade_system() { - apt_get_wrapper update \ + + apt_get_wrapper update \ || return 1 - apt_get_wrapper -y dist-upgrade \ + apt_get_wrapper -y dist-upgrade \ || return 2 + + if is_raspbian ; then + apt_get_wrapper -o Dpkg::Options::="--force-confold" \ + -y --force-yes install rpi-update \ + || return 3 + + rpi-update >> $YUNOHOST_LOG 2>&1 \ + || return 4 + + fi } installscript_dependencies() { @@ -107,6 +132,23 @@ installscript_dependencies() { -y --force-yes install \ $DEPENDENCIES \ || return 1 + + if is_raspbian ; then + DEPENDENCIES="ssl-cert lua-event lua-expat lua-socket lua-sec lua-filesystem" + apt_get_wrapper -o Dpkg::Options::="--force-confold" \ + -y --force-yes install \ + $DEPENDENCIES \ + || return 1 + wget -q https://build.yunohost.org/metronome_3.7.9+33b7572-1_armhf.deb \ + || return 1 + sha256sum -c <<<"d19c6b08afb8674d1257dc3349a60e88218c4c01133c53c1fdcb02e86b415a40 metronome_3.7.9+33b7572-1_armhf.deb" \ + || return 1 + dpkg -i metronome_3.7.9+33b7572-1_armhf.deb >> $YUNOHOST_LOG 2>&1 \ + || return 1 + apt-mark hold metronome >> $YUNOHOST_LOG 2>&1 \ + || return 1 + fi + } create_custom_config() { @@ -160,7 +202,7 @@ setup_package_source() { fi # Add YunoHost repository key to the keyring - wget -O- http://repo.yunohost.org/yunohost.asc -q | apt-key add - -qq > /dev/null + wget -O- https://repo.yunohost.org/yunohost.asc -q | apt-key add - -qq > /dev/null } apt_update() { @@ -254,6 +296,56 @@ restart_services() { return 0 } +del_user_pi() { + deluser --remove-all-files pi >> $YUNOHOST_LOG 2>&1 +} + +change_hostname() { + sed -i 's/raspberrypi/yunohost/g' /etc/hosts + sed -i 's/raspberrypi/yunohost/g' /etc/hostname +} + +setup_firstboot() { + + cat > /etc/init.d/yunohost-firstboot << EOF +#!/bin/sh +### BEGIN INIT INFO +# Provides: expand rootfs and Generates new ssh host keys on first boot +# Required-Start: \$remote_fs \$syslog +# Required-Stop: \$remote_fs \$syslog +# Default-Start: 2 3 4 5 +# Default-Stop: +# Short-Description: Generates new ssh host keys on first boot +# Description: Generates apt-get --purge clean new ssh host keys on $ +### END INIT INFO +echo "Expanding rootfs ..." +raspi-config --expand-rootfs +echo "Removing myself ..." +insserv -r /etc/init.d/yunohost-firstboot +rm -f /etc/init.d/yunohost-firstboot +rm /etc/yunohost/firstboot +echo "Rebooting ..." +reboot +EOF + chmod a+x /etc/init.d/yunohost-firstboot + insserv /etc/init.d/yunohost-firstboot + touch /etc/yunohost/firstboot +} + +clean_image() { + # Delete SSH keys + rm -f /etc/ssh/ssh_host_* >> $YUNOHOST_LOG 2>&1 + yes | ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa >> $YUNOHOST_LOG 2>&1 + yes | ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa >> $YUNOHOST_LOG 2>&1 + yes | ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key -N '' -t ecdsa -b 521 >> $YUNOHOST_LOG 2>&1 + + # Deleting logs ... + find /var/log -type f -exec rm {} \; >> $YUNOHOST_LOG 2>&1 + + # Purging apt ... + apt-get --purge clean >> $YUNOHOST_LOG 2>&1 +} + post_install() { # No postinstall in auto mode [[ "$AUTOMODE" == "1" ]] && return 0 @@ -322,7 +414,8 @@ set -u AUTOMODE=0 DISTRIB=stable -while getopts ":ad:h" option; do +BUILD_IMAGE=0 +while getopts ":aid:h" option; do case $option in a) AUTOMODE=1 @@ -331,6 +424,10 @@ while getopts ":ad:h" option; do d) DISTRIB=$OPTARG ;; + i) + # This hidden option will allow to build generic image for Rpi/Olimex + BUILD_IMAGE=1 + ;; h) usage exit 0 @@ -350,6 +447,12 @@ if ! step ensure_root ; then die "This script must be run as root" 1 fi +if is_raspbian ; then + if ! step ensure_pi_logout ; then + die "The user pi should be logged out" 14 + fi +fi + if ! step upgrade_system ; then die "Unable to update the system" 2 fi @@ -394,10 +497,36 @@ if ! step restart_services ; then die "Error caught during services restart" 12 fi -if ! step post_install ; then - die "Post-installation failed" 13 +if is_raspbian ; then + if ! step del_user_pi ; then + die "Unable to delete user pi" 15 + fi + + if ! step change_hostname ; then + die "Unable to change hostname" 16 + fi + + if ! step setup_firstboot ; then + die "Unable to setup firstboot" 17 + fi fi +if [[ "$BUILD_IMAGE" == "1" ]] ; then + if ! step clean_image ; then + die "Unable to clean image" 18 + fi +fi + +if is_raspbian ; then + # Reboot should be done before postinstall to be able to run iptables rules + reboot +fi + +if ! step post_install ; then + die "Post-installation failed" 13 +fi + + # Success ! success exit 0