summaryrefslogblamecommitdiff
path: root/initramfs-init
blob: b2375fece3b332688888b4b0ab269ee5c881c330 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
         
 

                                 





                                         


                     














                                                
                                                                  












                                                                         















                                                                                                             
                               
                           






































                                                                     





                                         









                                                    
                                                                     




                                     

       
                                    



                                                           
  











                                                                     
                             
 















                                                                         
                  
                                               
                 
                                   


                                                         

       
                          
                                        

                                                   

                                                        
                                                              

  







                                                                       
 
                                             
       






                                                                         
#!/bin/sh

# this is the init script version
VERSION=1.0
NEWROOT=/newroot
SINGLEMODE=no

# basic environment
export PATH=/usr/bin:/bin:/usr/sbin:/sbin

# needed devs
mknod /dev/null c 1 3

# 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. $*"
		echo "initramfs emergency recovery shell launched"
		exec /bin/busybox sh
	fi
}

scan_drivers() {
	if [ "$AUTODETECT" != no ] ; then
		find /sys -name modalias | xargs sort | while read a ; do
			modprobe $a 2>/dev/null \
				|| echo $a >> /tmp/hwdrivers.failed
		done
	fi
}

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"
}

# gotta start from somewhere :)
echo "Alpine Init $VERSION"

# 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

ALPINE_DEV=${KOPT_alpine_dev%%:*}
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
mknod -m 666 /dev/null c 1 3
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
if [ -f /etc/modules ] ; then
	sed 's/\#.*//g' < /etc/modules |
	while read module args; do
		modprobe -q $module $args
	done
fi
scan_drivers
scan_drivers
eend 0

# locate boot media and mount it
ebegin "Mounting boot media"
mount $ALPINE_MNT >/dev/null 2>&1
eend $?
ebegin "Mounting loopback device for kernel modules"
modprobe loop
mount -o loop,ro -t cramfs $ALPINE_MNT/$KOPT_BOOT_IMAGE.cmg /.modloop
rc=$?
if [ "$rc" = 0 ]; then
	rm -rf /lib/modules
	ln -sf /.modloop/modules /lib
fi
eend $?

if [ -d $ALPINE_MNT/firmware ]; then
	ebegin "Copying firmware from $ALPINE_MNT/firmware"
	mkdir -p /lib
	cp -R -a $ALPINE_MNT/firmware /lib/
	eend $?
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

mount -t tmpfs tmpfs $NEWROOT

# 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

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

# install new root
ebegin "Installing packages to root filesystem"
mkdir -p /etc/apk
for i in $ALPINE_MNT/packages/*; do
	echo $i >> /etc/apk/repositories
done
apk add --root /newroot --initdb --quiet --progress $pkgs
eend $?

# copy alpine release info
cp $ALPINE_MNT/.alpine-release $NEWROOT/
ln -sf /.alpine-release $NEWROOT/etc/alpine-release

# if there is no repositories file, then use the default
if ! [ -f $NEWROOT/etc/apk/repositories ]; then
	cp /etc/apk/repositories $NEWROOT/etc/apk/repositories
fi

# 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

ln -sf /.modloop/modules $NEWROOT/lib/modules
echo ""
if [ -x $NEWROOT/sbin/init ]; then
	exec /bin/busybox switch_root $NEWROOT /sbin/init $KOPT_init_args
fi

echo "initramfs emergency recovery shell launched"
exec /bin/busybox sh
reboot