03-download_generate_image.sh 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#!/bin/bash

## This script downloads and generates a suitable ext4 image from existing cloud
## images. For simplicity it currently only downloads from Ubuntu images, but it
## should not be a big effort to adapt to other cloud images.

source variables

function download() {
	echo "Downloading $2..."

	curl -s -o $1 $2
}

function download_if_not_present() {
	[ -f $1 ] || download $1 $2
}

function generate_image() {
	echo "Generating $IMAGE_ROOTFS..."

	truncate -s $IMAGE_SIZE $IMAGE_ROOTFS
	mkfs.ext4 $IMAGE_ROOTFS > /dev/null 2>&1

	local tmppath=/tmp/.$RANDOM-$RANDOM
	mkdir $tmppath
	sudo mount $IMAGE_ROOTFS -o loop $tmppath
	sudo tar -xf images/$UBUNTU_VERSION/download/$image_tar --directory $tmppath
	sudo umount $tmppath
	rmdir $tmppath
}

function extract_vmlinux() {
	echo "Extracting vmlinux to $KERNEL_IMAGE..."

	local extract_linux=/tmp/.$RANDOM-$RANDOM
	curl -s -o $extract_linux https://raw.githubusercontent.com/torvalds/linux/master/scripts/extract-vmlinux
	chmod +x $extract_linux
	$extract_linux images/$UBUNTU_VERSION/download/$kernel > $KERNEL_IMAGE
	rm $extract_linux
}


# Download components
mkdir -p images/$UBUNTU_VERSION/download

image_tar=$UBUNTU_VERSION-server-cloudimg-amd64-root.tar.xz
download_if_not_present \
	images/$UBUNTU_VERSION/download/$image_tar \
	https://cloud-images.ubuntu.com/$UBUNTU_VERSION/current/$image_tar

kernel=$UBUNTU_VERSION-server-cloudimg-amd64-vmlinuz-generic
download_if_not_present \
	images/$UBUNTU_VERSION/download/$kernel \
	https://cloud-images.ubuntu.com/$UBUNTU_VERSION/current/unpacked/$kernel

initrd=$UBUNTU_VERSION-server-cloudimg-amd64-initrd-generic
download_if_not_present \
	images/$UBUNTU_VERSION/download/$initrd \
	https://cloud-images.ubuntu.com/$UBUNTU_VERSION/current/unpacked/$initrd


# Generate image, kernel and link initrd
[ -f $IMAGE_ROOTFS ] || generate_image

[ -f $INITRD ] || ln -s download/$initrd $INITRD

[ -f $KERNEL_IMAGE ] || extract_vmlinux