Using LXD on Ubuntu 16.04

1. install lxd

$ sudo apt install lxd

2. Enable swap accounting

$ sudo vi /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="swapaccount=1"
$ sudo update-grub
$ sudo shutdown -r now

3. create lxd user

$ sudo useradd -s /bin/bash -m lxdadm
$ sudo passwd lxdadm
$ sudo adduser lxdadm lxd

4. init lxd

$ sudo lxd init

5. Creating the first container

$ sudo su - lxdadm
$ newgrp lxd

// create container ubuntu16
$ lxc launch ubuntu:xenial ubuntu16

// list lxc containers
$ lxc list
$ lxc info ubuntu16

// open a shell in ubuntu16
$ lxc exec ubuntu16 bash

6. Create container without starting it

$ lxc init ubuntu:xenial ubuntu16

6. List images

$ lxc image list
$ lxc image list images:
$ lxc image list ubuntu:

7. List containers

$ lxc list
$ lxc list --fast
$ lxc info <container>

8. Start/stop a container

$ lxc start 
$ lxc stop <container>
$ lxc stop  <container> --force
$ lxc restart <containter>
$ lxc restart <container> --force
$ lxc pause <container>

9. Profiles

$ lxc profile list
$ lxc profile show <profile>
$ lxc profile edit <profile>
$ lxc profile apply <container> <profile1>,<profile2>,...

10. Shell

$ lxc exec <container> bash
$ lxc exec <container> -- ls -lh /
$ lxc exec <container> --env mykey=myvalue

11. Files

$ lxc file pull <container>/<path> <dest>
$ lxc file pull <container>/<path> - //read file to standard output
$ lxc file push <source> <container>/<path>
$ lxc file edit <container>/<path>

11. Snapshot

$ lxc snapshot <container>
$ lxc snapshot <container> <snapshot name>
$ lxc info <container> //see snapshot
$ lxc restore <container> <snapshot name>
$ lxc move <container>/<snapshot name> <container>/<new snapshot name>
$ lxc delete <container>/<snapshot name>

12. Cloning/renaming/delting

$ lxc copy <source container> <destination container> 
$ lxc move <old name> <new name>  
$ lxc delete <container>

13. CPU limit

$ lxc config set my-container limits.cpu 2  //any 2 cpus

$ lxc config set my-container limits.cpu 1,3 // cpu #2 #4

$ lxc config set my-container limits.cpu 0-3,7-11

$ lxc config set my-container limits.cpu.allowance 10% // limit time 10% of total

$ lxc config set my-container limits.cpu.priority 0

14. Memory limit

$ lxc config set my-container limits.memory 256MB

15. Disk limit (requires btrfs or ZFS)

$ lxc config device set my-container root size 20GB

16. IO reading/writing limits

$ lxc config device set my-container root limits.read 20Iops
$ lxc config device set my-container root limits.write 10Iops

17. Autostart container

$ lxc config set container_name boot.autostart 1

18. Mount host directory

$ lxc config device add container_name device_name disk source=host_directory path=guest_directory