Skip to content

Commit

Permalink
Added docker bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
meefik committed Nov 5, 2019
1 parent b35ce0d commit 95f7f2c
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 96 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ COMMANDS:
-m - mount the container before start
stop [-u] [NAME ...] - stop all included or only specified components
-u - unmount the container after stop
sync URL - synchronize with the operating environment with server
status [NAME ...] - display the status of the container and components
help [NAME ...] - show this help or help of components
Expand Down
92 changes: 4 additions & 88 deletions cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
################################################################################

VERSION="2.4.1"
VERSION="2.5.0"

################################################################################
# Common
Expand Down Expand Up @@ -57,24 +57,6 @@ get_platform()
esac
}

get_qemu()
{
local arch="$1"
local qemu=""
local host_platform=$(get_platform)
local guest_platform=$(get_platform "${arch}")
if [ "${host_platform}" != "${guest_platform}" ]; then
case "${guest_platform}" in
arm) qemu="qemu-arm-static" ;;
arm_64) qemu="qemu-aarch64-static" ;;
x86) qemu="qemu-i386-static" ;;
x86_64) qemu="qemu-x86_64-static" ;;
*) qemu="" ;;
esac
fi
echo ${qemu}
}

get_uuid()
{
cat /proc/sys/kernel/random/uuid
Expand Down Expand Up @@ -241,9 +223,6 @@ chroot_exec()
mounts="${mounts} -b ${MOUNTS// / -b }"
fi
local emulator
if [ -z "${EMULATOR}" ]; then
EMULATOR=$(get_qemu ${ARCH})
fi
if [ -n "${EMULATOR}" ]; then
emulator="-q ${EMULATOR}"
fi
Expand All @@ -260,16 +239,6 @@ chroot_exec()
esac
}

sync_env()
{
local env_url="$1"
[ -n "${env_url}" ] || return 1
msg -n "Synchronization with server ... "
[ -e "${ENV_DIR}" ] || mkdir -p "${ENV_DIR}"
wget -q -O - "${env_url}" | tar xz -C "${ENV_DIR}" 1>&2
is_ok "fail" "done"
}

################################################################################
# Params
################################################################################
Expand Down Expand Up @@ -944,8 +913,6 @@ container_status()
local supported_fs=$(printf '%s ' $(grep -v nodev /proc/filesystems | sort))
msg "${supported_fs}"
[ -n "${CHROOT_DIR}" ] || return 0
msg -n "Installed system: "
local linux_version=$([ -r "${CHROOT_DIR}/etc/os-release" ] && . "${CHROOT_DIR}/etc/os-release"; [ -n "${PRETTY_NAME}" ] && echo "${PRETTY_NAME}" || echo "unknown")
msg "${linux_version}"
Expand All @@ -955,53 +922,11 @@ container_status()
component_exec "${INCLUDE}"
msg "Mounted parts: "
local is_mnt=0
local item
for item in $(grep "${CHROOT_DIR%/}" /proc/mounts | awk '{print $2}' | sed "s|${CHROOT_DIR%/}/*|/|g")
do
msg "* ${item}"
local is_mnt=1
done
[ "${is_mnt}" -ne 1 ] && msg " ...nothing mounted"
msg "Available mount points: "
local is_mountpoints=0
local mp
for mp in $(grep -v "${CHROOT_DIR%/}" /proc/mounts | grep ^/ | awk '{print $2":"$3}')
do
local part=$(echo ${mp} | awk -F: '{print $1}')
local fstype=$(echo ${mp} | awk -F: '{print $2}')
local block_size=$(stat -c '%s' -f ${part})
local available=$(stat -c '%a' -f ${part} | awk '{printf("%.1f",$1*'${block_size}'/1024/1024/1024)}')
local total=$(stat -c '%b' -f ${part} | awk '{printf("%.1f",$1*'${block_size}'/1024/1024/1024)}')
if [ -n "${available}" -a -n "${total}" ]; then
msg "* ${part} ${available}/${total} GB (${fstype})"
is_mountpoints=1
fi
done
[ "${is_mountpoints}" -ne 1 ] && msg " ...no mount points"
msg "Available partitions: "
local is_partitions=0
local dev
for dev in /sys/block/*/dev
do
if [ -f ${dev} ]; then
local devname=$(echo ${dev} | sed -e 's@/dev@@' -e 's@.*/@@')
[ -e "/dev/${devname}" ] && local devpath="/dev/${devname}"
[ -e "/dev/block/${devname}" ] && local devpath="/dev/block/${devname}"
[ -n "${devpath}" ] && local parts=$(fdisk -l ${devpath} 2>/dev/null | grep ^/dev/ | awk '{print $1}')
local part
for part in ${parts}
do
local size=$(fdisk -l ${part} 2>/dev/null | grep 'Disk.*bytes' | awk '{ sub(/,/,""); print $3" "$4}')
local type=$(fdisk -l ${devpath} 2>/dev/null | grep ^${part} | tr -d '*' | awk '{str=$6; for (i=7;i<=10;i++) if ($i!="") str=str" "$i; printf("%s",str)}')
msg "* ${part} ${size} (${type})"
local is_partitions=1
done
fi
done
[ "${is_partitions}" -ne 1 ] && msg " ...no available partitions"
}
helper()
Expand Down Expand Up @@ -1041,7 +966,6 @@ COMMANDS:
-m - mount the container before start
stop [-u] [NAME ...] - stop all included or only specified components
-u - unmount the container after stop
sync URL - synchronize with the operating environment with server
status [NAME ...] - display the status of the container and components
help [NAME ...] - show this help or help of components
Expand Down Expand Up @@ -1086,6 +1010,9 @@ fi
if [ -z "${CHROOT_DIR}" ]; then
CHROOT_DIR="${ENV_DIR}/mnt"
fi
if [ -z "${METHOD}" ]; then
METHOD="chroot"
fi
# parse options
OPTIND=1
Expand Down Expand Up @@ -1133,14 +1060,6 @@ WITHOUT_CHECK="false"
WITHOUT_DEPENDS="false"
REVERSE_DEPENDS="false"
EXCLUDE_COMPONENTS=""
case "${METHOD}" in
proot)
CHROOT_DIR="${TARGET_PATH}"
;;
*)
METHOD="chroot"
;;
esac
# make dirs
[ -d "${CONFIG_DIR}" ] || mkdir "${CONFIG_DIR}"
Expand Down Expand Up @@ -1314,9 +1233,6 @@ stop)
container_umount
fi
;;
sync)
sync_env "$@"
;;
status)
if [ $# -gt 0 ]; then
DO_ACTION='do_status'
Expand Down
6 changes: 6 additions & 0 deletions include/bootstrap/docker/deploy.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Linux Deploy Component
# (c) Anton Skshidlevsky <[email protected]>, GPLv3

NAME="docker"
DESC="Bootstrap for Docker containers"
DEPENDS="bootstrap"
57 changes: 57 additions & 0 deletions include/bootstrap/docker/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/sh
# Linux Deploy Component
# (c) Anton Skshidlevsky <[email protected]>, GPLv3

[ -n "${SUITE}" ] || SUITE="linux"

if [ -z "${ARCH}" ]
then
case "$(get_platform)" in
x86) ARCH="386" ;;
x86_64) ARCH="amd64" ;;
arm) ARCH="arm" ;;
arm_64) ARCH="arm64" ;;
esac
fi

[ -n "${SOURCE_PATH}" ] || SOURCE_PATH="library/ubuntu:18.04"

do_install()
{
msg ":: Installing ${COMPONENT} ... "

local image="${SOURCE_PATH%%:*}"
local tag="${SOURCE_PATH##*:}"
msg -n "Authorization in Docker repository ... "
local token=$(wget -q -O - "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${image}:pull" | grep -oE '"token":"[.a-zA-Z0-9_-]+"' | tr -d '"' | awk -F':' '{print $2}')
test -n "${token}"
is_ok "fail" "done" || return 1
msg -n "Fetching manifests from the repository ... "
local manifest=$(wget -q -O - --header "Authorization: Bearer ${token}" --header "Accept: application/vnd.docker.distribution.manifest.list.v2+json" "https://registry-1.docker.io/v2/${image}/manifests/${tag}" | sed 's/},{/\n/g' | grep "\"${ARCH}\".*\"${SUITE}\"" | grep -oE 'sha256:[a-f0-9]{64}')
test -n "${manifest}"
is_ok "fail" "done" || return 1
msg "Retrieving rootfs blobs: "
wget -q -O - --header "Authorization: Bearer ${token}" "https://registry-1.docker.io/v2/${image}/manifests/${manifest}" | tr '\n' ' ' | tr '{}' '\n' | grep 'tar.gzip.*digest' | grep -oE 'sha256:[0-9a-f]{64}' | while read digest
do
msg -n " * ${digest} ... "
wget -q -O - --header "Authorization: Bearer ${token}" "https://registry-1.docker.io/v2/${image}/blobs/${digest}" | tar xz -C "${CHROOT_DIR}"
is_ok "fail" "done"
done

return 0
}

do_help()
{
cat <<EOF
--arch="${ARCH}"
Architecture of the container, supported "arm", "arm64", "386" and "amd64".
--suite="${SUITE}"
OS of the container, supported "linux".
--source-path="${SOURCE_PATH}"
Installation source, can specify name and version of the container.
EOF
}
2 changes: 1 addition & 1 deletion include/bootstrap/fedora/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ then
esac
fi

[ -n "${SOURCE_PATH}" ] || SOURCE_PATH="http://dl.fedoraproject.org/pub/archive"
[ -n "${SOURCE_PATH}" ] || SOURCE_PATH="http://dl.fedoraproject.org/pub/archive/"

dnf_install()
{
Expand Down
6 changes: 3 additions & 3 deletions include/bootstrap/rootfs/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ rootfs_make()
fi

if [ "${TARGET_TYPE}" = "ram" ]; then
if [ ! -e "${TARGET_PATH}" ]; then
mkdir "${TARGET_PATH}"
fi
umount "${TARGET_PATH}"
if [ -z "${DISK_SIZE}" -o "${DISK_SIZE}" -le 0 ]; then
local ram_free=$(grep ^MemFree /proc/meminfo | awk '{print $2}')
let DISK_SIZE="${ram_free}/1024"
fi
msg -n "Making new disk image (${DISK_SIZE} MB) ... "
if [ ! -e "${TARGET_PATH}" ]; then
mkdir "${TARGET_PATH}"
fi
mount -t tmpfs -o size="${DISK_SIZE}M" tmpfs "${TARGET_PATH}"
is_ok "fail" "done" || return 1
fi
Expand Down
32 changes: 29 additions & 3 deletions include/core/emulator/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@
# Linux Deploy Component
# (c) Anton Skshidlevsky <[email protected]>, GPLv3

get_qemu()
{
local arch="$1"
local qemu=""
local host_platform=$(get_platform)
local guest_platform=$(get_platform "${arch}")
if [ "${host_platform}" != "${guest_platform}" ]; then
case "${guest_platform}" in
arm) qemu="qemu-arm-static" ;;
arm_64) qemu="qemu-aarch64-static" ;;
x86) qemu="qemu-i386-static" ;;
x86_64) qemu="qemu-x86_64-static" ;;
*) qemu="" ;;
esac
fi
echo ${qemu}
}

[ -n "${EMULATOR}" ] || EMULATOR=$(get_qemu ${ARCH})

do_configure()
Expand Down Expand Up @@ -90,6 +108,14 @@ do_stop()
msg "skip"
fi
;;
qemu-x86_64*)
if [ -e "/proc/sys/fs/binfmt_misc/qemu-x86_64" ]; then
echo -1 > /proc/sys/fs/binfmt_misc/qemu-x86_64
is_ok "fail" "done"
else
msg "skip"
fi
;;
qemu-arm*)
if [ -e "/proc/sys/fs/binfmt_misc/qemu-arm" ]; then
echo -1 > /proc/sys/fs/binfmt_misc/qemu-arm
Expand All @@ -98,9 +124,9 @@ do_stop()
msg "skip"
fi
;;
qemu-mipsel*)
if [ -e "/proc/sys/fs/binfmt_misc/qemu-mipsel" ]; then
echo -1 > /proc/sys/fs/binfmt_misc/qemu-mipsel
qemu-aarch64*)
if [ -e "/proc/sys/fs/binfmt_misc/qemu-aarch64" ]; then
echo -1 > /proc/sys/fs/binfmt_misc/qemu-aarch64
is_ok "fail" "done"
else
msg "skip"
Expand Down

0 comments on commit 95f7f2c

Please sign in to comment.