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
|
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
|
|
|
|
|
2016-01-13 12:10:21 +01:00
|
|
|
#
|
|
|
|
# Create this file to speed up boot process
|
|
|
|
#
|
|
|
|
cat > /tmp/create_swap.sh <<EOT
|
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# create swap and adds it into fstab
|
|
|
|
#
|
|
|
|
MEMTOTAL=$(awk 'BEGIN { printf "%.0f\n", '$(grep MemTotal /proc/meminfo | awk '{print $2}')'/1024/1024 }')
|
|
|
|
if [ ! -f "/var/swap" ]; then
|
|
|
|
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
|
|
|
|
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
|
|
|
|
# SSH Keys creation
|
|
|
|
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
|
|
|
|
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" -le "1" ]]; then
|
|
|
|
if [ -f "/etc/default/ramlog" ]; then
|
|
|
|
sed -e 's/TMPFS_RAMFS_SIZE=512m/TMPFS_RAMFS_SIZE=256m/g' -i /etc/default/ramlog
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ -f "/var/run/reboot" ]; then reboot; fi
|
|
|
|
rm -f /tmp/create_swap.sh
|
|
|
|
EOT
|
|
|
|
chmod +x /tmp/create_swap.sh
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2015-12-02 20:33:32 +01:00
|
|
|
|
2016-01-13 12:10:21 +01:00
|
|
|
#
|
|
|
|
#
|
2015-12-02 20:33:32 +01:00
|
|
|
# get some info about the board
|
|
|
|
CURKERNE=$(uname -r | sed 's/\([0-9]\+\.[0-9]\+\)\..*/\1/')
|
|
|
|
MACHINE=$(cat /run/machine.id)
|
2016-01-13 12:10:21 +01:00
|
|
|
DISTRIBUTION=$(lsb_release -cs)
|
|
|
|
|
2015-12-26 12:54:05 +01:00
|
|
|
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
|
2016-01-13 12:10:21 +01:00
|
|
|
root_partition=$file
|
|
|
|
break;
|
2015-12-26 12:54:05 +01:00
|
|
|
fi
|
|
|
|
done
|
2015-12-25 19:30:56 +01:00
|
|
|
rootfstype=$(blkid -s TYPE -o value $root_partition)
|
2014-10-08 07:39:24 +02:00
|
|
|
set -e
|
|
|
|
|
2016-01-13 12:10:21 +01: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
|
|
|
|
}
|
|
|
|
|
2014-10-08 07:39:24 +02:00
|
|
|
do_expand_rootfs(){
|
2016-01-13 12:10:21 +01:00
|
|
|
device="/dev/"$(lsblk -idn -o NAME | grep mmcblk0)
|
2015-12-02 20:33:32 +01:00
|
|
|
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
|
2015-12-02 20:33:32 +01:00
|
|
|
STARTFROM=$(($PARTEND+1))
|
|
|
|
[[ $PARTITIONS == 1 ]] && STARTFROM=$PARTSTART
|
2016-01-13 12:10:21 +01:00
|
|
|
((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
|
2016-01-20 09:07:56 +01:00
|
|
|
partprobe $device >/dev/null 2>&1 || s=$?
|
2016-01-13 12:10:21 +01:00
|
|
|
resize2fs $root_partition >/dev/null 2>&1 || true
|
2016-01-20 09:07:56 +01:00
|
|
|
if [[ "$DISTRIBUTION" == "wheezy" || "$s" != "0" ]]; then
|
2016-01-13 12:10:21 +01:00
|
|
|
touch /var/run/reboot
|
|
|
|
display_alert "Automatic reboot is needed. Please wait"
|
|
|
|
update-rc.d resize2fs defaults >/dev/null 2>&1
|
|
|
|
fi
|
2014-10-08 07:39:24 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
case "$1" in
|
2015-12-25 19:30:56 +01:00
|
|
|
start)
|
2016-01-13 12:10:21 +01:00
|
|
|
display_alert "Force password change upon first login"
|
|
|
|
chage -d 0 root
|
2015-12-25 19:30:56 +01:00
|
|
|
|
2016-01-13 12:10:21 +01:00
|
|
|
if [[ "$rootfstype" == "ext4" && ! -f "/root/.no_rootfs_resize" ]]; then
|
|
|
|
display_alert "Expand ext4 root filesystem"
|
|
|
|
do_expand_rootfs
|
2014-10-08 07:39:24 +02:00
|
|
|
fi
|
2016-01-13 12:10:21 +01:00
|
|
|
display_alert "Creating 128Mb emergency swap area"
|
|
|
|
display_alert "SSH keys recreation. One moment please"
|
|
|
|
display_alert "Updating packages"
|
|
|
|
#resize2fs $root_partition
|
|
|
|
/tmp/create_swap.sh &
|
|
|
|
update-rc.d -f firstrun remove >/dev/null 2>&1
|
2014-10-08 07:39:24 +02:00
|
|
|
;;
|
2016-01-13 12:10:21 +01:00
|
|
|
*)
|
2014-10-08 07:39:24 +02:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
exit 0
|