2014-12-03 00:31:57 +01:00
#!/bin/sh
# autoprovision stage 1: this script will be executed upon boot without a valid extroot (i.e. when rc.local is found and run from the internal overlay)
. /root/autoprovision-functions.sh
getPendriveSize( )
{
# this is needed for the mmc card in some (all?) Huawei 3G dongle.
# details: https://dev.openwrt.org/ticket/10716#comment:4
if [ -e /dev/sda ] ; then
# force re-read of the partition table
head /dev/sda >/dev/null
fi
if ( grep -q sda /proc/partitions) then
cat /sys/block/sda/size
else
echo 0
fi
}
hasBigEnoughPendrive( )
{
local size = $( getPendriveSize)
2015-05-26 03:32:54 +02:00
if [ $size -ge 600000 ] ; then
2014-12-03 00:31:57 +01:00
log " Found a pendrive of size: $(( $size / 2 / 1024 )) MB "
return 0
else
return 1
fi
}
setupPendrivePartitions( )
{
# erase partition table
dd if = /dev/zero of = /dev/sda bs = 1M count = 1
2014-12-10 02:11:30 +01:00
# sda1 is 'swap'
# sda2 is 'root'
# sda3 is 'data'
2014-12-03 00:31:57 +01:00
fdisk /dev/sda <<EOF
o
n
p
1
+64M
n
p
2
+512M
n
p
3
t
1
82
w
q
EOF
log "Finished partitioning /dev/sda using fdisk"
2014-12-10 02:11:30 +01:00
sleep 2
2014-12-03 00:31:57 +01:00
until [ -e /dev/sda1 ]
do
2014-12-10 02:11:30 +01:00
echo "Waiting for partitions to show up in /dev"
2014-12-03 00:31:57 +01:00
sleep 1
done
mkswap -L swap -U $swapUUID /dev/sda1
mkfs.ext4 -L root -U $rootUUID /dev/sda2
mkfs.ext4 -L data -U $dataUUID /dev/sda3
log "Finished setting up filesystems"
}
setupExtroot( )
{
mkdir -p /mnt/extroot
2014-12-10 02:11:30 +01:00
# TODO they said on the wiki that it's optional, an empty overlay also works...
2014-12-03 00:31:57 +01:00
# we need to make the internal overlay read-only, otherwise the two md5's may be different
# due to writing to the internal overlay from this point until the reboot.
# files: /.extroot.md5sum (extroot) and /etc/extroot.md5sum (internal)
2014-12-10 02:11:30 +01:00
#mount -o remount,ro /
#log "Remounted / as read-only"
2014-12-03 00:31:57 +01:00
2014-12-10 02:11:30 +01:00
mount -U $rootUUID /mnt/extroot
#tar -C /overlay -cvf - . | tar -C /mnt/extroot -xf -
2014-12-03 00:31:57 +01:00
# let's write a new rc.local on extroot which will shadow the one which is in the rom and runs stage1
2014-12-10 02:11:30 +01:00
mkdir -p /mnt/extroot/etc/
2014-12-03 00:31:57 +01:00
cat >/mnt/extroot/etc/rc.local <<EOF
/root/autoprovision-stage2.sh
exit 0
EOF
# make sure that we shadow the /var -> /tmp symlink with the extroot, so that /var is permanent
mkdir -p /mnt/extroot/var
# KLUDGE: but /var/state is assumed to be transient, see https://dev.openwrt.org/ticket/12228
cd /mnt/extroot/var
ln -s /tmp state
cd -
log "Finished setting up extroot"
}
autoprovisionStage1( )
{
signalAutoprovisionWorking
signalAutoprovisionWaitingForUser
signalWaitingForPendrive
until hasBigEnoughPendrive
do
echo "Waiting for a pendrive to be inserted"
sleep 3
done
signalAutoprovisionWorking # to make it flash in sync with the USB led
signalFormatting
sleep 1
setupPendrivePartitions
sleep 1
setupExtroot
sync
stopSignallingAnything
reboot
}
autoprovisionStage1