LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux From Scratch
User Name
Password
Linux From Scratch This Forum is for the discussion of LFS.
LFS is a project that provides you with the steps necessary to build your own custom Linux system.

Notices


Reply
  Search this Thread
Old 04-08-2017, 11:31 PM   #1
stoat
Member
 
Registered: May 2007
Distribution: LFS
Posts: 628

Rep: Reputation: 185Reputation: 185
A simple bash init scheme for LFS


After MrScoville's thread about init, I've been experimenting with a bash script for init. It does work and looks normal starting and stopping. I didn't invent this idea (see the references).

The system for the experiment is a clone of my main BLFS system. The original init was Busybox which was removed along with its inittab and four symlinks. My collection of custom-made initscripts in /etc/init.d remains and those are called by the bash init just like Busybox did.

The bash init itself was created from my 1, 2, and 3 scripts that I originally used with Runit but have continued to use almost unchanged with Busybox. Script 1 has calls to initscripts with start arguments for the usual system initialization stuff. Script 2 is a short script to prepare for shutdown. And script 3 calls the usual initscripts with stop arguments for shut down. Well, those three scripts were merged together unchanged and in order to create the init for this project. An agetty command was inserted at the end of the startup stuff to create a virtual terminal for login.

So when the kernel starts the init, it in turn runs the various initialization scripts in /etc/init.d as usual. When the process arrives at the agetty for login, the init progress stops right there until that terminal is logged out. The init is PID 1 and can never exit or the kernel panics.

Anyway, at this point I have my usual BLFS system in front of me. I can't tell the difference.

To begin the shutdown process I only have to exit Fluxbox to return to the tty command prompt and then log out of the virtual terminal. Logging out of the tty turns loose the waiting init which continues down the commands for shutting down. That also proceeds normally and looks good. I eventually created a small auxiliary Perl script to do those logouts from the Fluxbox menu. The init ends by deciding to reboot or poweroff and uses Magic SysRq codes to do those.

Below are the two files used in this project just to illustrate the method in my case. Any set of existing initscripts could be used with this method. The Perl shutdown helper script is simple to alter as needed.
Code:
#!/bin/sh
#
# Name:        init
#
# Description: An init that is launched as PID 1 by the kernel at
#              boot time. It runs the startup init scripts and opens
#              a virtual terminal for login. At shutdown time it
#              continues on to run the shutdown init scripts and ends
#              by setting up for reboot or poweroff.
#
# Usage:       /sbin/init
#
######################################################################

export PATH=/bin:/sbin:/usr/bin:/usr/sbin

# Print a console message...
echo "Init starting..."

# Mount the kernel virtual filesystems...
/etc/init.d/mountvirtfs start

# Load the kernel runtime parameters...
/etc/init.d/sysctl start

# Load modules listed in /etc/functions...
/etc/init.d/modules start

# Populate /dev and start udevd...
/etc/init.d/udev start

# Activate swap partitions...
/etc/init.d/swap start

# Check the filesystems...
/etc/init.d/checkfs start

# Mount all other partitions...
/etc/init.d/mountfs start

# Clean up after the last boot session...
/etc/init.d/cleanfs start

# Retry failed and skipped udev events...
/etc/init.d/udev_retry start

# Sychronize the system clock...
/etc/init.d/setclock start

# Bring up the local loopback network...
/etc/init.d/localnet start

# Check the kernel and system logs...
/etc/init.d/checklogs start

# Start the system log daemons...
/etc/init.d/sysklogd start

# Start iptables...
/etc/init.d/iptables start

# Bring up the network...
export IN_BOOT=1
/etc/init.d/network start
unset IN_BOOT

# Initialize the random number generator...
/etc/init.d/random start

# Start the service daemons...
/etc/init.d/alsa start
/etc/init.d/cups start
/etc/init.d/dbus start
/etc/init.d/fcron start
/etc/init.d/gpm start
/etc/init.d/ntpd start
/etc/init.d/postfix start
/etc/init.d/smartd start

# Start an extra virtual terminal, just in case. This one
# must be spawned first and then forked to the background...
/sbin/agetty tty2 &

# Start a virtual terminal for login...
/sbin/agetty --noclear tty1

# Clear the console and print a message...
clear
echo -e "\n  |        ___|    ___|"
echo "  |       |       ("
echo "  |        __|    ___ \\"
echo "  |       |            )"
echo " _____|  _|       ____/"
echo -e "\nPreparing for shutdown..."

# Stop the service daemons...
/etc/init.d/alsa stop
/etc/init.d/cups stop
/etc/init.d/dbus stop
/etc/init.d/fcron stop
/etc/init.d/gpm stop
/etc/init.d/ntpd stop
/etc/init.d/postfix stop
/etc/init.d/smartd stop

# Save the random number generator seed...
/etc/init.d/random stop

# Bring down the network...
/etc/init.d/network stop

# Sychronize the hardware clock...
/etc/init.d/setclock stop

# Stop the system log daemons...
/etc/init.d/sysklogd stop

# Stop the udev daemon...
/etc/init.d/udev stop

# Attempt to kill remaining processes...
/etc/init.d/sendsignals stop

# Unmount all non-virtual partitions & remount / ro...
/etc/init.d/mountfs stop

# De-activate swap partitions...
/etc/init.d/swap stop

# Bring down the local loopback network...
/etc/init.d/localnet stop

# Reboot or poweroff...
echo 1 > /proc/sys/kernel/sysrq
if [ -e /tmp/poweroff ]; then
   echo o > /proc/sysrq-trigger
else
   echo b > /proc/sysrq-trigger
fi

sleep inf
Code:
#!/usr/bin/perl
#
# Name:        shutdown
#
# Description: Part of the simple bash init that logs out of Fluxbox
#              and the virtual terminal to initiate shutdown steps
#              leading to reboot or poweroff. Reboot is the default
#              if no argument is passed.
#              
#
# Usage:       /sbin/shutdown [option]
#
#              -r
#                 Reboot
#
#              -h
#                 Poweroff
#
######################################################################

use strict;

if ($ARGV[0] eq "-h") {
   system("touch /tmp/poweroff");
}

my $process = `ps aux | grep fluxbox`;
(my $owner, my $pid, my $junk) = split(' ',$process);
my $killed = system("kill -SIGTERM $pid");

system("sleep 2");

my $process = `ps aux | grep bash | grep tty1`;
(my $owner, my $pid, my $junk) = split(' ',$process);
my $killed = system("kill -SIGKILL $pid");

exit 0;
So there it is. All any init really does is run startup scripts, wait for a signal to reboot or shutdown, and then run shutdown scripts. Fancier init schemes may do extra things such as multiple runlevels, resolve initscript dependencies, or monitor daemons and restart them if they stop for some reason. I either don't need those things or do them myself by other means.

Some references:
http://www.linux.it/~rubini/docs/init/
https://major.io/2009/01/29/linux-em...agic-commands/
https://felipec.wordpress.com/2013/11/04/init/

Last edited by stoat; 04-15-2017 at 05:13 PM.
 
Old 04-10-2017, 08:07 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
you beat me to it!
I have been doing much the same thing, I may post mine ( when I can do the documentation ) as a comparison, I do have service scripts that can monitor apps and relaunch if needed, and i also use the init scripts used with busybox, I've only tried this in a VM at the moment, trying to find the time to go the whole hog and put it on my main system.
 
Old 04-10-2017, 05:13 PM   #3
stoat
Member
 
Registered: May 2007
Distribution: LFS
Posts: 628

Original Poster
Rep: Reputation: 185Reputation: 185
I for one would like to see that when you have it ready. I can say right now that the idea works just fine and looks normal onscreen. The little issues I've encountered so far are either not important to me, or I have fixed them (or will). Right now I'm tweaking and playing with the little Perl script that is called from my Fluxbox menu to do the terminations of any user stuff and lastly X and the tty which makes init start the shutdown steps. Although that's not really necessary since it's a simple matter to manually exit the graphical session and then manually logout of the virtual terminal.

Last edited by stoat; 04-10-2017 at 06:18 PM.
 
Old 04-11-2017, 04:18 AM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
i'm away from my machine at the moment but i wilk collect the scripts together and upload them to git and post some details here.

to be continued ....
 
Old 04-11-2017, 10:41 AM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Well here you go this is a work in progress but seems pretty stable I have been using it to start up my desktop on my main machine via my xinitrc, start ing the desktop. wm, panels etc etc.
I have only used it as an init in a VM so far but it also seems pretty stable there as well there is a short movie in the git repo showing the boot sequence.

Get the repo with
Code:
git clone https://github.com/KeithDHedger/StartupServices.git
Have a look at the README for basic setup

BACKUP ALL RELEVANT FILES YOU HAVE BEEN WARNED!!!!!

I think I got everything in the repo but let me know if anything is missing, I hate documenting so bear with me!
 
Old 04-11-2017, 10:38 PM   #6
stoat
Member
 
Registered: May 2007
Distribution: LFS
Posts: 628

Original Poster
Rep: Reputation: 185Reputation: 185
I wanted to let you know that I installed your init system, and it works very well on my BLFS system. Congratulations on an impressive piece of work. But dude, that ain't simple. You've put some serious work into that. It's nice though having a sophisticated init but written in bash so amateurs like me can read and follow it more easily. I especially liked the multiple ttys that respawn. Installation instructions are good.

Over time I have strayed so far from the standard LFS init stuff that I replaced the initscripts and function file with mine. It still works normally.

Some comments and questions...
  1. iptables (no script, not mentioned, but could be added along with basic rule set)

  2. random (no script, not mentioned in /1, commented in /3 but wouldn't work if uncommented because the path is to /etc/runit/init.d)
    Code:
    # Save the random number generator seed
    #/etc/runit/init.d/22-random "stop"
  3. alsa (saves in /3, but no start for it in /1)

  4. I also use postfix and fcron and added those, but I don't know if you intended to include all the BLFS initscripts. Anyway, people can easily add things like that if they install them.

  5. I recommend calling those initscripts in /3 directly from /etc/init.d without the prefix numbers. They way they are called now they would break if someone renumbered them in the runlevel folders.
    Code:
    # Sync the hardware clock
    /etc/rc.d/rc1.d/08-setclock "stop"
    
    # Save the random number generator seed
    #/etc/runit/init.d/22-random "stop"
    
    # Turn swapping off
    /etc/rc.d/rc1.d/06-swap "stop"
    
    # Bring down the localnet
    /etc/rc.d/rc1.d/20-localnet "stop"
  6. Some of the initscripts source or refer to /lib/lsb/init-functions...
    Code:
    $ grep -ir "/lib/lsb/init-functions"
    etc/rc.d/init.d/httpd:. /lib/lsb/init-functions
    etc/rc.d/init.d/vboxballoonctrl-service:[ -f /lib/lsb/init-functions -a -f /etc/debian_release ] || NOLSB=yes
    etc/rc.d/init.d/vboxballoonctrl-service:    . /lib/lsb/init-functions
    etc/rc.d/init.d/vboxdrv:[ -f /etc/debian_release -a -f /lib/lsb/init-functions ] || NOLSB=yes
    etc/rc.d/init.d/vboxdrv:    . /lib/lsb/init-functions
    etc/rc.d/init.d/checkfs:. /lib/lsb/init-functions
    etc/rc.d/init.d/reboot:. /lib/lsb/init-functions
    etc/rc.d/init.d/vboxautostart-service:[ -f /etc/debian_release -a -f /lib/lsb/init-functions ] || NOLSB=yes
    etc/rc.d/init.d/vboxautostart-service:    . /lib/lsb/init-functions
    etc/rc.d/init.d/sendsignals:. /lib/lsb/init-functions
    etc/rc.d/init.d/vboxweb-service:[ -f /etc/debian_release -a -f /lib/lsb/init-functions ] || NOLSB=yes
    etc/rc.d/init.d/vboxweb-service:    . /lib/lsb/init-functions
  7. Do 1, 2, and 3 need to be in /etc/init.d? Only the set in StartupServices/startups matter. Right?
Anyway, that's all for now. But I intend to study it more because it's also a bash tutorial IMO. I learned a lot a new things from this project.
 
Old 04-12-2017, 04:48 AM   #7
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Thx for good review!
Like yourself I have strayed quite a bit from the lfs/blfs book an so some of the init scripts have been mucked about with while I have tried other things, I will go through and clean upnthe bits you have point3d out, it's probably overdue anyway!
I'll post back later with more details, I'm not at my machine at the moment.
 
Old 04-12-2017, 06:04 AM   #8
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
1)
I don't use iptables, my router has a pretty good firewall, but if you have a basic iptables script i'll add it to the repo.

