Project

General

Profile

Actions

Task #7397

open

Create a script to create a net-bootable alpine image

Added by Nico Schottelius almost 2 years ago. Updated almost 2 years ago.

Status:
New
Priority:
Normal
Assignee:
-
Start date:
11/30/2019
Due date:
% Done:

0%

Estimated time:
PM Check date:
Actions #1

Updated by Nico Schottelius almost 2 years ago

Current state:

set -e

version=3.10.3
tmpdir=$(mktemp -d)

file=alpine-netboot-${version}-x86_64.tar.gz
url=http://dl-cdn.alpinelinux.org/alpine/v3.10/releases/x86_64/$file

mkdir -p alpine-netboot
cd alpine-netboot

wget -c "$url" 

tar xvfz ${file}

mkdir rootfs
cd rootfs
zcat ../boot/initramfs-vanilla|  sudo cpio -idmv

# setup repos
# setpu network (?) - resolv.conf

exit 0
# http://dl-cdn.alpinelinux.org/alpine/v3.10/releases/x86_64/alpine-minirootfs-3.10.3-x86_64.tar.gz
[19:53] diamond:rootfs% sudo cp /etc/resolv.conf etc/
sudo mkdir root/.ssh

[19:56] diamond:rootfs% sudo mkdir root/.ssh
[19:56] diamond:rootfs% sudo cp ~/.ssh/id_ed25519.pub root/.ssh/authorized_keys
[19:57] diamond:rootfs% sudo chmod 0600 root/.ssh/authorized_keys
[19:57] diamond:rootfs% sudo chmod 0700 root/.ssh/

# chroot
[%T] %B%m%b:%c%# PATH=/bin:/sbin
[%T] %B%m%b:%c%# apk update
[%T] %B%m%b:%c%# apk upgrade

apk add openssh
apk add linux-vanilla
apk add openrc
rc-update add sshd

# build

[21:02] diamond:rootfs% sudo find . | sudo cpio -H newc -o | gzip -9 > ../alpine-initramfs.gz
[21:03] diamond:alpine-netboot% cp rootfs/boot/vmlinuz-vanilla alpine-kernel

[21:04] diamond:alpine-netboot% chmod a+r alpine-*;  scp alpine-initramfs.gz alpine-kernel root@router2.place5.ungleich.ch:/var/www/html

+ kernel panic :-)

Actions #2

Updated by Timothée Floure almost 2 years ago

Working:

#!/bin/sh

set -e
set -x

MAJOR_VERSION=3.10
MINOR_VERSION=3
IMAGE=alpine-minirootfs-$MAJOR_VERSION.$MINOR_VERSION-x86_64.tar.gz
SSH_KEYS=$(cat ~/.ssh/id_rsa.pub)
RESOLVCONF=/etc/resolv.conf

working_directory=$(dirname $0)
rootfs_tmpdir=$(mktemp -d)
rootfs_url="http://dl-cdn.alpinelinux.org/alpine/v$MAJOR_VERSION/releases/x86_64/$IMAGE" 

run_root () {
    chroot $rootfs_tmpdir /usr/bin/env \
        PATH=/bin:/sbin \
        /bin/sh -c "$*" 
}

if [ "$(whoami)" != 'root' ]; then
    echo "This script must be run as root." >&2
    exit 1
fi

# Download, extract inital rootfs.
curl "$rootfs_url" -o "$working_directory/$IMAGE" 
tar xf $IMAGE -C $rootfs_tmpdir

# Add SSH keys
run_root mkdir root/.ssh
echo $SSH_KEYS > $rootfs_tmpdir/root/.ssh/authorized_keys
run_root chmod 0600 root/.ssh/authorized_keys
run_root chmod 0700 root/.ssh

# Import local resolv.conf.
cat $RESOLVCONF > $rootfs_tmpdir/etc/resolv.conf

# Make sure init is found by the kernel.
run_root cp /sbin/init /init

# Layer atop base rootfs.
run_root apk update
run_root apk upgrade
run_root apk add openssh linux-vanilla openrc udev
run_root rc-update add udev
run_root rc-update add sshd

# Generate iniramfs image
(cd $rootfs_tmpdir; find . | cpio -H newc -o | gzip -9 > "$working_directory/alpine-initramfs.gz")
cp $rootfs_tmpdir/boot/vmlinuz-vanilla "$working_directory/alpine-kernel" 

# Upload to netboot server.
scp alpine-initramfs.gz alpine-kernel root@netboot.hack4glarus.ungleich.cloud:/var/www/html/

# Cleanup.
rm -r $rootfs_tmpdir
(cd "$working_directory/"; rm vmlinuz-vanilla alpine-kernel $IMAGE)
Actions

Also available in: Atom PDF