Products‎ > ‎HOWTOs and other docs‎ > ‎

How to create initrd.gz to boot OpenWRT-x86 from USB flash

# Copiryght: Djarvur group, 2009, info@djarvir.net
# License: FreeBSD
#
# This is not a real howto but log of the commands have to be issued
#
# The document is in form allowing to copy-and-paste it to terminal window
#
# Prequirements:
# OpenWRT-x86 root filesystem has to be mounted or even copied/unpacked to /tmp/rootfs
#

rm -rf /tmp/initrd
mkdir -p /tmp/initrd

copy_link_with_base()
  {
  test -L "$1/$2" &&
    {
    local basefile="`readlink "$1/$2"`"
    local filedir="`dirname "$2"`"
    local dstdir="`dirname  "$3/$filedir/$basefile"`"
    mkdir -pv "$dstdir"
    test -e "$3/$filedir/$basefile" ||
    cp -av "$1/$filedir/$basefile" "$3/$filedir/$basefile"
    }
  local dstdir="`dirname  "$3/$2"`"
  mkdir -pv "$dstdir"
  cp -av "$1/$2" "$3/$2"
  }

copy_loop()
  {
  while read fileName
    do
    copy_link_with_base "$1" "$fileName" "$2"
    done
  }

copy_loop ${mntDir}/rootfs /tmp/initrd <<EOF
/bin/ash
/bin/mount
/bin/umount
/bin/grep
/sbin/udevd
/sbin/udevtrigger
/sbin/udevsettle
/lib/libcrypt.so.0
/lib/libm.so.0
/lib/libc.so.0
/lib/ld-uClibc.so.0
/lib/libgcc_s.so.1
/lib/modules/2.6.25.20/usbcore.ko
/lib/modules/2.6.25.20/usb-storage.ko
/lib/modules/2.6.25.20/uhci-hcd.ko
/lib/modules/2.6.25.20/ehci-hcd.ko
/lib/modules/2.6.25.20/ohci-hcd.ko
/lib/modules/2.6.25.20/scsi_mod.ko
/lib/modules/2.6.25.20/sd_mod.ko
/sbin/insmod
/bin/sleep
/usr/bin/killall
/bin/cat
/sbin/switch_root
/usr/sbin/e2fsck
/usr/lib/libext2fs.so.2
/usr/lib/libcom_err.so.2
/usr/lib/libblkid.so.1
/usr/lib/libuuid.so.1
/usr/lib/libe2p.so.2
EOF

xargs -I% mkdir -vp /tmp/initrd/% <<EOF
/proc/
/sys/
/dev/
/real_root/
EOF

# Let's create init script. File itself is attached for reference
#
cat <<EOF > /tmp/initrd/init

#!/bin/ash -u

PATH="/bin:/sbin:/usr/bin:/usr/sbin"

mount proc  /proc -t proc
mount sysfs /sys  -t sysfs
mount udev  /dev  -t tmpfs -o mode=0755

udevd --daemon
udevtrigger
udevsettle

for module in usbcore scsi_mod sd_mod usb-storage uhci-hcd ohci-hcd ehci-hcd
  do insmod \$module; done

root=''
init='/init'

for param in \`cat /proc/cmdline\` none
  do
  echo "\$param" | grep -q '=' || continue
  eval "\$param"
  done

do_fsck()
  {
  e2fsck -p "\$1" ||
  e2fsck "\$1"
  }

umount_all()
  {
  killall -KILL udevd &&
  umount -f /dev &&
  umount -f /sys &&
  umount -f /proc
  }

test -n "\$root" &&
  {
  echo -n "Looking for the root fs device '\$root'"
  while ! test -b "\$root"; do echo -n '.'; /bin/sleep 1s; done
  echo 'found'

  do_fsck "\$root" &&
  mount "\$root" /real_root &&
  umount_all &&
  exec switch_root /real_root "\$init"
  } ||
  exec /bin/ash --login
EOF
#
chmod +x /tmp/initrd/init
#

pushd /tmp/initrd &&
find . -print|cpio -ov -H newc|gzip -9 -c > /tmp/initrd.gz &&
popd
 
# /tmp/initrd.gz is ready for deployment
Č
ċ
init
(1k)
Daniel Podolsky,
Nov 20, 2009, 4:27 AM
Comments