summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorParide Legovini <paride@ubuntu.com>2022-05-04 23:35:08 +0200
committerGitHub <noreply@github.com>2022-05-04 16:35:08 -0500
commitd1149988323bb88ec8ee6aa14bdd9d0618582771 (patch)
treed92f1f52bcf8851925c05e6def6809f0df77291f
parent76981572c36912ec9ec5b0c6616e3027714df37a (diff)
downloadcloud-init-git-d1149988323bb88ec8ee6aa14bdd9d0618582771.tar.gz
run-container: add support for LXD VMs (#1428)
Images of older releases (e.g. CentOS 7) can't run as containers on newer Ubuntu releases because they need a CGroupV1 host system. They can however run as VMs.
-rwxr-xr-xtools/run-container25
1 files changed, 18 insertions, 7 deletions
diff --git a/tools/run-container b/tools/run-container
index e049dfdc..182db0e9 100755
--- a/tools/run-container
+++ b/tools/run-container
@@ -38,6 +38,7 @@ Usage: ${0##*/} [ options ] [images:]image-ref
-p | --package build a binary package (.deb or .rpm)
-s | --source-package build source package (debuild -S or srpm)
-u | --unittest run unit tests
+ --vm use a VM instead of a container
Example:
* ${0##*/} --package --source-package --unittest centos/6
@@ -50,7 +51,7 @@ cleanup() {
if [ "$KEEP" = "true" ]; then
error "not deleting container '$CONTAINER' due to --keep"
else
- delete_container "$CONTAINER"
+ delete_instance "$CONTAINER"
fi
fi
}
@@ -340,6 +341,12 @@ wait_inside() {
wait_for_boot() {
local name="$1"
local out="" ret="" wtime=$DEFAULT_WAIT_MAX
+ local system_up=false
+ for i in {0..30}; do
+ [ "$i" -gt 1 ] && sleep 5
+ inside "$name" true 2>/dev/null && system_up=true && break
+ done
+ [ $system_up == true ] || { errorrc "exec command inside $name failed."; return; }
get_os_info_in "$name"
[ "$OS_NAME" = "debian" ] && wtime=300 &&
debug 1 "on debian we wait for ${wtime}s"
@@ -360,10 +367,12 @@ wait_for_boot() {
fi
}
-start_container() {
- local src="$1" name="$2"
+start_instance() {
+ local src="$1" name="$2" use_vm="$3"
debug 1 "starting container $name from '$src'"
- lxc launch "$src" "$name" || {
+ launch_flags=()
+ [ "$use_vm" == true ] && launch_flags+=(--vm)
+ lxc launch "$src" "$name" "${launch_flags[@]}" || {
errorrc "Failed to start container '$name' from '$src'";
return
}
@@ -371,7 +380,7 @@ start_container() {
wait_for_boot "$name"
}
-delete_container() {
+delete_instance() {
debug 1 "removing container $1 [--keep to keep]"
lxc delete --force "$1"
}
@@ -391,7 +400,7 @@ run_self_inside_as_cd() {
main() {
local short_opts="a:hknpsuv"
- local long_opts="artifacts:,dirty,help,keep,name:,package,source-package,unittest,verbose"
+ local long_opts="artifacts:,dirty,help,keep,name:,package,source-package,unittest,verbose,vm"
local getopt_out=""
getopt_out=$(getopt --name "${0##*/}" \
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
@@ -401,6 +410,7 @@ main() {
local cur="" next=""
local package=false srcpackage=false unittest="" name=""
local dirty=false artifact_d="."
+ local use_vm=false
while [ $# -ne 0 ]; do
cur="${1:-}"; next="${2:-}";
@@ -414,6 +424,7 @@ main() {
-s|--source-package) srcpackage=true;;
-u|--unittest) unittest=1;;
-v|--verbose) VERBOSITY=$((VERBOSITY+1));;
+ --vm) use_vm=true;;
--) shift; break;;
esac
shift;
@@ -443,7 +454,7 @@ main() {
trap cleanup EXIT
- start_container "$img_ref" "$name" ||
+ start_instance "$img_ref" "$name" "$use_vm" ||
{ errorrc "Failed to start container for $img_ref"; return; }
get_os_info_in "$name" ||