KaliLinux 2018.2: Install LXD for container

This article will describe installing LXD and running containers.

Table of Contents

1 Install LXD

Install snapd package with apt.

#!/bin/sh -e

# Enable AppArmor.
sudo systemctl enable apparmor
sudo systemctl start apparmor

# Install snapd.
sudo apt install -y snapd
sudo systemctl enable snapd
sudo systemctl start snapd
sudo sed -i /etc/sudoers \
     -e 's;secure_path="\(.*\)";secure_path=\1:/snap/bin;g'

Install lxd with snap.

#!/bin/sh -e

# Install LXD with snap.
sudo snap install lxd
sudo addgroup --system lxd

# Add user to lxd group.
sudo gpasswd -a "${USER}" lxd
sudo reboot

Initialize lxd.

#!/bin/sh


# Would you like to use LXD clustering? (yes/no) [default=no]: no
# Do you want to configure a new storage pool? (yes/no) [default=yes]: yes
# Name of the new storage pool [default=default]: default
# Name of the storage backend to use (dir, lvm, zfs) [default=zfs]: dir
# Would you like to connect to a MAAS server? (yes/no) [default=no]: no
# Would you like to create a new network bridge? (yes/no) [default=yes]: yes
# What should the new bridge be called? [default=lxdbr0]: lxdbr0
# What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”)
# [default=auto]: auto
# What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”)
# [default=auto]: none
# Would you like LXD to be available over the network? (yes/no)
# [default=no]: no
# Would you like stale cached images to be updated automatically? (yes/no)
# [default=yes] yes
# Would you like a YAML "lxd init" preseed to be printed? (yes/no)
# [default=no]: no

# Initialize LXD with NAT network.
lxd waitready
cat <<EOF | lxd init
no
yes
default
dir
no
yes
lxdbr0
auto
none
no
yes
no
EOF

2 Run container

Run Debian 9 container with lxd.

#!/bin/sh -e


# Wait that LXD daemon is up.
lxd waitready

# Create and start debian-9.
lxc launch images:debian/stretch debian-9

# Wait that network interface is up.
for trial in $(seq 30); do
  if lxc exec debian-9 ip a s eth0 | grep 'inet ' > /dev/null; then
    break
  fi
  sleep 1
done

# Check whether network interface can be up or not.
if [ "${trial}" -eq 30 ]; then
  echo "Network interface cannot be up"
  exit 1
fi

# Show network interface.
lxc exec debian-9 ip a s