Created
May 5, 2015 14:33
-
-
Save therealromster/70b3225fbceed7c4e454 to your computer and use it in GitHub Desktop.
initramfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Create the folder structure | |
mkdir -p work/initramfs/{bin,sbin,etc,proc,sys,newroot} | |
cd work | |
touch initramfs/etc/mdev.conf | |
cp /bin/busybox initramfs/bin/busybox | |
chmod +x initramfs/bin/busybox | |
ln -s busybox initramfs/bin/sh | |
# lvm | |
cp /usr/sbin/lvm.static initramfs/bin/lvm | |
cat << "__EOF__" > initramfs/init | |
#!/bin/sh | |
#Mount things needed by this script | |
mount -t proc proc /proc | |
mount -t sysfs sysfs /sys | |
#Disable kernel messages from popping onto the screen | |
echo 0 > /proc/sys/kernel/printk | |
#Clear the screen | |
clear | |
#Create all the symlinks to /bin/busybox | |
busybox --install -s | |
#Create device nodes | |
mknod /dev/null c 1 3 | |
mknod /dev/tty c 5 0 | |
mdev -s | |
# lvm | |
lvm vgscan --mknodes | |
lvm lvchange -aly vg/root | |
#Function for parsing command line options with "=" in them | |
# get_opt("init=/sbin/init") will return "/sbin/init" | |
get_opt() { | |
echo "$@" | cut -d "=" -f 2 | |
} | |
#Defaults | |
init="/sbin/init" | |
root="/dev/vg/root" | |
#Process command line options | |
for i in $(cat /proc/cmdline); do | |
case $i in | |
root\=*) | |
root=$(get_opt $i) | |
;; | |
init\=*) | |
init=$(get_opt $i) | |
;; | |
esac | |
done | |
#Mount the root device | |
mount "${root}" /newroot | |
#Check if $init exists and is executable | |
if [[ -x "/newroot/${init}" ]] ; then | |
#Unmount all other mounts so that the ram used by | |
#the initramfs can be cleared after switch_root | |
umount /sys /proc | |
#Switch to the new root and execute init | |
exec switch_root /newroot "${init}" | |
fi | |
#This will only be run if the exec above failed | |
echo "Failed to switch_root, dropping to a shell" | |
exec sh | |
__EOF__ | |
chmod +x initramfs/init | |
cd initramfs | |
find . | cpio -H newc -o > ../initramfs.cpio | |
cd - | |
cat initramfs.cpio | gzip > initramfs.igz | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment