clfs

clfs.git
git clone git://git.lenczewski.org/clfs.git
Log | Files | Refs | README

stage4.sh (2713B)


      1 #!/bin/sh
      2 
      3 . "$(dirname $0)/clfs-env.sh"
      4 
      5 echo "Building $TARGET initramfs and kernel..."
      6 
      7 # build initramfs
      8 echo "Building $TARGET initramfs..."
      9 
     10 mkdir -v \
     11 	$INITRAMFS/bin \
     12 	$INITRAMFS/dev \
     13 	$INITRAMFS/etc \
     14 	$INITRAMFS/mnt \
     15 	$INITRAMFS/proc \
     16 	$INITRAMFS/root \
     17 	$INITRAMFS/run \
     18 	$INITRAMFS/sbin \
     19 	$INITRAMFS/sys \
     20 	$INITRAMFS/usr
     21 
     22 mkdir -v \
     23 	$INITRAMFS/mnt/root
     24 
     25 mkdir -v \
     26 	$INITRAMFS/usr/bin \
     27 	$INITRAMFS/usr/sbin
     28 
     29 # TODO: does `mount -t devtmpfs none /dev` remove the need for this? if not,
     30 # should we build a custom cpio archive using the kernel gen_init_cpio tool?
     31 # cp -a /dev/null /dev/console /dev/tty $INITRAMFS/dev
     32 
     33 # build static toybox for initramfs
     34 echo "Building static toybox for initramfs..."
     35 cp -r $SOURCES/$TOYBOX_DIR $WORK/$TOYBOX_DIR
     36 cd $WORK/$TOYBOX_DIR
     37 
     38 make distclean
     39 make defconfig
     40 make LDFLAGS="--static" PREFIX=$INITRAMFS install -j$(nproc)
     41 
     42 cat > $INITRAMFS/init <<'EOF'
     43 #!/bin/toybox sh
     44 
     45 rescue_shell() {
     46 	echo "$1 : dropping to a rescue shell."
     47 	exec /bin/toybox sh
     48 }
     49 
     50 mount -t devtmpfs none /dev || rescue_shell "mounting /dev failed"
     51 mount -t proc none /proc || rescue_shell "mounting /proc failed"
     52 mount -t sysfs none /sys || rescue_shell "mounting /sys failed"
     53 mount -t tmpfs none /run || rescue_shell "mounting /run failed"
     54 
     55 # TODO: parse out kernel commandline
     56 init="/sbin/init"
     57 root=""
     58 mount_ro_rw="ro"
     59 
     60 for p in $(read -r < /proc/cmdline); do
     61 	case $p in
     62 		init=*) init="${param#init=} ;;
     63 		root=*) root="${param#root=} ;;
     64 		ro) mount_ro_rw="ro" ;;
     65 		rw) mount_ro_rw="rw" ;;
     66 	esac
     67 done
     68 
     69 [ "${root:-z}" = "z" ] && rescue_shell "no root specified, cannot mount"
     70 
     71 mount -o "$mount_ro_rw" "$root" /mnt/root || rescue_shell "mounting rootfs failed"
     72 
     73 umount /dev /proc /sys /run
     74 
     75 exec switch_root /mnt/root "$init" || \
     76 	rescue_shell "failed to switch_root to /mnt/root with init=$init"
     77 EOF
     78 
     79 chmod +x $INITRAMFS/init
     80 
     81 # build cpio archive
     82 echo "Building $TARGET initrd cpio archive..."
     83 ( cd $INITRAMFS ; find . | cpio -o -H newc --quiet > $BOOTROOT/$INITRD )
     84 
     85 # build linux-kernel
     86 echo "Building $TARGET $LINUX_DIR kernel..."
     87 if [ -f "$TARGETS/$TARGET-kconfig" ]; then
     88 	cp -r $SOURCES/$LINUX_DIR $WORK/$LINUX_DIR
     89 	cd $WORK/$LINUX_DIR
     90 
     91 	cp "$TARGETS/$TARGET-kconfig" .config
     92 
     93 	make HOSTCC=$HOSTCC ARCH=$LINUX_ARCH mrproper
     94 	make HOSTCC=$HOSTCC ARCH=$LINUX_ARCH CROSS_COMPILE=$TARGET-
     95 	make HOSTCC=$HOSTCC ARCH=$LINUX_ARCH CROSS_COMPILE=$TARGET- \
     96 		INSTALL_MOD_PATH=$SYSROOT modules_install
     97 
     98 	cp -v arch/$LINUX_ARCH/boot/bzImage $BOOTROOT/$KERNEL
     99 	[ -d arch/$LINUX_ARCH/boot/dts ] && \
    100 		cp -rv arch/$LINUX_ARCH/boot/dts $BOOTROOT/
    101 else
    102 	echo "No kconfig given ($TARGETS/$TARGET-kconfig missing), skipping building kernel."
    103 fi
    104 
    105 echo "Built $TARGET initramfs and kernel!"