2009-01-13 17:16:45 +00:00
|
|
|
#!/bin/sh
|
2009-01-07 13:55:26 +00:00
|
|
|
|
2009-01-28 17:21:07 +00:00
|
|
|
# this is the init script version
|
|
|
|
VERSION=1.0
|
2009-01-07 13:55:26 +00:00
|
|
|
NEWROOT=/newroot
|
|
|
|
SINGLEMODE=no
|
|
|
|
|
|
|
|
# basic environment
|
|
|
|
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
|
|
|
|
|
2009-02-11 16:02:19 +00:00
|
|
|
# needed devs
|
2009-03-03 17:43:26 +00:00
|
|
|
[ -c /dev/null ] || mknod -m 666 /dev/null c 1 3
|
2009-02-11 16:02:19 +00:00
|
|
|
|
2009-01-07 13:55:26 +00:00
|
|
|
# basic mounts
|
|
|
|
mount -t proc -o noexec,nosuid,nodev proc /proc
|
|
|
|
mount -t sysfs -o noexec,nosuid,nodev sysfs /sys
|
|
|
|
|
|
|
|
# some helpers
|
|
|
|
ebegin() {
|
|
|
|
echo -n " * $*: "
|
|
|
|
}
|
|
|
|
eend() {
|
|
|
|
local msg
|
|
|
|
if [ "$1" = 0 ] || [ $# -lt 1 ] ; then
|
|
|
|
echo "ok."
|
|
|
|
else
|
|
|
|
shift
|
|
|
|
echo "failed. $*"
|
2009-01-08 07:07:34 +00:00
|
|
|
echo "initramfs emergency recovery shell launched"
|
2009-01-07 13:55:26 +00:00
|
|
|
exec /bin/busybox sh
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
scan_drivers() {
|
|
|
|
if [ "$AUTODETECT" != no ] ; then
|
2009-02-26 08:52:36 +00:00
|
|
|
find /sys -name modalias | xargs sort -u | xargs modprobe -a 2> /dev/null
|
2009-01-07 13:55:26 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-01-13 17:16:45 +00:00
|
|
|
find_ovl() {
|
|
|
|
local mnt="$1"
|
|
|
|
local ovl
|
|
|
|
local lines
|
|
|
|
|
|
|
|
# look for apkovl's on mounted media
|
|
|
|
ovl=$( ls -1 "$mnt"/*.apkovl.tar.gz* 2>/dev/null ) || return 1
|
|
|
|
lines=$(echo "$ovl" | wc -l)
|
|
|
|
|
|
|
|
if [ $lines -gt 1 ] ; then
|
|
|
|
echo "ERROR: More than one apkovl file was found on $(basename $mnt). None will be read." >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
echo "$ovl"
|
|
|
|
}
|
|
|
|
|
2009-01-07 13:55:26 +00:00
|
|
|
# gotta start from somewhere :)
|
2009-01-28 17:21:07 +00:00
|
|
|
echo "Alpine Init $VERSION"
|
2009-01-07 13:55:26 +00:00
|
|
|
|
|
|
|
# read the kernel options
|
|
|
|
for i in `cat /proc/cmdline` ; do
|
|
|
|
case $i in
|
|
|
|
s|single|1)
|
|
|
|
SINGLEMODE=yes ;;
|
|
|
|
modules=*)
|
|
|
|
MODULES="`echo ${i#modules=} | tr ',' ' '`";;
|
|
|
|
noautodetect)
|
|
|
|
AUTODETECT=no;;
|
|
|
|
*=*) eval KOPT_$i ;;
|
|
|
|
*) eval KOPT_$i=yes ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2009-02-26 09:03:29 +00:00
|
|
|
# start bootcharting if wanted
|
|
|
|
if [ -n "$KOPT_chart" ]; then
|
|
|
|
ebegin "Starting bootchart logging"
|
2009-03-03 17:43:26 +00:00
|
|
|
/sbin/bootchartd start-initfs "$NEWROOT"
|
2009-02-26 09:03:29 +00:00
|
|
|
eend 0
|
|
|
|
fi
|
|
|
|
|
2009-01-07 13:55:26 +00:00
|
|
|
ALPINE_DEV=${KOPT_alpine_dev%%:*}
|
2009-02-18 14:53:58 +00:00
|
|
|
ALPINE_DEV_FS=${KOPT_alpine_dev##*:}
|
|
|
|
if [ "$ALPINE_DEV_FS" = "$ALPINE_DEV" ]; then
|
|
|
|
unset ALPINE_DEV_FS
|
|
|
|
fi
|
2009-01-07 13:55:26 +00:00
|
|
|
ALPINE_MNT=/media/$ALPINE_DEV
|
|
|
|
|
|
|
|
# hide kernel messages
|
|
|
|
dmesg -n 1
|
|
|
|
|
|
|
|
# setup /dev
|
|
|
|
ebegin "Starting mdev"
|
|
|
|
mount -t tmpfs -o exec,nosuid,mode=0755 mdev /dev
|
|
|
|
ln -s sr0 /dev/cdrom
|
|
|
|
echo "/sbin/mdev" > /proc/sys/kernel/hotplug
|
|
|
|
mdev -s
|
|
|
|
RC=$?
|
|
|
|
[ -d /dev/pts ] || mkdir -m 755 /dev/pts
|
|
|
|
[ -c /dev/ptmx ] || mknod -m 666 /dev/ptmx c 5 2
|
|
|
|
mount -t devpts -o gid=5,mode=0620,noexec,nosuid devpts /dev/pts
|
|
|
|
[ -d /dev/shm ] || mkdir /dev/shm
|
|
|
|
mount -t tmpfs -o nodev,nosuid,noexec shm /dev/shm
|
|
|
|
eend $RC
|
|
|
|
|
|
|
|
# load available drivers to get access to modloop media
|
|
|
|
ebegin "Loading boot drivers"
|
|
|
|
[ "$MODULES" ] && modprobe $MODULES 2> /dev/null
|
2009-01-13 17:16:45 +00:00
|
|
|
if [ -f /etc/modules ] ; then
|
|
|
|
sed 's/\#.*//g' < /etc/modules |
|
|
|
|
while read module args; do
|
|
|
|
modprobe -q $module $args
|
|
|
|
done
|
|
|
|
fi
|
2009-01-07 13:55:26 +00:00
|
|
|
scan_drivers
|
|
|
|
scan_drivers
|
|
|
|
eend 0
|
|
|
|
|
|
|
|
# locate boot media and mount it
|
|
|
|
ebegin "Mounting boot media"
|
2009-02-18 14:53:58 +00:00
|
|
|
mkdir -p $ALPINE_MNT
|
|
|
|
if [ -n "$ALPINE_DEV_FS" ]; then
|
|
|
|
mount_opts="-t $ALPINE_DEV_FS"
|
|
|
|
fi
|
|
|
|
mount $mount_opts /dev/$ALPINE_DEV $ALPINE_MNT >/dev/null 2>&1
|
2009-01-07 13:55:26 +00:00
|
|
|
eend $?
|
|
|
|
ebegin "Mounting loopback device for kernel modules"
|
|
|
|
modprobe loop
|
2009-02-19 08:33:52 +00:00
|
|
|
if [ -n "$KOPT_modloop" ]; then
|
|
|
|
modloop=$KOPT_modloop
|
|
|
|
else
|
|
|
|
modloop=$KOPT_BOOT_IMAGE.cmg
|
|
|
|
fi
|
|
|
|
mount -o loop,ro -t cramfs $ALPINE_MNT/$modloop /.modloop
|
2009-01-07 13:55:26 +00:00
|
|
|
rc=$?
|
|
|
|
if [ "$rc" = 0 ]; then
|
|
|
|
rm -rf /lib/modules
|
|
|
|
ln -sf /.modloop/modules /lib
|
|
|
|
fi
|
2009-02-12 07:30:02 +00:00
|
|
|
eend $?
|
|
|
|
|
2009-01-07 13:55:26 +00:00
|
|
|
if [ -d $ALPINE_MNT/firmware ]; then
|
2009-02-12 07:30:02 +00:00
|
|
|
ebegin "Copying firmware from $ALPINE_MNT/firmware"
|
|
|
|
mkdir -p /lib
|
|
|
|
cp -R -a $ALPINE_MNT/firmware /lib/
|
|
|
|
eend $?
|
2009-01-07 13:55:26 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# early console?
|
|
|
|
if [ "$SINGLEMODE" = "yes" ]; then
|
|
|
|
echo "Entering single mode. Type 'exit' to continue booting."
|
|
|
|
sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
# more drivers
|
|
|
|
ebegin "Loading hardware drivers"
|
|
|
|
scan_drivers
|
|
|
|
eend 0
|
|
|
|
|
2009-02-02 09:28:47 +00:00
|
|
|
mount -t tmpfs tmpfs $NEWROOT
|
2009-01-28 14:54:31 +00:00
|
|
|
|
2009-01-13 17:16:45 +00:00
|
|
|
# look for apkovl
|
|
|
|
for i in usb floppy cdrom; do
|
|
|
|
mount /media/$i 2>/dev/null || continue
|
|
|
|
ovl=$(find_ovl /media/$i)
|
|
|
|
[ -f "$ovl" ] && break
|
|
|
|
umount /media/$i 2>/dev/null
|
|
|
|
done
|
2009-02-19 15:59:48 +00:00
|
|
|
if ! [ -f "$ovl" ]; then
|
|
|
|
ovl=$(find_ovl $ALPINE_MNT)
|
|
|
|
fi
|
2009-01-13 17:16:45 +00:00
|
|
|
|
|
|
|
if [ -f "$ovl" ]; then
|
|
|
|
ebegin "Loading user settings from $ovl"
|
|
|
|
tar -C $NEWROOT -zxf "$ovl"
|
|
|
|
eend $?
|
|
|
|
umount /media/$i 2>/dev/null
|
|
|
|
pkgs=$(sed 's/\#.*//' $NEWROOT/etc/lbu/packages.list 2>/dev/null)
|
|
|
|
fi
|
|
|
|
|
2009-01-07 13:55:26 +00:00
|
|
|
# install new root
|
2009-01-08 07:07:34 +00:00
|
|
|
ebegin "Installing packages to root filesystem"
|
2009-02-11 16:02:19 +00:00
|
|
|
mkdir -p /etc/apk
|
2009-02-12 07:30:02 +00:00
|
|
|
for i in $ALPINE_MNT/packages/*; do
|
2009-02-11 16:02:19 +00:00
|
|
|
echo $i >> /etc/apk/repositories
|
|
|
|
done
|
2009-03-03 17:43:26 +00:00
|
|
|
if [ -n "$KOPT_chart" ]; then
|
|
|
|
pkgs="$pkgs acct"
|
|
|
|
fi
|
2009-03-04 06:37:51 +00:00
|
|
|
apkflags="--initdb --quiet --progress --force"
|
2009-02-23 12:06:05 +00:00
|
|
|
if [ -z "$KOPT_keep_apk_new" ]; then
|
2009-03-04 06:37:51 +00:00
|
|
|
apkflags=" --clean-protected"
|
2009-02-23 12:06:05 +00:00
|
|
|
fi
|
2009-03-04 06:37:51 +00:00
|
|
|
apk add --root /newroot $apkflags $pkgs >/dev/null
|
2009-03-04 06:31:17 +00:00
|
|
|
eend $?
|
2009-02-23 12:06:05 +00:00
|
|
|
|
2009-01-28 17:21:07 +00:00
|
|
|
# copy alpine release info
|
2009-02-12 07:30:02 +00:00
|
|
|
cp $ALPINE_MNT/.alpine-release $NEWROOT/
|
2009-01-28 17:21:07 +00:00
|
|
|
ln -sf /.alpine-release $NEWROOT/etc/alpine-release
|
|
|
|
|
2009-02-02 09:28:47 +00:00
|
|
|
# if there is no repositories file, then use the default
|
|
|
|
if ! [ -f $NEWROOT/etc/apk/repositories ]; then
|
2009-02-11 16:02:19 +00:00
|
|
|
cp /etc/apk/repositories $NEWROOT/etc/apk/repositories
|
2009-02-02 09:28:47 +00:00
|
|
|
fi
|
|
|
|
|
2009-03-03 17:43:26 +00:00
|
|
|
# setup bootchart for switch_root
|
|
|
|
chart_init=""
|
2009-02-26 09:03:29 +00:00
|
|
|
if [ -n "$KOPT_chart" ]; then
|
2009-03-03 17:43:26 +00:00
|
|
|
/sbin/bootchartd stop-initfs "$NEWROOT"
|
|
|
|
chart_init="/sbin/bootchartd start-rootfs"
|
2009-02-26 09:03:29 +00:00
|
|
|
fi
|
|
|
|
|
2009-01-07 13:55:26 +00:00
|
|
|
# switch over to new root
|
|
|
|
cat /proc/mounts | while read DEV DIR TYPE OPTS ; do
|
|
|
|
if [ "$DIR" != "/" -a "$DIR" != "$NEWROOT" -a -d "$DIR" ]; then
|
|
|
|
mkdir -p $NEWROOT/$DIR
|
|
|
|
mount -o move $DIR $NEWROOT/$DIR
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
sync
|
2009-01-13 17:16:45 +00:00
|
|
|
|
2009-01-28 14:54:31 +00:00
|
|
|
ln -sf /.modloop/modules $NEWROOT/lib/modules
|
2009-02-11 14:10:24 +00:00
|
|
|
echo ""
|
2009-01-13 17:16:45 +00:00
|
|
|
if [ -x $NEWROOT/sbin/init ]; then
|
2009-03-03 17:43:26 +00:00
|
|
|
exec /bin/busybox switch_root $NEWROOT $chart_init /sbin/init $KOPT_init_args
|
2009-01-13 17:16:45 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "initramfs emergency recovery shell launched"
|
|
|
|
exec /bin/busybox sh
|
|
|
|
reboot
|