diff --git a/stretch-amd64/bin/basefs_config b/stretch-amd64/bin/basefs_config new file mode 100755 index 0000000..d13c576 --- /dev/null +++ b/stretch-amd64/bin/basefs_config @@ -0,0 +1,60 @@ +#!/bin/sh + +TARGET=$1 +LANG=C +VERSION="$(date +%g.%m)" + +SOFTWARE="acl acpi-support-base attr bash-completion bind9-host binutils \ + bridge-utils bsdmainutils debootstrap dmidecode ethtool file gddrescue gdisk \ + hddtemp hdparm ifenslave iproute2 iputils-ping less lsof lvm2 lzip \ + mdadm mtr-tiny netcat-traditional net-tools ntfs-3g parted pciutils procps \ + psmisc rsync screen scrub smartmontools ssh strace sysstat tar telnet \ + traceroute tree usbutils vim vlan wget xz-utils systemd systemd-sysv" + +cat << EOF > ${TARGET}/usr/sbin/policy-rc.d +#!/bin/sh +echo "All runlevel operations denied by policy" >&2 +exit 101 +EOF +chmod a+x $TARGET/usr/sbin/policy-rc.d + +chroot ${TARGET} apt-get --yes install ${SOFTWARE} +chroot ${TARGET} systemctl enable systemd-networkd.service +chroot ${TARGET} systemctl enable systemd-resolved.service + +rm ${TARGET}/usr/sbin/policy-rc.d + +echo "Etc/UTC" > ${TARGET}/etc/timezone +chroot ${TARGET} dpkg-reconfigure -f noninteractive tzdata + +cat << EOF > ${TARGET}/etc/motd + +WARNING: Authorized access only! + +EOF +cat << EOF > ${TARGET}/etc/issue +WMI Rescue Linux (based on Debian GNU/Linux Stretch) +Version ${VERSION}, AMD64 + +EOF +cat << EOF > ${TARGET}/etc/systemd/network/all.network +[Match] + +[Network] +DHCP=both +EOF +cat << EOF > ${TARGET}/etc/systemd/resolved.conf +[Resolve] +DNS=8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844 +EOF +rm ${TARGET}/etc/resolv.conf +ln -s /run/systemd/resolve/resolv.conf ${TARGET}/etc/resolv.conf +cat << EOF > ${TARGET}/etc/screenrc +startup_message off +defscrollback 5000 +EOF +cat << EOF > ${TARGET}/etc/sysctl.d/60-panic.conf +kernel.panic = 5 +kernel.panic_on_oops = 1 +EOF +rsync -avS ${TARGET}/etc/skel/ ${TARGET}/root/ diff --git a/stretch-amd64/bin/basefs_init b/stretch-amd64/bin/basefs_init new file mode 100755 index 0000000..9b31ff7 --- /dev/null +++ b/stretch-amd64/bin/basefs_init @@ -0,0 +1,27 @@ +#!/bin/sh + +TARGET=$1 +LANG=C + +mkdir ${TARGET} + +debootstrap --arch=amd64 --variant=minbase stretch ${TARGET} http://httpredir.debian.org/debian + +cat << EOF > ${TARGET}/etc/apt/sources.list +deb http://httpredir.debian.org/debian stretch main contrib non-free +deb http://security.debian.org/ stretch/updates main contrib non-free +EOF + +cat << EOF > ${TARGET}/etc/apt/apt.conf.d/80small +APT::Install-Recommends '0'; +APT::Install-Suggests '0'; +EOF + +echo wmirescue > ${TARGET}/etc/hostname + +echo 'root:wmi' | chroot ${TARGET} chpasswd + +chroot ${TARGET} apt-get update +chroot ${TARGET} apt-get upgrade --yes +chroot ${TARGET} apt-get clean + diff --git a/stretch-amd64/bin/helperfs_config b/stretch-amd64/bin/helperfs_config new file mode 100755 index 0000000..4d67902 --- /dev/null +++ b/stretch-amd64/bin/helperfs_config @@ -0,0 +1,17 @@ +#!/bin/sh + +TARGET=$1 +LANG=C + +cat << EOF > $TARGET/usr/sbin/policy-rc.d +#!/bin/sh +echo "All runlevel operations denied by policy" >&2 +exit 101 +EOF +chmod a+x $TARGET/usr/sbin/policy-rc.d + +chroot ${TARGET} mount -t proc proc /proc +chroot ${TARGET} apt-get --yes install busybox-static linux-image-4.9.0-3-amd64 +chroot ${TARGET} apt-get --yes install linux-headers-4.9.0-3-amd64 +chroot ${TARGET} apt-get --yes install zfs-dkms +chroot ${TARGET} umount /proc diff --git a/stretch-amd64/bin/ramdisk_config b/stretch-amd64/bin/ramdisk_config new file mode 100755 index 0000000..0394072 --- /dev/null +++ b/stretch-amd64/bin/ramdisk_config @@ -0,0 +1,80 @@ +#!/bin/sh + +TARGET=$1 +LANG=C + +cat << EOF > ${TARGET}/message +** WMI Rescue Linux (amd64) booting ** +EOF + +for cmd in cat echo find insmod mkdir mount sh switch_root tar; do + ln -s busybox ${TARGET}/bin/${cmd} +done + +cat << EOF > ${TARGET}/init +#!/bin/sh +# WMI Rescue Linux init script. +# (C)2011 MichaƂ Siejak +# (C)2012-2017 Mateusz Hromada + +# Print message banner +cat /message + +# Mount virtual filesystems +mount -t proc proc /proc +mount -t sysfs sysfs /sys +mount -t devtmpfs devfs /dev + +# Create dev directories +mkdir -p /dev/pts +mkdir -p /dev/shm + +# Create mount points +mkdir /rootfs +mkdir /mnt +mkdir /mnt/squashfs +mkdir /mnt/tmpfs + +# Load modules +insmod /lib/modules/4.9.0-3-amd64/kernel/drivers/block/loop.ko +insmod /lib/modules/4.9.0-3-amd64/kernel/fs/squashfs/squashfs.ko +insmod /lib/modules/4.9.0-3-amd64/kernel/fs/overlayfs/overlay.ko + +# Mount root filesystem +echo "INITRAMFS: Mounting overlayfs branches ..." +mount -t squashfs -o loop,ro /rootfs.squash /mnt/squashfs +mount -t tmpfs -o mode=755,rw tmpfs /mnt/tmpfs +mkdir /mnt/tmpfs/rw +mkdir /mnt/tmpfs/work +mount -t overlay -o rw,workdir=/mnt/tmpfs/work,upperdir=/mnt/tmpfs/rw,lowerdir=/mnt/squashfs overlay /rootfs + +# Extract any addon packages +if [ -n "$(find . -maxdepth 1 -name '*.tar')" ]; then + for i in /*.tar; do + echo "INITRAMFS: Extracting: $i ..." + tar x -f $i -C /rootfs + done +fi + +echo "INITRAMFS: Preparing rootfs environment ..." +# Move aufs branches to new root +mkdir -p /rootfs/media/.squashfs +mkdir -p /rootfs/media/.tmpfs +mount --move /mnt/squashfs /rootfs/media/.squashfs +mount --move /mnt/tmpfs /rootfs/media/.tmpfs + +# Move virtual filesystems to new root +mount --move /proc /rootfs/proc +mount --move /sys /rootfs/sys +mount --move /dev /rootfs/dev + +# Switch to new root filesystem +echo "INITRAMFS: Switching to new rootfs ..." +exec switch_root -c /dev/console /rootfs /sbin/init + +# Should not reach here +echo "INITRAMFS: Could not switch to new rootfs! System halted." + +exit 0 +EOF +chmod a+x ${TARGET}/init diff --git a/stretch-amd64/bin/ramdisk_make b/stretch-amd64/bin/ramdisk_make new file mode 100755 index 0000000..3550d34 --- /dev/null +++ b/stretch-amd64/bin/ramdisk_make @@ -0,0 +1,13 @@ +#!/bin/sh + +TARGET=$1 +RAMDISK=$2 + + +if [ ! -d "${RAMDISK}" ]; then + echo "Usage: $0 " + exit 255 +fi + +echo "Generating initramfs..." +(cd ${RAMDISK}; find . | cpio -H newc -o | gzip -1) > ${TARGET} diff --git a/stretch-amd64/bin/ramdisk_modules b/stretch-amd64/bin/ramdisk_modules new file mode 100755 index 0000000..f7aa6e2 --- /dev/null +++ b/stretch-amd64/bin/ramdisk_modules @@ -0,0 +1,27 @@ +#!/bin/sh + +TARGET=$1 +ROOTFS=$2 +VERSION=$3 + +if [ ! -d "$TARGET" -o ! -d "$ROOTFS" ]; then + echo "Usage: $0 " + exit 255 +fi + +add_module() { + for kmod in $(modprobe --dirname=${ROOTFS} --set-version="$VERSION" --ignore-install --quiet --show-depends "$1" | awk '/^insmod/ { print $2 }'); do + path=$(realpath --relative-to ${ROOTFS} ${kmod}) + if [ -e $TARGET/$path ]; then + continue + fi + mkdir -p $TARGET/$(dirname $path) + cp $kmod $TARGET/$(dirname $path) + done +} + +echo "Loading modules..." +for i in loop squashfs overlay; do + add_module $i +done + diff --git a/stretch-amd64/bin/rootfs_clear b/stretch-amd64/bin/rootfs_clear new file mode 100755 index 0000000..b15d1a8 --- /dev/null +++ b/stretch-amd64/bin/rootfs_clear @@ -0,0 +1,42 @@ +#!/bin/sh + +TARGET=$1 + +CLEANDIRS="\ +tmp \ +usr/doc \ +usr/include \ +usr/info \ +usr/man \ +usr/share/doc \ +usr/share/doc-base \ +usr/share/groff \ +usr/share/info \ +usr/share/locale \ +usr/share/man \ +var/cache/apt/archives \ +var/cache/man \ +var/lib/apt/lists \ +var/tmp" + +if [ ! -d "${TARGET}" ]; then + echo "Usage: $0 " + exit 255 +fi + +echo "Cleaning up ${TARGET}..." + +find ${TARGET} -name \*~ -delete +for dir in ${CLEANDIRS}; do + rm -rf ${TARGET}/${dir}/* +done +rm -f ${TARGET}/var/cache/apt/*.bin +rm -f ${TARGET}/root/.{bash_history,viminfo} + +mkdir -p ${TARGET}/var/lib/apt/lists/partial +mkdir -p ${TARGET}/var/cache/apt/archives/partial + +for file in $(find ${TARGET}/var/log -type f); do + : > ${file} +done + diff --git a/stretch-amd64/bin/rootfs_mksquashfs b/stretch-amd64/bin/rootfs_mksquashfs new file mode 100755 index 0000000..1bb3279 --- /dev/null +++ b/stretch-amd64/bin/rootfs_mksquashfs @@ -0,0 +1,13 @@ +#!/bin/sh + +TARGET=$1 +DEST=$2 + +if [ ! -d "${TARGET}" ]; then + echo "Usage: $0 " + exit 255 +fi + +echo "Generating squashfs..." +rm -f ${DEST} +mksquashfs ${TARGET} ${DEST} -comp xz -b 1M -no-exports -noI -Xbcj x86 diff --git a/stretch-amd64/mkrescue b/stretch-amd64/mkrescue new file mode 100755 index 0000000..801a247 --- /dev/null +++ b/stretch-amd64/mkrescue @@ -0,0 +1,30 @@ +#!/bin/sh + +TARGET=$1 + +mkdir ${TARGET} + +./bin/basefs_init ${TARGET}/basefs + +cp -a ${TARGET}/basefs ${TARGET}/helperfs +./bin/helperfs_config ${TARGET}/helperfs + +./bin/basefs_config ${TARGET}/basefs + +cp -a ${TARGET}/basefs ${TARGET}/rootfs +./bin/rootfs_clear ${TARGET}/rootfs +mkdir -p ${TARGET}/rootfs/lib/modules +rsync -aS ${TARGET}/helperfs/lib/modules/ ${TARGET}/rootfs/lib/modules/ + +for dir in sys lib/modules dev bin proc; do + mkdir -p ${TARGET}/ramdisk/${dir} +done +./bin/rootfs_mksquashfs ${TARGET}/rootfs ${TARGET}/ramdisk/rootfs.squash +./bin/ramdisk_modules ${TARGET}/ramdisk ${TARGET}/rootfs 4.9.0-3-amd64 +cp ${TARGET}/helperfs/bin/busybox ${TARGET}/ramdisk/bin/busybox +./bin/ramdisk_config ${TARGET}/ramdisk + +mkdir ${TARGET}/output +./bin/ramdisk_make ${TARGET}/output/initrd.img ${TARGET}/ramdisk + +cp ${TARGET}/helperfs/boot/vmlinuz-4.9.0-3-amd64 ${TARGET}/output/vmlinuz