ubuntuinitrd

What is `hooker script` actually in `scripts/init-top/all_generic_ide`?


I'm studying init in Ubuntu's ramdisk. In scripts/init-bottom/udev I saw this code snippets:

PREREQ=""
prereqs()
{
    echo "$PREREQ"
}
case $1 in
# get pre-requisites
prereqs)
    prereqs
    exit 0
    ;;
esac

It appears almost every scripts in scripts/** scripts(like scripts/init-top/all_generic_ide,scripts/local-bottom/ntfs_3g,scripts/local-premount/fixrtc,etc). Although they may have some subtle differences, but most of them are just have same snippets on the top of body code.

I read the wiki from Ubuntu: initramfs-tools

I totally have no idea what its purpose. I think I can realize its flow but just can't figure out what it gonna do?


Solution

  • This boilerplate code just prints the value of PREREQ when the script is called with the parameter prereqs and then exits with zero.

    The man page of initramfs-tools says in HOOK SCRIPTS -> Boot scripts:

    No guarantees are made as to the order in which the different scripts are executed unless the prereqs are setup in the script.

    So the scripts in scripts/init-bottom will be executed in an arbitrary order on boot (if they do not specify prereqs). If these script depend on each other, the scripts have to define prereqs to ensure the correct execution order.

    For example, one scripts might mounts /home (call it mount_home) and one script creates new user accounts (call it create_users). Then the second script needs to declare the dependency on the first one (calling create_user prereqs should return mount_home) by using changing the boilerplate code:

    PREREQ="mount_home"
    prereqs()
    {
        echo "$PREREQ"
    }
    case $1 in
    # get pre-requisites
    prereqs)
        prereqs
        exit 0
        ;;
    esac
    

    If your script does not depend on other scripts inside the same directory, you can just copy the boilerplate code.