Basic LXC Snippets
@ ヨハネス · Saturday, Jun 20, 2020 · 1 minute read · Update at Feb 3, 2021

Some basic LXC snippets that keep recurring.

Motivation

I keep forgetting the exact syntax of these commands, so than I have to use a lot of history grepping to know what I did last time. Time to write some useful snippets instead.

Preparations

Set a few variables so the commands run smooth. The container will be started under this name, and when importing, this name is expected to be present as a tar.gz file.

CONTAINER="my_container"
echo $CONTAINER

Exporting an image

This can be used to transfer the container as a whole to another host or back up the entire thing

SNAPSHOT="$CONTAINER/latest"
IMAGENAME="$CONTAINER""_cont"
echo "Deleting previous snapshot.."
lxc -v delete $SNAPSHOT
echo "Creating snapshot.."
lxc -v snapshot "$CONTAINER" latest
echo "Publish snapshot.."
lxc -v publish $SNAPSHOT --alias $IMAGENAME
echo "Exporting container as tar archive.."
lxc -v image export $IMAGENAME "./$CONTAINER"

Importing an image

This can be used to import an existing container tar.gz archive

IMAGENAME="$CONTAINER""_cont"
IMPORTNAME="$CONTAINER"".tar.gz"
echo "Deleting previous image.."
lxc -v image delete $IMAGENAME
echo "Importing image.."
lxc -v image import $IMPORTNAME --alias $IMAGENAME
echo "Launching container.."
lxc -v launch $IMAGENAME $CONTAINER

Social Links