Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the manifest to build kernel/mkinitramfs. #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions kernel/mkinitramfs/init
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#!/usr/bin/sh
# This file based in part on the mkinitramfs script for the LFS LiveCD
# written by Alexander E. Patrakov and Jeremy Huntwork.

PATH=/bin:/usr/bin:/sbin:/usr/sbin
export PATH

function problem()
{
printf "Encountered a problem!\n\nDropping you to a shell.\n\n"
sh
}

function no_device()
{
printf "The device %s, which is supposed to contain the\n" $1
printf "root file system, does not exist.\n"
printf "Please fix this problem and exit this shell.\n\n"
}

function no_mount()
{
printf "Could not mount device %s\n" $1
printf "Sleeping forever. Please reboot and fix the kernel command line.\n\n"
printf "Maybe the device is formatted with an unsupported file system?\n\n"
printf "Or maybe filesystem type autodetection went wrong, in which case\n"
printf "you should add the rootfstype=... parameter to the kernel command line.\n\n"
printf "Available partitions:\n"
}

function do_mount_root()
{
mkdir /.root
[ -n "$rootflags" ] && rootflags="$rootflags,"
rootflags="$rootflags$ro"

case "$root" in
/dev/* ) device=$root ;;
UUID=* ) eval $root; device="/dev/disk/by-uuid/$UUID" ;;
LABEL=*) eval $root; device="/dev/disk/by-label/$LABEL" ;;
"" ) echo "No root device specified." ; problem ;;
esac

while [ ! -b "$device" ] ; do
no_device $device
problem
done

if ! mount -n -t "$rootfstype" -o "$rootflags" "$device" /.root ; then
no_mount $device
cat /proc/partitions
while true ; do sleep 10000 ; done
else
echo "Successfully mounted device $root"
fi
}

function do_try_resume()
{
case "$resume" in
UUID=* ) eval $resume; resume="/dev/disk/by-uuid/$UUID" ;;
LABEL=*) eval $resume; resume="/dev/disk/by-label/$LABEL" ;;
esac

if $noresume || ! [ -b "$resume" ]; then return; fi

ls -lH "$resume" | ( read x x x x maj min x echo -n ${maj%,}:$min > /sys/power/resume )
}

init=/usr/bin/init
root=
rootdelay=
rootfstype=auto
ro="ro"
rootflags=
device=
resume=
noresume=false

mount -n -t devtmpfs devtmpfs /dev
mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys
mount -n -t tmpfs tmpfs /run

read -r cmdline < /proc/cmdline

for param in $cmdline ; do
case $param in
init=* ) init=${param#init=} ;;
root=* ) root=${param#root=} ;;
rootdelay=* ) rootdelay=${param#rootdelay=} ;;
rootfstype=*) rootfstype=${param#rootfstype=} ;;
rootflags=* ) rootflags=${param#rootflags=} ;;
resume=* ) resume=${param#resume=} ;;
noresume ) noresume=true ;;
ro ) ro="ro" ;;
rw ) ro="rw" ;;
esac
done

# udevd location depends on version
if [ -x /sbin/udevd ]; then
UDEVD=/sbin/udevd
elif [ -x /lib/udev/udevd ]; then
UDEVD=/lib/udev/udevd
elif [ -x /lib/systemd/systemd-udevd ]; then
UDEVD=/lib/systemd/systemd-udevd
else
echo "Cannot find udevd nor systemd-udevd"
problem
fi

${UDEVD} --daemon --resolve-names=never
udevadm trigger
udevadm settle

if [ -f /etc/mdadm.conf ]; then
mdadm -As
fi

if [ -x /sbin/vgchange ]; then
/sbin/vgchange -a y > /dev/null
fi

if [ -n "$rootdelay" ]; then
sleep "$rootdelay"
fi

do_try_resume # This function will not return if resuming from disk
do_mount_root

killall -w ${UDEVD##*/}

exec switch_root /.root "$init" "$@"
129 changes: 129 additions & 0 deletions kernel/mkinitramfs/mkinitramfs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env bash
# This file based in part on the mkinitramfs script for the LFS LiveCD
# written by Alexander E. Patrakov and Jeremy Huntwork.

