Linux/initrd

From Omnia
Jump to navigation Jump to search

initrd

Extract initrd image:

cat initrd.img | gzip -d | cpio -dmvi

gunzip < /boot/initrd.img | cpio -i --make-directories

gzip -dc /boot/initrd-2.6.16-026test014.4-smp.cpio | cpio -id

gunzip initrd.cpio.gz
cpio -iI initrd.cpio

Build initd image:

find ./ | cpio -H newc -o | gzip > ../initrd.img
find ./ | cpio -H newc -o > initrd.cpio
gzip initrd.cpio
mv initrd.cpio.gz initrd.img

Build within chroot:

mount /dev/sda1 /mnt
chroot /mnt
/sbin/mkinitrd /boot/initrd.img 3.10.0  # /lib/modules

--

Extract initramfs: [1]

If the output of file -L /initrd.img is ASCII cpio archive (SVR4 with no CRC), consider the initrd image to have microcode prepended.

extract the microcode by and take node of the blocks in the STDERR display (in my case 48).

 cd path/to/initrd/; cpio -i </initrd.img 

extract the basic initrd by (adjust skip values matching the cpio STDERR output).

cd path/to/initrad/; dd if=/initrd.img of=initrd.img bs=512 skip=48; zcat initrd.img |cpio -i

Otherwise, extract the initrd directly by

cd path/to/initrad/; zcat /initrd.img |cpio -i


References:

Sample initrd

CentOS 5.7

Tree structure:

|-- bin
|   |-- dmraid
|   |-- insmod
|   |-- kpartx
|   |-- modprobe -> /sbin/nash
|   `-- nash
|-- dev
|   |-- console
|   |-- mapper
|   |-- null
|   |-- ptmx
|   |-- ram -> ram1
|   |-- ram0
|   |-- ram1
|   |-- rtc
|   |-- systty
|   |-- tty
|   |-- tty0
|   |-- tty1
|   |-- tty10
|   |-- tty11
|   |-- tty12
|   |-- tty2
|   |-- tty3
|   |-- tty4
|   |-- tty5
|   |-- tty6
|   |-- tty7
|   |-- tty8
|   |-- tty9
|   |-- ttyS0
|   |-- ttyS1
|   |-- ttyS2
|   |-- ttyS3
|   `-- zero
|-- etc
|-- init
|-- lib
|   |-- ata_piix.ko
|   |-- dm-log.ko
|   |-- dm-mem-cache.ko
|   |-- dm-message.ko
|   |-- dm-mod.ko
|   |-- dm-raid45.ko
|   |-- dm-region_hash.ko
|   |-- ehci-hcd.ko
|   |-- ext3.ko
|   |-- firmware
|   |-- jbd.ko
|   |-- libata.ko
|   |-- mptbase.ko
|   |-- mptscsih.ko
|   |-- mptspi.ko
|   |-- ohci-hcd.ko
|   |-- scsi_mod.ko
|   |-- scsi_transport_spi.ko
|   |-- sd_mod.ko
|   `-- uhci-hcd.ko
|-- proc
|-- sbin -> bin
|-- sys
`-- sysroot

init:

#!/bin/nash

mount -t proc /proc /proc
setquiet
echo Mounting proc filesystem
echo Mounting sysfs filesystem
mount -t sysfs /sys /sys
echo Creating /dev
mount -o mode=0755 -t tmpfs /dev /dev
mkdir /dev/pts
mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
mkdir /dev/shm
mkdir /dev/mapper
echo Creating initial device nodes
mknod /dev/null c 1 3
mknod /dev/zero c 1 5
mknod /dev/urandom c 1 9
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
echo Setting up hotplug.
hotplug
echo Creating block device nodes.
mkblkdevs
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
echo "Loading jbd.ko module"
insmod /lib/jbd.ko
echo "Loading ext3.ko module"
insmod /lib/ext3.ko
echo "Loading scsi_mod.ko module"
insmod /lib/scsi_mod.ko
echo "Loading sd_mod.ko module"
insmod /lib/sd_mod.ko
echo "Loading scsi_transport_spi.ko module"
insmod /lib/scsi_transport_spi.ko
echo "Loading mptbase.ko module"
insmod /lib/mptbase.ko
echo "Loading mptscsih.ko module"
insmod /lib/mptscsih.ko
echo "Loading mptspi.ko module"
insmod /lib/mptspi.ko
echo "Loading libata.ko module"
insmod /lib/libata.ko
echo "Loading ata_piix.ko module"
insmod /lib/ata_piix.ko
echo "Loading dm-mem-cache.ko module"
insmod /lib/dm-mem-cache.ko
echo "Loading dm-mod.ko module"
insmod /lib/dm-mod.ko
echo "Loading dm-log.ko module"
insmod /lib/dm-log.ko
echo "Loading dm-region_hash.ko module"
insmod /lib/dm-region_hash.ko
echo "Loading dm-message.ko module"
insmod /lib/dm-message.ko
echo "Loading dm-raid45.ko module"
insmod /lib/dm-raid45.ko
echo Waiting for driver initialization.
stabilized --hash --interval 1000 /proc/scsi/scsi
mkblkdevs
echo Scanning and configuring dmraid supported devices
resume LABEL=SWAP-sda2
echo Creating root device.
mkrootdev -t ext3 -o defaults,ro /dev/sda3
echo Mounting root filesystem.
mount /sysroot
echo Setting up other filesystems.
setuproot
echo Switching to new root and running init.
switchroot

