From f6c317b38bf576f0d8d49ef21f8272d273d126b6 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 17 Feb 2024 14:35:16 +0100 Subject: [PATCH] create disks --- .envrc | 1 + .gitignore | 1 + debian-image/create_image.sh | 76 ++++++++++++++++++++++++++++++++++++ debian-image/reset.sh | 7 ++++ debian-image/setup.sh | 48 +++++++++++++++++++++++ shell.nix | 3 ++ 6 files changed, 136 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100755 debian-image/create_image.sh create mode 100755 debian-image/reset.sh create mode 100755 debian-image/setup.sh create mode 100644 shell.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..1d953f4 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a55bd1f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/debian-image/build diff --git a/debian-image/create_image.sh b/debian-image/create_image.sh new file mode 100755 index 0000000..89b838d --- /dev/null +++ b/debian-image/create_image.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +# https://mvallim.github.io/kubernetes-under-the-hood/documentation/create-linux-image.html + +SCRIPT=$(dirname "$0") +BUILD="$SCRIPT/build" +IMAGE="$BUILD/debian-image.raw" + +mkdir -p "$BUILD" + +if ! [ -f "$IMAGE" ]; then + # Create a 30GB disk + dd \ + if=/dev/zero \ + of="$IMAGE" \ + bs=1 \ + count=0 \ + seek=32212254720 \ + status=progress + + sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | sudo fdisk "$IMAGE" +o # clear the in memory partition table +n # new partition +p # primary partition +1 # partition number 1 + # default - start at beginning of disk ++512M # 512 MB boot parttion +n # new partition +p # primary partition +2 # partion number 2 + # default, start immediately after preceding partition + # default, extend partition to end of disk +a # make a partition bootable +1 # bootable partition is partition 1 -- /dev/loop0p1 +p # print the in-memory partition table +w # write the partition table +q # and we're done +EOF +else + echo "INFO: Skipping disk creation" +fi + +if ! [ -e "/dev/loop0" ]; then + sudo losetup -fP "$IMAGE" + sudo losetup -a + + sudo fdisk -l /dev/loop0 + sudo mkfs.ext4 /dev/loop0p1 # /boot + sudo mkfs.ext4 /dev/loop0p2 # / +else + echo "INFO: Skipping loop device setup" +fi + + +mkdir -p "$BUILD/chroot" +sudo mount /dev/loop0p2 "$BUILD/chroot/" + +if ! [ -d "$BUILD/chroot/bin" ]; then + sudo debootstrap \ + --arch=amd64 \ + --variant=minbase \ + --components "main" \ + --include "ca-certificates,cron,iptables,isc-dhcp-client,libnss-myhostname,ntp,ntpdate,rsyslog,ssh,sudo,dialog,whiptail,man-db,curl,dosfstools,e2fsck-static" \ + bookworm \ + "$BUILD/chroot" \ + http://deb.debian.org/debian/ +else + echo "INFO: Skipping debian bootstrap" +fi + +sudo mount --bind /dev "$BUILD/chroot/dev" +sudo mount --bind /run "$BUILD/chroot/run" + +sudo cp "$SCRIPT/setup.sh" "$BUILD/chroot/usr/local/bin" + +sudo chroot "$BUILD/chroot" /usr/local/bin/setup.sh diff --git a/debian-image/reset.sh b/debian-image/reset.sh new file mode 100755 index 0000000..53a18e6 --- /dev/null +++ b/debian-image/reset.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +SCRIPT=$(dirname "$0") +BUILD="$SCRIPT/build" + +sudo umount "$BUILD/chroot/" +rm -r "$BUILD" diff --git a/debian-image/setup.sh b/debian-image/setup.sh new file mode 100755 index 0000000..eb715e9 --- /dev/null +++ b/debian-image/setup.sh @@ -0,0 +1,48 @@ +#!/bin/bash +# ^^ not compatible on purpose, this only runs inside debian + +function info { + echo "INFO DEB:" "$@" +} + +export PATH="/usr/local/bin:/usr/bin:/bin" +export HOME=/root +export LC_ALL=C + +info "Hello from debian!" + +info "Setting up mounts" + +mount none -t proc /proc +mount none -t sysfs /sys +mount none -t devpts /dev/pts + +info "Configuring the system" + +echo "debian-image" > /etc/hostname +cat < /etc/apt/sources.list +deb http://deb.debian.org/debian/ bookworm main contrib non-free +deb-src http://deb.debian.org/debian/ bookworm main contrib non-free + +deb http://deb.debian.org/debian/ bookworm-updates main contrib non-free +deb-src http://deb.debian.org/debian/ bookworm-updates main contrib non-free + +deb http://deb.debian.org/debian-security bookworm-security main +deb-src http://deb.debian.org/debian-security bookworm-security main +EOF + +cat < /etc/fstab +# /etc/fstab: static file system information. +# +# Use 'blkid' to print the universally unique identifier for a +# device; this may be used with UUID= as a more robust way to name devices +# that works even if disks are added and removed. See fstab(5). +# +# +/dev/sda2 / ext4 errors=remount-ro 0 1 +/dev/sda1 /boot ext4 defaults 0 2 +EOF + +apt-get update +apt-get install -y apt-utils +apt-get install -y systemd-sysv diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..a686082 --- /dev/null +++ b/shell.nix @@ -0,0 +1,3 @@ +{ pkgs ? import { } }: pkgs.mkShell { + packages = with pkgs; [ debootstrap ]; +}