How to Add curl to Ubuntu 10.04 Server’s initramfs and Execute a Boot Script with Network Access


6 views

When working with Ubuntu 10.04 Server, you might need to execute network-dependent operations during the early boot stage. This requires modifying the initramfs to include curl and ensuring the network is initialized before your script runs. Here's how to achieve this reliably, even across kernel upgrades.

First, create or edit the configuration file for initramfs-tools:

sudo nano /etc/initramfs-tools/initramfs.conf

Add the following line to include curl:

MODULES=net
BUSYBOX=y
KEYMAP=n
COMPCACHE_SIZE=""
DEVICE=
IP=
BOOT=local
MODULES=most
BUSYBOX=y
ADDITIONAL_PACKAGES="curl"

Create a script that will execute during initramfs stage. Place it in the hooks directory:

sudo nano /etc/initramfs-tools/scripts/init-premount/custom_curl

Here's an example script that waits for network connectivity:

#!/bin/sh
PREREQ=""
prereqs() {
    echo "$PREREQ"
}
case $1 in
    prereqs)
        prereqs
        exit 0
        ;;
esac

. /scripts/functions

wait_for_network() {
    while ! ip route | grep -q default; do
        sleep 1
    done
}

wait_for_network
curl http://example.com/api/checkin --data "hostname=$(hostname)"

Make the script executable:

sudo chmod +x /etc/initramfs-tools/scripts/init-premount/custom_curl

After making these changes, update your initramfs:

sudo update-initramfs -u

To test if curl is included in your initramfs:

lsinitramfs /boot/initrd.img-$(uname -r) | grep curl

To maintain these changes when upgrading kernels, create a post-install hook:

sudo nano /etc/kernel/postinst.d/zz-update-initramfs

Add the following content:

#!/bin/sh
version="$1"
update-initramfs -k "${version}" -u

Make it executable:

sudo chmod +x /etc/kernel/postinst.d/zz-update-initramfs
  • Check boot logs with dmesg | grep initramfs
  • Test network functionality in initramfs shell (accessible via recovery mode)
  • Verify curl binary is present in initramfs with lsinitramfs

Initramfs (initial RAM filesystem) is a temporary root filesystem used during Linux boot before the real rootfs is mounted. In Ubuntu 10.04 Server, it's built using initramfs-tools. The critical components are:

/etc/initramfs-tools/
├── hooks/      # Scripts that modify initramfs content
├── init-bottom/ # Late-init scripts
├── init-premount/ # Early-init scripts
└── scripts/    # Main boot scripts

To include curl and its dependencies, create a hook script:

# /etc/initramfs-tools/hooks/curl_hook
#!/bin/sh
PREREQ=""
prereqs() {
    echo "$PREREQ"
}
case $1 in
    prereqs)
        prereqs
        exit 0
        ;;
esac

. /usr/share/initramfs-tools/hook-functions

# Copy curl binary and libraries
copy_exec /usr/bin/curl /bin
copy_exec /usr/lib/libcurl.so.4
copy_exec /usr/lib/libssl.so.1.0.0
copy_exec /usr/lib/libcrypto.so.1.0.0
copy_exec /usr/lib/libz.so.1

Make it executable:

sudo chmod +x /etc/initramfs-tools/hooks/curl_hook

Create a script to set up networking before curl executes:

# /etc/initramfs-tools/scripts/init-premount/networking
#!/bin/sh
PREREQ=""
prereqs() {
    echo "$PREREQ"
}
case $1 in
    prereqs)
        prereqs
        exit 0
        ;;
esac

# Wait for network interfaces
ip link set eth0 up
udhcpc -i eth0 || true

Add your custom script that runs curl:

# /etc/initramfs-tools/scripts/local-premount/curl_script
#!/bin/sh
PREREQ=""
prereqs() {
    echo "$PREREQ"
}
case $1 in
    prereqs)
        prereqs
        exit 0
        ;;
esac

# Example: Fetch configuration
curl -s http://example.com/boot_config > /tmp/config
[ -s /tmp/config ] && . /tmp/config

Update initramfs and test:

sudo update-initramfs -u -k all

To debug, add break=premount to kernel parameters in GRUB, then inspect /bin/curl availability.

Create a package configuration file:

# /etc/initramfs-tools/conf.d/curl
COPY_TO_INITRAMFS=1

This ensures your changes persist when new kernels are installed.

  • Check /var/log/syslog for initramfs errors
  • Test with lsinitramfs /boot/initrd.img-$(uname -r)
  • Increase boot verbosity by removing quiet splash from GRUB