Created
June 16, 2023 17:51
-
-
Save aaronhurt/bc2b1e0c8535029c609b7dd2f692d06b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -ex | |
## create new sparese image | |
dd if=/dev/zero of=ubbie-raw.img bs=1 count=0 seek=1G | |
## partition the disk | |
parted -s ubbie-raw.img mklabel gpt | |
parted -s ubbie-raw.img mkpart primary fat32 1MiB 501MiB | |
parted -s ubbie-raw.img mkpart primary ext4 501MiB 5GiB | |
parted -s ubbie-raw.img mkpart primary ext4 5GiB 100% | |
parted -s ubbie-raw.img toggle 1 esp | |
## make sure it looks right :) | |
parted -s ubbie-raw.img print | |
## create loop device - 7 should be free | |
losetup -P /dev/loop7 ubbie-raw.img | |
## format partitions | |
mkfs.fat -F 32 /dev/loop7p1 | |
mkfs.ext4 -F /dev/loop7p2 | |
mkfs.ext4 -F /dev/loop7p3 | |
## create label for cloud image root | |
e2label /dev/loop7p2 cloudimg-rootfs | |
## mount the new system | |
mkdir -p /mnt/newroot | |
mount -t ext4 /dev/loop7p2 /mnt/newroot | |
## download and install rootfs | |
wget -q -O - \ | |
https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64-root.tar.xz | \ | |
tar -xvJf - -C /mnt/newroot | |
## prepare and mount efi partition | |
mkdir -p /mnt/newroot/boot/efi | |
mount -t fat32 /dev/loop7p1 /mnt/newroot/boot/efi | |
## prepare chroot | |
mount -t proc /proc /mnt/newroot/proc | |
mount -t sysfs /sys /mnt/newroot/sys | |
mount -o bind /dev /mnt/newroot/dev | |
mount -o bind /dev/pts /mnt/newroot/dev/pts | |
## copy resolver settings | |
mkdir -p /mnt/newroot/run/systemd/resolve | |
cp -f /etc/resolv.conf /mnt/newroot/run/systemd/resolve/stub-resolv.conf | |
## chroot and modify new root | |
chroot /mnt/newroot /bin/bash << "EOT" | |
## update apt and install kernel | |
export DEBIAN_FRONTEND=noninteractive | |
apt-get update | |
apt-get install -y linux-generic | |
apt-get clean | |
apt-get autoclean | |
## install grub and update grub config | |
grub-install --target=x86_64-efi /dev/loop7 | |
update-grub | |
## add a standard user (password is "ubuntu") | |
useradd -G admin,sudo -m -U -s /bin/bash -p \ | |
'$6$xcvUejcYaF4ppAWH$mc0xGb7YW985N0YUGRwfQ3HbcnQL70hKTOe0obH7zSqZ0dR5cDL7d6gRTYQd8dTNw4qLTQFQn2tuMaLEMn1IN0' \ | |
ubuntu | |
EOT | |
## cleanup chroot mounts | |
umount /mnt/newroot/boot/efi | |
umount /mnt/newroot/proc | |
umount /mnt/newroot/sys | |
umount /mnt/newroot/dev/pts | |
umount /mnt/newroot/dev | |
## clean resolver | |
rm -f /mnt/newroot/run/systemd/resolve/stub-resolv.conf | |
## create default network config | |
cat > /mnt/newroot/etc/systemd/network/80-dhcp.network << "EOT" | |
[Match] | |
Name=en* | |
[Network] | |
DHCP=yes | |
EOT | |
## cleanup root mount, eject alpine iso, and reboot | |
## you may have to manually remove image from virtualbox | |
umount /mnt/newroot | |
## cleanup loop device | |
losetup -d /dev/loop7 | |
## All done! We have a new system in ubbie-raw.img | |
## that can be written to any medium to boot a live system |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment