Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

snap: cross-build snap images #99

Merged
merged 2 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ prime/
stage/
snap/.snapcraft/
snap/snapcraft.yaml
snap-build/*.log
snap-build/*.img
snap-build/*.fd
snap-build/id_rsa*
snap-build/seed/user-data
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ $(SNAPCRAFT_FILE): %: %.in Makefile $(YQ) $(VERSIONS_YAML_FILE) $(VERSION_FILE)
snap: $(SNAPCRAFT_FILE)
snapcraft -d

snap-xbuild:
cd $(MK_DIR)/snap-build; ./xbuild.sh -a all

clean:
rm $(SNAPCRAFT_FILE)

Expand Down
11 changes: 11 additions & 0 deletions snap-build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Cross-build snap images

Build Kata Containers snap images for all supported architectures using virtual machines.

## Usage

Run following command to build the snap images for all supported images.

```
./xbuild.sh -a all
```
20 changes: 20 additions & 0 deletions snap-build/config_amd64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
#
# Copyright (c) 2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

local arch_qemu="x86_64"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you can use local here as these are globals and local is for functions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can if you source it inside a function 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooooh - that's clever! I hadn't spotted that ;)

local arch_image="bionic-server-cloudimg-amd64.img"
local arch_image_url="https://cloud-images.ubuntu.com/bionic/current/${arch_image}"
local arch_bios=""
local arch_bios_url=""
local arch_qemu_cpu="qemu64"
local arch_qemu_machine="pc"
local arch_qemu_extra_opts=""
if [ "$(arch)" == "x86_64" ];then
arch_qemu_cpu="host"
arch_qemu_machine="pc,accel=kvm"
arch_qemu_extra_opts="-enable-kvm"
fi
15 changes: 15 additions & 0 deletions snap-build/config_arm64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
#
# Copyright (c) 2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

local arch_qemu="aarch64"
local arch_image="bionic-server-cloudimg-arm64.img"
local arch_image_url="https://cloud-images.ubuntu.com/bionic/current/${arch_image}"
local arch_bios="QEMU_EFI.fd"
local arch_bios_url="https://releases.linaro.org/components/kernel/uefi-linaro/latest/release/qemu64/${arch_bios}"
local arch_qemu_cpu="cortex-a57"
local arch_qemu_machine="virt,usb=off"
local arch_qemu_extra_opts=""
15 changes: 15 additions & 0 deletions snap-build/config_ppc64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
#
# Copyright (c) 2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

local arch_qemu="ppc64"
local arch_image="bionic-server-cloudimg-ppc64el.img"
local arch_image_url="https://cloud-images.ubuntu.com/bionic/current/${arch_image}"
local arch_bios="QEMU_EFI.fd"
local arch_bios_url="https://releases.linaro.org/components/kernel/uefi-linaro/latest/release/qemu64/${arch_bios}"
local arch_qemu_cpu="POWER8"
local arch_qemu_machine="pseries,usb=off"
local arch_qemu_extra_opts="-echr 0x05 -boot c"
115 changes: 115 additions & 0 deletions snap-build/lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/bin/bash
#
# Copyright (c) 2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

error(){
msg="$*"
echo "ERROR: $msg" >&2
}

die(){
error "$*"
exit 1
}

make_random_ip_addr() {
echo "127.$((1 + RANDOM % 240)).$((1 + RANDOM % 240)).$((1 + RANDOM % 240))"
}

make_random_port() {
echo "$((11060 + RANDOM % 1000))"
}

get_dnssearch() {
echo "$(grep search /etc/resolv.conf | cut -d' ' -f 2)"
}

get_dns() {
v="$(grep nameserver /etc/resolv.conf | cut -d' ' -f2 | sed -e 's/^/"/g' -e 's/$/",/g')"
echo ${v} | sed -e 's|,$||g'
}

download() {
url="$1"
outdir="$2"
pushd "${outdir}"
curl -LO ${url}
ret=$?
popd
return ${ret}
}

setup_image() {
img_url=$1
img=$2
[ -f "${img}" ] && return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to be fragile - if the download fails and the script is re-run, the full image will never be downloaded.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

Copy link
Author

@devimc devimc Jul 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if download fails, output file is removed

{ download "${img_url}" "$(dirname ${img})"; ret=$?; } || true
[ ${ret} != 0 ] && rm -f "${img}" && return
qemu-img resize "${img}" +5G
}

# arg1: ip
# arg2: port
# arg3: ssh key
# arg4: timeout in minutes
# return: 0 on success, 1 otherwise
ping_vm() {
ip="$1"
port="$2"
sshkeyfile="$3"
timeout=$4
minute=60
sleeptime=10
timeoutsec=$((timeout*minute))
tries=$((timeoutsec/sleeptime))

for i in $(seq 1 ${tries}); do
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i "${sshkeyfile}" "${ip}" -p "${port}" true && return 0
sleep ${sleeptime}
done

return 1
}

# arg1: qemu system: ppc64, aarch64 or x86_64
# arg2: cpu model
# arg3: machine type
# arg4: ip
# arg5: port
# arg6: image path
# arg7: seed image path
# arg8: extra options
run_qemu() {
local arch="${1}"
local cpu="${2}"
local machine="${3}"
local ip="${4}"
local port="${5}"
local image="${6}"
local seed_img="${7}"
local extra_opts="${8}"
local ssh_key_file="id_rsa"
local ping_timeout=15

local img_opts="-drive file=${image},if=virtio,format=qcow2,aio=threads"
local seed_opts="-drive file=${seed_img},if=virtio,media=cdrom"
if [ "${arch}" == "aarch64" ]; then
img_opts="-device virtio-blk-device,drive=image -drive file=${image},if=none,id=image,aio=threads"
seed_opts="-device virtio-blk-device,drive=cloud -drive file=${seed_img},if=none,id=cloud,format=raw"
fi

qemu-system-${arch} -cpu "${cpu}" -machine "${machine}" -smp cpus=4 -m 2048M \
-net nic,model=virtio -device virtio-rng-pci -net user,hostfwd=tcp:${ip}:${port}-:22,dnssearch="$(get_dnssearch)" \
${img_opts} ${seed_opts} \
-display none -vga none -daemonize ${extra_opts}
[ $? != 0 ] && return 1

# depending of the host's hw, it takes for around ~15 minutes
ping_vm "${ip}" "${port}" "${ssh_key_file}" ${ping_timeout}
[ $? != 0 ] && return 1

return 0
}
2 changes: 2 additions & 0 deletions snap-build/seed/meta-data
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
instance-id: snapid
local-hostname: snap
21 changes: 21 additions & 0 deletions snap-build/seed/user-data.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#cloud-config
@APT_PROXY@
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

package_upgrade: false
users:
- lock-passwd: true
name: @USER@
shell: /bin/bash
ssh-authorized-keys:
- @SSH_KEY@
sudo: ALL=(ALL) NOPASSWD:ALL
write_files:
- content: |
[Service]
Environment=@DOCKER_ENV@
path: /etc/systemd/system/docker.service.d/http-proxy.conf
- content: |
@ENV@
path: /etc/environment
- content: |
{"dns": [@DOCKER_DNS@]}
path: /etc/docker/daemon.json
48 changes: 48 additions & 0 deletions snap-build/snap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash
#
# Copyright (c) 2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

# Setup the environment and build the snap image.
# This script runs in the VM.

set -x -e

sudo apt-get update -y
sudo apt-get install -y \
build-essential \
cpio \
docker.io \
golang-go \
libattr1-dev \
libcap-dev \
libcap-ng-dev \
libdw-dev \
libelf-dev \
libfdt-dev \
libglib2.0-dev \
libiberty-dev \
libnewt-dev \
libpci-dev \
libpixman-1-dev \
librbd-dev \
libssl-dev \
libz-dev \
openssl \
python \
snapcraft \
snapd

# start docker
sudo systemctl start docker

# clone packaging reposiory and make snap
packaging_repo_url=https://github.com/kata-containers/packaging
packaging_dir=~/packaging
sudo rm -rf ${packaging_dir}
git clone ${packaging_repo_url} ${packaging_dir}
pushd ${packaging_dir}
sudo -E PATH=$PATH make snap
sudo chown ${USER}:${USER} *.snap
Loading