dedupe binaries

This commit is contained in:
2026-01-27 12:32:34 +00:00
parent 819d7cee89
commit a7c6e58b02

View File

@@ -1,7 +1,6 @@
{ {
lib, lib,
runCommand, runCommand,
symlinkJoin,
makeInitrdNG, makeInitrdNG,
uutils-coreutils-noprefix, uutils-coreutils-noprefix,
bash, bash,
@@ -17,8 +16,6 @@
writeScript, writeScript,
kmod, kmod,
linuxPackages_latest, linuxPackages_latest,
glibc,
gcc-unwrapped,
}: }:
let let
@@ -40,174 +37,58 @@ let
mount -t sysfs sys /sys mount -t sysfs sys /sys
mount -t devtmpfs dev /dev mount -t devtmpfs dev /dev
# Load essential kernel modules for hardware support
echo "Loading kernel modules..."
modprobe -a \
nvme sd_mod usb_storage ata_piix ahci \
ext4 vfat btrfs xfs \
e1000e igb r8169 virtio_net \
virtio_blk virtio_scsi \
>/dev/null 2>&1 || true
# Display welcome message
echo ""
echo "==============================="
echo " Rescue System"
echo "==============================="
echo ""
echo "Available utilities:"
echo " Shell: bash"
echo " Files: ls, cat, less, cp, mv, rm, mkdir (uutils-coreutils)"
echo " Disk: mount, fdisk, parted, mkfs.ext4, mkfs.vfat, blkid"
echo " Text: vim, grep, find, head, tail"
echo " System: ps, kill, chmod, chown"
echo ""
echo "Kernel modules included."
echo "Type 'exit' or Ctrl+D to reboot"
echo ""
# Drop to rescue shell # Drop to rescue shell
exec /bin/bash exec /bin/bash
''; '';
# Package binaries to include # Packages to include (all binaries from each package will be included)
packageBinaries = [ packages = [
# uutils-coreutils (core utilities) uutils-coreutils-noprefix
{ bash
pkg = uutils-coreutils-noprefix; util-linux
bins = [ e2fsprogs
"ls" dosfstools
"cat" parted
"cp" vim-full
"mv" findutils
"rm" gnugrep
"mkdir" procps
"rmdir" less
"chmod" kmod
"chown"
"ln"
"touch"
"head"
"tail"
"dd"
"echo"
"pwd"
"true"
"false"
];
}
# bash (shell)
{
pkg = bash;
bins = [
"bash"
"sh"
];
}
# util-linux (mount, disk utilities)
{
pkg = util-linux;
bins = [
"mount"
"umount"
"fdisk"
"blkid"
"mkswap"
"lsblk"
];
}
# e2fsprogs (ext filesystem tools)
{
pkg = e2fsprogs;
bins = [
"mkfs.ext4"
"e2fsck"
"resize2fs"
];
}
# dosfstools (FAT filesystem tools)
{
pkg = dosfstools;
bins = [
"mkfs.vfat"
"fsck.vfat"
];
}
# parted (partitioning tool)
{
pkg = parted;
bins = [ "parted" ];
}
# vim (text editor)
{
pkg = vim-full;
bins = [
"vim"
"vi"
];
}
# findutils (find)
{
pkg = findutils;
bins = [ "find" ];
}
# gnugrep (grep)
{
pkg = gnugrep;
bins = [ "grep" ];
}
# procps (process utilities)
{
pkg = procps;
bins = [
"ps"
"kill"
];
}
# less (pager)
{
pkg = less;
bins = [ "less" ];
}
# kmod (module loading)
{
pkg = kmod;
bins = [
"modprobe"
"lsmod"
];
}
]; ];
# Merge glibc and gcc libraries into one directory # Generate binary entries for makeInitrdNG by auto-discovering all binaries
mergedLibs = symlinkJoin { binaryEntries =
name = "merged-libs"; let
paths = [ allEntries = lib.flatten (
glibc
gcc-unwrapped.lib
];
};
# Generate binary entries for makeInitrdNG
binaryEntries = lib.flatten (
map ( map (
entry: pkg:
let
binDir = "${pkg}/bin";
# Get all files in the bin directory
binFiles = if builtins.pathExists binDir then builtins.attrNames (builtins.readDir binDir) else [ ];
in
map (bin: { map (bin: {
source = "${entry.pkg}/bin/${bin}"; source = "${binDir}/${bin}";
target = "/bin/${bin}"; target = "/bin/${bin}";
}) entry.bins }) binFiles
) packageBinaries ) packages
); );
# Deduplicate by target path, keeping first occurrence
deduped = lib.foldl' (
acc: entry: if builtins.any (e: e.target == entry.target) acc then acc else acc ++ [ entry ]
) [ ] allEntries;
in
deduped;
# Build the initrd
initrd = makeInitrdNG { initrd = makeInitrdNG {
name = "mrescue-initrd"; name = "mrescue-initrd";
compressor = "zstd"; compressor = "zstd";
compressorArgs = [ compressorArgs = [
#"-19" #"-19"
"-9" "-12"
"-T0" "-T0"
]; # Maximum compression, all threads ];
contents = [ contents = [
# Init script # Init script
@@ -215,23 +96,13 @@ let
source = init; source = init;
target = "/init"; target = "/init";
} }
# Merged C libraries (glibc + gcc) # Kernel modules (not ELF binaries, must be added manually)
{
source = "${mergedLibs}/lib";
target = "/lib";
}
# Dynamic linker (also at /lib64 for compatibility)
{
source = "${mergedLibs}/lib/ld-linux-x86-64.so.2";
target = "/lib64/ld-linux-x86-64.so.2";
}
# Kernel modules
{ {
source = "${linuxPackages_latest.kernel.modules}/lib/modules"; source = "${linuxPackages_latest.kernel.modules}/lib/modules";
target = "/lib/modules"; target = "/lib/modules";
} }
] ]
++ binaryEntries; ++ binaryEntries; # makeInitrdNG will auto-resolve dependencies for these
}; };
in in