create disks

This commit is contained in:
nora 2024-02-17 14:35:16 +01:00
commit f6c317b38b
6 changed files with 136 additions and 0 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use nix

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/debian-image/build

76
debian-image/create_image.sh Executable file
View file

@ -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

7
debian-image/reset.sh Executable file
View file

@ -0,0 +1,7 @@
#!/usr/bin/env bash
SCRIPT=$(dirname "$0")
BUILD="$SCRIPT/build"
sudo umount "$BUILD/chroot/"
rm -r "$BUILD"

48
debian-image/setup.sh Executable file
View file

@ -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 <<EOF > /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 <<EOF > /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).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/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

3
shell.nix Normal file
View file

@ -0,0 +1,3 @@
{ pkgs ? import <nixpkgs> { } }: pkgs.mkShell {
packages = with pkgs; [ debootstrap ];
}