CentOS 5.7 Boot from USB stick to NFS root

Tree structure:

|-- bin
|   |-- dmraid
|   |-- insmod
|   |-- kpartx
|   |-- modprobe -> /sbin/nash
|   `-- nash
|-- dev
|   |-- console
|   |-- mapper
|   |-- null
|   |-- ptmx
|   |-- ram -> ram1
|   |-- ram0
|   |-- ram1
|   |-- rtc
|   |-- systty
|   |-- tty
|   |-- tty0
|   |-- tty1
|   |-- tty10
|   |-- tty11
|   |-- tty12
|   |-- tty2
|   |-- tty3
|   |-- tty4
|   |-- tty5
|   |-- tty6
|   |-- tty7
|   |-- tty8
|   |-- tty9
|   |-- ttyS0
|   |-- ttyS1
|   |-- ttyS2
|   |-- ttyS3
|   `-- zero
|-- etc
|-- init
|-- lib
|   |-- ata_piix.ko
|   |-- dm-log.ko
|   |-- dm-mem-cache.ko
|   |-- dm-message.ko
|   |-- dm-mod.ko
|   |-- dm-raid45.ko
|   |-- dm-region_hash.ko
|   |-- e1000e.ko
|   |-- ehci-hcd.ko
|   |-- firmware
|   |-- libata.ko
|   |-- libsas.ko
|   |-- lockd.ko
|   |-- mvsas.ko
|   |-- nfs.ko
|   |-- nfs_acl.ko
|   |-- ohci-hcd.ko
|   |-- pata_marvell.ko
|   |-- scsi_mod.ko
|   |-- scsi_transport_sas.ko
|   |-- sd_mod.ko
|   |-- sunrpc.ko
|   |-- uhci-hcd.ko
|   `-- usb-storage.ko
|-- proc
|-- sbin -> bin
|-- sys
|-- sysroot
`-- var
    `-- lib
        `-- dhclient

initrd

#!/bin/nash

mount -t proc /proc /proc
setquiet
echo Mounting proc filesystem
echo Mounting sysfs filesystem
mount -t sysfs /sys /sys
echo Creating /dev
mount -o mode=0755 -t tmpfs /dev /dev
mkdir /dev/pts
mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
mkdir /dev/shm
mkdir /dev/mapper
echo Creating initial device nodes
mknod /dev/null c 1 3
mknod /dev/zero c 1 5
mknod /dev/urandom c 1 9
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
echo Setting up hotplug.
hotplug
echo Creating block device nodes.
mkblkdevs
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
echo "Loading sunrpc.ko module"
insmod /lib/sunrpc.ko
echo "Loading nfs_acl.ko module"
insmod /lib/nfs_acl.ko
echo "Loading lockd.ko module"
insmod /lib/lockd.ko
echo "Loading nfs.ko module"
insmod /lib/nfs.ko
echo "Loading e1000e.ko module"
insmod /lib/e1000e.ko
echo Bringing up eth2
netname 00:1B:21:94:6A:21 eth2
network --device eth2 --bootproto dhcp
rename /var/lib/dhclient/dhclient.leases /var/lib/dhclient/dhclient-eth2.leases
echo "Loading scsi_mod.ko module"
insmod /lib/scsi_mod.ko
echo "Loading sd_mod.ko module"
insmod /lib/sd_mod.ko
echo "Loading scsi_transport_sas.ko module"
insmod /lib/scsi_transport_sas.ko
echo "Loading libata.ko module"
insmod /lib/libata.ko
echo "Loading libsas.ko module"
insmod /lib/libsas.ko
echo "Loading mvsas.ko module"
insmod /lib/mvsas.ko
echo "Loading pata_marvell.ko module"
insmod /lib/pata_marvell.ko
echo "Loading ata_piix.ko module"
insmod /lib/ata_piix.ko
echo "Loading usb-storage.ko module"
insmod /lib/usb-storage.ko
echo Waiting for driver initialization.
stabilized /proc/bus/usb/devices
echo "Loading dm-mem-cache.ko module"
insmod /lib/dm-mem-cache.ko
echo "Loading dm-mod.ko module"
insmod /lib/dm-mod.ko
echo "Loading dm-log.ko module"
insmod /lib/dm-log.ko
echo "Loading dm-region_hash.ko module"
insmod /lib/dm-region_hash.ko
echo "Loading dm-message.ko module"
insmod /lib/dm-message.ko
echo "Loading dm-raid45.ko module"
insmod /lib/dm-raid45.ko
echo Waiting for driver initialization.
stabilized --hash --interval 1000 /proc/scsi/scsi
mkblkdevs
echo Scanning and configuring dmraid supported devices
echo Creating root device.
mkrootdev -t nfs -o rsize=8192,wsize=8192,timeo=14,intr 10.50.8.120:/home/esx/ken/kesx
echo Mounting root filesystem.
mount /sysroot
echo Setting up other filesystems.
setuproot
echo Copying DHCP lease for eth2
cp /var/lib/dhclient/dhclient-eth2.leases /sysroot/dev/.dhclient-eth2.leases
echo Switching to new root and running init.
switchroot

keywords