#!/bin/bash set -Eeuxo pipefail TMP_DIR=${TMP_DIR:=$PWD} MNT_DIR=$TMP_DIR/newroot INITRD_DIR=$TMP_DIR/initramfs RESULT_DIR=$TMP_DIR/files KERNEL_DIR=$RESULT_DIR/kernel [[ -d $MNT_DIR ]] || mkdir -p $MNT_DIR [[ -d $INITRD_DIR ]] || mkdir -p $INITRD_DIR [[ -d $RESULT_DIR ]] || mkdir -p $RESULT_DIR [[ -d $KERNEL_DIR ]] || mkdir -p $KERNEL_DIR # Configs overwritable via environment variables RELEASE=${RELEASE:=unstable} FLAVOUR=${FLAVOUR:=debian} # Either 'debian' or 'ubuntu' INCLUDES=${INCLUDES:="openssh-server,init,curl,parted,xz-utils,vim,locales-all,less,dmidecode,iputils-ping,fping,tcpdump,rsync,ethtool,lldpd,iproute2,net-tools,sudo,gnupg,tcpdump,mtr-tiny,ifupdown,ipmitool,iptables,telnet,netcat,bridge-utils,vlan,ifstat,ncurses-term,wget,pciutils,dhcpcd5,python,efibootmgr,busybox-static"} MIRROR=${MIRROR:="https://mirrors.wit.com/debian"} fail() { echo "" echo "FAILED: $1" exit 1 } cancel() { fail "CTRL-C detected" } trap cancel INT if [ $FLAVOUR == "debian" ]; then BOOT_PKG="linux-image-$ARCH" elif [ $FLAVOUR == "ubuntu" ]; then BOOT_PKG="linux-image-generic" fi ################### ## build root fs ## ################### echo "Installing Debian $RELEASE..." debootstrap --variant=minbase --include=$INCLUDES,$BOOT_PKG $RELEASE $MNT_DIR $MIRROR || fail "cannot install $RELEASE" echo localhost > $MNT_DIR/etc/hostname cat < $MNT_DIR/etc/hosts 127.0.0.1 localhost # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters EOF echo "ipv4: \4 ipv6: \6" >>$MNT_DIR/etc/issue rm -f $MNT_DIR/etc/apt/sources.list echo 'deb https://mirrors.wit.com/debian sid main contrib non-free' > $MNT_DIR/etc/apt/sources.list echo 'deb https://mirrors.wit.com/debcore sid main' >> $MNT_DIR/etc/apt/sources.list curl https://mirrors.wit.com/debcore/public.key | LANG=C DEBIAN_FRONTEND=noninteractive chroot $MNT_DIR apt-key add - LANG=C DEBIAN_FRONTEND=noninteractive chroot $MNT_DIR apt-get update LANG=C DEBIAN_FRONTEND=noninteractive chroot $MNT_DIR apt-get clean cat /dev/null > $MNT_DIR/etc/machine-id mkdir -p $MNT_DIR/root/.ssh echo ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDd7rLlS1NmTpBr5KP5ryuA/euGD8I6uc2RCg4sCIlvH0FhEPb123QuMVImHi23ftVP61cKZXm8MlTtAoLHduYtGMHCkJWAAkiIpPetAP2KPIpuadtgIS8xuD/TCYjl0xNXLh0M1C7i7HOnTd8yr+3QNjUppyDdKjLvMQbPWZZTU5rt7CYoGlrxHjieCkq9jj8kRjRARUaAJ4DHEgMFUDIcq3JYluzzkgPK/JFwoq/IokVQCr5qfQRwr3SCkD4sIuGTj+J67uzabIr/xDBqlrMW3T+7YfY12ciHpijob+l7xESkJ+6Gxh56z8llBkGiVyh3UqnmW4MvfuAA/D3Dzhwr afrank@adams-mbp.lan > $MNT_DIR/root/.ssh/authorized_keys cat < $MNT_DIR/etc/systemd/system/start-me-up.service [Unit] Description=WIT System Startup Wants=network-online.target After=network-online.target [Service] Type=oneshot ExecStart=/usr/local/bin/start_me_up.sh RemainAfterExit=true ExecStop=/usr/local/bin/shut_me_down.sh StandardOutput=journal [Install] WantedBy=multi-user.target EOF cat < $MNT_DIR/usr/local/bin/start_me_up.sh #!/bin/bash eval "kernel_args=( \$(cat /proc/cmdline) )" for i in "\${kernel_args[@]}" do if [ \${i:0:4} = exec ] then export -- "\$i" fi done echo \$execstartup eval \$execstartup EOF cat < $MNT_DIR/usr/local/bin/shut_me_down.sh #!/bin/bash eval "kernel_args=( \$(cat /proc/cmdline) )" for i in "\${kernel_args[@]}" do if [ \${i:0:4} = exec ] then export -- "\$i" fi done echo \$execshutdown eval \$execshutdown EOF cat <$MNT_DIR/etc/dhcpcd.conf hostname duid slaac hwaddr noipv4ll option domain_name_servers, domain_name, domain_search, host_name option classless_static_routes, ntp_servers, interface_mtu require dhcp_server_identifier EOF chmod +x $MNT_DIR/usr/local/bin/start_me_up.sh chmod +x $MNT_DIR/usr/local/bin/shut_me_down.sh chroot $MNT_DIR systemctl enable start-me-up || fail "failed to enable start-me-up" chroot $MNT_DIR usermod -p '$1$A5VSg/1j$qqNLwyTlU5BH1IUMPStFB0' root chroot $MNT_DIR ssh-keygen -q -f /root/.ssh/id_rsa -C root@pxeboot -N "" echo "### public ssh key for root of this install" cat $MNT_DIR/root/.ssh/id_rsa.pub ################## ## build initrd ## ################## mkdir -p $INITRD_DIR/bin cp $MNT_DIR/bin/busybox $INITRD_DIR/bin/busybox cat <<-"EOF" >$INITRD_DIR/init #!/bin/busybox sh # Dump to sh if something fails error() { echo "Jumping into the shell..." setsid cttyhack sh } # Populate /bin with binaries from busybox /bin/busybox --install /bin mkdir -p /proc mount -t proc proc /proc mkdir -p /sys mount -t sysfs sysfs /sys mkdir -p /sys/dev mkdir -p /var/run mkdir -p /dev mkdir -p /dev/pts mount -t devpts devpts /dev/pts # Populate /dev mdev -s mkdir -p /newroot mount -t tmpfs -o size=${size:=10000}m tmpfs /newroot || error echo "Extracting rootfs... " xz -d -c -f rootfs.tar.xz | tar -x -f - -C /newroot || error mount --move /sys /newroot/sys mount --move /proc /newroot/proc mount --move /dev /newroot/dev exec switch_root /newroot /sbin/init || error EOF chmod +x $INITRD_DIR/init tar -cJf $INITRD_DIR/rootfs.tar.xz -C $MNT_DIR . #[ -d /usr/lib/linux-image-* ] && cp -r $MNT_DIR/usr/lib/linux-image-* $RESULT_DIR/dtbs rm $MNT_DIR/boot/initrd.img-* mv $MNT_DIR/boot/vmlinuz-* $KERNEL_DIR/vmlinuz-$ARCH cd $INITRD_DIR find . -print0 | cpio --null -ov --format=newc | gzip -9 > $KERNEL_DIR/initramfs-$ARCH.gz cd - apt-get update if [[ $ARCH = amd64 ]] then apt-get -y install grub-common grub-efi-$ARCH-bin grub-pc-bin grub-mknetdir --directory=/usr/lib/grub/i386-pc/ --net-directory=$RESULT_DIR/ grub-mknetdir --directory=/usr/lib/grub/x86_64-efi/ --net-directory=$RESULT_DIR/ else apt-get -y install grub-common grub-efi-$ARCH-bin grub-mknetdir --net-directory=$RESULT_DIR/ fi ls -la $RESULT_DIR/ exit 0