2)
Removed, don't know how that creapt in the random start up script from BLFS Boot Scripts handles this.

3)
Alsa should be configured by a standard udev rules, so doesn't actually need an init script, have a look at '/lib64/udev/rules.d/90-alsa-restore.rules', that line just makes sure any changes are saved.

4)
No I don't intend to include all the BLFS scripts in the repo thay are easily added by the user, the BLFS scripts Makefile can/should be use when installing a start up script and it will put the file in the correct place and create a symlink, that's why I kept the folder hierarchy the same so it would be compatable.

5)
Agreed, done.

6)
The /lib/lsb/init-functions script is part of the BLFS Boot Scripts and really should be installed for 3rd party start up scripts, in fact if you have installed any start up scripts from (B)LFS you probably already have it installed, both it and my own functions script basically do the same thing and have grown a bit organically, there is some duplication, I will rewrite the functions script to incorporate all the needed sub routines but it may take a while!

7)
Correct only needed in startups, this was a hangover from my main system which still uses busybox init, ( I was lazy and just copied the folders 'as is' ), removed from repo.
 
Old 04-12-2017, 06:41 AM   #9
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Removed the vbox scripts/links as they are installed by the virtualbox installer and don't belong in the repo.
 
Old 04-12-2017, 07:41 PM   #10
stoat
Member
 
Registered: May 2007
Distribution: LFS
Posts: 628

Original Poster
Rep: Reputation: 185Reputation: 185
When you have some time from your real life, here are some more things to consider...

1. Things in /etc/rc.d/init.d that are not used and maybe should go:
halt
reboot
sendsignals
functions
2. In /etc/StartupServices/startups/1, should $1 in line 7 be $i? As it is now, it just prints "start" on a line by itself.
Code:
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin

for i in /etc/rc.d/rc1.d/*
  do
	  if [ -x "$i" ];then
	  	echo "$1 start"
		  "$i" start
		  if [ $? -ne 0 ];then
			  echo "Problem running $i"
			  /sbin/sulogin
			  exit 1
		  fi
	  fi
done
3. I get no hostname printed on the tty login prompt line. It prints (none). For me, adding this line back to the localnet script fixes that (see the original localnet file in the book)...
Code:
[ -r /etc/hostname ] && HOSTNAME=`cat /etc/hostname`
P.S.: I also have a visual mess on my console at boot time. The alignment of [ OK ] plus some stty error messages that may be related to the same thing. I have been looking through your init-function and the scripts. So far I don't see what is the matter. I will continue to work on it. It may be something local to me. Also see the kernel messaging intermingled with the init stuff. Some of that may be caused by the redirects to /dev/null in the scripts (some of them look like wrong syntax to me such as just "> /dev/null" in mountfs.
Click image for larger version

Name:	100_4954.jpg
Views:	56
Size:	164.1 KB
ID:	24763
P.P.S.: When you get this project polished, you should think of a name for it. Then introduce it in a new thread where it can be post #1.

Last edited by stoat; 04-12-2017 at 08:06 PM.
 
Old 04-12-2017, 08:20 PM   #11
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
I am in rhe process of cleaning up, found a bug that i need to fix 1st, but I will look at the rest of ghis tomorrow, I have fixed some of this especially the visual bit, but must have forgot to do a commit before I shutdown ... to be continued
 
Old 04-13-2017, 02:27 PM   #12
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Quote:
Originally Posted by stoat View Post
When you have some time from your real life, here are some more things to consider...

1. Things in /etc/rc.d/init.d that are not used and maybe should go:
halt
reboot
sendsignals
functions
2. In /etc/StartupServices/startups/1, should $1 in line 7 be $i? As it is now, it just prints "start" on a line by itself.
Code:
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin

for i in /etc/rc.d/rc1.d/*
  do
	  if [ -x "$i" ];then
	  	echo "$1 start"
		  "$i" start
		  if [ $? -ne 0 ];then
			  echo "Problem running $i"
			  /sbin/sulogin
			  exit 1
		  fi
	  fi
done
3. I get no hostname printed on the tty login prompt line. It prints (none). For me, adding this line back to the localnet script fixes that (see the original localnet file in the book)...
Code:
[ -r /etc/hostname ] && HOSTNAME=`cat /etc/hostname`
P.S.: I also have a visual mess on my console at boot time. The alignment of [ OK ] plus some stty error messages that may be related to the same thing. I have been looking through your init-function and the scripts. So far I don't see what is the matter. I will continue to work on it. It may be something local to me. Also see the kernel messaging intermingled with the init stuff. Some of that may be caused by the redirects to /dev/null in the scripts (some of them look like wrong syntax to me such as just "> /dev/null" in mountfs.
Attachment 24763
P.P.S.: When you get this project polished, you should think of a name for it. Then introduce it in a new thread where it can be post #1.
1)
Removed all except halt this is used by checkfs
2)
Leftover debug code, removed.
3)
Done.

Fixed the visual stuff but needs more work, do a git pull for the changes, I wont post anymore here ( sorry for hijacking your thread! ) I will start a new one soon.
 
Old 04-13-2017, 03:34 PM   #13
stoat
Member
 
Registered: May 2007
Distribution: LFS
Posts: 628

Original Poster
Rep: Reputation: 185Reputation: 185
Quote:
Originally Posted by Keith Hedger

...I wont post anymore here ( sorry for hijacking your thread! ) I will start a new one soon.
I didn't mean that. Seriously. My part of this thread is pretty much over with. I think your project is important enough not to be buried in here. That's all I meant. Until then, feel free to continue batting around development stuff for it right here if you want to.

Last edited by stoat; 04-13-2017 at 03:36 PM.
 
Old 04-13-2017, 08:20 PM   #14
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
nah its fine it needs to go in its own thread not a problem
 
Old 04-13-2017, 10:16 PM   #15
stoat
Member
 
Registered: May 2007
Distribution: LFS
Posts: 628

Original Poster
Rep: Reputation: 185Reputation: 185
  1. The console screen does look better now regarding the alignment of the bracketed items to the right of the messages. They still are not all the way to the right, but at least they are aligned now.

  2. I wish I could get rid of the messages from the commands in /etc/StartupServices/servicedata/services. Redirects to /dev/null don't do the job. Minor issue though. The kernel spew is still mixed in there too, and I am already using "kernel.printk = 4 4 1 7" in /etc/sysctl to inhibit some of that stuff that is not very useful. But admittedly, I'm sort of OCD about the console at startup and shutdown.

  3. For me, the sulogin command in /etc/StartupServices/startups/1 goes into an infinite loop when a problem is encountered. I had to comment out the sulogin and exit commands in 1 to be able to see what to fix.

  4. Reboot and poweroff work okay.

  5. From my point of view anyway, you may be getting close.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Researchers offer simple scheme to stop the next Stuxnet LXer Syndicated Linux News 0 02-26-2017 03:01 PM
Busybox init for LFS stoat Linux From Scratch 34 02-01-2015 08:59 AM
wget: scheme missing?? (when run from bash script only) psycroptic Linux - Networking 1 01-10-2012 04:58 PM
Partition Scheme (Ubuntu/LFS/home etc). Recommendation Please? Ubunoob001 Linux - Newbie 1 04-11-2010 12:15 AM
LFS and init-ng ParticleHunter Linux From Scratch 1 03-29-2006 05:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux From Scratch

All times are GMT -5. The time now is 05:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration