Skip to content

Instantly share code, notes, and snippets.

@bencord0
Last active June 4, 2018 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bencord0/6407310 to your computer and use it in GitHub Desktop.
Save bencord0/6407310 to your computer and use it in GitHub Desktop.
Instructions for building a systemd images for Gentoo See: http://bencord0.wordpress.com/2013/09/02/systemd-stage3/ for details.
#!/bin/bash
dracut -f /root/initramfs -i /root/systemd.squashfs /root.squashfs
chmod a+r /root/initramfs
#!/bin/bash
MAKEOPTS="-j$(grep processor /proc/cpuinfo|wc -l)"
emerge -u sys-kernel/aufs-sources
cd /usr/src/linux
zcat /proc/config.gz > .config
make olddefconfig
make $MAKEOPTS targz-pkg
cd /
tar xzvf /usr/src/linux/linux*.tar.gz
depmod
cp /boot/vmlinuz* /root/vmlinuz
#!/bin/bash
rm -f /root/systemd.squashfs||true;
mksquashfs chroot /root/systemd.squashfs
#!/bin/bash
TOPDIR=$(dirname $0)
cd "$TOPDIR"
EMERGE_FLAGS="--buildpkg --update --jobs"
DBUS_DEPS="sys-libs/glibc \
sys-libs/cracklib \
sys-libs/pam \
sys-apps/shadow \
sys-apps/baselayout"
rm -rf "chroot"
mkdir "chroot-prepare" "chroot"
tar xavpf stage-template.tar.gz -C chroot-prepare
tar xavpf stage-template.tar.gz -C chroot
# Stop when things go wrong
set -ex
# note: dbus's pkg_setup phase needs some files to exist in the chroot
# Building binary packages also installs compile-time dependencies
emerge $EMERGE_FLAGS --usepkg --config-root=chroot-prepare --root=chroot-prepare \
--oneshot --nodeps $DBUS_DEPS
emerge $EMERGE_FLAGS --usepkg --config-root=chroot-prepare --root=chroot-prepare \
--oneshot --nodeps sys-auth/pambase
emerge $EMERGE_FLAGS --usepkg --config-root=chroot-prepare --root=chroot-prepare \
world
# Only install the runtime dependencies
emerge $EMERGE_FLAGS --usepkgonly --config-root=chroot --root=chroot \
--oneshot --nodeps $DBUS_DEPS
emerge $EMERGE_FLAGS --usepkgonly --config-root=chroot --root=chroot \
--oneshot --nodeps sys-auth/pambase
emerge $EMERGE_FLAGS --usepkgonly --config-root=chroot --root=chroot \
world
# Blank out the default root password
sed -i -e '/root/ s/*//' chroot/etc/shadow
# Don't bother looking for other filesystems (esp. SWAP)
echo -n > chroot/etc/fstab
# Start networking on boot
ln -s 'chroot/usr/lib64/systemd/system/dhcpcd.service' \
'chroot/etc/systemd/system/multi-user.target.wants/dhcpcd.service'
tar cJf /root/stage3-systemd.tar.xz -C chroot .
#!/bin/bash
cd /usr/lib/dracut/modules.d
###########
# Console #
###########
mkdir -p 80console
cat << EOF > 80console/module-setup.sh
#!/bin/bash
check() {
return 0
}
depends() {
return 0
}
install() {
inst "\$moddir/console-tty0.conf" /etc/cmdline.d/console-tty0.conf
inst "\$moddir/console-ttyS0.conf" /etc/cmdline.d/console=ttyS0.conf
}
EOF
chmod +x 80console/module-setup.sh
echo 'console=tty0' > 80console/console-tty0.conf
echo 'console=ttyS0' > 80console/console-ttyS0.conf
##################################
# Rootfs = aufs(squashfs, tmpfs) #
##################################
mkdir -p 81squashedaufs-root
cat << EOF > 81squashedaufs-root/module-setup.sh
#!/bin/bash
check() {
return 0
}
depends() {
return 0
}
install() {
inst_hook cmdline 81 "\$moddir/cmdline-squashedaufs-root.sh"
inst_hook mount 81 "\$moddir/mount-squashedaufs-root.sh"
inst_hook pre-pivot 81 "\$moddir/pre-pivot-squashedaufs-root.sh"
inst "\$moddir/squashedaufs-root.conf" /etc/cmdline.d/squashedaufs-root.conf
}
EOF
chmod +x 81squashedaufs-root/module-setup.sh
cat << EOF > 81squashedaufs-root/cmdline-squashedaufs-root.sh
#!/bin/sh
case "\$root" in
*.squashfs)
wait_for_dev "\$root"
rootok=1
USING_SQUASHEDAUFS=1
;;
esac
EOF
chmod +x 81squashedaufs-root/cmdline-squashedaufs-root.sh
cat << EOF > 81squashedaufs-root/mount-squashedaufs-root.sh
#!/bin/bash
mount_squashfs_as_aufs()
{
info "Creating a tmpfs for root"
mkdir -p /tmproot
mount -t tmpfs tmpfs /tmproot -o size=90%
info "Mounting squashfs"
mkdir -p /squashroot
mount -t squashfs "\$root" /squashroot
info "Unioning rootfs"
mount -t aufs -o br:/tmproot:/squashroot none /sysroot
}
if [ -n USING_SQUASHEDAUFS ]
then
mount_squashfs_as_aufs
fi
EOF
chmod +x 81squashedaufs-root/mount-squashedaufs-root.sh
cat << EOF >> 81squashedaufs-root/pre-pivot-squashedaufs-root.sh
#!/bin/bash
mkdir -p /sysroot/lib/modules
cp -r /lib/modules/* /sysroot/lib/modules/
EOF
chmod +x 81squashedaufs-root/pre-pivot-squashedaufs-root.sh
echo 'root=/root.squashfs' >> 81squashedaufs-root/squashedaufs-root.conf
#!/bin/bash
DIRS="
dev
etc/portage/package.keywords
etc/portage/package.use
home
proc
root
sys
usr/portage
var/lib/portage
"
for dir in $DIRS; do
mkdir -p stage-template/$dir
done
ln -sf /usr/portage/profiles/default/linux/amd64/13.0 stage-template/etc/make.profile
cat << EOF > stage-template/etc/portage/make.conf
ACCEPT_KEYWORDS="~amd64"
FEATURES="buildpkg parallel-fetch parallel-install"
MAKEOPTS="-j8"
USE="-bindist -consolekit systemd"
EOF
cat << EOF > stage-template/var/lib/portage/world
app-editors/vim
app-portage/eix
net-dns/bind-tools
net-misc/dhcpcd
sys-apps/dbus
sys-apps/iproute2
sys-apps/systemd
sys-boot/grub
sys-boot/os-prober
sys-fs/btrfs-progs
sys-fs/lvm2
sys-fs/squashfs-tools
sys-kernel/dracut
EOF
tar czf stage-template.tar.gz -C stage-template .
# Example usage in clean qemu environment
#
# $ qemu-kvm \
# $ -m 10G \
# $ -smp 6 \
# $ -kernel vmlinuz \
# $ -initrd initramfs \
# $ -net nic,model=virtio \
# $ -net user \
# $ -nographic \
# $ -append console=ttyS0
# Which is ofcourse, useless without me supplying
# the vmlinuz and initramfs files.
# Creation of the initramfs is sill a manual task
# It does some things atypical to a standard distro initramfs.
# - Only a kernel and initramfs are supplied to qemu, there
# is no stateful disk image.
# - The root= kernel cmdline is set inside the initramfs,
# not supplied to qemu's "-append".
# - The VM uses a lot of RAM. All writes are directed
# towards an RAM backed AUFS rootfs.
# - QEMU User networking provides a crippeled network
# environment, sufficient enough for TCP to download
# the portage tree and distfiles.
# - No other special networking needs to be made. Qemu can be run
# as a non-privilaged user.
# - While the kernel is fairly standard
# (sys-kernel/aufs-sources), however, network drivers and other
# config is taylored for my environment.
# - Kernel modules insude the initramfs are coupled to
# the kernel version. (In theory, the initramfs could be
# bundled into the kernel too)
# - The initramfs is at least as big as any generated
# tarball since the initramfs will effectively perform
# a stage3 install during the boot process. It is a neat
# chicken/egg problem that requires the stage3 to build the
# initramfs, and the qemu/kernel/initramfs to (cleanly) build
# the stage tarball.
# - My custom dracut module is not documented (or described) here.
# Once the VM has booted, root login is permitted (without password)
# Set SYNC and GENTOO_MIRRORS variables in /etc/portage/make.conf first.
emerge --sync
eix-update
# Enable ssh, not sure why the ebuild didn't set these directories properly
chown root /var/empty
chmod 755 /var/empty
systemctl start sshd
# Run /usr/bin/passwd to set a password and enable remote logins
# (optional) passwd
# For some reason, binutils postinstall actions are not run properly.
# Manually make the symlinks, orphans will eventually be replaced
ln -sf /usr/x86_64-pc-linux-gnu/binutils-bin/2.23.2/* /usr/bin
emerge binutils
source /etc/profile
emerge -uDNvj dev-vcs/git world
git clone https://gist.github.com/6407310.git
(cd /; patch -p0 -l < /root/6407310/user.eclass.patch)
cd 6407310
bash stage-template.sh
bash build_stage3.sh
bash build_squashfs.sh
bash build_kernel.sh
bash prepare_dracut.sh
bash build_initramfs.sh
--- /usr/portage/eclass/user.eclass.orig 2012-06-22 20:31:13.000000000 +0100
+++ /usr/portage/eclass/user.eclass 2013-09-01 16:03:48.000000000 +0100
@@ -92,9 +92,13 @@
grep "${key}:\*:" /etc/${db}
;;
*)
- # ignore output if nscd doesn't exist, or we're not running as root
- nscd -i "${db}" 2>/dev/null
- getent "${db}" "${key}"
+ if [[ "${ROOT}" != "/" ]] ; then
+ chroot "${ROOT}" getent "${db}" "${key}"
+ else
+ # ignore output if nscd doesn't exist, or we're not running as root
+ nscd -i "${db}" 2>/dev/null
+ getent "${db}" "${key}"
+ fi
;;
esac
}
@@ -247,7 +251,7 @@
;;
*)
- useradd -r "${opts[@]}" "${euser}" || die
+ useradd -R "${ROOT}" -r "${opts[@]}" "${euser}" || die
;;
esac
@@ -340,7 +344,7 @@
opts="-g ${egid}"
fi
# We specify -r so that we get a GID in the system range from login.defs
- groupadd -r ${opts} "${egroup}" || die
+ groupadd -R "${ROOT}" -r ${opts} "${egroup}" || die
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment