Creating an Initramfs: Difference between revisions

From CBLFS
Jump to navigationJump to search
Michael.six (talk | contribs)
I wrote this script myself compiled from various information I found by googling
 
Michael.six (talk | contribs)
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Package-Introduction|The mkinitramfs script creates a gzip compressed cpio archive using busybox and a simple shell init script, which can be used in place of an initrd to do such things as load kernel modules before the system boots, in order to access functionality that is not compiled into the kernel, and therefore must be loaded, such as usb disks, scsi disks, etc. This is different than loading modules automatically at boot time, because those are loaded after the root disk is mounted, fsck'd etc, and typically an initrd contains modules which are loaded into the kernel before anything else, to fascilitate mounting the root fs. IE kernel doesn't have reiserfs support compiled in, but your root fs is formatted reiserfs}}
{{Package-Introduction|The mkinitramfs script creates a gzip compressed cpio archive using busybox and a simple shell init script, which can be used in place of an initrd to do such things as load kernel modules before the system boots, in order to access functionality that is not compiled into the kernel, and therefore must be loaded, such as usb disks, scsi disks, etc. This is different than loading modules automatically at boot time, because those are loaded after the root disk is mounted, fsck'd etc, and typically an initrd contains modules which are loaded into the kernel before anything else, to fascilitate mounting the root fs. IE kernel doesn't have reiserfs support compiled in, but your root disk is formatted reiserfs}}
== Dependencies ==
== Dependencies ==


Line 9: Line 9:


== Creating the mkinitramfs script ==
== Creating the mkinitramfs script ==
 
cat > /usr/sbin/mkinitramfs << "EOF"
  cat > /usr/sbin/mkinitramfs << "END"
#!/bin/bash
  #!/bin/bash
errmsg(){
  # This is a bash script to create a busybox
  cat << EOT
  # based gzipped cpio archive to be used as
   [ERROR] $@
   # an initrd
EOT
  INITRAMFS_USE_KERNEL="$(uname -r)"
}
  INITRAMFS_OUTPUT="/boot/initramfs.igz"
killme(){
  until [ -z "${1}" ]; do
  errmsg "$@"
    case ${1} in
  errmsg "Exiting ..."
      -c) rm -rf /boot/initramfs-tree
  exit 1
      ;;
}
      -k)
helpme(){
      INITRAMFS_USE_KERNEL="${2}"
  cat << EOT
      shift
  $(basename ${0}) is a script to automagically make
      ;;
  a gzip compressed cpio archive containsing busybox
      -m)
  which can act as an initrd.
      INITRAMFS_USE_MODULES="${2}"
      shift
-c              - remove initramfs-tree before creating the
      ;;
                  initramfs
      -v|--verbose)
      INITRAMFS_USE_VERBOSE="true"
-v|--verbose   - use verbose messages
      ;;
      -m)
-k <version>    - use modules from kernel <version>
      INITRAMFS_USE_MODULES="${2}"
      shift
-m <arg1,arg2>  - comma separated list of additional modules
      ;;
                  besides the ones contained in the file
      -o)
                  /etc/sysconfig/mkinitramfs.modules to add
      INITRAMFS_OUTPUT="${2}"
                  to the initramfs archive
      shift
      ;;
-o <arg>        - output finished archive to <arg> instead
     esac
                  of /boot/initramfs.igz
    shift
  done
-r|--rootfs     - use device as root device
  ermsg(){
    cat << EOF
Example:
  [ERROR] $@
EOT
  EOF
}
  }
usage(){
  killme(){
  cat << EOT
    ermsg "$@"
  $(basename ${0}) -c -k 2.6.30.2 -m ext3 -r /dev/hda3 -o /boot/initramfs.igz-2.6.30.2
    ermsg "Exiting ..."
EOT
    exit 1
}
  }
vrbmsg(){
  vrbmsg(){
  if [ "${initramfs_use_verbose}" ]; then
    if [ "${INITRAMFS_USE_VERBOSE}" = 'true' ]; then
    cat << EOT
      cat << EOF
$@
  $@
EOT
  EOF
  fi
    fi
}
  }
