2014-11-26 12:33:58 +01:00
|
|
|
#!/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:
|
2015-12-02 20:33:32 +01:00
|
|
|
# Should-Start: armhwinfo
|
2014-10-12 09:22:50 +02:00
|
|
|
# Default-Start: 2 3 4 5
|
|
|
|
# Default-Stop: 0 1 6
|
2016-03-17 20:16:26 +01:00
|
|
|
# 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
|
2016-01-13 12:10:21 +01:00
|
|
|
#
|
|
|
|
# Create this file to speed up boot process
|
|
|
|
#
|
2016-02-23 13:10:14 +01:00
|
|
|
|
2016-02-29 16:13:15 +01:00
|
|
|
# Immediately exit if not called by init system
|
2016-02-23 13:10:14 +01:00
|
|
|
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
|
2016-01-13 12:10:21 +01:00
|
|
|
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
|
2016-02-22 12:10:38 +01:00
|
|
|
MEMTOTAL=$(( $(awk -F" " '/^MemTotal/ {print $2}' </proc/meminfo) / 1024 ))
|
2016-01-30 19:31:48 +01:00
|
|
|
FREESIZE=\$(df -hm / | awk '/\// {print \$(NF-2)}')
|
2016-02-03 20:57:18 +01:00
|
|
|
if [[ ! -f "/var/swap" && "\$FREESIZE" -gt "132" ]]; then
|
2016-03-13 10:14:34 +02:00
|
|
|
fallocate -l 128M /var/swap >/dev/null 2>&1
|
2016-03-28 12:40:55 +00:00
|
|
|
if [ $? -eq 1 ]; then dd if=/dev/zero of=/var/swap bs=1M count=128 status=noxfer >/dev/null 2>&1; fi
|
2016-01-13 12:10:21 +01:00
|
|
|
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
|
|
|
|
# RAMLOG
|
|
|
|
if [[ "$(apt-cache policy ramlog | grep Installed)" != "" ]]; then
|
|
|
|
service ramlog enable
|
|
|
|
# if we have 1G ram reduce RAMLOG size
|
2016-02-22 12:10:38 +01:00
|
|
|
if [[ "\$MEMTOTAL" -lt "1100" ]]; then
|
2016-01-13 12:10:21 +01:00
|
|
|
if [ -f "/etc/default/ramlog" ]; then
|
|
|
|
sed -e 's/TMPFS_RAMFS_SIZE=512m/TMPFS_RAMFS_SIZE=256m/g' -i /etc/default/ramlog
|
|
|
|
fi
|
2016-02-22 12:10:38 +01:00
|
|
|
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
|
2016-01-13 12:10:21 +01:00
|
|
|
fi
|
|
|
|
fi
|
2016-03-13 10:34:25 +02:00
|
|
|
while [ -f /tmp/firstrun_running ]; do sleep 1; done
|
2016-01-13 12:10:21 +01:00
|
|
|
if [ -f "/var/run/reboot" ]; then reboot; fi
|
|
|
|
rm -f /tmp/create_swap.sh
|
|
|
|
EOT
|
|
|
|
chmod +x /tmp/create_swap.sh
|
|
|
|
|
2016-02-23 13:10:14 +01:00
|
|
|
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)
|
2016-02-25 18:23:24 +01:00
|
|
|
# Mainline kernel fix
|
|
|
|
[ -f /proc/device-tree/model ] && HARDWARE=$(awk '/Hardware/ {print $4}' </proc/cpuinfo)
|
2016-03-26 18:27:37 +01:00
|
|
|
case ${DISTRIBUTION} in
|
|
|
|
wheezy)
|
|
|
|
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)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
ROOTFS=$(findmnt / | awk -F" " '/\/dev\// {print $2"\t"$3}')
|
|
|
|
set ${ROOTFS}
|
|
|
|
root_partition=$1
|
|
|
|
rootfstype=$2
|
|
|
|
;;
|
|
|
|
esac
|
2016-02-23 13:10:14 +01:00
|
|
|
set -e
|
|
|
|
} # collect_informations
|
2014-10-08 07:39:24 +02:00
|
|
|
|
2016-02-22 12:10:38 +01:00
|
|
|
display_alert() {
|
2016-03-26 18:27:37 +01:00
|
|
|
if [ "${DISTRIBUTION}" == "wheezy" ]; then
|
|
|
|
echo -e "[\e[0;32m ok \x1B[0m] ${1}" > /dev/tty1
|
2016-03-28 09:24:53 +02:00
|
|
|
elif [ "${DISTRIBUTION}" == "jessie" ]; then
|
|
|
|
echo -e "[\e[0;32m OK \x1B[0m] ${1}" > /dev/tty1
|
2016-02-22 12:10:38 +01:00
|
|
|
else
|
2016-03-26 18:27:37 +01:00
|
|
|
echo -e " * ${1}" > /dev/tty1
|
2016-02-22 12:10:38 +01:00
|
|
|
fi
|
2016-01-13 12:10:21 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 22:33:20 +01:00
|
|
|
autodetect_sunxi() {
|
2016-02-23 13:10:14 +01:00
|
|
|
# This function adjusts script.bin, hostname and cpufreq settings based on
|
2016-02-29 16:13:15 +01:00
|
|
|
# /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
|
2016-03-12 22:33:20 +01:00
|
|
|
# the internal Ethernet PHY or Banana Pro vs. Pi) can be shipped.
|
2016-02-25 13:17:13 +01:00
|
|
|
#
|
|
|
|
# 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 ]]
|
2016-02-24 11:47:54 +01:00
|
|
|
|
2016-03-23 13:33:19 +01:00
|
|
|
# trigger red or blue LED as user feedback
|
2016-04-02 19:30:55 +02:00
|
|
|
echo heartbeat >/sys/class/leds/*red*/trigger 2>/dev/null || echo heartbeat >/sys/class/leds/*blue*/trigger 2>/dev/null
|
2016-02-22 12:10:38 +01:00
|
|
|
|
2016-03-28 12:40:55 +00:00
|
|
|
# wait for armhwinfo
|
|
|
|
sleep 3
|
|
|
|
read MACHINE </run/machine.id
|
2016-02-29 22:10:04 +01:00
|
|
|
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")"
|
2016-02-22 12:10:38 +01:00
|
|
|
case ${MACHINE} in
|
2016-03-12 22:33:20 +01:00
|
|
|
"Banana Pi")
|
|
|
|
ln -sf /boot/bin/bananapi.bin /boot/script.bin
|
|
|
|
;;
|
2016-02-22 12:10:38 +01:00
|
|
|
"Orange Pi+"*)
|
2016-02-29 22:10:04 +01:00
|
|
|
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
|
2016-02-22 12:10:38 +01:00
|
|
|
;;
|
|
|
|
"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
|
2016-05-03 15:56:53 +03:00
|
|
|
sed -i -e 's/MAX_SPEED=1296000/MAX_SPEED=1200000/' /etc/default/cpufrequtils
|
2016-02-22 12:10:38 +01:00
|
|
|
;;
|
|
|
|
"Orange Pi Lite")
|
|
|
|
ln -sf /boot/bin/orangepilite.bin /boot/script.bin
|
2016-05-03 15:56:53 +03:00
|
|
|
sed -i -e 's/MAX_SPEED=1296000/MAX_SPEED=1200000/' /etc/default/cpufrequtils
|
2016-02-22 12:10:38 +01:00
|
|
|
;;
|
2016-02-29 22:10:04 +01:00
|
|
|
"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
|
|
|
|
;;
|
2016-03-20 22:10:47 +01:00
|
|
|
"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
|
|
|
|
;;
|
2016-03-23 13:33:19 +01:00
|
|
|
"NanoPi M1")
|
|
|
|
ln -sf /boot/bin/nanopim1.fex /boot/script.bin
|
|
|
|
sed -i -e 's/MAX_SPEED=1296000/MAX_SPEED=1200000/' /etc/default/cpufrequtils
|
|
|
|
;;
|
2016-02-22 12:10:38 +01:00
|
|
|
esac
|
2016-03-12 22:33:20 +01:00
|
|
|
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
|
2016-03-07 16:41:03 +01:00
|
|
|
[ -f /etc/wicd/wired-settings.conf ] && \
|
2016-04-27 22:11:56 +02:00
|
|
|
sed -i "s/^dhcphostname =.*/dhcphostname = ${NEWHOSTNAME}/" /etc/wicd/wired-settings.conf && \
|
|
|
|
sed -i "s/wpa_driver =.*/wpa_driver = none/" /etc/wicd/manager-settings.conf
|
2016-03-12 22:33:20 +01:00
|
|
|
[ -f /boot/bin/orangepih3.bin ] && rm /boot/bin/orangepih3.bin
|
2016-02-22 12:10:38 +01:00
|
|
|
touch /var/run/reboot
|
2016-03-12 22:33:20 +01:00
|
|
|
} # autodetect_sunxi
|
2016-02-22 12:10:38 +01:00
|
|
|
|
|
|
|
do_expand_rootfs() {
|
2016-03-26 18:27:37 +01:00
|
|
|
# get device node for boot media
|
2016-04-14 18:45:11 +02:00
|
|
|
DEVICE="/dev/"$(lsblk -idn -o NAME | grep -w mmcblk0)
|
2016-03-26 18:27:37 +01:00
|
|
|
if [ "${DEVICE}" = "/dev/" ]; then return ; fi
|
|
|
|
QUOTED_DEVICE=$(echo "${DEVICE}" | sed 's:/:\\\/:g')
|
|
|
|
|
|
|
|
# get count of partitions and their boundaries
|
2016-04-14 18:45:11 +02:00
|
|
|
PARTITIONS=$(( $(grep -c ${DEVICE##*/}p /proc/partitions) ))
|
2016-03-26 18:27:37 +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}
|
|
|
|
|
|
|
|
# check whether a resizing rule is defined. We will take this value if it's not too low. In
|
|
|
|
# this case the value will be ignored and resizing to the whole card size happens.
|
|
|
|
if [ -f "/root/.rootfs_resize" ]; then
|
|
|
|
read RESIZE_VALUE <"/root/.rootfs_resize"
|
|
|
|
case ${RESIZE_VALUE} in
|
|
|
|
*%)
|
|
|
|
# percentage value, we try to use 16MiB to align partitions since this is
|
|
|
|
# the erase block size of more recent SD cards (512 byte sectors, so we use 32
|
|
|
|
# as divider and substract 1)
|
|
|
|
PERCENTAGE=$(echo ${RESIZE_VALUE} | tr -c -d '[:digit:]')
|
|
|
|
LASTSECTOR=$(( 32 * $(parted ${DEVICE} unit s print -sm | awk -F":" "/^${QUOTED_DEVICE}/ {printf (\"%0d\", ( \$2 * ${PERCENTAGE} / 3200))}") -1 ))
|
|
|
|
if [ ${LASTSECTOR} -lt ${PARTEND} ]; then unset LASTSECTOR ; fi
|
|
|
|
;;
|
|
|
|
*s)
|
|
|
|
# sector value, we use it directly
|
|
|
|
LASTSECTOR=$(echo ${RESIZE_VALUE} | tr -c -d '[:digit:]')
|
|
|
|
if [ ${LASTSECTOR} -lt ${PARTEND} ]; then unset LASTSECTOR ; fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
# check device capacity. If 4GB or below do not use whole card but leave a 5% spare area
|
|
|
|
# to help older cards with wear leveling and garbage collection. In case this reduced card
|
|
|
|
# capacity is less than the actual image capacity this is a clear sign that someone wants
|
|
|
|
# to use Armbian on a card of inappropriate size so he gets what he deserves (at least he
|
|
|
|
# should know what he's doing)
|
|
|
|
CAPACITY=$(parted ${DEVICE} unit s print -sm | awk -F":" "/^${QUOTED_DEVICE}/ {printf (\"%0d\", \$2 / ( 1024 / \$4 ))}")
|
|
|
|
if [ ${CAPACITY} -lt 4000000 ]; then
|
2016-03-28 12:40:55 +00:00
|
|
|
# we also run a q&d benchmark to be able to identify cards way too slow easily
|
2016-03-27 16:05:39 +00:00
|
|
|
cd /root
|
|
|
|
echo -e "\n### quick iozone test:\c" >>/var/log/armhwinfo.log
|
|
|
|
iozone -e -I -a -s 1M -r 4k -i 0 -i 1 -i 2 | grep '^ 1024' | sed 's/ 1024 //' >>/var/log/armhwinfo.log
|
2016-03-26 18:27:37 +01:00
|
|
|
SPAREAREA=$(( ${CAPACITY} / 20000 ))
|
|
|
|
LASTSECTOR=$(parted ${DEVICE} unit s print -sm | awk -F":" "/^${QUOTED_DEVICE}/ {print \$2 - (${SPAREAREA} * 1024 * ( 1024 / \$4 ))}")
|
|
|
|
if [ ${LASTSECTOR} -lt ${PARTEND} ]; then unset LASTSECTOR ; fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Start resizing
|
|
|
|
((echo d; echo $PARTITIONS; echo n; echo p; echo ; echo $STARTFROM; echo ${LASTSECTOR} ; echo w;) | fdisk ${DEVICE}) >/dev/null || true
|
2016-02-22 12:10:38 +01:00
|
|
|
s=0
|
|
|
|
fsck -f $root_partition >/dev/null 2>&1 || true
|
2016-03-26 18:27:37 +01:00
|
|
|
partprobe ${DEVICE} >/dev/null 2>&1 || s=$?
|
2016-02-22 12:10:38 +01:00
|
|
|
resize2fs $root_partition >/dev/null 2>&1 || true
|
2016-03-26 18:27:37 +01:00
|
|
|
|
|
|
|
# check whether reboot is necessary for resize2fs to take effect
|
2016-02-22 12:10:38 +01:00
|
|
|
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
|
2016-02-23 13:10:14 +01:00
|
|
|
return 0
|
2014-10-08 07:39:24 +02:00
|
|
|
}
|
|
|
|
|
2016-02-29 16:13:15 +01:00
|
|
|
check_prerequisits() {
|
|
|
|
for needed_tool in fdisk parted partprobe resize2fs ; do
|
|
|
|
which ${needed_tool} >/dev/null 2>&1 || exit 1
|
|
|
|
done
|
|
|
|
} # check_prerequisits
|
|
|
|
|
2016-02-23 13:10:14 +01:00
|
|
|
main() {
|
2016-02-29 16:13:15 +01:00
|
|
|
check_prerequisits
|
2016-02-23 13:10:14 +01:00
|
|
|
collect_informations
|
|
|
|
|
|
|
|
if [[ "$rootfstype" == "ext4" && ! -f "/root/.no_rootfs_resize" ]]; then
|
2016-03-28 09:24:53 +02:00
|
|
|
display_alert "Resizing root filesystem."
|
2016-02-23 13:10:14 +01:00
|
|
|
do_expand_rootfs
|
|
|
|
fi
|
|
|
|
|
2016-03-28 09:24:53 +02:00
|
|
|
display_alert "Starting 128Mb emergency swap area creation."
|
|
|
|
display_alert "Starting SSH keys recreation."
|
|
|
|
|
2016-03-13 10:34:25 +02:00
|
|
|
touch /tmp/firstrun_running
|
2016-02-23 13:10:14 +01:00
|
|
|
/tmp/create_swap.sh &
|
|
|
|
|
2016-03-12 22:33:20 +01:00
|
|
|
if [ "X${HARDWARE}" = "Xsun8i" -o "X${HARDWARE}" = "Xsun7i" ]; then
|
|
|
|
autodetect_sunxi
|
2016-02-23 13:10:14 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
update-rc.d -f firstrun remove >/dev/null 2>&1
|
|
|
|
sed -i 's/allow-hotplug\ eth0/auto eth0/' /etc/network/interfaces.default
|
2016-03-13 10:34:25 +02:00
|
|
|
rm /tmp/firstrun_running
|
2016-02-23 13:10:14 +01:00
|
|
|
} # main
|
2014-10-08 07:39:24 +02:00
|
|
|
|
2016-02-23 13:10:14 +01:00
|
|
|
main
|
2014-10-08 07:39:24 +02:00
|
|
|
exit 0
|