Skip to content

Instantly share code, notes, and snippets.

@ntrepid8
Last active February 22, 2022 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntrepid8/e60a64702c6589d94f845a52e67318e1 to your computer and use it in GitHub Desktop.
Save ntrepid8/e60a64702c6589d94f845a52e67318e1 to your computer and use it in GitHub Desktop.
LXC/LXD Notes

LXC/LXD Notes

Some notes on how to install and configure lxc/lxd containers.

Read the 'getting started' docs first:

Lots of good examples:

Links:

These notes were created for LXD version 2.21:

$ lxc --version
2.21

$ lxd --version
2.21

Install

Install on Ubuntu 16.04 like this:

$ sudo apt install zfsutils-linux
$ sudo apt install -t xenial-backports lxd lxd-client

It's important to use the backports repo to get a version new enough to have networking configuration built-in. Only versions >= 2.3 have the new networking.

After installation run the configuration script, defaults are fine:

$ sudo lxd init
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, zfs) [default=zfs]: 
Create a new ZFS pool (yes/no) [default=yes]? 
Would you like to use an existing block device (yes/no) [default=no]? 
Size in GB of the new loop device (1GB minimum) [default=43GB]: 
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 to create a new 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]? 
LXD has been successfully configured.

Links:

Launch first container

$ lxc launch ubuntu:xenial first

Get a shell

As a normal user:

$ lxc exec ${container-name} -- sudo --login --user ubuntu

As root:

$ lxc exec ${container-name} -- /bin/bash

Static DHCP Lease

Attach a static list to a single container like this:

$ lxc network attach lxdbr0 ${container-name} eth0
$ lxc config device set ${container-name} eth0 ipv4.address 10.233.40.2

Links:

Autostart at boot

$ lxc config set ${container-name} boot.autostart true

Links:

Snapshots

Take a snapshot:

$ lxc snapshot ${container-name} ${snapshot-name}

List snapshots:

$ lxc info ${container-name}

Restore a snapshot:

$ lxc restore ${container-name} ${snapshot-name}

Links:

Add physical NIC to container

Delegate an entire NIC to a container like this:

$ lxc config device add ${container-name} wlp1s0 nic name=wlp1s0 nictype=physical parent=wlp1s0

Then configure as normal with wpasupplicant and wireless-tools:

If the eth0 interface needs to be disabled on boot in Ubuntu 16.04 add this file:

/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

With the following contents:

network: {config: disabled}

Notes:

  • I also had to configure the default route since it seemed to not exist anymore when I disabled eth0.
  • It seems to be important to use the same device name in the container as in the host

Push a file

$ lxc file push path/to/source ${container-name}/path/to/dest

Pull a file

$ lxc file pull ${container-name}/path/to/source path/to/dest

Mapping folders from the host into the container

Images

Use images to create backups that can be exported to a file or pushed to anther LXD server. To create an image, first create a snapshot:

$ lxc snapshot riverpl backup-2022-02-22

Next "publish" that snapshot as an image (it wont't be public):

$ lxc publish riverpl/backup-2022-02-22 --alias riverpl_backup_2022-02-22 description="Backup on 2022-02-22"

Finally, export the published image as a tarball:

$ lxc image export riverpl_backup_2022-02-22 ~/backups/riverpl_backup_2022-02-22.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment