diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2017-08-09 10:09:37 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2017-08-09 10:09:37 +0200 |
commit | 43dc85ba7873de02518d112ab2d2cb460654fa46 (patch) | |
tree | fcd09c79b359bb217469cf8f8e4ce963d5ec0ccc | |
parent | 2d79739f013774f9acf64d1087f14fecdfdf6744 (diff) |
Build custom kernel with longer command line
-rwxr-xr-x | bootstrap | 223 | ||||
-rwxr-xr-x | init | 2 |
2 files changed, 161 insertions, 64 deletions
@@ -19,15 +19,16 @@ # # 1 - bootstrap phase 1 # 2 - bootstrap phase 2 -# 3 - setup -# 4 - create footfs -# 5 - create kernel image and initrd +# 3 - kernel build +# 4 - setup +# 5 - create footfs +# 6 - create kernel image and initrd # usage="usage: $0" id="$(id -un)" btrfs=/btrfs -release="unstable" +release="testing" mirror="https://deb.debian.org/debian/" passwd="123" #@@ TMP root passwd. macaddr="de:ad:be:ef:b8:da" # Mac address for testing. @@ -42,7 +43,7 @@ function info () { echo "$*" 1>&2; } function error () { info "$*"; exit 1; } stage="1" -stage_max="5" +stage_max="6" while [ "$#" -gt 0 ]; do case "$1" in @@ -104,15 +105,18 @@ fi # Spawn a systemd namespace container (systemd-nspawn) # -function nspawn () # <systemd-nspawn-args> +function nspawn () # <root> <systemd-nspawn-args> { - sudo systemd-nspawn --register=no -D "$root" "$@" + local r="$1" + shift + + sudo systemd-nspawn --register=no -D "$r" "$@" # systemd-nspawn may create the /var/lib/machines subvolume which prevents # the deletion of the containing submodule. So we clean it up. # - if sudo btrfs subvol show "$root/var/lib/machines" 1>/dev/null 2>&1; then - sudo btrfs subvol delete "$root/var/lib/machines" + if sudo btrfs subvol show "$r/var/lib/machines" 1>/dev/null 2>&1; then + sudo btrfs subvol delete "$r/var/lib/machines" fi } @@ -120,14 +124,24 @@ function nspawn () # <systemd-nspawn-args> # # write <<<'localhost' /etc/hostname # -function write () # <path> +function write () # <path> [<root>] { - sudo tee "$root$1" >/dev/null + local r="$2" + if [ -z "$r" ]; then + r="$root" + fi + + sudo tee "$r$1" >/dev/null } -function append () # <path> +function append () # <path> [<root>] { - sudo tee -a "$root$1" >/dev/null + local r="$2" + if [ -z "$r" ]; then + r="$root" + fi + + sudo tee -a "$r$1" >/dev/null } # Stage 1: debootstrap, phase 1. @@ -142,11 +156,13 @@ if [ "$stage" -eq "1" ]; then # # - systemd-container seems to be required by host systemd-nspawn. # + # - no linux-image-amd64 since building custom below + # comps="main,contrib,non-free" - pkgs="locales,klibc-utils,sudo,systemd-container" + pkgs="locales,klibc-utils,sudo,systemd-container,udev" - pkgs+=",linux-image-amd64,irqbalance,pciutils,usbutils" + pkgs+=",kmod,linux-base,firmware-linux-free,irqbalance,pciutils,usbutils" pkgs+=",intel-microcode,amd64-microcode" @@ -202,7 +218,6 @@ if [ "$stage" -le "2" ]; then #!/bin/bash trap "exit 1" ERR - set -x # Hack around systemd bug#79306 (changes /etc/localtime) by removing it now @@ -228,24 +243,24 @@ chattr +i /etc/localtime # cat <<EOF1 >/etc/os-release NAME="Build OS" -VERSION="0.5.0" +VERSION="0.6.0" ID=buildos ID_LIKE=debian -PRETTY_NAME="Build OS 0.5.0 (Based on Debian)" -VERSION_ID="0.5" +PRETTY_NAME="Build OS 0.6.0 (Based on Debian)" +VERSION_ID="0.6" HOME_URL="https://build2.org/" SUPPORT_URL="https://lists.build2.org/" BUG_REPORT_URL="https://lists.build2.org/" EOF1 cat <<EOF1 >/etc/issue -Build OS 0.5.0 (Based on Debian) \n \l +Build OS 0.6.0 (Based on Debian) \n \l EOF1 cat <<EOF1 >/etc/motd -Welcome to Build OS 0.5.0 (https://build2.org)! +Welcome to Build OS 0.6.0 (https://build2.org)! EOF1 @@ -275,24 +290,135 @@ EOF # - Failed to create directory .../sys/fs/selinux: Read-only file system is # harmless and fixed upstream (systemd issue#3748). # - nspawn /bootstrap/bootstrap + nspawn "$root" /bootstrap/bootstrap subvol_snapshot -r "$root" "$root-2" fi -# Stage 3: setup. +# Stage 3: kernel build. # if [ "$stage" -le "3" ]; then - # Create the setup script/service that will finish the setup from within the - # installation via systemd-nspawn --boot. + # Create the setup service that will be used by both this stage and the + # setup stage below. Note that we will do actual building (which requires + # installing extra packages) in a snapshot on the side. # sudo mkdir -p "$root/bootstrap" + + # Note that when started via systemd-nspawn, we get /dev/console, not + # /dev/tty0. + # + write <<EOF /usr/lib/systemd/system/buildos-setup.service +[Unit] +Description=build os setup +After=default.target +Conflicts=console-getty.service + +[Service] +Type=idle +TimeoutStartSec=infinity +RemainAfterExit=true +ExecStart=/bootstrap/setup +StandardInput=tty-force +StandardOutput=inherit +StandardError=inherit +TTYPath=/dev/console +TTYReset=yes +TTYVHangup=yes + +[Install] +WantedBy=default.target +EOF + + sudo mkdir -p "$root/usr/lib/systemd/system/default.target.wants" + sudo ln -sf "$root/usr/lib/systemd/system/buildos-setup.service" \ + "$root/usr/lib/systemd/system/default.target.wants/buildos-setup.service" + + # Create the kernel build snapshot, write the script that does the build + # from within the installation and boot it up via systemd-nspawn --boot. + # + # Add this line before shutdown if need to debug/check things. + # + # setsid /bin/bash -c "exec /bin/bash -i <>/dev/console 1>&0 2>&1" + # + subvol_delete "$root-3-kernel" + subvol_snapshot "$root" "$root-3-kernel" + + write <<EOF /bootstrap/setup "$root-3-kernel" +#!/bin/bash + +trap "exit 1" ERR +set -x + +# This seems to be the simplest method of building the standard Debian +# kernel with adjusted configuration. Taken from the Debian Kernel Handbook. +# +apt-get update +apt-get install -y build-essential linux-source +cd /usr/src +tar xf linux-source-* +mv linux-source-*/ linux +xzcat linux-config-*/config.amd64_none_amd64.xz >linux/.config +cd linux + +# Adjust kernel command line size limit. +# +sed -i -re 's/^(#define COMMAND_LINE_SIZE ).+$/\1 4096/' arch/x86/include/asm/setup.h +make oldconfig +scripts/config --disable DEBUG_INFO + +make clean +make deb-pkg LOCALVERSION=-buildos KDEB_PKGVERSION=1 -j 8 + +# Clean up and shutdown. +# +rm /bootstrap/setup +systemctl poweroff + +EOF + sudo chmod u+x "$root-3-kernel/bootstrap/setup" + + nspawn "$root-3-kernel" --boot + + # Copy the kernel over and install it. + # + sudo cp "$root-3-kernel/usr/src/linux-image-"*.deb "$root/usr/src/" + write <<EOF /bootstrap/setup #!/bin/bash trap "exit 1" ERR +set -x + +dpkg -i /usr/src/linux-image-*.deb +rm -rf /usr/src/* +cd / +ln -s boot/vmlinuz-* /vmlinuz + +# Clean up and shutdown. +# +rm /bootstrap/setup +systemctl poweroff +EOF + sudo chmod u+x "$root/bootstrap/setup" + + nspawn "$root" --boot + + subvol_snapshot -r "$root" "$root-3" +fi + +# Stage 4: setup. +# +if [ "$stage" -le "4" ]; then + + # Write the setup script (the service is already there from stage 3) that + # will finish the setup. + # + write <<EOF /bootstrap/setup +#!/bin/bash + +trap "exit 1" ERR set -x # Create the build user, /build home directory. Make a password-less sudo'er. @@ -317,50 +443,21 @@ rm /usr/lib/systemd/system/default.target.wants/buildos-setup.service rm /usr/lib/systemd/system/buildos-setup.service rm -r /bootstrap -# Shutdown the container from within. +# Shutdown. # systemctl poweroff EOF sudo chmod u+x "$root/bootstrap/setup" - # Note that when started via systemd-nspawn, we get /dev/console, not - # /dev/tty0. - # - write <<EOF /usr/lib/systemd/system/buildos-setup.service -[Unit] -Description=build os setup -After=default.target -Conflicts=console-getty.service - -[Service] -Type=idle -TimeoutStartSec=infinity -RemainAfterExit=true -ExecStart=/bootstrap/setup -StandardInput=tty-force -StandardOutput=inherit -StandardError=inherit -TTYPath=/dev/console -TTYReset=yes -TTYVHangup=yes + nspawn "$root" --boot -[Install] -WantedBy=default.target -EOF - - sudo mkdir -p "$root/usr/lib/systemd/system/default.target.wants" - sudo ln -sf "$root/usr/lib/systemd/system/buildos-setup.service" \ - "$root/usr/lib/systemd/system/default.target.wants/buildos-setup.service" - - nspawn --boot - - subvol_snapshot -r "$root" "$root-3" + subvol_snapshot -r "$root" "$root-4" fi -# Stage 4: generate rootfs. +# Stage 5: generate rootfs. # -if [ "$stage" -le "4" ]; then +if [ "$stage" -le "5" ]; then # Note that there is also initramfs image that is embedded into kernel. In # Debian it contains just /dev/console and /root/. @@ -379,12 +476,12 @@ if [ "$stage" -le "4" ]; then gzip -9 > "$owd/buildos-rootfs.cpio.gz" cd "$owd" - subvol_snapshot -r "$root" "$root-4" + subvol_snapshot -r "$root" "$root-5" fi -# Stage 5: generate initrd. +# Stage 6: generate initrd. # -if [ "$stage" -le "5" ]; then +if [ "$stage" -le "6" ]; then # Generate buildid and store it in /etc/os-release and in buildos-buildid. # These are used by the monitor to detect when it's time to reboot. @@ -423,7 +520,7 @@ EOF cp "$root/vmlinuz" buildos-image echo "$buildid" >buildos-buildid - subvol_snapshot -r "$root" "$root-5" + subvol_snapshot -r "$root" "$root-6" fi exit 0 @@ -17,7 +17,7 @@ function error () info "$*"; fi - # The setsid voodoo (take from Debian init's panic()) is to enable job + # The setsid voodoo (taken from Debian init's panic()) is to enable job # control. # info "type Ctrl-D to exit shell and reboot" |