March 3, 2015

replace systemd with a simple busybox inittab

1. install busybox
2. create file /etc/inittab with the following content:
# Start "rc init" on boot
::sysinit:/opt/rc init

# Set up the TTY's 1 through 4
tty1::askfirst:/sbin/agetty -8 -s 38400 tty1 linux
tty2::respawn:/sbin/agetty -8 -s 38400 tty2 linux

# Stop all services on shutdown
::shutdown:/opt/rc shutdown

# Killing everything on shutdown
::shutdown:echo :: sending SIGTERM to all
::shutdown:/bin/kill -s TERM -1
::shutdown:sleep 1
::shutdown:echo :: sending SIGKILL to all
::shutdown:/bin/kill -s KILL -1

# Unmount everything on shutdown
::shutdown:echo :: unmounting everything
::shutdown:/bin/umount -a -r
::shutdown:/bin/mount -o remount,ro /

3. ln -s /bin/busybox /opt/init
4. create /opt/rc with the following content:
#!/bin/sh
on_boot() {
    #===================
    # mount the API filesystem
    # /proc, /sys, /run, /dev, /run/lock, /dev/pts, /dev/shm
    echo 3 mounting API filesystem...
    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
    mountpoint -q /dev     || mount -t devtmpfs dev /dev -o mode=0755,nosuid
    mkdir -p /dev/pts /dev/shm
    mountpoint -q /dev/pts || mount -t devpts devpts /dev/pts -o mode=0620,gid=5,nosuid,noexec
    mountpoint -q /dev/shm || mount -t tmpfs shm /dev/shm -o mode=1777,nosuid,nodev

    #===================
    # initialize system
    echo 3 setting up loopback device...
    /usr/sbin/ip link set up dev lo

    echo 3 initializing udev...
        busybox mdev -s
        echo /sbin/mdev > /proc/sys/kernel/hotplug

    echo 3 setting hostname...
    cat /etc/hostname >| /proc/sys/kernel/hostname

    echo 3 mounting...
    mount -a
    mount -o remount,rw /

        dhclient eth0&
        /etc/init.d/ssh start&
}

on_shutdown() {
    #===================
    echo 3 shutting down udev...
        killall busybox
        killall mdev

    #===================
    # umount the API filesystem
    echo 3 unmounting API filesystem...
    umount -r /run
}

#===================
# handle arguments
case "$1" in
init)
    on_boot;;
shutdown)
    on_shutdown;;
esac

5. reboot and add the following parameter to your kernel command line on grub:
init=/opt/init

Enjoy

No comments:

Post a Comment