build/scripts/firstrun

142 lines
4.6 KiB
Text
Raw Normal View History

#!/bin/bash
2014-10-08 07:39:24 +02:00
### BEGIN INIT INFO
# Provides: firstrun
2014-10-12 20:39:42 +02:00
# Required-Start: $all
2014-10-08 07:39:24 +02:00
# Required-Stop:
# Should-Start: armhwinfo
2014-10-12 09:22:50 +02:00
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
2014-10-08 07:39:24 +02:00
# Short-Description: Script to run when first starting
# Description: Something needs to be done when is
# starting at first time.
2014-10-12 09:22:50 +02:00
# regenerate ssh host key
2014-10-08 07:39:24 +02:00
### END INIT INFO
N=/etc/init.d/firstrun
# get some info about the board
CURKERNE=$(uname -r | sed 's/\([0-9]\+\.[0-9]\+\)\..*/\1/')
MACHINE=$(cat /run/machine.id)
2014-10-08 07:39:24 +02:00
MEMTOTAL=$(awk 'BEGIN { printf "%.0f\n", '$(grep MemTotal /proc/meminfo | awk '{print $2}')'/1024/1024 }')
root_device=$(mountpoint -d /)
for file in /dev/* ; do
CURRENT_DEVICE=$(printf "%d:%d" $(stat --printf="0x%t 0x%T" $file))
if [ $CURRENT_DEVICE = $root_device ]; then
root_partition=$file
break;
fi
done
rootfstype=$(blkid -s TYPE -o value $root_partition)
2014-10-08 07:39:24 +02:00
set -e
do_expand_rootfs(){
device="/dev/"$(lsblk -idn -o NAME | grep mmcblk0)
PARTITIONS=$(($(fdisk -l $device | grep $device | wc -l)-1))
2015-12-17 09:17:25 +01:00
PARTSTART=$(parted $device unit s print -sm | tail -1 | cut -d: -f2 | sed 's/s//') # start of first partition
PARTEND=$(parted $device unit s print -sm | head -3 | tail -1 | cut -d: -f3 | sed 's/s//') # end of first partition
STARTFROM=$(($PARTEND+1))
[[ $PARTITIONS == 1 ]] && STARTFROM=$PARTSTART
((echo d; echo $PARTITIONS; echo n; echo p; echo ; echo $STARTFROM; echo ; echo w;) | fdisk $device)>/dev/null
2014-10-08 07:39:24 +02:00
return 0
}
case "$1" in
start)
reboot=false
# Expand root fs if it's EXT4
if [[ $rootfstype == ext4 && ! -f "/root/.no_rootfs_resize" ]]; then
# display message to first console
echo "" >/dev/tty1
toilet -f standard "first run" >/dev/tty1
echo "" >/dev/tty1
echo "System might reboot automatically. Please wait!" >/dev/tty1
echo "" >/dev/tty1
echo -e "[\e[0;32m ok \x1B[0m] Expanding rootfs." >/dev/tty1
if do_expand_rootfs;then
echo -e "[\e[0;32m ok \x1B[0m] Expanding rootfs suceeded, rebooting automatically." >/dev/tty1
update-rc.d resize2fs defaults >/dev/null 2>&1
reboot=true
else
echo "Expanding rootfs has failed, see log files." >/dev/tty1
fi
else
echo -e "[\e[0;32m ok \x1B[0m] First run." >/dev/tty1
echo -e "[\e[0;32m ok \x1B[0m] Expanding rootfs disabled or not possible." >/dev/tty1
echo -e "[\e[0;32m ok \x1B[0m] Creating 128Mb emergency swap area"
dd if=/dev/zero of=/var/swap bs=1024 count=131072 status=noxfer >/dev/null 2>&1
chown root:root /var/swap
chmod 0600 /var/swap
mkswap /var/swap >/dev/null 2>&1
swapon /var/swap >/dev/null 2>&1
echo "/var/swap none swap sw 0 0" >> /etc/fstab
echo 'vm.swappiness=0' >> /etc/sysctl.conf
update-rc.d -f resize2fs remove
fi
# Lamobo R1 exception
if [[ $MACHINE == Lamobo* ]]; then
ln -sf /boot/bin/lamobo-r1.bin /boot/script.bin
ln -sf /etc/network/interfaces.r1 /etc/network/interfaces
# alter hostname
echo "lamobo-r1" > /etc/hostname
sed 's/ssid=\(.*\)/ssid=lamobo/g' -i /etc/hostapd.conf
sed 's/ssid=\(.*\)/ssid=lamobo/g' -i /etc/hostapd.conf-rt
if [[ $CURKERNE == "3.4" ]]; then
sed -e 's/a20_tp//g' -i /etc/modules
# alter configuration for hostapd
sed 's/DAEMON_SBIN=\(.*\)/DAEMON_SBIN=\/usr\/sbin\/hostapd-rt/g' -i /etc/init.d/hostapd
sed 's/DAEMON_CONF=\(.*\)/DAEMON_CONF=\/etc\/hostapd.conf-rt/g' -i /etc/init.d/hostapd
else
# alter configuration for hostapd
sed 's/DAEMON_SBIN=\(.*\)/DAEMON_SBIN=\/usr\/sbin\/hostapd/g' -i /etc/init.d/hostapd
sed 's/DAEMON_CONF=\(.*\)/DAEMON_CONF=\/etc\/hostapd.conf/g' -i /etc/init.d/hostapd
fi
fi
# RAMLOG
if [[ $(apt-cache policy ramlog | grep Installed) != "" ]]; then
service ramlog enable
# if we have 1G ram reduce RAMLOG size
if (($MEMTOTAL <= 1)); then
if [ -f "/etc/default/ramlog" ]; then
echo -e "[\e[0;32m ok \x1B[0m] Reducing Ramlog size to 256m"
sed -e 's/TMPFS_RAMFS_SIZE=512m/TMPFS_RAMFS_SIZE=256m/g' -i /etc/default/ramlog
fi
fi
fi
# SSH Keys creation
echo -e "[\e[0;32m ok \x1B[0m] SSH keys recreation. One moment please" >/dev/tty1
rm -f /etc/ssh/ssh_host*
dpkg-reconfigure openssh-server >/dev/null 2>&1
# Package updating
if [ "$(fping 8.8.8.8 | grep alive)" != "" ]; then
set +e
echo -e "[\e[0;32m ok \x1B[0m] Updating packages list. One moment please" >/dev/tty1
apt-get update >/dev/null 2>&1
apt-get -y -qq autoremove >/dev/null 2>&1
fi
update-rc.d -f firstrun remove >/dev/null 2>&1
2014-10-08 07:39:24 +02:00
if $reboot;then
/sbin/reboot
fi
;;
*)
2014-10-08 07:39:24 +02:00
echo "Usage: $N {start}" >&2
exit 1
;;
esac
exit 0