Nethence Next Door Labs Laboratory Webmail Your IP BBDock
Next Door Laboratories print | donate | html/css | terms of use
Home | Unix | Windows | Oracle | Hardware | Mechanics | Scripts | Configs

Custom rescue/ghost initramfs



Introduction

Initramfs is just the newer way for Linux to build an initrd. It uses cpio instead of a simple ext2 filesystem image. Here's how to build an LVM2 capable initramfs. Two flavors are described :

- Busybox and Slackware init

- Busybox and Redhat init


Note. the Busybox preparation may be done as user but you'll have to be root to extract the device files from the cpio archive. So might be convenient to stay as root all along.


See http://pbraun.nethence.com/sysutils_linux/busybox.html on how to prepare a Busybox system.



Busybox and Slackware init

Note. you need to be root to extract the device files from the cpio archive


Make sure you've got XZ (http://tukaani.org/xz/) first. It's available on recent Slackware distros. It's also available as package with Redhat/CentOS,

rpm -q xz

Prerequesties,

wget ftp://ftp.slackware.at/slackware-13.1/isolinux/initrd.img
mv -f initrd.img initrd.dist
wget ftp://ftp.slackware.at/slackware-13.1/slackware/a/lvm2-2.02.64-i486-1.txz
xz -d lvm2-2.02.64-i486-1.txz

don't forget initrd's relative kernel image, it might help to build the PXE or memstick later on !

wget ftp://ftp.slackware.at/slackware-13.1/kernels/hugesmp.s/bzImage

Extract a sample initramfs,

rm -rf sample/
mkdir sample/
cd sample/
gzip -dc ../initrd.dist | cpio -id
cd ..

note. "-id" or "--extract --make-directories"


Prepare the rescue initramfs,

rm -rf tmp/
mkdir -p tmp/etc/ tmp/lib/ tmp/_keymap/
cp -a sample/dev/ tmp/
cp -a sample/lib/modules/ tmp/lib/
cp -a sample/etc/group tmp/etc/
cp -a sample/etc/passwd tmp/etc/
tar xzf sample/etc/keymaps.tar.gz -C tmp/_keymap/
sed 's/mknod --mode=600/mknod/' sample/dev/devmap_mknod.sh > tmp/dev/devmap_mknod.sh
chmod +x tmp/dev/devmap_mknod.sh

Eventually enable PCMCIA,

#mkdir -p tmp/etc/rc.d
#cp -a sample/etc/rc.d/rc.pcmcia tmp/etc/rc.d

Note. for rc.pcmcia to work you also need pcmciautils package


Enable the network connectivity,

cp -a sample/scripts/network.sh tmp/sbin/
chmod +x tmp/sbin/network.sh

Make it LVM2 capable,

rm -rf addons/
mkdir addons/
mkdir -p tmp/sbin/
tar xf lvm2-2.02.64-i486-1.tar -C addons/
cp addons/sbin/lvm.static tmp/sbin/lvm

Note. this isn't needed,

#wget ftp://ftp.slackware.at/slackware-12.2/slackware/a/device-mapper-1.02.28-i486-1.tgz
#tar xzf device-mapper-1.02.28-i486-1.tgz -C addons

Note. and there's no udev.static binary provided by Slackware.


Create the initialization script,

cd tmp/
cat > init <<EOF9
#!/bin/sh

/bin/rm -f /etc/mtab* /etc/nologin 1> /dev/null 2> /dev/null

mkdir -p /proc /sys
mount -t proc none /proc
mount -t sysfs none /sys

/dev/makedevs.sh
/dev/devmap_mknod.sh
lvm vgscan --mknodes --ignorelockingfailure
lvm vgchange -ay --ignorelockingfailure
#blkid (floppy error)

loadkmap < /_keymap/fr-latin1.bmap

echo
echo "run network.sh to enable network cards"
echo

exec /linuxrc
EOF9
chmod 700 init
cd ..

Note. change the keymap accordingly


Configure linuxrc,

cd tmp/
mkdir -p etc/init.d/
touch etc/init.d/rcS
chmod +x etc/init.d/rcS
cd ..

Tune your environment,

cd tmp/
cat > .profile <<EOF9
alias ll='ls -alkF'
EOF9
cd ..


Finish up

Generate the initramfs and clean up,

cd tmp/
find . | cpio -co | gzip -9 > ../initrd.img
cd ..
#mkdir -p /tftpboot/rescue32/
#cp bzImage /tftpboot/rescue32/
#cp initrd.img /tftpboot/rescue32/


Busybox and Redhat init

Add udev support and userspace LVM2 tools,

cp /sbin/udevd.static sbin/udevd
cp /sbin/lvm.static sbin/lvm
#cp /sbin/dmsetup.static sbin/dmsetup
#cp /sbin/kpartx.static sbin/kpartx

Note. not using dmsetup, "vgscan --mknodes" does the job

Note. not using "kpartx", udev does the job

Note. the "lvm" binary itself is enought, no need for "/etc/lvm/" conf & subdirs.


Add the missing modules,

tmp=`ls -1t /lib/modules/ | grep xen$ | head -1`
cp -f \
$tmp/kernel/fs/jbd/jbd.ko \
$tmp/kernel/fs/ext3/ext3.ko \
$tmp/kernel/drivers/scsi/scsi_mod.ko \
$tmp/kernel/drivers/scsi/sd_mod.ko \
$tmp/kernel/drivers/md/dm-mod.ko \
$tmp/kernel/drivers/md/dm-log.ko \
$tmp/kernel/drivers/md/dm-mem-cache.ko \
$tmp/kernel/drivers/md/dm-region_hash.ko \
$tmp/kernel/drivers/md/dm-message.ko \
lib
unset tmp
chmod 600 lib/*.ko

Note. to make a XEN guest capable initramfs, add,

#$tmp/kernel/drivers/xen/blkfront/xenblk.ko \

Create the "init" file,

cat > init <<EOF9
#!/bin/sh

mkdir -p /proc /sys /dev/pts /dev/shm /dev/mapper

mount -t proc none /proc
mount -t sysfs none /sys
mount -t devpts none /dev/pts

mknod /dev/null c 1 3
mknod /dev/zero c 1 5
mknod /dev/systty c 4 0
mknod /dev/tty c 5 0
# mknod /dev/console c 5 1
mknod /dev/ptmx c 5 2
mknod /dev/rtc c 10 135
mknod /dev/tty0 c 4 0
mknod /dev/tty1 c 4 1
mknod /dev/tty2 c 4 2
mknod /dev/tty3 c 4 3
mknod /dev/tty4 c 4 4
mknod /dev/tty5 c 4 5
mknod /dev/tty6 c 4 6
mknod /dev/tty7 c 4 7
mknod /dev/tty8 c 4 8
mknod /dev/tty9 c 4 9
mknod /dev/tty10 c 4 10
mknod /dev/tty11 c 4 11
mknod /dev/tty12 c 4 12
mknod /dev/ttyS0 c 4 64
mknod /dev/ttyS1 c 4 65
mknod /dev/ttyS2 c 4 66
mknod /dev/ttyS3 c 4 67

udevd -d

insmod /lib/xenblk.ko
insmod /lib/jbd.ko
insmod /lib/ext3.ko
insmod /lib/scsi_mod.ko
insmod /lib/sd_mod.ko
insmod /lib/dm-mod.ko
insmod /lib/dm-log.ko
insmod /lib/dm-mem-cache.ko
insmod /lib/dm-region_hash.ko
insmod /lib/dm-message.ko

lvm vgscan --mknodes --ignorelockingfailure
#lvm vgchange -ay --ignorelockingfailure
blkid

exec /linuxrc
EOF9
chmod 700 init

Note. "/dev/console" is created by the kernel.



Footnotes

We could also add KSH93 (http://www.research.att.com/~gsf/cgi-bin/download.cgi?action=list&name=ksh) into "bin/",

cp ../ksh.2009-05-05.linux.i386 bin/ksh

and install its dependencies (nonsegneg when applicable),

ldd ksh
cp /lib/libdl.so.2 lib
cp /lib/i686/nosegneg/libm.so.6 lib
cp /lib/i686/nosegneg/libc.so.6 lib
cp /lib/ld-linux.so.2 lib

Note. "linux-gate.so.1" is provided by the kernel.

Using the nonsegneg (when applicable) libraries should even prevent from disabling TLS,

#echo 'hwcap 0 nosegneg' >> etc/ld.so.conf


RHEL/XEN footnotes

Use the bare domU kernel,

cp `ls -1t /boot/vmlinuz-*xen | head -1` vmlinuz

note. the RHEL XEN kernel has both, dom0 and domU support.

and temporarly change the guest configuration,

...
kernel = "/data/guest1/vmlinuz"
ramdisk = "/data/guest1/initrd.img"
#bootloader = "/usr/bin/pygrub"
...

A simple way to add the "xenblk" module to the default initrd,

mkinitrd --fstab=/data/guest3/loop/etc/fstab \
--omit-lvm-modules --omit-raid-modules --omit-scsi-modules \
--with=xenblk --with=jbd --with=ext3 \
initrd.img 2.6.18-128.1.10.el5xen

We could also add USB support into the initramfs, adding this to its init,

echo "Loading ehci-hcd.ko module"
insmod /lib/ehci-hcd.ko
echo "Loading ohci-hcd.ko module"
insmod /lib/ohci-hcd.ko
echo "Loading uhci-hcd.ko module"
insmod /lib/uhci-hcd.ko

mount -t usbfs /proc/bus/usb /proc/bus/usb

Without udev, we would have to make the block device nodes. As for XEN tap:aio devices,

mknod /dev/xvda b 202 0
mknod /dev/xvdb b 202 16
mknod /dev/xvdc b 202 32
mknod /dev/xvdd b 202 48
mknod /dev/xvde b 202 64
mknod /dev/xvdf b 202 80
mknod /dev/xvdg b 202 96
mknod /dev/xvdh b 202 112

and make the partition devices nodes with "partprobe" or "kpartx",

kpartx -a /dev/xvda


References

Busybox : http://www.busybox.net/

Info on the 'in memory' filesystems : http://www.kernel.org/doc/Documentation/filesystems/ramfs-rootfs-initramfs.txt

Ash (Almquist Shell) Variants : http://www.in-ulm.de/~mascheck/various/ash/

ttylinux downloads : http://minimalinux.org/ttylinux/download.html

Buildroot: making Embedded Linux easy : http://buildroot.uclibc.org/

XEN + LVM : http://frlinux.net/?section=virtualisation&article=207

BusyBox simplifies embedded Linux systems : http://www.ibm.com/developerworks/library/l-busybox/

Coreutils - busybox : http://siso.sourceforge.net/kernel-busybox.html



Last update: Dec 24, 2011
Copyright © 2007-2013 Pierre-Philipp Braun