Skip to content

Instantly share code, notes, and snippets.

@copy
Last active February 20, 2021 20:02
Show Gist options
  • Save copy/6b58c33bb311ebd759b8bf3ce6dba82b to your computer and use it in GitHub Desktop.
Save copy/6b58c33bb311ebd759b8bf3ce6dba82b to your computer and use it in GitHub Desktop.
set -ve
OUTFILE=/tmp/disk
CONTAINER_NAME=alpine-x86-full
dd if=/dev/zero of=$OUTFILE bs=1k count=1M
(
echo o # Create a new empty DOS partition table
echo n # Add a new partition
echo p # Primary partition
echo 1 # Partition number
echo 2048 # First sector
echo # Last sector (Accept default: varies)
echo a # make bootable
echo w # Write changes
echo q # quit
) | fdisk $OUTFILE
# 1048576 is 2048 (first sector) * 512 (sector size)
mkfs.ext4 -F -E offset=1048576 $OUTFILE
kpartx -a -v $OUTFILE
function finish_kpartx {
kpartx -d $OUTFILE
}
trap finish_kpartx EXIT
# XXX: Assumes loop0
mount /dev/mapper/loop0p1 /mnt
function finish_mount {
umount /mnt
finish_kpartx
}
trap finish_mount EXIT
docker export $CONTAINER_NAME | tar -xvC /mnt/
grub-install --recheck --target=i386-pc --locales= --themes= --fonts= --root-directory /mnt/ /dev/loop0
cat > /mnt/boot/grub/grub.cfg << 'EOF'
set root='hd0' # XXX: I believe this has no significance, but is required to exist by grub
set timeout_style=menu
set timeout=0
menuentry 'Linux' {
#insmod ext2
#insmod gzio
#insmod fat
set root='hd0,msdos1'
echo 'Loading Linux linux ...'
linux /boot/vmlinuz nosplash debug verbose rw root=/dev/sda1 rootfstype=ext4
#linux /boot/vmlinuz-virthardened nosplash debug verbose rw root=/dev/sda1 rootfstype=ext4
echo 'Loading initial ramdisk ...'
initrd /boot/initramfs-vanilla
#initrd /boot/initramfs-virthardened
}
EOF
# sync should only necessary if the disk is left mounted, but we'll leave it
# here in case something goes wrong
sync
FROM i386/alpine:latest
# https://wiki.gentoo.org/wiki/OpenRC/Baselayout_1_to_2_migration#Boot_runlevel
# https://wiki.alpinelinux.org/wiki/Installing_Alpine_Linux_in_a_chroot#Entering_your_chroot
RUN apk update && \
apk upgrade && \
apk add --no-cache linux-vanilla vim openrc && \
rc-update add devfs sysinit && \
rc-update add root boot && \
rc-update add mtab boot && \
rc-update add mount-ro shutdown && \
rc-update add killprocs shutdown && \
echo 'rc_sys=""' >> /etc/rc.conf
#rc-update add mdev sysinit && \
COPY motd issue inittab fstab /etc/
/dev/sda1 / ext4 rw,relatime,data=ordered 0 0
tmpfs /tmp tmpfs nodev,nosuid 0 0
# /etc/inittab
::sysinit:/sbin/openrc sysinit
::sysinit:/sbin/openrc boot
::wait:/sbin/openrc default
# Set up a couple of getty's
tty1::respawn:/bin/sh
tty2::respawn:/sbin/getty 38400 tty2
tty3::respawn:/sbin/getty 38400 tty3
tty4::respawn:/sbin/getty 38400 tty4
tty5::respawn:/sbin/getty 38400 tty5
tty6::respawn:/sbin/getty 38400 tty6
# Put a getty on the serial port
#ttyS0::respawn:/sbin/getty -L ttyS0 115200 vt100
ttyS0::respawn:/bin/sh
# Stuff to do for the 3-finger salute
::ctrlaltdel:/sbin/reboot
# Stuff to do before rebooting
::shutdown:/sbin/openrc shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment