171 lines
4.4 KiB
Nix
171 lines
4.4 KiB
Nix
|
{
|
||
|
pkgs,
|
||
|
patosPkgs,
|
||
|
...
|
||
|
}:
|
||
|
pkgs.writeShellApplication {
|
||
|
name = "mkinitrd";
|
||
|
|
||
|
runtimeInputs = with pkgs; [
|
||
|
patchelf
|
||
|
cpio
|
||
|
gzip
|
||
|
];
|
||
|
|
||
|
text = ''
|
||
|
echo "Building initram disk"
|
||
|
mkdir -p root
|
||
|
pushd root
|
||
|
|
||
|
### create directories
|
||
|
mkdir -p etc dev proc sys tmp root
|
||
|
ln -sf usr/bin bin
|
||
|
ln -sf usr/bin sbin
|
||
|
ln -sf usr/lib lib
|
||
|
ln -sf usr/lib lib64
|
||
|
ln -sf ../proc/self/mounts etc/mtab
|
||
|
ln -sf ../usr/lib/systemd/systemd init
|
||
|
|
||
|
### install systemd
|
||
|
cp -Pr ${patosPkgs.systemd.out}/* ./
|
||
|
find . -type d -exec chmod 755 {} \;
|
||
|
rm -rf ./usr/include
|
||
|
rm -rf ./usr/sbin
|
||
|
# set default target to basic
|
||
|
mkdir usr/lib/systemd/system/basic.target.wants
|
||
|
ln -sf basic.target usr/lib/systemd/system/default.target
|
||
|
# remove first boot
|
||
|
rm -f usr/lib/systemd/system/sysinit.target.wants/systemd-firstboot.service
|
||
|
# remove vconsole setup
|
||
|
rm -f usr/lib/udev/rules.d/90-vconsole.rules
|
||
|
|
||
|
### install PatOS glibc
|
||
|
cp -Pr ${patosPkgs.glibc.out}/lib/*.so* ./usr/lib/
|
||
|
|
||
|
### install kernel modules
|
||
|
cp -Pr ${patosPkgs.kernel.kernel}/lib/modules ./usr/lib/
|
||
|
find usr/lib/modules -type d -exec chmod 755 {} \;
|
||
|
|
||
|
### install busybox
|
||
|
cp ${pkgs.busybox.out}/bin/busybox ./usr/bin/
|
||
|
usr/bin/busybox --list | xargs -I {} ln -sf busybox usr/bin/{}
|
||
|
|
||
|
### install dbus broker
|
||
|
cp -r ${patosPkgs.dbus-broker.out}/* ./
|
||
|
ln -sf ../dbus-broker.service usr/lib/systemd/system/basic.target.wants/dbus.service
|
||
|
ln -sf ../dbus.socket usr/lib/systemd/system/sockets.target.wants/dbus.socket
|
||
|
|
||
|
### install lib kmod
|
||
|
cp -P ${pkgs.kmod.lib}/lib/* ./usr/lib
|
||
|
cp -P ${pkgs.kmod.out}/bin/* ./usr/bin
|
||
|
|
||
|
### install libbpf
|
||
|
cp -P ${pkgs.libbpf.out}/lib/libbpf* ./usr/lib
|
||
|
|
||
|
### Find and install all shared libs
|
||
|
find . -type f -executable -exec ldd {} \; 2> /dev/null | awk '{print $3}' | grep -v systemd | grep -v glibc | sort -u | xargs cp -t usr/lib
|
||
|
find . -type f -executable -exec chmod 755 {} \;
|
||
|
|
||
|
|
||
|
### Create needed files
|
||
|
echo patos > ./etc/hostname
|
||
|
cat <<EOF > ./etc/os-release
|
||
|
NAME="PatOS"
|
||
|
PRETTY_NAME="PatOS Platform"
|
||
|
ID=patos
|
||
|
EOF
|
||
|
|
||
|
cat <<EOF > ./etc/passwd
|
||
|
root::0:0:root:/root:/bin/sh
|
||
|
bin:x:1:1:bin:/bin:/usr/bin/nologin
|
||
|
daemon:x:2:2:daemon:/:/usr/bin/nologin
|
||
|
mail:x:8:12:mail:/var/spool/mail:/usr/bin/nologin
|
||
|
ftp:x:14:11:ftp:/srv/ftp:/usr/bin/nologin
|
||
|
http:x:33:33:http:/srv/http:/usr/bin/nologin
|
||
|
uuidd:x:68:68:uuidd:/:/usr/bin/nologin
|
||
|
messagebus:x:81:81:messagebus:/:/usr/bin/nologin
|
||
|
nobody:x:99:99:nobody:/:/usr/bin/nologin
|
||
|
EOF
|
||
|
chmod 644 ./etc/passwd
|
||
|
|
||
|
cat <<EOF > ./etc/group
|
||
|
root:x:0:root
|
||
|
bin:x:1:root,bin,daemon
|
||
|
daemon:x:2:root,bin,daemon
|
||
|
sys:x:3:root,bin
|
||
|
adm:x:4:root,daemon
|
||
|
tty:x:5:
|
||
|
disk:x:6:root
|
||
|
lp:x:7:daemon
|
||
|
mem:x:8:
|
||
|
kmem:x:9:
|
||
|
wheel:x:10:root
|
||
|
ftp:x:11:
|
||
|
mail:x:12:
|
||
|
uucp:x:14:
|
||
|
log:x:19:root
|
||
|
utmp:x:20:
|
||
|
locate:x:21:
|
||
|
rfkill:x:24:
|
||
|
smmsp:x:25:
|
||
|
proc:x:26:
|
||
|
http:x:33:
|
||
|
games:x:50:
|
||
|
lock:x:54:
|
||
|
uuidd:x:68:
|
||
|
messagebus:x:81:
|
||
|
network:x:90:
|
||
|
video:x:91:
|
||
|
audio:x:92:
|
||
|
optical:x:93:
|
||
|
floppy:x:94:
|
||
|
storage:x:95:
|
||
|
scanner:x:96:
|
||
|
input:x:97:
|
||
|
power:x:98:
|
||
|
nobody:x:99:
|
||
|
EOF
|
||
|
chmod 644 ./etc/group
|
||
|
|
||
|
# FIXME: remove this later (just to get a shell in the initramfs)
|
||
|
cat <<EOF > usr/lib/systemd/system/demo.service
|
||
|
[Unit]
|
||
|
Description=Debug Shell (/bin/sulogin)
|
||
|
Conflicts=shutdown.target
|
||
|
Before=shutdown.target
|
||
|
|
||
|
[Service]
|
||
|
Environment=HOME=/root
|
||
|
WorkingDirectory=/root
|
||
|
ExecStart=/bin/sulogin
|
||
|
Type=idle
|
||
|
StandardInput=tty-force
|
||
|
StandardOutput=inherit
|
||
|
StandardError=inherit
|
||
|
KillMode=process
|
||
|
IgnoreSIGPIPE=no
|
||
|
SendSIGHUP=yes
|
||
|
Restart=always
|
||
|
|
||
|
[Install]
|
||
|
WantedBy=basic.target
|
||
|
EOF
|
||
|
ln -sf ../demo.service usr/lib/systemd/system/basic.target.wants/demo.service
|
||
|
|
||
|
# FIXME: ELF patching. Is there a better way?
|
||
|
find . -type f -executable -exec patchelf --set-rpath /lib:/usr/lib:/usr/lib/systemd {} \; 2> /dev/null
|
||
|
find . -type f -executable -exec patchelf --set-interpreter /lib/ld-linux-x86-64.so.2 {} \; 2> /dev/null
|
||
|
patchelf --remove-rpath ./usr/lib/ld-linux-x86-64.so.2
|
||
|
|
||
|
# strip binaries
|
||
|
find . -type f -executable -exec strip {} \; 2> /dev/null
|
||
|
find . -type d -exec chmod 755 {} \;
|
||
|
|
||
|
# gen initrd
|
||
|
find . -print0 | cpio --null --owner=root:root -o --format=newc | gzip -9 > ../initrd.gz
|
||
|
|
||
|
popd
|
||
|
rm -rf root
|
||
|
'';
|
||
|
}
|