Table of Contents
- Technical Report: Rapid Prototyping System using Btrfs and Systemd-nspawn
Technical Report: Rapid Prototyping System using Btrfs and Systemd-nspawn
Introduction
The purpose of this technical report is to outline the architecture, strategies, and implementation details for a rapid prototyping system that leverages btrfs, systemd-nspawn, and overlay filesystems. The system aims to improve space efficiency, facilitate easy reconfiguration, and solve partial upgrade problems in a composable and resilient manner.
System Architecture
Btrfs Filesystem Setup
-
Subvolumes:
@init: An empty subvolume used as a template.@base: Contains the root filesystem.@overlay: Contains overlay configurations for testing various applications and system features.
-
Loop Device:
- The btrfs filesystem is mounted as a loop device from a file residing on tmpfs, to leverage the benefits of RAM (speed and volatility for rapid prototyping).
Systemd-nspawn Containers
-
Base Container:
- Utilizes
@basefor the root filesystem. - Provides a clean, up-to-date environment that other overlays can be tested against.
- Utilizes
-
Overlay Containers:
- Use
@overlaysubvolumes that contain specific configurations and applications for testing purposes.
- Use
Space Efficiency and Reflink Copy
Using the cp --reflink command, we can create space-efficient copies of the subvolumes, which allows for rapid reconstruction and reconfiguration without consuming additional disk space.
Implementation Details
Setting up Btrfs Subvolumes
# Create btrfs subvolume
btrfs subvolume create /mnt/mybtrfs/@init
btrfs subvolume create /mnt/mybtrfs/@base
btrfs subvolume create /mnt/mybtrfs/@overlay
# Mount btrfs subvolumes using systemd-nspawn
systemd-nspawn --directory=/mnt/mybtrfs/@base
# Use reflink to create space-efficient copies
cp --reflink=always /mnt/mybtrfs/@base /mnt/mybtrfs/@overlay/base01
Supporting Multiple Overlays
The flexibility of btrfs and overlayfs allows for the combination of different subvolumes, enabling a composable configuration.
# Create overlayfs mount
mount -t overlay overlay -o lowerdir=/mnt/mybtrfs/@base,upperdir=/mnt/mybtrfs/@overlay/base01,workdir=/mnt/mybtrfs/@workdir /mnt/overlayfs_root
OverlayFS for Layered Configuration
OverlayFS allows us to layer subvolumes, creating a union of multiple directories:
mount -t overlay overlay -o lowerdir=/mnt/btrfs/@base,upperdir=/mnt/btrfs/@overlay,workdir=/mnt/btrfs/work /mnt/overlay
This setup allows the overlay subvolume to compose over the base, providing a testing ground for new features without altering the base.
Example Ansible Playbook for Overlay Configuration
---
- name: Apply configuration to @overlay
hosts: localhost
tasks:
- name: Ensure package is installed
pacman:
name: "{{ item }}"
state: present
with_items:
- nginx
- postgresql
- name: Configure nginx
copy:
src: nginx.conf
dest: /mnt/overlayfs_root/etc/nginx/nginx.conf
Updating the @base and Reflink Copies
To address partial upgrade issues, the @base subvolume is updated periodically. New updates are applied to @overlay by creating a fresh reflink copy of @base.
# Update @base
pacman -Syu --root /mnt/mybtrfs/@base
# Create new reflinked overlay
cp --reflink=always /mnt/mybtrfs/@base /mnt/mybtrfs/@overlay/base02
# Apply playbooks to new overlay
ansible-playbook -i localhost playbook.yml
Exploring Btrfs Seeding and NBD
Btrfs Seeding
Btrfs seeding allows the creation of a read-only seed device that can be used to populate a file system.
btrfs device add -f /dev/seed-device /mnt/mybtrfs
btrfs device remove /dev/seed-device /mnt/mybtrfs
Network Block Device (NBD)
For live systems, NBD can be used to share updates over the network.
modprobe nbd
qemu-nbd -c /dev/nbd0 /path/to/btrfs.img
mount /dev/nbd0 /mnt/nbd
Conclusion
This architecture empowers rapid prototyping by leveraging btrfs subvolumes, reflink copies, and systemd-nspawn containers. It ensures space efficiency and resilience through overlay filesystems and can be extended to live environments using NBD. The solution also addresses partial upgrade issues by allowing updates to @base and reconstructing overlays efficiently. Future improvements can involve further automation and exploration of btrfs seeding for optimized deployment scenarios.
References
- Btrfs Documentation: https://btrfs.wiki.kernel.org/index.php/Main_Page
- Systemd-nspawn Manual: https://www.freedesktop.org/software/systemd/man/systemd-nspawn.html
- Arch Linux Pacman Documentation: https://wiki.archlinux.org/title/pacman
Orchestration Architect Overview
Tasks
Repository Structure
Branch 0.8.0-assets
Assets Branch - Stores assets and relevant metadata
This needs to be added to LFS and tied to assets branch.
Branch 0.8.0-seed
The minimal or last previous version required files to migrate/build the pristine environment. This process is to be automated with a CI/CD pipeline.
Branch 0.8.0-pristine
The workspace after bootstrapping the 0.8.0-seed branch. Updated via diffs between pristine and the bootstrapped 0.8.0-seed
Branch 0.8.0-dev
WIP filling this in