返回
Featured image of post ArchLinux 安装 LXD

ArchLinux 安装 LXD

安装 LXD 并创建容器

我的系统是 Archlinux, 但是有时候测试软件或者编译的时候需要 Debian 或者 Ubuntu 系统,尝试过虚拟机方案,虽然有 KVM 的加持,基本感受不到性能损失,但是磁盘遭不住了,创建几个虚拟机之后就占用一大半。然后看到 virt-manager 里面 lxc,了解到了 LXD。

安装

不得不说 Archlinux 的软件仓库是真的香

sudo pacman -S lxd

立即启动 lxd 并开机自启动

sudo systemctl enable --now lxd

赋予当前用户直接操作 lxd 的权限

sudo usermod -a -G lxd $USER
newgrp lxd #立即生效

初始化

如果没有其他需求默认就行,一路回车

$ sudo 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, lvm) [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]:

添加国内镜像

这里用的是北京外国语大学的镜像,可以改成清华大学的镜像

lxc remote add mirrors https://mirrors.bfsu.edu.cn/lxc-images/ --protocol=simplestreams --public
lxc image list mirrors:

启用非特权模式和用户空间 Idmap

echo "root:100000:65536
$USER:100000:65536" | sudo tee -a /etc/subuid /etc/subgid
echo "lxc.idmap = u 0 100000 65536
lxc.idmap = g 0 100000 65536" | sudo tee -a /etc/lxc/default.conf

注意: 对进行此配置之前创建的容器无效,具体表现为非 root 用户无法启动该容器

重启 lxd

sudo systemctl restart lxd

创建容器进行测试

这里创建了一个名为 debian 的 Debian 11 容器

lxc launch mirrors:debian/11 debian

进入终端

lxc exec debian -- /bin/bash

容器端口映射到主机

添加映射

lxc config device add <容器名> <映射名> proxy listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80

删除映射

lxc config device remove <容器名> <映射名>

其他

LXD 还有容器嵌套,NVIDIA GPU 共享等功能,可以自己去 Google 探索

Q: 为什么用 LXD 而不是 Docker
A: Docker 注重的是应用程序分发,开箱即用,而 LXD 注重的是命名空间隔离,所以 LXD 容器更适合去当作一个独立的系统去用

Built with Hugo
Theme Stack designed by Jimmy