Run your own scripts with autorun


Autorun overview

The autorun feature allows you to run scripts automatically at startup of the system. Each autorun script can manage a task. For example, you can create a backup script that makes a backup of a database, another for cleaning a system, …

By default, the autorun script(s) may be copied in the root of the CDRom, outside of the squashfs compressed loop image file, but other sources are available : local file system, network share, an HTTP/HTTPS server.

The script may be named autorun if alone. If many scripts have to be chained, you may name them autorun0, autorun1,… . In this case, specific autorun scripts may be allowed with an autoruns= startup option given at boot time prompt. For example, you may add autoruns=2,3,4 to the boot command line.

You can use autorun to perform completely automatic tasks using SystemRescueCd. There is a dedicated chapter that explains How to use autorun to manage server that are in a remote datacenter.

Options provided by the autorun

These options may be used at boot time, on the boot command line.

  • ar_source=xxx: place where the autoruns are stored. It may be the root directory of a partition (/dev/sda1), an nfs share (nfs://192.168.1.1:/path/to/scripts), a samba share (smb://192.168.1.1/path/to/scripts), or an http/https directory (http://192.168.1.1/path/to/scripts).. Support for HTTPS was added in August 2020 so make sure you use a recent version if you need this feature.
  • autoruns=[0-9]: comma separated list of the autorun scripts that have to be run. For instance if you use autoruns=0,2,7 then the following autorun scripts will be executed: autorun0, autorun2, autorun7. Use autoruns=no to disable all the autorun scripts with a number.
  • ar_nowait: do not wait for a keypress after the autorun scripts have been executed.
  • ar_ignorefail: continue to execute the scripts chain even if a script failed (returned a non-zero status)
  • ar_nodel: do not delete the temporary copy of the autorun scripts located in /var/autorun/tmp after execution
  • ar_disable: completely disable autorun, the simple autorun script will not be executed

Summary of scripts execution rules

At startup, a list of locations is checked against the presence of autorun files. They are, successively:

  • if the ar_source= parameter was passed at startup, the root directory of the given location. Devices are mounted to /mnt/autorun, allowing writing data into them if the device is not write-protected.
    • Hard disk: ar_source=/dev/sda2 (autorun scripts in 2nd partition of 1st IDE hard disk)
    • NFS shares: ar_source=nfs-server:/nfs/exported/directory
    • Samba shares: ar_source=//samba-server/share (recommended without user/password).
    • Http/https server: ar_source=https://web-server/adminscripts/
  • the root directory of the CD-ROM
  • the superuser home directory (/root)
  • the /usr/share/sys.autorun directory

If autorun files are found in some location, they are run and the process ends. Last two locations require rebuilding of SystemRescueCd and are to be used for advanced or test purposes.

In each source location, there are two possible modes of operation :

  • simple one : if a shell script named autorun is found, it is run
  • more flexible : if autorun# scripts are found (# is a digit from 0 to 9) and either
    • autoruns= boot parameter was NOT specified, or
    • autoruns= boot parameter value contains #

Example: autoruns=0,1,4 In this example, only autorun0, autorun1 or autorun4 scripts may be run if present. Other scripts, such as autorun2, autorun3 and autorun5, will be ignored, then authorized scripts are run in alphanumeric order. Whenever a script returns a non-zero code, the processing stops and next scripts are not run. You can prevent any autorun\# execution with an autorun= boot parameter without any digit, ex: autoruns=no. This has no effect on autorun script.

Although it is possible to put both autorun and autorun# scripts, this should probably be avoided.

The script has to be any valid shell script. Perl, python, … etc. scripts are not supported at this time. Windows end-of-line terminators are translated to allow running shell scripts written with a MS editor.

Examples of autorun scripts

#!/bin/bash
(
  ifconfig eth0 | head -n 3
  fdisk -l /dev/sda
) | tee -a /mnt/autorun/report
sync
exit 0

In this script we assume that we have passed the floppy-disk as the source of autorun with ar_source=fd0 boot-parameter. It will accumulate infos about eth0 network interface and disk partitions into a file named report on the first floppy drive. The BIOS boot sequence must specify the CD first, the diskette must be write-enabled, the CD and diskette must be inserted at startup time. Hint: The parameter -a after the command tee has the following effect: the file is not overwritten but the output is appended.

If network is automatically configured at boot (DHCP), you may send infos through an NFS or a samba share for example.

Do not forget to give the autorun location at startup. Say that you share through NFS a writeable directory named /nfs/backup on host myserver, you have to specify: ar_source=nfs://myserver:/nfs/backup at SystemRescueCd boot prompt and to put in the shared directory an autorun script such as this one :

#!/bin/bash
date=$(date +%Y%m%d)
cat /dev/sda | gzip > /mnt/autorun/$date.sda.gz && exit 0
echo $date sda backup error
exit 1

If sufficient space is provided in the NFS share, a compressed image of your first IDE disk will be copied there. Be aware that there may be a 2GB maximum file size when copying across the network or to a FAT filesystem.

You must ensure that name resolution works if you intend to access the NFS server by its name instead of its IP address.


Documentation
Manual (EN)
LVM Guide
Disk partitioning
Networking

Project
Git repository
Bug reports