@@ -26,6 +26,7 @@ DEPEND="
2626 coreos-base/coreos-init:=
2727 sys-apps/azure-vm-utils[dracut]
2828 sys-apps/baselayout
29+ sys-apps/busybox
2930 sys-apps/coreutils
3031 sys-apps/findutils
3132 sys-apps/grep
@@ -72,6 +73,16 @@ src_prepare() {
7273 use amd64 && config_update " CONFIG_EXTRA_FIRMWARE_DIR=\" ${fw_dir} \" "
7374}
7475
76+ copy_in () {
77+ # Simple setup, assume we only have /lib64 to care about
78+ cp " ${ESYSROOT} " /usr/" $1 " ./" $1 "
79+ for LIBFILE in $( patchelf --print-needed ./" $1 " ) ; do
80+ if [ ! -e ./lib64/" ${LIBFILE} " ]; then
81+ copy_in /lib64/" ${LIBFILE} "
82+ fi
83+ done
84+ }
85+
7586src_compile () {
7687 local BE_ARGS=()
7788
@@ -89,6 +100,66 @@ src_compile() {
89100
90101 tc-export PKG_CONFIG
91102 " ${ESYSROOT} " /usr/bin/update-bootengine -k " ${KV_FULL} " -o " ${S} " /build/bootengine.cpio " ${BE_ARGS[@]} " || die
103+ # Copy full initrd over to /usr as filesystem image
104+ mkdir " ${S} " /build/bootengine || die
105+ pushd " ${S} " /build/bootengine || die
106+ sudo lsinitrd --unpack " ${S} " /build/bootengine.cpio || die
107+ sudo mksquashfs . " ${S} " /build/bootengine.img -noappend -xattrs-exclude ^btrfs. || die
108+ popd || die
109+ # Create minimal initrd
110+ microcode=$( cpio -t < " ${S} " /build/bootengine.cpio 2>&1 > /dev/null | cut -d " " -f 1)
111+ # Only keep early cpio for microcode
112+ truncate -s $(( microcode* 512 )) " ${S} " /build/bootengine.cpio || die
113+ # Debug: List contents after truncation
114+ cpio -t < " ${S} " /build/bootengine.cpio
115+ mkdir " ${S} " /build/minimal || die
116+ pushd " ${S} " /build/minimal || die
117+ mkdir -p {etc,bin,sbin,dev,proc,sys,dev,lib,lib64,usr/bin,usr/sbin,usr/lib,usr/lib64,realinit,sysusr/usr}
118+ mkdir -p lib/modules/" ${KV_FULL} " /
119+ cp " ${ESYSROOT} " /usr/lib/modules/" ${KV_FULL} " /modules.* lib/modules/" ${KV_FULL} " /
120+ mkdir -p lib/modprobe.d/
121+ cp " ${ROOTFS} " /lib/modprobe.d/* lib/modprobe.d/
122+ # TODO: the hard part: what to include (networking is out of scope)
123+ MODULES=(" fs/overlayfs" " fs/squashfs" " drivers/block/loop.ko.xz" " fs/btrfs" " drivers/nvme" " drivers/scsi" " drivers/ata" " drivers/block" " drivers/char/virtio_console.ko.xz" " drivers/hv" " drivers/input/serio" " drivers/mmc" " drivers/usb" " drivers/hid" )
124+ for MODULE in " ${MODULES[@]} " ; do
125+ if [ -f " ${ESYSROOT} " /usr/lib/modules/" ${KV_FULL} " /kernel/" ${MODULE} " ]; then
126+ MODULE_DIR=$( dirname " ${MODULE} " )
127+ mkdir -p lib/modules/" ${KV_FULL} " /kernel/" ${MODULE_DIR} "
128+ cp " ${ESYSROOT} " /usr/lib/modules/" ${KV_FULL} " /kernel/" ${MODULE} " lib/modules/" ${KV_FULL} " /kernel/" ${MODULE} "
129+ elif [ -d " ${ESYSROOT} " /usr/lib/modules/" ${KV_FULL} " /kernel/" ${MODULE} " ]; then
130+ mkdir -p lib/modules/" ${KV_FULL} " /kernel/" ${MODULE} "
131+ cp -r " ${ESYSROOT} " /usr/lib/modules/" ${KV_FULL} " /kernel/" ${MODULE} " /* lib/modules/" ${KV_FULL} " /kernel/" ${MODULE} " /
132+ else
133+ die " wrong module type/not found: ${ESYSROOT} " /usr/lib/modules/" ${KV_FULL} " /kernel/" ${MODULE} "
134+ fi
135+ done
136+ for MODULE in $( find ./lib/modules/" ${KV_FULL} " / -type f) ; do
137+ MODULE=$( basename " ${MODULE} " | sed " s/\.ko\.xz$//" )
138+ for DEP in $( modprobe -S " ${KV_FULL} " -d " ${ESYSROOT} " -D " ${MODULE} " | grep " ^insmod " | sed " s/^insmod //" ) ; do
139+ DEP=$( echo " ${DEP} " | sed " s,${ESYSROOT} ,/," )
140+ DEP_DIR=$( dirname " ${DEP} " )
141+ mkdir -p ./" ${DEP_DIR} "
142+ cp " ${ESYSROOT} " /" ${DEP} " ./" ${DEP} "
143+ done
144+ done
145+ echo ' $MODALIAS=.* 0:0 660 @/sbin/modprobe "$MODALIAS"' > ./etc/mdev.conf
146+ copy_in /bin/veritysetup
147+ copy_in /bin/busybox
148+ # We can't use busybox's modprobe because it doesn't support the globs in module.alias, breaking module loading
149+ copy_in /sbin/kmod
150+ ln -s /sbin/kmod ./sbin/modprobe
151+ cp -a " ${ESYSROOT} " /usr/bin/minimal-init ./init
152+ # Make it easier to debug by not relying too much on the first commands
153+ ln -s /bin/busybox ./bin/sh
154+ mknod ./dev/console c 5 1
155+ mknod ./dev/null c 1 3
156+ mknod ./dev/tty c 5 0
157+ mknod ./dev/urandom c 1 9
158+ mknod ./dev/random c 1 8
159+ mknod ./dev/zero c 1 5
160+ # No compression because CONFIG_INITRAMFS_COMPRESSION_XZ should take care of it
161+ find . -print0 | cpio --null --create --verbose --format=newc >> " ${S} " /build/bootengine.cpio
162+ popd || die
92163 kmake " $( kernel_target) "
93164
94165 # sanity check :)
@@ -111,4 +182,7 @@ src_install() {
111182 # For easy access to vdso debug symbols in gdb:
112183 # set debug-file-directory /usr/lib/debug/usr/lib/modules/${KV_FULL}/vdso/
113184 kmake INSTALL_MOD_PATH=" ${ED} /usr/lib/debug/usr" vdso_install
185+
186+ insinto " /usr/lib/flatcar"
187+ doins build/bootengine.img
114188}
0 commit comments