function main() {
if [[ "$#" -ne 2 ]]; then
echo "Usage: $0 <kernel_version> <output_file>"
exit 1
fi

declare kver="$1"
declare initramfs_file="$2"

echo "Generating $initramfs_file (Linux v$kver)..."

# Create a temporary directory to work in
declare workdir="$(mktemp -d)"

# Create a directory to store the name of libraries to import
declare unsorted=$(mktemp)

# Create the base directory structure
mkdir -p $workdir/{dev,usr/bin,run,sys,proc}
mkdir -p $workdir/usr/lib64/{firmware,systemd}
mkdir -p $workdir/etc/{modprobe.d,udev/rules.d}

ln -s usr/lib64 $workdir/lib
ln -s usr/lib64 $workdir/lib64
ln -s lib64 $workdir/usr/lib
ln -s usr/bin $workdir/bin
ln -s usr/bin $workdir/sbin

touch $workdir/etc/modprobe.d/modprobe.conf

# Create the necessary device nodes
mknod -m 640 $workdir/dev/console c 5 1
mknod -m 664 $workdir/dev/null c 1 3

# Install the udev configuration files
if [[ -f /etc/udev/udev.conf ]]; then
cp /etc/udev/udev.conf $workdir/etc/udev/udev.conf
fi

for file in $(find /etc/udev/rules.d/ -type f); do
cp $file $workdir/etc/udev/rules.d
done

# Install any firmware present
if [[ -d /usr/lib64/firmware ]]; then
cp -a /usr/lib64/firmware $workdir/usr/lib64
fi

# Copy the init script
cp -r /usr/share/mkinitramfs/init $workdir/usr/bin/

# Binaries we'll need to copy from the host that the init script requires
declare binaries="sh cat cp dd killall ls mkdir mknod mount umount sed"
declare binaries="$binaries sleep ln rm uname readlink basename modprobe"
declare binaries="$binaries blkid switch_root lsmod insmod kmod udevadm"

# Install binaries
for f in $binaries ; do
ldd /usr/bin/$f | sed "s/\t//" | cut -d " " -f1 >> $unsorted
cp /usr/bin/$f $workdir/usr/bin
done

# Add udevd libraries
if [[ -x /usr/lib64/udev/udevd ]] ; then
ldd /usr/lib64/udev/udevd | sed "s/\t//" | cut -d " " -f1 >> $unsorted
elif [[ -x /usr/lib64/systemd/systemd-udevd ]] ; then
ldd /usr/lib64/systemd/systemd-udevd | sed "s/\t//" | cut -d " " -f1 >> $unsorted
fi

# Install libraries
sort $unsorted | uniq | while read library ; do
if [[ "$library" == "linux-vdso.so.1" ]] || [[ "$library" == "linux-gate.so.1" ]]; then
continue
fi
if [[ "$library" == "/lib64/ld-linux-x86-64.so.2" ]]; then
cp "$library" "$workdir/$library"
elif [[ -f "/usr/lib64/$library" ]]; then
cp /usr/lib64/$library $workdir/usr/lib64
elif [[ -f "/usr/lib64/systemd/$library" ]]; then
cp /usr/lib64/systemd/$library $workdir/usr/lib64/systemd/
else
echo "Can't find library $library. Aborting."
exit 1
fi
done

# Copy the udev and systemd directories
if [[ -d /usr/lib64/udev ]]; then
cp -a /usr/lib64/udev $workdir/usr/lib64
fi
if [[ -d /usr/lib64/systemd ]]; then
cp -a /usr/lib64/systemd $workdir/usr/lib64
fi
if [[ -d /usr/lib64/elogind ]]; then
cp -a /usr/lib64/elogind $workdir/usr/lib64
fi

# Install the kernel modules if requested
if [ -n "$kver" ]; then
find \
/usr/lib64/modules/$kver/kernel/{crypto,fs,lib} \
/usr/lib64/modules/$kver/kernel/drivers/{block,ata,md,firewire} \
/usr/lib64/modules/$kver/kernel/drivers/{scsi,message,pcmcia,virtio} \
/usr/lib64/modules/$kver/kernel/drivers/usb/{host,storage} \
-type f 2> /dev/null | cpio --make-directories -p --quiet $workdir

mkdir -p $workdir/usr/lib64/modules/$kver

cp /usr/lib64/modules/$kver/modules.{builtin,order} \
$workdir/usr/lib64/modules/$kver

depmod -b $workdir $kver
fi

# Generate the initramfs
( cd $workdir ; find . | cpio -o -H newc --quiet ) > $initramfs_file
#xz -e --check=none -z -f -9

# Remove the temporary directory and file
rm -rf $workdir $unsorted

echo "done."
}

main $@
51 changes: 51 additions & 0 deletions kernel/mkinitramfs/mkinitramfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-

import os
import glob
import stdlib
from stdlib.template import basic
from stdlib.split.drain_all import drain_all
from stdlib.manifest import manifest


def install_mkinitramfs():
pass


@manifest(
name='mkinitramfs',
category='kernel',
description='''
A set of tools to build and install an initramfs.
''',
tags=['linux', 'kernel', 'debian'],
maintainer='[email protected]',
licenses=[],
upstream_url='https://debian.org/',
kind=stdlib.kind.Kind.EFFECTIVE,
versions_data=[
{
'semver': '1.0.0',
'fetch': [{
'file': './mkinitramfs',
}, {
'file': './init',
},
],
},
],
)
def build(build):
packages = basic.build()

packages['kernel/mkinitramfs'].drain_build_cache('mkinitramfs', 'usr/bin/')
packages['kernel/mkinitramfs'].drain_build_cache('init', 'usr/share/mkinitramfs/')

packages['kernel/mkinitramfs'].requires('sys-apps/coreutils')
packages['kernel/mkinitramfs'].requires('sys-apps/bash')
packages['kernel/mkinitramfs'].requires('sys-apps/util-linux')
packages['kernel/mkinitramfs'].requires('sys-apps/systemd')
packages['kernel/mkinitramfs'].requires('sys-apps/cpio')

return packages