Build LXD container

LXD can create a virtual container.

LXD Init

Initial settings. Create bridge interface and zfs partition.

Initial server setup with Ubuntu20.04

ZFS partition is no format, no mount.

lxd init
1. Would you like to use LXD clustering? (yes/no) [default=no]:
2. Do you want to configure a new storage pool? (yes/no) [default=yes]:
3. Name of the new storage pool [default=default]: lxd
4. Name of the storage backend to use (btrfs, dir, lvm, zfs) [default=zfs]:
5. Create a new ZFS pool? (yes/no) [default=yes]:
6. Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]: yes
7. Path to the existing block device: /dev/ubuntu-vg/lxd
7.) Size in GB of the new loop device (1GB minimum) [default=6GB]:
8. Would you like to connect to a MAAS server? (yes/no) [default=no]:
9. Would you like to create a new local network bridge? (yes/no) [default=yes]: no
10. Would you like to configure LXD to use an existing bridge or host interface? (yes/no) [default=no]: yes
11. Name of the existing bridge or host interface: br2
12. Would you like LXD to be available over the network? (yes/no) [default=no]:
13. Would you like stale cached images to be updated automatically? (yes/no) [default=yes]
14. Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: yes

ブリッジインターフェイスが複数あるなら

lxc profile edit default
devices:
  eth0:
    name: eth0
    nictype: bridged
    parent: br0
    type: nic
  eth1:
    name: eth1
    nictype: bridged
    parent: br1
    type: nic
  eth2:
    name: eth2
    nictype: bridged
    parent: br2
    type: nic

Create containe

Build, boot, login

lxc init ubuntu:20.04 dns4-loog
lxc start dns4-loog
lxc exec dns4-loog bash

Log in and make initial settings.

# Network configulation
vi /etc/netplan/99-netplan.yaml
network:
version: 2
ethernets:
    eth0:
        dhcp4: false
        dhcp6: true
        addresses: [192.168.8.47/24]
    eth1:
        dhcp4: false
        dhcp6: true
        addresses: [192.168.9.47/24]
    eth2:
        dhcp4: false
        addresses: [192.168.24.47/24]
        gateway4: 192.168.24.9
        nameservers:
            addresses: [8.8.8.8]

# Apply
netplan apply

timedatectl set-timezone Asia/Tokyo

apt update
apt upgrade -y

Image backup and restore

// snapshot 
lxc snapshot dns4-loog

// infomation
lxc info dns4-loog

// Create image (If you don't specify a snapshot, the container will pause)
lxc publish dns4-loog/snap0 --alias dns4-loog-img

// Output the image as an archive file (tar.gz)
lxc image export dns4-loog-img

restore

// restore snapshot
lxc restore dns4-loog snap0

// Create image from archive file.
lxc image import 0ba...s.tar.gz --alias dns4-centwo-img

// Create container from image.
lxc init dns4-centwo-img dns4-centwo

// Delete container or snapshot.
lxc delete dns4-centwo          // Be carefull !
lxc delete dns4-centwo/snap0