fnd_mod(){
  if [ ! -x /bin/busybox ]; then
  module="$(find /lib/modules/${initramfs_use_kernel} -name ${1}.ko)"
    killme "The initramfs needs busybox ..."
  if [ ! -n "${module}" ]; then
  fi
    module="$(find /lib/modules/${initramfs_use_kernel} -name ${1}.ko.gz)"
  if [ ! -x /usr/bin/cpio ]; then
  fi
    killme "The initramfs needs cpio ..."
  cat << EOT
  fi
${module}
  if [ ! -x /bin/gzip ]; then
EOT
    killme "The initramfs needs gzip ..."
  unset module
  fi
}
  rm -f /tmp/mkinitramfs.modules
initramfs_use_kernel="$(uname -r)"
  rm -f /tmp/mkinitramfs.moddeps
initramfs_output="/boot/initramfs.igz"
  get_moddeps(){
initramfs_use_rootfs="/dev/hda2"
    if [ -f /lib/modules/${INITRAMFS_USE_KERNEL}/modules.dep ]; then
initramfs_use_rootfstype="ext3"
      grep "${1}": /lib/modules/${INITRAMFS_USE_KERNEL}/modules.dep \
until [ -z "${1}" ]; do
  | sed -e "s|${1}:||"
  case "${1}" in
    else
    -c)
       cat << EOF
      rm -rf /boot/initramfs-tree
  get_moddeps failure
      ;;
  EOF
    -v|--verbose)
    fi
      initramfs_use_verbose='true'
  }
       ;;
  fnd_module(){
    -k)
    fnd_mod_outpt="$(find /lib/modules/${INITRAMFS_USE_KERNEL} -name ${1}.ko)"
      initramfs_use_kernel="${2}"
    if [ -z "${fnd_mod_oupt}" ]; then
      shift
       fnd_mod_outpt="$(find /lib/modules/${INITRAMFS_USE_KERNEL} -name ${1}.ko.gz)"
      ;;
    fi
    -m)
    echo ${fnd_mod_outpt} | sed -e "s|/lib/modules/${INITRAMFS_USE_KERNEL}/||"
      initramfs_use_modules="${2}"
  }
       shift
  if [ ! -d /boot/initramfs-tree ]; then
      ;;
    mkdir /boot/initramfs-tree &&
    -o)
    mkdir /boot/initramfs-tree/{etc,bin,sbin,lib} &&
      initramfs_output="${2}"
    mkdir /boot/initramfs-tree/{proc,root,mnt,sys}
      shift
    mkdir /boot/initramfs-tree/lib/modules \
      ;;
  || killme "Problem making the initramfs-tree ..."
    -r|--rootfs)
    ln -s . /boot/initramfs-tree/usr
      initramfs_use_rootfs="${2}"
  fi
      shift
  if [ -f /etc/sysconfig/initramfs.modules ]; then
      ;;
    cat /etc/sysconfig/initramfs.modules | while read line; do
    -h|--help)
      if [ -n "${line}" -a "$(head -c1 <<<${line})" != "#" ]; then
      helpme
        if [ -n "$(fnd_module ${line})" ]; then
      usage
          fnd_module ${line} >> /tmp/mkinitramfs.modules
      exit 0
        else
      ;;
          ermsg "Couldn't find module ${line}"
    -u|--usage)
        fi
      usage
      fi
      exit 0
    done
      ;;
  fi
  esac
  if [ -n "${INITRAMFS_USE_MODULES}" ]; then
  shift
    for ITEM in $(echo ${INITRAMFS_USE_MODULES//,/ }); do
done
      if [ -n "$(fnd_module ${ITEM})" ]; then
for mkinitramfs_tool in /bin/busybox /usr/bin/cpio /bin/gzip; do
        fnd_module ${ITEM} >> /tmp/mkinitramfs.modules
  if [ ! -x "${mkinitramfs_tool}" ]; then
      else
    killme "Required tool ${mkinitramfs_tool} missing!"
        ermsg "Couldn't find module ${ITEM}"
  fi
      fi
done
    done
if [ ! -d "$(dirname ${initramfs_output})" ]; then
    unset ITEM
  killme "Can not create ${initramfs_output}"
  fi
fi
  touch /tmp/mkinitramfs.moddeps
if [ ! -d '/boot/initramfs-tree' ]; then
  if [ -f /tmp/mkinitramfs.modules ]; then
  mkdir /boot/initramfs-tree
    cat /tmp/mkinitramfs.modules | while read IMOD; do
  mkdir /boot/initramfs-tree/{bin,sbin,lib,etc,proc,root,sys,mnt}
      vrbmsg "generating module dependencies for ${IMOD}" &&
  mkdir /boot/initramfs-tree/lib/modules/${initramfs_use_kernel}
      if [ "$(get_moddeps ${IMOD})" != "get_moddeps failure" ]; then
  ln -s . /boot/initramfs-tree/usr
        if [ -n "$(get_moddeps ${IMOD})" ]; then
fi
          for ITEM in $(get_moddeps ${IMOD}); do
if [ -f /etc/sysconfig/initramfs.modules ]; then
            if [ -z "$(grep ${IMOD} /tmp/mkinitramfs.moddeps)" ]; then
  cat /etc/sysconfig/initramfs.modules | while read line; do
              echo "${ITEM}" >> /tmp/mkinitramfs.moddeps
    if [ -n "${line}" -a "$(head -c1 <<<${line})" != "#" ]; then
            fi
      module="$(fnd_mod ${line})"
          done
      if [ -n "${module}" ]; then
          unset ITEM
        vrbmsg "Found ${module}"
        fi
        case "${module}" in
      fi
          *.ko)
      if [ -z "$(grep ${IMOD} /tmp/mkinitramfs.moddeps)" ]; then
            cat ${module} > /boot/initramfs-tree/lib/modules/${initramfs_use_kernel}/$(basename ${module})
        echo "${IMOD}" >> /tmp/mkinitramfs.moddeps
            ;;
      fi
          *.ko.gz)
    done
            gzip -dc ${module} > /boot/initramfs-tree/lib/modules${initramfs_use_kernel}/$(basename ${module} .gz)
  fi
            :;
  unset IMOD
        esac
  if [ -f /tmp/mkinitramfs.moddeps ]; then
      else
    cat /tmp/mkinitramfs.moddeps | while read line; do
        errmsg "Couldn't find ${line}"
      case ${line} in
      fi
        *.ko.gz)
    fi
        gzip -dc /lib/modules/${INITRAMFS_USE_KERNEL}/${line} \
  done
  > /boot/initramfs-tree/lib/modules/$(basename ${line} .ko.gz).ko
fi
        echo "$(basename ${line} .ko.gz)" >> /boot/initramfs-tree/etc/modules
if [ -n "${initramfs_use_modules}" ]; then
        ;;
  for line in ${initramfs_use_modules//,/ }; do
        *.ko)
    module="$(fnd_mod ${line})"
        cat /lib/modules/${MKINITRAMFS_USE_KERNEL}/${line} \
    if [ -n "${module}" ]; then
  > /boot/initramfs-tree/lib/modules/$(basename ${line})
      vrbmsg "Found ${module}"
        echo "$(basename ${line} .ko)" >> /boot/initramfs-tree/etc/modules
      case "${module}" in
        ;;
        *.ko)
      esac
          cat ${module} > /boot/initramfs-tree/lib/modules/${initramfs_use_kernel}/$(basename ${module})
    done
          echo "$(basename ${module} .ko)" >> /boot/initramfs-tree/etc/modules
  fi
          ;;
  # Now for the init
        *.ko.gz)
  cat << "EOT" >/boot/initramfs-tree/init
          gzip -dc ${module} > /boot/initramfs-tree/lib/modules/${initramfs_use_kernel}/$(basename ${module} .gz)
  #!/bin/busybox sh
          echo "$(basename ${module} .ko.gz)" >> /boot/initramfs-tree/etc/modules
  # Mount things which are needed by this script
          ;;
  /bin/busybox mount -t proc proc /proc
      esac
  /bin/busybox mount -t sysfs sysfs /sys
    else
  # Disable kernel messages from popping up on
      errmsg "Couldn't find ${line}"
  # the screen
    fi
  /bin/busybox echo 0 > /proc/sys/kernel/printk
  done
  # Clear the screen
fi
  /bin/busybox clear
cat /bin/busybox > /boot/initramfs-tree/bin/busybox
  /bin/busybox cat << EOF
chmod 755 /boot/initramfs-tree/bin/busybox
  This system is uses Community Beyond Linux From Scratch
cat > /boot/initramfs-tree/init << EOT
  <http://cblfs.cross-lfs.org/>
#!/bin/busybox sh
  EOF
# Mount things which are needed by this script
  # Create the symlinks to busybox
/bin/busybox mount -t proc proc /proc
  /bin/busybox --install -s
/bin/busybox mount -t sysfs sysfs /sys
  # Create device nodes
# Disable kernel messages from popping up on
  /bin/busybox mknod /dev/null c 1 3
# the screen
  /bin/busybox mknod /dev/tty c 5 0
/bin/busybox echo 0 > /proc/sys/kernel/printk
  # Check for modules
# Clear the screen
  if [ -f /etc/modules ]; then
/bin/busybox clear
    /bin/busybox depmod /lib/modules
/bin/busybox cat << END
    /bin/busybox cat /etc/modules | while read line; do
  This system uses Community Beyond Linux From Scratch
      if [ -n "${line}" ]; then
  <http://cblfs.cross-lfs.org/>
        /bin/busybox modprobe ${line/.ko/}
END
      fi
# Create the symlinks to busybox
    done
/bin/busybox --install -s
  fi
# Create device nodes
  # Sleep for devices to connect, like USB
/bin/busybox mknod /dev/null c 1 3
  /bin/busybox sleep 5
/bin/busybox mknod /dev/tty c 5 0
  /bin/busybox mdev -s
# Check for modules
  # Function for parsing command line options with
if [ -f /etc/modules ]; then
  # '=' in them. Ex. get_opt("init=/sbin/init")
  /bin/busybox depmod /lib/modules
  # would return "/sbin/init"
  /bin/busybox cat /etc/modules | while read line; do
  get_opt() {
    if [ -n "\${line}" ]; then
  echo "$@" | /bin/busybox cut -d "=" -f 2
      /bin/busybox modprobe \${line/.ko/}
  }
    fi
  # Defaults
  done
  init="/sbin/init"
fi
  root="/dev/hda1"
# Sleep for devices to connect, like USB
  runlevel="3"
/bin/busybox sleep 5
  for i in $(cat /proc/cmdline); do
/bin/busybox mdev -s
  case ${i} in
# Function for parsing command line options with
  root\=*)
# '=' in them. Ex. get_opt("init=/sbin/init")
  root=$(get_opt ${i})
# would return "/sbin/init"
  ;;
get_opt() {
  init\=*)
  echo "\$@" | /bin/busybox cut -d "=" -f 2
  init=$(get_opt ${i})
}
  ;;
# Defaults
  runlevel\=*)
init="/sbin/init"
  runlevel=$(get_opt ${i})
root="${initramfs_use_rootfs}"
  ;;
runlevel="3"
  esac
for i in \$(cat /proc/cmdline); do
  done
  case \${i} in
  # Mount the root device
    root\=*)
  mount -o ro "${root}" /root
      root=\$(get_opt \${i})
  # Below is an example for loading a disk image from
      ;;
  # a linux partition
    init\=*)
  if [ -f /root/lfs-root.img ]; then
      init=\$(get_opt \${i})
    BNROOT=$(/bin/busybox basename "${root}")
      ;;
    umount root
    runlevel\=*)
    mkdir -p /mnt/"${BNROOT}" &&
      runlevel=\$(get_opt \${i})
    mount "${root}" /mnt/"${BNROOT}" &&
      ;;
    mount -o loop /mnt/"${BNROOT}"/lfs-root.img /root &&
  esac
    # Make sure that we don't lose access to the partition
done
    # containing the disk image
# Mount the root device
    mkdir -p /root/mnt/"${BNROOT}" &&
mount -o ro "\${root}" /root
    mount /mnt/"${BNROOT}" /root/mnt/"${BNROOT}"
# Below is an example of how to use the initramfs
    unset BNROOT
# to load clfs or lfs from a disk image
  fi
#if [ -f /root/lfs-root.img ]; then
  # Check if the $init exists, and is executable
BNROOT=\$(/bin/busybox basename "\${root}")
  if [[ -x "/root/${init}" ]]; then
umount root
  # Unmount all other mounts so that the ram used by
mkdir -p /mnt/"\${BNROOT}" &&
  # the initramfs can be cleared after switch_root
mount "\${root}" /mnt/"\${BNROOT}" &&
  umount /sys /proc
mount -o loop /mnt/"\${BNROOT}"/lfs-root.img /root &&
 
# mkdir -p /root/mnt/"\${BNROOT}" &&
  # Switch to the new root and execute the init at
mount /mnt/"\${BNROOT}" /root/mnt/"\${BNROOT}"
  # the specified runlevel
unset BNROOT
  exec switch_root /root "${init}" "${runlevel}"
#fi
  fi
if [ -x "/root/\${init}" ]; then
 
  # Unmount all other mounts so that the ram used by
  # This will only be run if the exec above failed
  # the initramfs can be cleared after switch_root
  echo "Failed 'exec switch_root /root \"${init}\" \"${runlevel}\"'"
  umount /sys /proc
  exec sh
  # Switch to the new root and execute the init at
  EOT
  # the specified runlevel
  chmod 0755 /boot/initramfs-tree/init
  exec switch_root /root "\${init}" "\${runlevel}"
  cat /bin/busybox > /boot/initramfs-tree/bin/busybox
fi
  chmod 0755 /boot/initramfs-tree/bin/busybox
echo "Failed 'exec switch_root /root \\"\${init}\\" \\"\${runlevel}\\"'"
  if [ ! -d "$(dirname ${INITRAMFS_OUTPUT})" ]; then
exec sh
    killme "Cannot create ${INITRAMFS_OUTPUT}"
EOT
  fi
chmod 755 /boot/initramfs-tree/init
  ( cd /boot/initramfs-tree &&
( cd /boot/initramfs-tree
    find . | cpio -H newc -o | gzip -9c > ${INITRAMFS_OUTPUT} || exit 1
  find . | cpio -H newc -o | gzip -9c > ${initramfs_output}
  ) || exit 1
) || exit 1
  rm -f /tmp/mkinitramfs.modules
EOF
  rm -f /tmp/mkinitramfs.moddeps
chmod 755 /usr/sbin/mkinitramfs
  unset INITRAMFS_OUTPUT INITRAMFS_USE_KERNEL INITRAMFS_USE_MODULES INITRAMFS_USE_KERNEL
  END
  chmod 0755 /usr/sbin/mkinitramfs

Latest revision as of 21:55, 30 October 2009

Introduction to Creating an Initramfs

The mkinitramfs script creates a gzip compressed cpio archive using busybox and a simple shell init script, which can be used in place of an initrd to do such things as load kernel modules before the system boots, in order to access functionality that is not compiled into the kernel, and therefore must be loaded, such as usb disks, scsi disks, etc. This is different than loading modules automatically at boot time, because those are loaded after the root disk is mounted, fsck'd etc, and typically an initrd contains modules which are loaded into the kernel before anything else, to fascilitate mounting the root fs. IE kernel doesn't have reiserfs support compiled in, but your root disk is formatted reiserfs

Project Homepage: Unknown

Dependencies

Required

Note

The init script created for the initramfs will drop to the shell in busybox if there is an error, while only a few things are actually needed for the init to function, while compiling busybox you might want to keep in mind that it may need additional functionality to be able to work properly if the initramfs drops to the shell. Also this assumes that busybox is compiled statically linked, if that is not the case, additional libraries will be required to be added to the initramfs-tree. To test this run 'file /bin/busybox'. If file reports it as dynamically linked, running 'ldd /bin/busybox' should help you determine what libraries may be needed.

Creating the mkinitramfs script

cat > /usr/sbin/mkinitramfs << "EOF"
#!/bin/bash
errmsg(){
  cat << EOT
 [ERROR] $@
EOT
}
killme(){
  errmsg "$@"
  errmsg "Exiting ..."
  exit 1
}
helpme(){
  cat << EOT
 $(basename ${0}) is a script to automagically make
 a gzip compressed cpio archive containsing busybox
 which can act as an initrd.

-c              - remove initramfs-tree before creating the
                  initramfs

-v|--verbose    - use verbose messages

-k <version>    - use modules from kernel <version>

-m <arg1,arg2>  - comma separated list of additional modules
                  besides the ones contained in the file
                  /etc/sysconfig/mkinitramfs.modules to add
                  to the initramfs archive

-o <arg>        - output finished archive to <arg> instead
                  of /boot/initramfs.igz

-r|--rootfs     - use device as root device

Example:
EOT
}
usage(){
  cat << EOT
 $(basename ${0}) -c -k 2.6.30.2 -m ext3 -r /dev/hda3 -o /boot/initramfs.igz-2.6.30.2
EOT
}
vrbmsg(){
  if [ "${initramfs_use_verbose}" ]; then
    cat << EOT
$@
EOT
  fi
}
fnd_mod(){
  module="$(find /lib/modules/${initramfs_use_kernel} -name ${1}.ko)"
  if [ ! -n "${module}" ]; then
    module="$(find /lib/modules/${initramfs_use_kernel} -name ${1}.ko.gz)"
  fi
  cat << EOT
${module}
EOT
  unset module
}
initramfs_use_kernel="$(uname -r)"
initramfs_output="/boot/initramfs.igz"
initramfs_use_rootfs="/dev/hda2"
initramfs_use_rootfstype="ext3"
until [ -z "${1}" ]; do
  case "${1}" in
    -c)
     rm -rf /boot/initramfs-tree
     ;;
    -v|--verbose)
     initramfs_use_verbose='true'
     ;;
    -k)
     initramfs_use_kernel="${2}"
     shift
     ;;
    -m)
     initramfs_use_modules="${2}"
     shift
     ;;
    -o)
     initramfs_output="${2}"
     shift
     ;;
    -r|--rootfs)
     initramfs_use_rootfs="${2}"
     shift
     ;;
    -h|--help)
     helpme
     usage
     exit 0
     ;;
    -u|--usage)
     usage
     exit 0
     ;;
  esac
  shift
done
for mkinitramfs_tool in /bin/busybox /usr/bin/cpio /bin/gzip; do
  if [ ! -x "${mkinitramfs_tool}" ]; then
    killme "Required tool ${mkinitramfs_tool} missing!"
  fi
done
if [ ! -d "$(dirname ${initramfs_output})" ]; then
  killme "Can not create ${initramfs_output}"
fi
if [ ! -d '/boot/initramfs-tree' ]; then
  mkdir /boot/initramfs-tree
  mkdir /boot/initramfs-tree/{bin,sbin,lib,etc,proc,root,sys,mnt}
  mkdir /boot/initramfs-tree/lib/modules/${initramfs_use_kernel}
  ln -s . /boot/initramfs-tree/usr
fi
if [ -f /etc/sysconfig/initramfs.modules ]; then
  cat /etc/sysconfig/initramfs.modules | while read line; do
    if [ -n "${line}" -a "$(head -c1 <<<${line})" != "#" ]; then
      module="$(fnd_mod ${line})"
      if [ -n "${module}" ]; then
        vrbmsg "Found ${module}"
        case "${module}" in
          *.ko)
           cat ${module} > /boot/initramfs-tree/lib/modules/${initramfs_use_kernel}/$(basename ${module})
           ;;
          *.ko.gz)
           gzip -dc ${module} > /boot/initramfs-tree/lib/modules${initramfs_use_kernel}/$(basename ${module} .gz)
           :;
        esac
      else
        errmsg "Couldn't find ${line}"
      fi
    fi
  done
