HOWTO Custom Initramfs From Scratch
From Gentoo Linux Wiki
This article will show you how to construct a custom initramfs from the ground up, to do whatever you want. This article will focus on creating an initramfs for lvm2 root.
Contents |
[edit] Working Directories
First, we set up some working directories:
| Code: Set up working directories |
cd /usr/src
mkdir -p initramfs/{usr/portage,image/{dev,proc,newroot}}
|
[edit] Chroot
Then, we download and unpack a stage3 to initramfs-chroot:
| Code: Create minimal gentoo in chroot |
cd initramfs wget http://your-favourite-mirror/pub/gentoo/releases/x86/current/stage3-i686-2007.0.tar.bz2 tar xjvpf stage3-i686-2007.0.tar.bz2 |
We pull in some things from our host to keep things easy:
| Code: Link stuff from host to save space and time |
mount -o bind /dev dev/ mount -o bind /dev/pts dev/pts mount -t sysfs sysfs-chroot sys mount -t proc proc-chroot proc mount -o bind /usr/portage usr/portage cp /etc/resolv.conf etc/ |
Then we chroot:
| Code: Chroot |
chroot . bash env-update source /etc/profile |
[edit] Making Uclibc
We make ourselves a minimal c library, in this case uclibc:
| Code: Emerge uclibc |
emerge -avt uclibc |
[edit] Static busybox and LVM2
We make ourselves a static busybox and lvm2:
| Code: Emerge static busybox and lvm2 |
USE="static" ROOT="/image" emerge -avt busybox lvm2 |
[edit] Finishing the Image
We remove all the extraneous cruft that got installed, and pull in a few things that were missing:
| Code: Cleanup |
cd /image
rm -r usr/{doc,share,man,info}
cp -av /dev/{console,null} dev/
|
[edit] Boot Script
We write a script that will set up our lvm and then hop into the new root:
| Code: Boot Script |
cat <<END > /image/linuxrc
#!/bin/ash
fail() {
echo
echo "hrm, something is broke, have a shell"
exec ash
}
echo -n Mounting /proc...
mount -t proc proc /proc || fail
echo " OK"
echo -n Mounting tmpfs over /dev...
mount -t tmpfs dev-tmpfs /dev
echo " OK"
echo -n Starting Mdev...
mdev -s
echo " OK"
echo -n Starting LVM2
vgchange -ay || fail
echo " OK"
echo -n Creating Nodes
vgmknodes || fail
echo " OK"
echo -n Mounting rootfs...
mount -t YOUR-ROOTFS-TYPE -o ro,OTHER-OPTIONS /dev/YOUR-VG/YOUR-ROOT-LV /newroot
echo " OK"
echo Umounting intermediaries...
umount /proc
umount /dev
echo " OK"
echo Switching root, hold on!
switch_root /newroot /sbin/init
END
|
[edit] Kernel Setup
We set our kernel up to use the new image:
| Linux Kernel Configuration: Initramfs Options |
General setup ---> [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support (/usr/src/initramfs/image) Initramfs source file(s) |
Now make and install your kernel as usual:
| Code: Install new kernel |
cd /usr/src/linux make O=../linux-build bzImage modules modules_install && \ mount /boot && \ cp ../linux-build/arch/i386/boot/bzImage /boot/kernel-$(( head -n5 Makefile | sed 's/ //g'; echo 'echo $VERSION.$PATCHLEVEL.$SUBLEVEL$EXTRAVERSION'; ) | bash) && \ mv /boot/new /boot/current && \ ln -s kernel-$(( head -n5 Makefile | sed 's/ //g'; echo 'echo $VERSION.$PATCHLEVEL.$SUBLEVEL$EXTRAVERSION'; ) | bash) /boot/new && \ umount /boot |
[edit] Final Step
Reboot and see how you went!
[edit] See Also
Please format this article according to the guidelines and Wikification suggestions, then remove this notice {{Wikify}} from the article
