Last update: 01 November 2022 - Alpine 3.16 has lxd packages in contrib
Lxc is a great technology. I’ve been using it since 2012 and I’ve always appreciated its lightweight approach and efficiency. Nowadays KVM and hardware virtualization extensions have reached a very good level of performance. While a KVM VM allows to run another kernel and another OS, it ties specific resources to the VM and this isn’t always the best solution. Lxc uses the host’s kernel and shares its ram so the host machine runs lxc container’s processes as local processes - no slowdown, no resource wasting.
Alpine Linux is a great distribution. It’s minimal, reliable, secure and easy to deploy. It’s great for containers but it’s also great for low footprint servers, like hypervisors. It’s good as Xen Dom0, it’s good as KVM host and - no surprise - is great as Lxc host too.
Alpine Linux is great for low energy devices (Raspberry Pis, etc.), too, and has an efficient and reliable out-of-the-box read-only file system support. But I’ll write more about it in another post.
Lxd is a great solution. It uses lxc but simplifies a lot of operations like new container creation, profile management, automatic snapshots, clustering, etc. The “official” best option to use lxd is installing Ubuntu, Snap and so on. I don’t want to use Ubuntu to install Snap. I don’t want to use Snap just to install lxd. That’s why I’m choosing to use lxd with Alpine.
Long story short: this video shows all the steps described below:
Installing Alpine Linux on BTRFS Root File System
Alpine Linux installer supports btrfs as rootfs destination but the setup-alpine command doesn’t allow you to select it: it will partition your disk and create a ext4 standard root file system.
So let’s start with the installation but, then, we’ll interrupt it at disk selection:
Start with setup-alpine and set network, hostname, password, etc. Stop at:
Available disks are:
sda (20.5 GB QEMU QEMU HARDDISK )
Which disk(s) would you like to use? (or '?' for help or 'none') [none]
Press ctrl+C and interrupt the script.
Now we can install Alpine Linux on btrfs root system:
test:~# export ROOTFS=btrfs
And, then:
test:~# setup-disk /dev/sda
The following disk is selected:
sda (20.5 GB QEMU QEMU HARDDISK )
How would you like to use it? ('sys', 'data', 'lvm' or '?' for help) [?] sys
WARNING: The following disk(s) will be erased:
sda (20.5 GB QEMU QEMU HARDDISK )
WARNING: Erase the above disk(s) and continue? (y/n) [n] y
Creating file systems...
btrfs-progs v5.10
See http://btrfs.wiki.kernel.org for more information.
Label: (null)
UUID: 6ace888a-2f36-41e8-86b1-3e4214b9e34e
Node size: 16384
Sector size: 4096
Filesystem size: 15.17GiB
Block group profiles:
Data: single 8.00MiB
Metadata: DUP 256.00MiB
System: DUP 8.00MiB
SSD detected: no
Incompat features: extref, skinny-metadata
Runtime features:
Checksum: crc32c
Number of devices: 1
Devices:
ID SIZE PATH
1 15.17GiB /dev/sda3
Installing system on /dev/sda3:
/mnt/boot is device /dev/sda1
Installation is complete. Now eject the installation media and reboot.
After rebooting, let’s check that root file system is on btrfs:
test:~# cat /etc/fstab
UUID=aacbfd28-df47-4945-9c0c-279faef361a0 / btrfs rw,relatime,space_cache,subvolid=5,subvol=/ 0 1
Ok, it is. Generally speaking, I prefer to use compression on btrfs. compress=lzo is enough, so you can add it on your fstab.
Now, let’s install lxd.
Edit your /etc/apk/repositories and let it be like this:
http://dl-cdn.alpinelinux.org/alpine/v3.16/main
http://dl-cdn.alpinelinux.org/alpine/v3.16/community
Let’s follow the tutorial I wrote on Alpine Linux wiki and let’s install lxc, lxd, lxcfs and dbus (not needed but recommended to avoid problems with some containers).
After rebooting, you have a fully functional lxd-enabled Alpine Linux installation.
Let’s now configure lxd:
test:~# lxd init
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 (btrfs, dir) [default=btrfs]:
Would you like to create a new btrfs subvolume under /var/lib/lxd? (yes/no) [default=yes]:
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 the LXD server 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]:
As you can see, lxd automatically detected that /var/lib/lxd is on a btrfs file system and creates a sub volume.
Now we can launch our first container:
test:~# lxc launch images:alpine/3.16 t1
Creating t1
Starting t1
Ok, done. Let’s check if it’s working:
test:~# lxc info t1
Name: t1
Status: RUNNING
Type: container
Architecture: x86_64
PID: 2648
Created: 2021/11/03 08:55 UTC
Last Used: 2021/11/03 08:55 UTC
Resources:
Processes: 5
CPU usage:
CPU usage (in seconds): 0
Memory usage:
Memory (current): 3.38MiB
Memory (peak): 5.04MiB
Network usage:
eth0:
Type: broadcast
State: UP
Host interface: vethdc207601
MAC address: 00:16:3e:50:d1:ef
MTU: 1500
Bytes received: 3.76kB
Bytes sent: 1.79kB
Packets received: 26
Packets sent: 16
IP addresses:
inet: 10.189.248.21/24 (global)
inet6: fd42:34cb:9408:433c:216:3eff:fe50:d1ef/64 (global)
inet6: fe80::216:3eff:fe50:d1ef/64 (link)
lo:
Type: loopback
State: UP
MTU: 65536
Bytes received: 0B
Bytes sent: 0B
Packets received: 0
Packets sent: 0
IP addresses:
inet: 127.0.0.1/8 (local)
inet6: ::1/128 (local)
It’s running. Everything is ok. For more complex lxc setup (bridges, profiles, etc.) you can follow the excellent lxd documentation.
This kind of setup is easy and lightweight, easy to backup (a new article will follow) and reliable.