New crash of the Raspberry : the importance of scripts in init.d
Two days ago I have (yet) had a wonderful crash of the Raspberry with a corruption of the SD card. I really don't see where it can come but I thought “Not serious, I have a backup that I will restore 2 minutes”. Unfortunately it was the theory as you will see.
The why and how
In my automatic backup system that I explain in step 15 here, I pointed out that my home I added on the USB stick a script in /etc/init.d so when I boot top the backup will start automatically. Wholesale, the SD card request to boot on the USB stick, the USB stick is the backup and then returned it to the SD card.
The only problem is that this script is found to be executed before dropbear. And at the end of the script I do a reboot to get back on the SD card. So no way of me log in order to my restores !!! I can already hear the sniggering at the back of the room ! 🙂
Past the time where it is super pissed (ça peut durer longtemps chez moi 😉 ), I recovered a Debian for Virtualbox image (here), installed partclone and after assembling my key and my card, I restored. And me re this is ! 🙂
But in all that, I understood that swinging a script in /etc/init.d, make a Update.RC - d It is not as simple. So I decided to concentrate on the operation of these scripts on Debian which is the most widely used distribution (as a basis) on Raspberry Pi.
It is what it : init ?
In every UNIX system, CA starts the program that is called init and which is located in /sbin. This program, from its configuration file /etc/inittab, raises a series of scripts in a defined order to initialize the system.The first scripts that are executed are those located in /etc//etc/RCS.d (I pass on the details of the type of execution based on the files extension)
# Boot-time system configuration/initialization script. # This is run first except when booting in emergency (-b) mode. If::sysinit:// etc/init.d/rcS
It comes here to launch the basic services for the proper functioning of the bone : mount drives, load modules into the kernel, enable the network, etc.
Once this series of scripts completed, Linux enters run level requested by the system. Kezako ca ? Linux, like most systems that are compatible with System V, has 7 execution levels :
- 0 (system shutdown),
- 1 (single-user mode or maintenance mode),
- 2 from 5 (various multi-user modes),
- 6 (system restart)
Each level has its own directory which contains links to the scripts to run. Thus, enter in the XY level will launch the scripts in the /etc/rcXY.d/ directory . On our favorite Debian-based distribution, the default level is 2.
The process init will therefore look at what it has to do with this information through the configuration file /etc/inittab. The system seeks what is located in the directory /etc/rc2.d and executes it.
We find our nginx scripts, rtorrentd, Dropbear, etc.
Here's to the functioning of init and how are selected programs that will be launched at startup. Turn to the customization.
Adding new scripts : update-rc.d
How to add a script to the startup of the Raspberry ? Found throughout the tutorials the following command
update-rc.d <name of the script> defaults
This command takes care of creating a link between the script and directories of levels 2, 3, 4 and 5 for its start-up and 0, 1 and 6 for its judgment. It's like this that everything shuts down properly when doing a halt or shutdown.
It is therefore good practice to add the launch of our small web server, FTP or well yet owncloud. For me I knew defaults, Remove and voila. The bare necessities.
The only small detail, and it is he who makes the difference, It is the defaults When adding script. It is from there that come my concerns to connect !
Car Update.RC - d knows how to manage dependencies between scripts if asked. With the defaults, In addition to scripts that are created, It adds a level of default priority which is 20. Therefore all the scripts we add have the same priority. And are therefore executed in alphabetical order. Therefore, backup_sd is before dropbear ….
A solution (We'll see more far that there is another even more interesting) Therefore, change the priority level of the backup script so it launches after dropbear.
Remove the old script with the following command
Update.RC - d <name of the script> Remove
Add the script
Update.RC - d <name of the script> Start 30 2 3 4 5 . stop 70 0 1 6 .
So we start the script with a priority of 30 so after those with a priority of 20. To stop, even if our script does nothing, It should be stated and the rule is that the priority of judgment is equal to 100 under the priority of launch.
The LSB, This is for your own good !
If you added a personal script, You can be had this kind of message
update-rc.d: using dependency based boot sequencing insserv: warning: script ' K02inadyn ' missing LSB tags and overrides insserv: warning: script 'inadyn' missing LSB tags and overrides
The origin of these messages is that the script does not contain any header LSB. The meaning of LSB is Linux Standards Base. This allows to standardize scripts that are run by the system with the following rules
- Have at least the start actions, stop, restart, force-reload, and status
- Have a return code
- Document dependencies
Here's to what looks like the header :
### BEGIN INIT INFO # Provides: ScriptName # Required-Start: $$remote_fs $syslog # Required-Stop: $$remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start daemon at boot time # Description: Enable service provided by daemon. ### END INIT INFO
For those who want more details it is here that ca is going (http://wiki.debian.org/LSBInitScripts). The options are numerous. An example of what could be put in place for our script would be :
### BEGIN INIT INFO # Provides: backup_sd # Required-Start: $CL. # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Script to start the backup process # Description: Perform a full backup of all system partitions defined on the SD card ### END INIT INFO
And this is ! Voila. Adding the script doesn't generate more error, After all the others thanks to $CL., and so in case of need it can be interrupted to make one restores !
I hope not to have been too long and that these details will help you to add and configure scripts to your precious startup !