stali-init

Stali init files. To be shared.
git clone git://r-36.net/stali-init
Log | Files | Refs

commit ee166e16177a417eedeaedae2ef20e1d4861b6ae
Author: Christoph Lohmann <20h@r-36.net>
Date:   Wed, 17 Apr 2013 21:04:54 +0200

Initial commit of init scripts.

Diffstat:
inittab.busybox | 16++++++++++++++++
rc.init | 195+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rc.local | 8++++++++
rc.multi | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rc.shutdown | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rc.single | 24++++++++++++++++++++++++
6 files changed, 390 insertions(+), 0 deletions(-)

diff --git a/inittab.busybox b/inittab.busybox @@ -0,0 +1,16 @@ +# +# /etc/inittab +# + +::sysinit:/bin/rc.init +::once:/bin/rc.multi + +tty1::respawn:/sbin/agetty --noclear -8 38400 tty1 linux +tty2::respawn:/sbin/agetty --noclear -8 38400 tty2 linux +tty3::respawn:/sbin/agetty --noclear -8 38400 tty3 linux +tty4::respawn:/sbin/agetty --noclear -8 38400 tty4 linux + +::ctrlaltdel:/bin/busybox reboot -d 3 +::restart:/bin/busybox init +::shutdown:/bin/rc.shutdown + diff --git a/rc.init b/rc.init @@ -0,0 +1,195 @@ +#!/bin/busybox ash + +. /cfg/rc.conf + +mkdir -p /dev +mountpoint -q /dev || mount -t devtmpfs dev /dev -o mode=0755,nosuid +touch /dev/mdev.log +mkdir -p /dev/pts +mountpoint -q /dev/pts || mount -n -t devpts devpts /dev/pts -o mode=0620,gid=5,nosuid,noexec,noatime +bootlogd -c -l /run/boot.log -p /run/bootlogd.pid + +printf "\n\n" +printf " sta.li\n" +printf "\n\n" + +printf "Mounting base filesystems.\n" +mkdir -p /proc +mkdir -p /sys +mkdir -p /run +mountpoint -q /proc || mount -t proc proc /proc -o nosuid,noexec,nodev +mountpoint -q /sys || mount -t sysfs sys /sys -o nosuid,noexec,nodev +mountpoint -q /run || mount -t tmpfs run /run -o mode=0755,nosuid,nodev + +mkdir -p -m 1777 /run/lock +mkdir -p /dev/shm +mountpoint -q /dev/shm || mount -n -t tmpfs shm /dev/shm -o mode=1777,nosuid,nodev,noatime + +mountpoint -q /sys/fs/cgroup || mount -n -t cgroup nodev /sys/fs/cgroup + +if [ ! -e /run/initramfs/fsck-root ]; +then + printf "Remounting root ro.\n" + mount -o remount,ro / +fi + +if [ -s /cfg/locale.conf ]; +then + printf "Reading /cfg/locale.conf.\n" + eval $(cat /cfg/locale.conf) +fi + +if [ -n $(printf "%s" "$LANG" | grep -i utf) ]; +then + printf "Setting ttys to be UTF-8.\n" + for i in /dev/tty[0-9]*; do + kbd_mode -u < ${i} &>/dev/null + printf "\e%%G" > ${i} &>/dev/null + done + echo 1 >| /sys/module/vt/parameters/default_utf8 +else + printf "Setting ttys to legacy.\n" + for i in /dev/tty[0-9]*; do + kbd_mode -a < ${i} &>/dev/null + printf "\e%%@" > ${i} &>/dev/null + done + echo 0 >| /sys/module/vt/parameters/default_utf8 +fi + +if [ -s /cfg/vconsole.conf ]; +then + printf "Reading vconsole.conf.\n" + eval $(cat /cfg/vconsole.conf) + [ -n "$FONT" ] && CONSOLEFONT="$FONT" + [ -n "$FONT_MAP" ] && CONSOLEMAP="$FONT_MAP" +fi + +[ -n "$KEYMAP" ] && loadkeys -q "$KEYMAP" + +HWCLOCK_PARAMS="-s" +case $HARDWARECLOCK in + "") + ;; + UTC) + HWCLOCK_PARAMS="${HWCLOCK_PARAMS} -u" + ;; + localtime) + HWCLOCK_PARAMS="${HWCLOCK_PARAMS} -l" + ;; + *) + HWCLOCK_PARAMS="" + ;; +esac + +if [ -n "$HWCLOCK_PARAMS" ]; +then + printf "Setting hwclock.\n" + [ -n "$TIMEZONE" ] && export TZ="$TIMEZONE" + hwclock $HWCLOCK_PARAMS + unset TZ +fi + +printf "Running nldev.\n" +if [ -e /proc/self/fd ]; +then + rm -f /dev/fd 2>/dev/null + ln -sf /proc/self/fd /dev/fd +fi + +nldev -b + +printf "Loading MODULES from rc.conf.\n" +for i in $MODULES; +do + /sbin/modprobe "${i}" +done + +printf "Triggering all device events.\n" +nltrigger add all + +printf "Setting up lo device.\n" +[ -d /sys/class/net/lo ] && ip link set up dev lo + +[ "$USEDMRAID" = "yes" ] && printf "Setting up dmraid.\n" && env dmraid -i -ay +[ "$USEBTRFS" = "yes" ] && printf "Setting up btrfs.\n" && env btrfs device scan +[ "$USELVM" = "yes" ] && printf "Setting up lvm.\n" && env vgchange --sysinit -a y >/dev/null + +# TODO: crypttab support + +if [ ! -e /fastboot ]; +then + printf "Running fsck -ATa.\n" + fsck -ATa + if [ $? -gt 1 ]; + then + printf "Filesystem errors exist. Please fix manually.\n" + /bin/busybox ash + reboot + fi +fi + +printf "Remounting /.\n" +mount -o remount,rw / +printf "Mounting all filesystems in /cfg/fstab.\n" +mount -a + +[ "$USELVM" = "yes" ] && printf "Activating lvm monitor.\n" && env vgchange --monitor y >/dev/null + +printf "Activating swap.\n" +swapon -a & + +if [ -n "$TIMEZONE" ]; +then + printf "Setting local timezone to '%s',\n" "${TIMEZONE}" + local zonefile="/usr/share/zoneinfo/${TIMEZONE}" + if [ ! -e "${zonefile}" ]; + then + printf "'%s' is not a valid timezone.\n" "${TIMEZONE}" + else + if [ ! -L /cfg/localtime ] || [ ! /cfg/localtime -ef "${zonefile}" ]; + then + ln -sf "${zoneinfo}" /cfg/localtime + fi + fi +fi + +printf "Applying random seed to /dev/urandom.\n" +RANDOM_SEED=/var/lib/misc/random-seed +if [ -s $RANDOM_SEED ]; +then + cat $RANDOM_SEED > /dev/urandom +else + dd if=/dev/random of=/dev/urandom count=1 &>/dev/null +fi + +printf "Cleaning up /var.\n" +if [ ! -L /var/lock ] && [ ! /var/lock -ef /run/lock ]; +then + rm -rf /var/lock 2>/dev/null + ln -sf /run/lock /var/lock +fi + +if [ ! -L /var/run ] && [ ! /var/run -ef /run ]; +then + rm -rf /var/run 2>/dev/null + ln -sf /run /var/run +fi + +[ -s /cfg/hostname ] && HOSTNAME=$(cat /cfg/hostname) +if [ -n "$HOSTNAME" ]; +then + printf "Setting hostname to '%s'.\n" "${HOSTNAME}" + printf "%s\n" "$HOSTNAME" > /proc/sys/kernel/hostname +fi + +printf "Storing dmesg output into /var/log/dmesg.log.\n" +env dmesg > /var/log/dmesg.log +if [ -e /proc/sys/kernel/dmesg_restrict ] && [ $(cat /proc/sys/kernel/dmesg_restrict) = "1" ]; +then + chmod 0600 /var/log/dmesg.log +else + chmod 0644 /var/log/dmesg.log +fi + +# next file is stali.multi + diff --git a/rc.local b/rc.local @@ -0,0 +1,8 @@ +#!/bin/busybox ash +# +# /etc/rc.local: Local multi-user startup script. +# + +printf "Running services.\n" +svc -s 2>&1 >/dev/null + diff --git a/rc.multi b/rc.multi @@ -0,0 +1,61 @@ +#!/bin/busybox ash + +. /cfg/rc.conf + +nlpid=$(pidof nldev) +if [ -n $nlpid ]; +then + printf "Killing single nldev.\n" + kill -9 $nlpid +fi + +printf "Taking over from init nldev to multi nldev.\n" +run_nldev &>/dev/null & + +printf "Triggering all events again.\n" +nltrigger add all + +printf "Running sysctl.\n" +sysctl -p /cfg/sysctl.conf &>/dev/null +for path in /usr/lib/sysctl.d /cfg/sysctl.d /run/sysctl.d; +do + if [ -d $path ]; + then + for file in $(ls -1 $path); + do + sysctl -p $file &>/dev/null + done + fi +done + +printf "Running binfmt.\n" +mountpoint -q /proc/sys/fs/binfmt_misc || \ + mount -t binfmt_misc binfmt /proc/sys/fs/binfmt_misc +for path in /usr/lib/binfmt.d /cfg/binfmt.d /run/binfmt.d; +do + [ ! -d $path ] && continue; + for file in $(ls -1 $path); + do + grep "^:" $file | \ + while read -r line; + do + printf "%s" "$line" \ + > /proc/sys/fs/binfmt_misc/register + done + done +done + +sleep 1 +printf "Running rc.local.\n" +[ -x /bin/rc.local ] && /bin/rc.local + +printf "Killing bootlogd.\n" +if [ -f /run/bootlogd.pid ]; +then + kill $(cat /run/bootlogd.pid) + rm -f /run/bootlogd.pid +fi +[ -e /run/boot.log ] && mv /run/boot.log /var/log/boot + +# next is tty1 from stali.inittab + diff --git a/rc.shutdown b/rc.shutdown @@ -0,0 +1,86 @@ +#!/bin/busybox ash + +. /cfg/rc.conf + +# avoid staircase effect +stty onlcr + +printf "Shutting down.\n" + +[ -x /bin/rc.local.shutdown ] && /bin/rc.local.shutdown + +svc -k 2>&1 >/dev/null + +printf "Saving random seed for next boot.\n" +RANDOM_SEED=/var/lib/misc/random-seed +rm -f $RANDOM_SEED +POOL_FILE=/proc/sys/kernel/random/poolsize +if [ -r $POOL_FILE ]; +then + read POOL_SIZE < $POOL_FILE + POOL_SIZE=$(($POOL_SIZE / 8)) +else + POOL_SIZE=512 +fi +dd if=/dev/urandom of=$RANDOM_SEED count=1 bs=$POOL_SIZE &>/dev/null + +printf "Halting.\n" +halt -w + +[ "$USELVM" = "yes" ] && printf "Stopping lvm monitor.\n" && env vgchange --monitor n + +printf "Swapoff.\n" +swapoff -a &>/dev/null +printf "Unmounting all filesystems.\n" +umount -a &>/dev/null + +# TODO: crypttab support + +[ "$USELVM" = "yes" ] && printf "Stopping lvm completely.\n" && env vgchange --sysinit -a n + +if [ -x /run/initramfs/shutdown ]; +then + printf "Going back to initramfs.\n" + if [ $RUNLEVEL -eq 0 ]; + then + action="poweroff" + else + action="reboot" + fi + + mount -o bind /run/initramfs /run/initramfs + + mount -o private /run/initramfs + mount -o private / + + # bind all api mounts + for i in sys proc dev run oldroot; + do + mkdir -p /run/initramfs/${i} + done + mount -o bind /sys /run/initramfs/sys + mount -o bind /proc /run/initramfs/proc + mount -o bind /dev /run/initramfs/dev + mount -o bind /run /run/initramfs/run + + cd /run/initramfs + pivot_root . oldroot + + /oldroot/sbin/init u + + exec chroot . /shutdown $action </dev/console >/dev/console 2>&1 +else + mount -o remount,ro / + + if [ $RUNLEVEL -eq 0 ]; + then + printf "POWERR OFF\n" + poweroff -f + else + printf "REBOOTING\n" + env kexec -e &>/dev/null + reboot -f + fi + +fi + diff --git a/rc.single b/rc.single @@ -0,0 +1,24 @@ +#!/bin/busybox ash + +. /cfg/rc.conf + +if [ "$PREVLEVEL" != "N" ]; +then + svc -k 2>&1 >/dev/null +fi + +if [ -f /run/bootlogd.pid ]; +then + printf "Stopping bootlogd.\n" + touch /var/log/boot + kill $(cat /run/bootlogd.pid) + rm -f /run/bootlogd.pid +fi + +if [ $RUNLEVEL -eq 1 ]; +then + printf "Entering single-user mode.\n" + kill -HUP 1 + exec init -t1 S +fi +