Last active
January 16, 2017 09:30
-
-
Save higebu/c152e73c20d438e33134 to your computer and use it in GitHub Desktop.
mkimage_vyos.sh
This file contains hidden or 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/bash | |
# You need to install overlayroot | |
# sudo apt-get install overlayroot | |
LANG=C | |
set -e | |
set -x | |
docker_user=$(docker info | grep Username | awk '{print $2}') | |
tag="$docker_user/vyos" | |
vyos_iso_url=$1 | |
vyos_iso_local=/tmp/vyos.iso | |
so3group_key_url=http://vyos.net/so3group_maintainers.key | |
so3group_key_local=/tmp/so3group_maintainers.key | |
CD_ROOT=/tmp/cdrom | |
CD_SQUASH_ROOT=/tmp/cdsquash | |
SQUASHFS_IMAGE="${CD_ROOT}/live/filesystem.squashfs" | |
WRITE_ROOT=/tmp/wroot | |
READ_ROOT=/tmp/squashfs | |
INSTALL_ROOT=/tmp/inst_root | |
# Create directories | |
mkdir -p ${CD_ROOT} | |
mkdir -p ${CD_SQUASH_ROOT} | |
mkdir -p ${WRITE_ROOT} | |
mkdir -p ${READ_ROOT} | |
mkdir -p ${INSTALL_ROOT} | |
# ---- Set up the specified ISO release ---- | |
# Download VyOS ISO release | |
curl ${vyos_iso_url} -o ${vyos_iso_local} | |
# Fetch VyOS ISO GPG signature | |
curl ${vyos_iso_url}.asc -o ${vyos_iso_local}.asc | |
# Fetch SO3 Group GPG key | |
curl ${so3group_key_url} -o ${so3group_key_local} | |
# Install SO3 Group GPG key | |
gpg --import ${so3group_key_local} | |
# Validate ISO GPG signature | |
gpg --verify ${vyos_iso_local}.asc ${vyos_iso_local} | |
# Mount ISO | |
sudo mount -o loop -t iso9660 ${vyos_iso_local} ${CD_ROOT} | |
# Verify checksums of all the files in the ISO image | |
pushd ${CD_ROOT} | |
md5sum -c md5sum.txt | |
popd | |
# Mount squashfs image from ISO | |
sudo mount -o loop -t squashfs ${SQUASHFS_IMAGE} ${CD_SQUASH_ROOT} | |
# Read version string from iso packages | |
version_string=$(cat ${CD_ROOT}/live/filesystem.packages | grep ^vyatta-version | awk '{print $2}') | |
# Debug version string as read from ISO | |
echo "This is version ${version_string}" | |
# ---- Install image from ISO ---- | |
# Create {{ WRITE_ROOT }} directories | |
RW_DIR=${WRITE_ROOT}/boot/${version_string}/live-rw | |
mkdir -p ${RW_DIR} | |
# Copy squashfs image from ISO to root partition | |
cp -p ${SQUASHFS_IMAGE} ${WRITE_ROOT}/boot/${version_string}/${version_string}.squashfs | |
# Copy boot files (kernel and initrd images) from ISO to root partition | |
find ${CD_SQUASH_ROOT}/boot -maxdepth 1 \( -type f -o -type l \) -exec cp -dp {} ${WRITE_ROOT}/boot/${version_string}/ \; | |
# Mount squashfs image from root partition | |
sudo mount -o loop -t squashfs ${WRITE_ROOT}/boot/${version_string}/${version_string}.squashfs ${READ_ROOT} | |
# Copy all files to installation directory | |
sudo cp -pr ${READ_ROOT}/* ${INSTALL_ROOT}/ | |
## ---- VyOS configuration ---- | |
# Make sure that config partition marker exists | |
sudo touch ${INSTALL_ROOT}/opt/vyatta/etc/config/.vyatta_config | |
# Copy default config file to config directory | |
sudo chroot --userspec=root:vyattacfg ${INSTALL_ROOT} cp /opt/vyatta/etc/config.boot.default /opt/vyatta/etc/config/config.boot | |
# Change permissions on the new config file | |
sudo chmod 755 ${INSTALL_ROOT}/opt/vyatta/etc/config/config.boot | |
# Add interface eth0 and set address to dhcp | |
sudo sed -i '/interfaces {/ a ethernet eth0 {\n address dhcp\n }' ${INSTALL_ROOT}/opt/vyatta/etc/config/config.boot | |
# Remove Linux Kernel | |
LINUX_IMAGE=$(sudo chroot ${INSTALL_ROOT} dpkg -l | grep linux-image | awk '{print $2}') | |
sudo chroot ${INSTALL_ROOT} apt-get -y remove --purge linux-firmware ${LINUX_IMAGE} | |
sudo chroot ${INSTALL_ROOT} rm -rf lib/modules/* | |
suite=$(sudo chroot ${INSTALL_ROOT} cat /opt/vyatta/etc/version | grep Description | awk '{print $4}' | sed 's/(//' | sed 's/)//') | |
# Generate Docker image | |
dir="$(mktemp -d ${TMPDIR:-/var/tmp}/docker-mkimage.XXXXXXXXXX)" | |
rootfsDir=$INSTALL_ROOT | |
tarFile="$dir/rootfs.tar.xz" | |
touch "$tarFile" | |
sudo tar --numeric-owner -Jcaf "$tarFile" -C "$rootfsDir" --transform='s,^./,,' . | |
echo >&2 "+ cat > '$dir/Dockerfile'" | |
cat > "$dir/Dockerfile" <<'EOF' | |
FROM scratch | |
ADD rootfs.tar.xz / | |
EOF | |
docker build -t "$tag" "$dir" | |
docker tag $tag:latest $tag:$version_string | |
docker tag $tag:latest $tag:$suite | |
echo "Cleanup" | |
sudo umount ${READ_ROOT} | |
sudo umount ${CD_SQUASH_ROOT} | |
sudo umount ${CD_ROOT} | |
sudo rm -rf ${READ_ROOT} | |
sudo rm -rf ${CD_SQUASH_ROOT} | |
sudo rm -rf ${CD_ROOT} | |
sudo rm -rf ${WRITE_ROOT} | |
sudo rm -rf ${INST_ROOT} | |
sudo rm -rf $dir |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment