Site Tools


initramfs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
initramfs [2013/06/04 10:07] paulsmithinitramfs [2013/06/04 10:13] (current) paulsmith
Line 1: Line 1:
 +====== initramfs ======
  
 +
 +===== 1. Create structure =====
 +<code>
 +mkdir /usr/src/initramfs
 +cd /usr/src/initramfs
 +mkdir -p bin lib dev etc mnt/root proc root sbin sys
 +</code>
 +
 +===== 2. Build busybox =====
 +<code>
 +USE="static" emerge --root=/usr/src/initramfs/ -v busybox
 +</code>
 +
 +===== 3. Copy minimal device nodes =====
 +<code>
 +cp -a /dev/{null,console,tty,sd??} /usr/src/initramfs/dev/
 +</code>
 +
 +===== 4. Create /usr/src/initramfs/init =====
 +<code>
 +#!/bin/busybox sh
 +
 +# Mount the /proc and /sys filesystems.
 +mount -t proc none /proc
 +mount -t sysfs none /sys
 +
 +#setup mdev 
 +echo /sbin/mdev > /proc/sys/kernel/hotplug
 +/sbin/mdev -s
 +
 +# Do your stuff here.
 +echo "This script mounts rootfs and boots it up, nothing more!"
 +
 +rescue_shell() {
 +    echo "Something went wrong. Dropping you to a shell."
 +    busybox --install -s
 +    exec /bin/sh
 +}
 +
 +uuidlabel_root() {
 +    for cmd in $(cat /proc/cmdline) ; do
 +        case $cmd in
 +        root=*)
 +            dev=${cmd#root=}
 +            type=${dev%%=*}
 +            if [ $type = "LABEL" ] || [ $type = "UUID" ] ; then
 +                mount -o ro $(findfs "$dev") /mnt/root
 +            else
 +                mount -o ro ${dev} /mnt/root
 +            fi
 +            ;;
 +        esac
 +    done
 +}
 +
 +# Mount the root filesystem.
 +uuidlabel_root || rescue_shell
 +
 +# Clean up.
 +umount /proc
 +umount /sys
 +
 +# Boot the real thing.
 +exec switch_root /mnt/root /sbin/init
 +</code>
 +
 +<code>
 +chmod +x /usr/src/initramfs/init
 +</code>
 +
 +
 +===== 5. Installing options =====
 +====== 5.1. Built into kernel (my prefered way) ======
 +<code>
 +cd /usr/src/linux
 +make menuconfig
 +General setup  ---> 
 +  [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support 
 +  (/usr/src/initramfs) Initramfs source file(s) 
 +
 +
 +make -j4 && make modules_install
 +</code>
 +
 +====== 5.2. Build a initrd for grub ======
 +cd /usr/src/initramfs/
 +find . -print0 | cpio --null -ov --format=newc | gzip -9 > /boot/initramfs.cpio.gz
 +
 +edit grub < 1.0(add the initrd line):
 +<code>
 +  title Gentoo
 +  root (hd0,0)
 +  kernel /kernel64
 +  initrd /initramfs.cpio.gz
 +</code>
 +
 +edit grub > 2
 +<code>
 +menuentry 'Gentoo 64 Linux (UUID)' {
 +        root=hd0,1
 +        linux /boot/kernel64 root=UUID=d2e3c17f-75db-4c95-87ce-760c8e41e2ca
 +        initrd  /boot/initramfs.cpio.gz
 +}
 +
 +</code>