blob: 5bd48b8f128cf4f74ec36d82adafe8c7582f36be [file] [log] [blame]
#!/bin/bash
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Script to generate a new empty base container for moblab, to use:
# 1) In a chroot install the most up to date lxc:
# sudo USE=-python emerge lxc
# 2) Have a moblab online with the same version of lxc
# 3) ./build_base_image.sh <base_image_version_number> <ip of moblab>
set -x
DIRECTORY=$(pwd)
CONTAINER_NAME="moblab_base_${1}"
DISTRIBUTION="ubuntu"
REVISION="xenial"
ARCH="amd64"
MOBLAB_IP_ADDRESS="${2}"
MOBLAB_DIRECTORY="/mnt/moblab/containers"
PACKAGES="wget curl python2.7 unzip rsync openjdk-8-jdk"
function generate_package_install_script() {
read -r -d '' PACKAGE_INSTALL_SCRIPT << EOM
set -x
# Wait for the networking to come up.
sleep 60
# Ensure the container is up to date
sudo apt-get -y update
sudo apt-get -y upgrade
# Install the required packages
sudo apt-get -y install ${PACKAGES}
# Install the google cloud sdk
export CLOUD_SDK_REPO="cloud-sdk-\$(lsb_release -c -s)"
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | \
sudo apt-key add -
echo "deb http://packages.cloud.google.com/apt \$CLOUD_SDK_REPO main" | \
sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
sudo apt-get -y update
sudo apt-get -y install google-cloud-sdk
EOM
}
function generate_container_control_script() {
read -r -d '' CONTAINER_INIT_SCRIPT << EOM
cd "${MOBLAB_DIRECTORY}"
sudo lxc-stop -P "${MOBLAB_DIRECTORY}" -n ${CONTAINER_NAME}
sudo rm -rf "${CONTAINER_NAME}"
sudo tar xf "${CONTAINER_NAME}".tar
sudo rm "${CONTAINER_NAME}".tar
sudo lxc-start -P "${MOBLAB_DIRECTORY}" -n "${CONTAINER_NAME}" \
-d -l trace -o lxc-start-trace-"${CONTAINER_NAME}"
sudo lxc-attach -P "${MOBLAB_DIRECTORY}" -n "${CONTAINER_NAME}" \
-- /usr/local/bin/container_install_script.sh
sudo lxc-stop -P "${MOBLAB_DIRECTORY}" -n "${CONTAINER_NAME}"
sudo tar -cJpf "${CONTAINER_NAME}".tar.xz "${CONTAINER_NAME}"
EOM
}
function create_new_empty_container() {
# Create a new empty container using a lxc templates
sudo rm -rf "${DIRECTORY}/${CONTAINER_NAME}"
sudo lxc-create -P "${DIRECTORY}" -t download -n "${CONTAINER_NAME}" \
-- -d "${DISTRIBUTION}" -r "${REVISION}" -a "${ARCH}"
# Set up the networking for the container on moblab
local network_config="lxc.net.0.type = veth\nlxc.net.0.flags = up\nlxc.net.0.link = lxcbr0\nlxc.net.0.hwaddr = 00:16:3e:0a:31:c2"
sudo sed -E s#"^lxc.rootfs.path = dir.*$"#"lxc.rootfs.path = dir:/mnt/moblab/containers/${CONTAINER_NAME}/rootfs\n"# -i "${DIRECTORY}/${CONTAINER_NAME}/config"
sudo sed -E s#"^lxc.net.0.type.*$"#"${network_config}"# -i "${DIRECTORY}/${CONTAINER_NAME}/config"
# Install a script that will setup the container on the moblab.
rm /tmp/container_install_script
echo "${PACKAGE_INSTALL_SCRIPT}" >> /tmp/container_install_script
local install_script_file="${DIRECTORY}"/"${CONTAINER_NAME}"\
/rootfs/usr/local/bin/container_install_script.sh
sudo cp /tmp/container_install_script "${install_script_file}"
sudo chmod uog+x "${install_script_file}"
# Tar up the newly created container preserving permissions
cd "${DIRECTORY}" || exit
sudo tar -czpf "${CONTAINER_NAME}".tar "${CONTAINER_NAME}"
}
function copy_container_to_moblab() {
scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
"${CONTAINER_NAME}".tar \
moblab@"${MOBLAB_IP_ADDRESS}":/mnt/moblab/containers
}
function start_container_and_run_install_script() {
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
moblab@"${MOBLAB_IP_ADDRESS}" "${CONTAINER_INIT_SCRIPT}"
}
function retrieve_container_from_moblab() {
sudo rm -rf "${DIRECTORY}/${CONTAINER_NAME}.tar.zx"
scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
moblab@"${MOBLAB_IP_ADDRESS}"\
:/mnt/moblab/containers/"${CONTAINER_NAME}".tar.xz .
}
# Generate a new empty continer in a chroot you can not create new containers
# on moblab due /etc being RO filesystem
generate_package_install_script
create_new_empty_container
# Copy the newly created container to the moblab
copy_container_to_moblab
# Unpack and start the container on moblab, run the install script
generate_container_control_script
start_container_and_run_install_script
retrieve_container_from_moblab