build/scripts/firstrun

222 lines
7.4 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
# Short-Description: PLEASE BE PATIENT AND DO NOT INTERRUPT THE FIRST REBOOT
2014-10-08 07:39:24 +02:00
# 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
#
# Create this file to speed up boot process
#
# Immediately exit if not called by init system
if [ "X$1" != "Xstart" ]; then
exit 1
fi
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# create helper script to set swap settings
cat > /tmp/create_swap.sh <<EOT
#!/bin/bash
#
# create swap and adds it into fstab
#
2016-02-19 14:15:09 +01:00
# SSH Keys creation
rm -f /etc/ssh/ssh_host*
dpkg-reconfigure openssh-server >/dev/null 2>&1
MEMTOTAL=$(( $(awk -F" " '/^MemTotal/ {print $2}' </proc/meminfo) / 1024 ))
FREESIZE=\$(df -hm / | awk '/\// {print \$(NF-2)}')
2016-02-03 20:57:18 +01:00
if [[ ! -f "/var/swap" && "\$FREESIZE" -gt "132" ]]; then
fallocate -l 128M /var/swap >/dev/null 2>&1
if [ $? -eq 1 ]; then dd if=/dev/zero of=/var/swap bs=1024 count=131072 status=noxfer >/dev/null 2>&1; fi
chown root:root /var/swap
chmod 0600 /var/swap
mkswap /var/swap >/dev/null 2>&1
swapon /var/swap >/dev/null 2>&1
if ! grep -q swap /etc/fstab; then echo "/var/swap none swap sw 0 0" >> /etc/fstab; fi
if ! grep -q swap /etc/sysctl.conf; then echo "vm.swappiness=0" >> /etc/sysctl.conf; fi
fi
# Package updating
if [ "$(fping 8.8.8.8 | grep alive)" != "" ]; then
apt-get update >/dev/null
fi
# RAMLOG
if [[ "$(apt-cache policy ramlog | grep Installed)" != "" ]]; then
service ramlog enable
# if we have 1G ram reduce RAMLOG size
if [[ "\$MEMTOTAL" -lt "1100" ]]; then
if [ -f "/etc/default/ramlog" ]; then
sed -e 's/TMPFS_RAMFS_SIZE=512m/TMPFS_RAMFS_SIZE=256m/g' -i /etc/default/ramlog
fi
elif [[ "\$MEMTOTAL" -lt "600" ]]; then
if [ -f "/etc/default/ramlog" ]; then
sed -e 's/TMPFS_RAMFS_SIZE=512m/TMPFS_RAMFS_SIZE=192m/g' -i /etc/default/ramlog
fi
fi
fi
while [ -f /tmp/firstrun_running ]; do sleep 1; done
if [ -f "/var/run/reboot" ]; then reboot; fi
rm -f /tmp/create_swap.sh
EOT
chmod +x /tmp/create_swap.sh
collect_informations() {
# get some info about the board
CURKERNE=$(uname -r | sed 's/\([0-9]\+\.[0-9]\+\)\..*/\1/')
DISTRIBUTION=$(lsb_release -cs)
HARDWARE=$(awk '/Hardware/ {print $3}' </proc/cpuinfo)
# Mainline kernel fix
[ -f /proc/device-tree/model ] && HARDWARE=$(awk '/Hardware/ {print $4}' </proc/cpuinfo)
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)
set -e
} # collect_informations
2014-10-08 07:39:24 +02:00
display_alert() {
if [ "$DISTRIBUTION" == "wheezy" ]; then
echo -e "[\e[0;32m ok \x1B[0m] $1" > /dev/tty1
else
echo -e " * $1" > /dev/tty1
fi
}
autodetect_sunxi() {
# This function adjusts script.bin, hostname and cpufreq settings based on
# /run/machine.id so that two OS images (one built for GbE H3 devices like
# Orange Pi Plus or Banana Pi M2+ and one for the other H3 devices using
# the internal Ethernet PHY or Banana Pro vs. Pi) can be shipped.
#
# TODO for mainline kernel: Ship with u-boot debs for all Oranges and install
# the right one instead of trying to relink script.bin if detecting mainline
# kernel [[ -f /proc/device-tree/model ]]
# trigger red LED as user feedback
if [ -f /sys/class/leds/red_led/trigger ]; then
echo heartbeat >/sys/class/leds/red_led/trigger
fi
# wait for armhwinfo
sleep 3
MACHINE="$(tail -n1 /run/machine.id)"
NEWHOSTNAME="$(echo "${MACHINE}" | tr '[:upper:]' '[:lower:]' | sed -e 's/+/plus/' -e 's/\ //g')"
ScriptBinName="$(echo "${NEWHOSTNAME}" | sed -e 's/2mini$/2/g' -e 's/plus2$/plus/g').bin"
ScriptBinUsed="$(readlink -f "/boot/script.bin")"
case ${MACHINE} in
"Banana Pi")
ln -sf /boot/bin/bananapi.bin /boot/script.bin
;;
"Orange Pi+"*)
if [ "X${ScriptBinName}" != "X${ScriptBinUsed##*/}" ]; then
# wrong detection due to disabled Ethernet on 1st boot
ln -sf /boot/bin/orangepi2.bin /boot/script.bin
NEWHOSTNAME="orangepi2"
fi
;;
"Orange Pi 2"*)
ln -sf /boot/bin/orangepi2.bin /boot/script.bin
;;
"Orange Pi PC")
ln -sf /boot/bin/orangepipc.bin /boot/script.bin
;;
"Orange Pi One")
ln -sf /boot/bin/orangepione.bin /boot/script.bin
sed -i -e 's/MIN_SPEED=480000/MIN_SPEED=648000/' \
-e 's/MAX_SPEED=1296000/MAX_SPEED=1200000/' /etc/default/cpufrequtils
;;
"Orange Pi Lite")
ln -sf /boot/bin/orangepilite.bin /boot/script.bin
sed -i -e 's/MIN_SPEED=480000/MIN_SPEED=648000/' \
-e 's/MAX_SPEED=1296000/MAX_SPEED=1200000/' /etc/default/cpufrequtils
;;
"Banana Pi M2+")
if [ "X${ScriptBinName}" != "X${ScriptBinUsed##*/}" ]; then
# wrong detection due to disabled Ethernet on 1st boot
ln -sf /boot/bin/orangepipc.bin /boot/script.bin
NEWHOSTNAME="orangepipc"
fi
;;
"Beelink X2")
ln -sf /boot/bin/beelinkx2.bin /boot/script.bin
sed -i -e 's/MIN_SPEED=480000/MIN_SPEED=648000/' \
-e 's/MAX_SPEED=1296000/MAX_SPEED=1200000/' /etc/default/cpufrequtils
;;
esac
echo "${NEWHOSTNAME}" >/etc/hostname
sed -i -e "s/^::1 localhost.*/::1 localhost ${NEWHOSTNAME} ip6-localhost ip6-loopback/" \
-e "s/^127.0.0.1 localhost.*/127.0.0.1 localhost ${NEWHOSTNAME}/" /etc/hosts
[ -f /etc/wicd/wired-settings.conf ] && \
sed -i "s/^dhcphostname =.*/dhcphostname = ${NEWHOSTNAME}/" /etc/wicd/wired-settings.conf
[ -f /boot/bin/orangepih3.bin ] && rm /boot/bin/orangepih3.bin
touch /var/run/reboot
} # autodetect_sunxi
do_expand_rootfs() {
device="/dev/"$(lsblk -idn -o NAME | grep mmcblk0)
PARTITIONS=$(($(fdisk -l $device | grep $device | wc -l)-1))
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 || true
s=0
fsck -f $root_partition >/dev/null 2>&1 || true
partprobe $device >/dev/null 2>&1 || s=$?
resize2fs $root_partition >/dev/null 2>&1 || true
FREESIZE=$(df -hm / | awk '/\// {print $(NF-2)}')
if [[ "$DISTRIBUTION" == "wheezy" || "$s" != "0" || "$FREESIZE" -lt "152" ]]; then
touch /var/run/reboot
display_alert "Automatic reboot is needed. Please wait"
update-rc.d resize2fs defaults >/dev/null 2>&1
fi
return 0
2014-10-08 07:39:24 +02:00
}
check_prerequisits() {
for needed_tool in fdisk parted partprobe resize2fs ; do
which ${needed_tool} >/dev/null 2>&1 || exit 1
done
} # check_prerequisits
main() {
check_prerequisits
collect_informations
if [[ "$rootfstype" == "ext4" && ! -f "/root/.no_rootfs_resize" ]]; then
display_alert "Expand ext4 root filesystem"
do_expand_rootfs
fi
display_alert "Creating 128Mb emergency swap area"
display_alert "SSH keys recreation. One moment please"
display_alert "Updating packages"
touch /tmp/firstrun_running
/tmp/create_swap.sh &
if [ "X${HARDWARE}" = "Xsun8i" -o "X${HARDWARE}" = "Xsun7i" ]; then
autodetect_sunxi
fi
update-rc.d -f firstrun remove >/dev/null 2>&1
sed -i 's/allow-hotplug\ eth0/auto eth0/' /etc/network/interfaces.default
rm /tmp/firstrun_running
} # main
2014-10-08 07:39:24 +02:00
main
2014-10-08 07:39:24 +02:00
exit 0