Table of Contents

LXC - Install LXC

sudo snap install lxd

or

sudo apt install lxc 

NOTE: The snap version will most likely be more up to date.


Install LXC Utils

sudo apt install lxc-utils lxc-templates

Get Info

lxc info

returns:

If this is your first time running LXD on this machine, you should also run: lxd init
To start your first instance, try: lxc launch ubuntu:18.04

Configure LXD

Before you can create an instance, you need to configure LXD.

lxd init

NOTE: This will prompt for how to configure it. Just take the defaults for now.

  • Would you like to use LXD clustering? (yes/no) [default=no]:
  • Do you want to configure a new storage pool? (yes/no) [default=yes]:
  • Name of the new storage pool [default=default]:
  • Name of the storage backend to use (dir, lvm, ceph, btrfs) [default=btrfs]:
  • Create a new BTRFS pool? (yes/no) [default=yes]:
  • Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]:
  • Size in GB of the new loop device (1GB minimum) [default=30GB]:
  • Would you like to connect to a MAAS server? (yes/no) [default=no]:
  • Would you like to create a new local network bridge? (yes/no) [default=yes]:
  • What should the new bridge be called? [default=lxdbr0]:
  • What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
  • What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
  • Would you like LXD to be available over the network? (yes/no) [default=no]:
  • Would you like stale cached images to be updated automatically? (yes/no) [default=yes]
  • Would you like a YAML “lxd init” preseed to be printed? (yes/no) [default=no]:

Create ~/.config/lxc

mkdir ~/.config/lxc

Create ~/.config/lxc/default.conf

~/.config/lxc/default.conf
lxc.include = /etc/lxc/default.conf
lxc.idmap = u 0 100000 65536
lxc.idmap = g 0 100000 65536

Test

lxc-create -t download -n test

NOTE: The download option is used to display a list of all available images, allowing one to be selected.

NOTE: An error may be seen:

Setting up the GPG keyring
ERROR: Unable to fetch GPG key from keyserver
lxc-create: test: lxccontainer.c: create_run_template: 1626 Failed to create container from template
lxc-create: test: tools/lxc_create.c: main: 319 Failed to create container test

Try using the following:

lxc-create -t download -n test -- --keyserver hkp://p80.pool.sks-keyservers.net:80

or

lxc-create -n test -t download -- --no-validate

returns:

Setting up the GPG keyring
Downloading the image index
 
---
DIST	RELEASE	ARCH	VARIANT	BUILD
---
alpine	3.10	amd64	default	20210103_13:00
alpine	3.10	arm64	default	20210103_13:00
alpine	3.10	armhf	default	20210103_13:00
alpine	3.10	i386	default	20210103_13:00
...
centos	8	amd64	default	20210103_07:41
centos	8	arm64	default	20210103_07:08
centos	8	ppc64el	default	20210103_07:08
debian	bullseye	amd64	default	20210103_05:24
debian	bullseye	arm64	default	20210103_05:24
...
ubuntu	trusty	arm64	default	20210103_07:42
ubuntu	trusty	armhf	default	20210103_07:42
ubuntu	trusty	i386	default	20210103_07:42
ubuntu	trusty	ppc64el	default	20210103_07:42
ubuntu	xenial	amd64	default	20210103_07:42
ubuntu	xenial	arm64	default	20210103_07:43
ubuntu	xenial	armhf	default	20210103_07:43
...

Select which image to use

In this example the following is chosen:

Distribution: 
ubuntu
Release: 
xenial
Architecture: 
amd64
 
Downloading the image index
Downloading the rootfs
Downloading the metadata
The image cache is now ready
Unpacking the rootfs
 
---
 
You just created an Ubuntu xenial amd64 (20210103_07:42) container.
 
To enable SSH, run: apt install openssh-server
No default root or user password are set by LXC.

Check the container status

lxc-info -n test

returns:

Name:           test
State:          STOPPED

Start the Container

lxc-start -n test

NOTE: The following error may be seen:

lxc-start: test: lxccontainer.c: wait_on_daemonized_start: 851 Received container state "ABORTING" instead of "RUNNING"
lxc-start: test: tools/lxc_start.c: main: 308 The container failed to start
lxc-start: test: tools/lxc_start.c: main: 311 To get more details, run the container in foreground mode
lxc-start: test: tools/lxc_start.c: main: 313 Additional information can be obtained by setting the --logfile and --logpriority options

By default, unprivileged users are not allowed to create any network device on the host. To fix:

/etc/lxc/lxc-usernet
peter veth lxcbr0 10

NOTE: This means that peter is allowed to create up to 10 veth devices connected to the lxcbr0 bridge.


Check the container status again

lxc-info -n test

returns:

Name:           test
State:          RUNNING
PID:            793414
IP:             10.0.3.101
Link:           veth1000_qkOr
 TX bytes:      1.96 KiB
 RX bytes:      11.54 KiB
 Total bytes:   13.49 KiB

NOTE: This shows it is running.


Get a Shell inside the Container

lxc-attach -n test

NOTE: Enter exit to exit the Container Shell, back to the host.


Stop the Test Container

lxc-stop -n test

Remove the Test Container

lxc-destroy -n test

References

https://linuxcontainers.org/lxd/docs/master/

https://linuxcontainers.org/lxd/getting-started-cli/