projects/aports·blob v1.9 · refs

195 lines · 4.2 KiB

1#!/bin/sh
2
3# this is the init script version
4VERSION=1.0
5NEWROOT=/newroot
6SINGLEMODE=no
7
8# basic environment
9export PATH=/usr/bin:/bin:/usr/sbin:/sbin
10
11# needed devs
12mknod /dev/null c 1 3
13
14# basic mounts
15mount -t proc -o noexec,nosuid,nodev proc /proc
16mount -t sysfs -o noexec,nosuid,nodev sysfs /sys
17
18# some helpers
19ebegin() {
20 echo -n " * $*: "
21}
22eend() {
23 local msg
24 if [ "$1" = 0 ] || [ $# -lt 1 ] ; then
25 echo "ok."
26 else
27 shift
28 echo "failed. $*"
29 echo "initramfs emergency recovery shell launched"
30 exec /bin/busybox sh
31 fi
32}
33
34scan_drivers() {
35 if [ "$AUTODETECT" != no ] ; then
36 find /sys -name modalias | xargs sort | while read a ; do
37 modprobe $a 2>/dev/null \
38 || echo $a >> /tmp/hwdrivers.failed
39 done
40 fi
41}
42
43find_ovl() {
44 local mnt="$1"
45 local ovl
46 local lines
47
48 # look for apkovl's on mounted media
49 ovl=$( ls -1 "$mnt"/*.apkovl.tar.gz* 2>/dev/null ) || return 1
50 lines=$(echo "$ovl" | wc -l)
51
52 if [ $lines -gt 1 ] ; then
53 echo "ERROR: More than one apkovl file was found on $(basename $mnt). None will be read." >&2
54 return 1
55 fi
56 echo "$ovl"
57}
58
59# gotta start from somewhere :)
60echo "Alpine Init $VERSION"
61
62# read the kernel options
63for i in `cat /proc/cmdline` ; do
64 case $i in
65 s|single|1)
66 SINGLEMODE=yes ;;
67 modules=*)
68 MODULES="`echo ${i#modules=} | tr ',' ' '`";;
69 noautodetect)
70 AUTODETECT=no;;
71 *=*) eval KOPT_$i ;;
72 *) eval KOPT_$i=yes ;;
73 esac
74done
75
76ALPINE_DEV=${KOPT_alpine_dev%%:*}
77ALPINE_MNT=/media/$ALPINE_DEV
78
79# hide kernel messages
80dmesg -n 1
81
82# setup /dev
83ebegin "Starting mdev"
84mount -t tmpfs -o exec,nosuid,mode=0755 mdev /dev
85mknod -m 666 /dev/null c 1 3
86ln -s sr0 /dev/cdrom
87echo "/sbin/mdev" > /proc/sys/kernel/hotplug
88mdev -s
89RC=$?
90[ -d /dev/pts ] || mkdir -m 755 /dev/pts
91[ -c /dev/ptmx ] || mknod -m 666 /dev/ptmx c 5 2
92mount -t devpts -o gid=5,mode=0620,noexec,nosuid devpts /dev/pts
93[ -d /dev/shm ] || mkdir /dev/shm
94mount -t tmpfs -o nodev,nosuid,noexec shm /dev/shm
95eend $RC
96
97# load available drivers to get access to modloop media
98ebegin "Loading boot drivers"
99[ "$MODULES" ] && modprobe $MODULES 2> /dev/null
100if [ -f /etc/modules ] ; then
101 sed 's/\#.*//g' < /etc/modules |
102 while read module args; do
103 modprobe -q $module $args
104 done
105fi
106scan_drivers
107scan_drivers
108eend 0
109
110# locate boot media and mount it
111ebegin "Mounting boot media"
112mount $ALPINE_MNT >/dev/null 2>&1
113eend $?
114ebegin "Mounting loopback device for kernel modules"
115modprobe loop
116mount -o loop,ro -t cramfs $ALPINE_MNT/$KOPT_BOOT_IMAGE.cmg /.modloop
117rc=$?
118if [ "$rc" = 0 ]; then
119 rm -rf /lib/modules
120 ln -sf /.modloop/modules /lib
121fi
122eend $?
123
124if [ -d $ALPINE_MNT/firmware ]; then
125 ebegin "Copying firmware from $ALPINE_MNT/firmware"
126 mkdir -p /lib
127 cp -R -a $ALPINE_MNT/firmware /lib/
128 eend $?
129fi
130
131# early console?
132if [ "$SINGLEMODE" = "yes" ]; then
133 echo "Entering single mode. Type 'exit' to continue booting."
134 sh
135fi
136
137# more drivers
138ebegin "Loading hardware drivers"
139scan_drivers
140eend 0
141
142mount -t tmpfs tmpfs $NEWROOT
143
144# look for apkovl
145for i in usb floppy cdrom; do
146 mount /media/$i 2>/dev/null || continue
147 ovl=$(find_ovl /media/$i)
148 [ -f "$ovl" ] && break
149 umount /media/$i 2>/dev/null
150done
151
152if [ -f "$ovl" ]; then
153 ebegin "Loading user settings from $ovl"
154 tar -C $NEWROOT -zxf "$ovl"
155 eend $?
156 umount /media/$i 2>/dev/null
157 pkgs=$(sed 's/\#.*//' $NEWROOT/etc/lbu/packages.list 2>/dev/null)
158fi
159
160# install new root
161ebegin "Installing packages to root filesystem"
162mkdir -p /etc/apk
163for i in $ALPINE_MNT/packages/*; do
164 echo $i >> /etc/apk/repositories
165done
166apk add --root /newroot --initdb --quiet --progress $pkgs
167eend $?
168
169# copy alpine release info
170cp $ALPINE_MNT/.alpine-release $NEWROOT/
171ln -sf /.alpine-release $NEWROOT/etc/alpine-release
172
173# if there is no repositories file, then use the default
174if ! [ -f $NEWROOT/etc/apk/repositories ]; then
175 cp /etc/apk/repositories $NEWROOT/etc/apk/repositories
176fi
177
178# switch over to new root
179cat /proc/mounts | while read DEV DIR TYPE OPTS ; do
180 if [ "$DIR" != "/" -a "$DIR" != "$NEWROOT" -a -d "$DIR" ]; then
181 mkdir -p $NEWROOT/$DIR
182 mount -o move $DIR $NEWROOT/$DIR
183 fi
184done
185sync
186
187ln -sf /.modloop/modules $NEWROOT/lib/modules
188echo ""
189if [ -x $NEWROOT/sbin/init ]; then
190 exec /bin/busybox switch_root $NEWROOT /sbin/init $KOPT_init_args
191fi
192
193echo "initramfs emergency recovery shell launched"
194exec /bin/busybox sh
195reboot