Merge pull request #166 from marcov/debian-rootfs

osbuilder: Add support for debian rootfs
This commit is contained in:
James O. D. Hunt 2018-09-21 08:24:28 +01:00 committed by GitHub
commit 0bd79918d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 161 additions and 80 deletions

View File

@ -24,4 +24,4 @@ before_script:
- ".ci/setup.sh" - ".ci/setup.sh"
script: script:
- "travis_wait 30 .ci/run.sh" - "travis_wait 50 .ci/run.sh"

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env bash
# #
# Copyright (c) 2017 Intel Corporation # Copyright (c) 2017 Intel Corporation
# #
@ -26,9 +26,9 @@ usage()
cat <<EOT cat <<EOT
Usage: ${script_name} [options] <rootfs-dir> Usage: ${script_name} [options] <rootfs-dir>
This script will create a Kata Containers image file of This script will create a Kata Containers image file of
an adequate size based on the <rootfs-dir> directory. an adequate size based on the <rootfs-dir> directory.
The size of the image can be also be specified manually The size of the image can be also be specified manually
by '-s' flag. by '-s' flag.
Options: Options:
-h Show this help -h Show this help
@ -63,15 +63,17 @@ do
h) usage ;; h) usage ;;
o) IMAGE="${OPTARG}" ;; o) IMAGE="${OPTARG}" ;;
r) ROOT_FREE_SPACE="${OPTARG}" ;; r) ROOT_FREE_SPACE="${OPTARG}" ;;
s) IMG_SIZE=${OPTARG} s) {
if [ ${IMG_SIZE} -le 0 ]; then IMG_SIZE=${OPTARG}
die "Image size has to be greater than 0 MB." if [ ${IMG_SIZE} -le 0 ]; then
fi die "Image size has to be greater than 0 MB."
if [ ${IMG_SIZE} -gt ${MAX_IMG_SIZE_MB} ]; then fi
die "Image size should not be greater than ${MAX_IMG_SIZE_MB} MB." if [ ${IMG_SIZE} -gt ${MAX_IMG_SIZE_MB} ]; then
fi die "Image size should not be greater than ${MAX_IMG_SIZE_MB} MB."
;; fi
f) FS_TYPE="${OPTARG}" ;; }
;;
f) FS_TYPE="${OPTARG}" ;;
esac esac
done done
@ -140,94 +142,131 @@ OLD_IMG_SIZE=0
align_memory() align_memory()
{ {
remaining=$(($IMG_SIZE % $MEM_BOUNDARY)) remaining=$(($IMG_SIZE % $MEM_BOUNDARY))
if [ "$remaining" != "0" ];then if [ "$remaining" != "0" ];then
warning "image size '$IMG_SIZE' is not aligned to memory boundary '$MEM_BOUNDARY', aligning it" warning "image size '$IMG_SIZE' is not aligned to memory boundary '$MEM_BOUNDARY', aligning it"
IMG_SIZE=$(($IMG_SIZE + $MEM_BOUNDARY - $remaining)) IMG_SIZE=$(($IMG_SIZE + $MEM_BOUNDARY - $remaining))
fi fi
} }
# Calculate image size based on the rootfs # Calculate image size based on the rootfs
calculate_img_size() calculate_img_size()
{ {
IMG_SIZE=${IMG_SIZE:-$MEM_BOUNDARY} IMG_SIZE=${IMG_SIZE:-$MEM_BOUNDARY}
align_memory align_memory
if [ -n "$ROOT_FREE_SPACE" ] && [ "$IMG_SIZE" -gt "$ROOTFS_SIZE" ]; then if [ -n "$ROOT_FREE_SPACE" ] && [ "$IMG_SIZE" -gt "$ROOTFS_SIZE" ]; then
info "Ensure that root partition has at least ${ROOT_FREE_SPACE}MB of free space" info "Ensure that root partition has at least ${ROOT_FREE_SPACE}MB of free space"
IMG_SIZE=$(($IMG_SIZE + $ROOT_FREE_SPACE)) IMG_SIZE=$(($IMG_SIZE + $ROOT_FREE_SPACE))
fi fi
} }
# Cleanup unmount()
cleanup()
{ {
sync sync
umount -l ${MOUNT_DIR} umount -l ${MOUNT_DIR}
rmdir ${MOUNT_DIR} rmdir ${MOUNT_DIR}
fsck -D -y "${DEVICE}p1"
losetup -d "${DEVICE}"
} }
detach()
{
losetup -d "${DEVICE}"
# From `man losetup` about -d option:
# Note that since Linux v3.7 kernel uses "lazy device destruction".
# The detach operation does not return EBUSY error anymore if
# device is actively used by system, but it is marked by autoclear
# flag and destroyed later
info "Waiting for ${DEVICE} to detach"
local i=0
local max_tries=5
while [[ "$i" < "$max_tries" ]]; do
sleep 1
# If either the 'p1' partition has disappeared or partprobe failed, then
# the loop device should be correctly detached
if ! [ -b "${DEVICE}p1" ] || ! partprobe -s ${DEVICE}; then
break
fi
((i+=1))
echo -n "."
done
[[ "$i" == "$max_tries" ]] && die "Cannot detach ${DEVICE}"
info "detached"
}
create_rootfs_disk() create_rootfs_disk()
{ {
ATTEMPT_NUM=$(($ATTEMPT_NUM+1)) ATTEMPT_NUM=$(($ATTEMPT_NUM+1))
info "Create root disk image. Attempt ${ATTEMPT_NUM} out of ${MAX_ATTEMPTS}." info "Create root disk image. Attempt ${ATTEMPT_NUM} out of ${MAX_ATTEMPTS}."
if [ ${ATTEMPT_NUM} -gt ${MAX_ATTEMPTS} ]; then if [ ${ATTEMPT_NUM} -gt ${MAX_ATTEMPTS} ]; then
die "Unable to create root disk image." die "Unable to create root disk image."
fi fi
calculate_img_size calculate_img_size
if [ ${OLD_IMG_SIZE} -ne 0 ]; then if [ ${OLD_IMG_SIZE} -ne 0 ]; then
info "Image size ${OLD_IMG_SIZE}MB too small, trying again with size ${IMG_SIZE}MB" info "Image size ${OLD_IMG_SIZE}MB too small, trying again with size ${IMG_SIZE}MB"
fi fi
info "Creating raw disk with size ${IMG_SIZE}M" info "Creating raw disk with size ${IMG_SIZE}M"
qemu-img create -q -f raw "${IMAGE}" "${IMG_SIZE}M" qemu-img create -q -f raw "${IMAGE}" "${IMG_SIZE}M"
OK "Image file created" OK "Image file created"
# Kata runtime expect an image with just one partition # Kata runtime expect an image with just one partition
# The partition is the rootfs content # The partition is the rootfs content
info "Creating partitions" info "Creating partitions"
parted "${IMAGE}" --script "mklabel gpt" \ parted "${IMAGE}" --script "mklabel gpt" \
"mkpart ${FS_TYPE} 1M -1M" "mkpart ${FS_TYPE} 1M -1M"
OK "Partitions created" OK "Partitions created"
# Get the loop device bound to the image file (requires /dev mounted in the # Get the loop device bound to the image file (requires /dev mounted in the
# image build system and root privileges) # image build system and root privileges)
DEVICE=$(losetup -P -f --show "${IMAGE}") DEVICE=$(losetup -P -f --show "${IMAGE}")
#Refresh partition table #Refresh partition table
partprobe "${DEVICE}" partprobe -s "${DEVICE}"
# Poll for the block device p1
local i=0
local max_tries=5
while [[ "$i" < "$max_tries" ]]; do
[ -b "${DEVICE}p1" ] && break
((i+=1))
echo -n "."
sleep 1
done
[[ "$i" == "$max_tries" ]] && die "File ${DEVICE}p1 is not a block device"
MOUNT_DIR=$(mktemp -d osbuilder-mount-dir.XXXX) MOUNT_DIR=$(mktemp -d osbuilder-mount-dir.XXXX)
info "Formating Image using ext4 format" info "Formatting Image using ext4 filesystem"
mkfs.ext4 -q -F -b "${BLOCK_SIZE}" "${DEVICE}p1" mkfs.ext4 -q -F -b "${BLOCK_SIZE}" "${DEVICE}p1"
OK "Image formated" OK "Image formatted"
info "Mounting root partition" info "Mounting root partition"
mount "${DEVICE}p1" "${MOUNT_DIR}" mount "${DEVICE}p1" "${MOUNT_DIR}"
OK "root partition mounted" OK "root partition mounted"
RESERVED_BLOCKS_PERCENTAGE=3 RESERVED_BLOCKS_PERCENTAGE=3
info "Set filesystem reserved blocks percentage to ${RESERVED_BLOCKS_PERCENTAGE}%" info "Set filesystem reserved blocks percentage to ${RESERVED_BLOCKS_PERCENTAGE}%"
tune2fs -m "${RESERVED_BLOCKS_PERCENTAGE}" "${DEVICE}p1" tune2fs -m "${RESERVED_BLOCKS_PERCENTAGE}" "${DEVICE}p1"
AVAIL_DISK=$(df -B M --output=avail "${DEVICE}p1" | tail -1) AVAIL_DISK=$(df -B M --output=avail "${DEVICE}p1" | tail -1)
AVAIL_DISK=${AVAIL_DISK/M} AVAIL_DISK=${AVAIL_DISK/M}
info "Free space root partition ${AVAIL_DISK} MB" info "Free space root partition ${AVAIL_DISK} MB"
# if the available disk space is less than rootfs size, repeat the process # if the available disk space is less than rootfs size, repeat the process
# of disk creation by adding 5% in the inital assumed value $ROOTFS_SIZE # of disk creation by adding 5% in the inital assumed value $ROOTFS_SIZE
if [ $ROOTFS_SIZE -gt $AVAIL_DISK ]; then if [ $ROOTFS_SIZE -gt $AVAIL_DISK ]; then
# Increase the size but remain aligned to 128 # Increase the size but remain aligned to 128
MEM_BOUNDARY=$(($MEM_BOUNDARY+128)) MEM_BOUNDARY=$(($MEM_BOUNDARY+128))
rm -f ${IMAGE} OLD_IMG_SIZE=${IMG_SIZE}
OLD_IMG_SIZE=${IMG_SIZE} unset IMG_SIZE
unset IMG_SIZE unmount
cleanup detach
create_rootfs_disk rm -f ${IMAGE}
fi create_rootfs_disk
fi
} }
create_rootfs_disk create_rootfs_disk
@ -237,6 +276,9 @@ info "Copying content from rootfs to root partition"
cp -a "${ROOTFS}"/* ${MOUNT_DIR} cp -a "${ROOTFS}"/* ${MOUNT_DIR}
OK "rootfs copied" OK "rootfs copied"
cleanup unmount
# Optimize
fsck.ext4 -D -y "${DEVICE}p1"
detach
info "Image created. Virtual size: ${IMG_SIZE}MB." info "Image created. Virtual size: ${IMG_SIZE}MB."

View File

@ -0,0 +1,13 @@
#
# Copyright (c) 2018 SUSE
#
# SPDX-License-Identifier: Apache-2.0
# NOTE: OS_VERSION is set according to config.sh
from debian:@OS_VERSION@
# RUN commands
RUN apt-get update && apt-get install -y curl wget systemd debootstrap git build-essential
# This will install the proper golang to build Kata components
@INSTALL_GO@

View File

@ -0,0 +1,12 @@
#
# Copyright (c) 2018 SUSE
#
# SPDX-License-Identifier: Apache-2.0
OS_VERSION=${OS_VERSION:-9.5}
# Set OS_NAME to the desired debian "codename"
OS_NAME=${OS_NAME:-"stretch"}
# NOTE: Re-using ubuntu rootfs configuration, see 'ubuntu' folder for full content.
source $script_dir/ubuntu/$CONFIG_SH

View File

@ -0,0 +1,7 @@
#
# Copyright (c) 2018 SUSE
#
# SPDX-License-Identifier: Apache-2.0
# NOTE: Re-using ubuntu rootfs lib, see 'ubuntu' folder for details.
source ${script_dir}/ubuntu/$LIB_SH

View File

@ -440,6 +440,12 @@ test_distro_ubuntu()
run_test "${name}" "" "ubuntu" "service" "no" run_test "${name}" "" "ubuntu" "service" "no"
} }
test_distro_debian()
{
local -r name="Can create and run debian image"
run_test "${name}" "" "debian" "service" "no"
}
test_distro_fedora() test_distro_fedora()
{ {
@ -526,6 +532,7 @@ test_all_distros()
test_distro_centos test_distro_centos
test_distro_alpine test_distro_alpine
test_distro_ubuntu test_distro_ubuntu
test_distro_debian
if [ $MACHINE_TYPE != "ppc64le" ]; then if [ $MACHINE_TYPE != "ppc64le" ]; then
test_distro_clearlinux test_distro_clearlinux