fi
if [ -n "${initramfs_use_modules}" ]; then
  for line in ${initramfs_use_modules//,/ }; do
    module="$(fnd_mod ${line})"
    if [ -n "${module}" ]; then
      vrbmsg "Found ${module}"
      case "${module}" in
        *.ko)
         cat ${module} > /boot/initramfs-tree/lib/modules/${initramfs_use_kernel}/$(basename ${module})
         echo "$(basename ${module} .ko)" >> /boot/initramfs-tree/etc/modules
         ;;
        *.ko.gz)
         gzip -dc ${module} > /boot/initramfs-tree/lib/modules/${initramfs_use_kernel}/$(basename ${module} .gz)
         echo "$(basename ${module} .ko.gz)" >> /boot/initramfs-tree/etc/modules
         ;;
      esac
    else
      errmsg "Couldn't find ${line}"
    fi
  done
fi
cat /bin/busybox > /boot/initramfs-tree/bin/busybox
chmod 755 /boot/initramfs-tree/bin/busybox
cat > /boot/initramfs-tree/init << EOT
#!/bin/busybox sh
# Mount things which are needed by this script
/bin/busybox mount -t proc proc /proc
/bin/busybox mount -t sysfs sysfs /sys
# Disable kernel messages from popping up on
# the screen
/bin/busybox echo 0 > /proc/sys/kernel/printk
# Clear the screen
/bin/busybox clear
/bin/busybox cat << END
 This system uses Community Beyond Linux From Scratch
 <http://cblfs.cross-lfs.org/>
END
# Create the symlinks to busybox
/bin/busybox --install -s
# Create device nodes
/bin/busybox mknod /dev/null c 1 3
/bin/busybox mknod /dev/tty c 5 0
# Check for modules
if [ -f /etc/modules ]; then
  /bin/busybox depmod /lib/modules
  /bin/busybox cat /etc/modules | while read line; do
    if [ -n "\${line}" ]; then
      /bin/busybox modprobe \${line/.ko/}
    fi
  done
fi
# Sleep for devices to connect, like USB
/bin/busybox sleep 5
/bin/busybox mdev -s
# Function for parsing command line options with
# '=' in them. Ex. get_opt("init=/sbin/init")
# would return "/sbin/init"
get_opt() {
  echo "\$@" | /bin/busybox cut -d "=" -f 2
}
# Defaults
init="/sbin/init"
root="${initramfs_use_rootfs}"
runlevel="3"
for i in \$(cat /proc/cmdline); do
  case \${i} in
    root\=*)
     root=\$(get_opt \${i})
     ;;
    init\=*)
     init=\$(get_opt \${i})
     ;;
    runlevel\=*)
     runlevel=\$(get_opt \${i})
     ;;
  esac
done
# Mount the root device
mount -o ro "\${root}" /root
# Below is an example of how to use the initramfs
# to load clfs or lfs from a disk image
#if [ -f /root/lfs-root.img ]; then
#  BNROOT=\$(/bin/busybox basename "\${root}")
#  umount root
#  mkdir -p /mnt/"\${BNROOT}" &&
#  mount "\${root}" /mnt/"\${BNROOT}" &&
#  mount -o loop /mnt/"\${BNROOT}"/lfs-root.img /root &&
#  mkdir -p /root/mnt/"\${BNROOT}" &&
#  mount /mnt/"\${BNROOT}" /root/mnt/"\${BNROOT}"
#  unset BNROOT
#fi
if [ -x "/root/\${init}" ]; then
  # Unmount all other mounts so that the ram used by
  # the initramfs can be cleared after switch_root
  umount /sys /proc
  # Switch to the new root and execute the init at
  # the specified runlevel
  exec switch_root /root "\${init}" "\${runlevel}"
fi
echo "Failed 'exec switch_root /root \\"\${init}\\" \\"\${runlevel}\\"'"
exec sh
EOT
chmod 755 /boot/initramfs-tree/init
( cd /boot/initramfs-tree
  find . | cpio -H newc -o | gzip -9c > ${initramfs_output}
) || exit 1
EOF
chmod 755 /usr/sbin/mkinitramfs