使用与主机操作系统类似的映像创建LXC容器:
root@node51 [~]# lxc launch images:ubuntu/18.04 demo -s rpool
Creating demo
Starting demo
停止容器:
root@node51 [~]# lxc stop demo
挂载LXC存储卷,以便我们可以从中获取一些元数据:
root@node51 [~]# zfs mount rpool/lxd/containers/demo
将元数据复制到某个地方(比如 /tmp/demo/
):
root@node51 [~]# rsync -avHXShPs --exclude rootfs/ /var/snap/lxd/common/lxd/storage-pools/rpool/containers/demo/ /tmp/demo/
sending incremental file list
created directory /tmp/demo
./
backup.yaml
2.05K 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=4/6)
metadata.yaml
529 100% 516.60kB/s 0:00:00 (xfr#2, to-chk=3/6)
templates/
templates/hostname.tpl
21 100% 20.51kB/s 0:00:00 (xfr#3, to-chk=1/6)
templates/hosts.tpl
140 100% 136.72kB/s 0:00:00 (xfr#4, to-chk=0/6)
sent 3.12K bytes received 135 bytes 6.50K bytes/sec
total size is 2.74K speedup is 0.84
删除LXC创建的ZFS数据集:
root@node51 [~]# zfs destroy rpool/lxd/containers/demo
将ZFS数据集克隆为LXC期望的相同名称:
root@node51 [~]# zfs clone rpool/ROOT/os@20180516T091126CDT rpool/lxd/containers/demo
将mountpoint设置为原始挂载点:
root@node51 [~]# zfs set mountpoint=/var/snap/lxd/common/lxd/storage-pools/rpool/containers/demo rpool/lxd/containers/demo
为新容器数据创建rootfs目录:
root@node51 [~]# mkdir -v /var/snap/lxd/common/lxd/storage-pools/rpool/containers/demo/rootfs/
mkdir: created directory '/var/snap/lxd/common/lxd/storage-pools/rpool/containers/demo/rootfs/'
扩展shell的globbing功能 确保即将到来 mv
获取所有文件系统数据:
root@node51 [~]# shopt -s extglob ; shopt -s dotglob
稍微缩短即将发布的命令 cd
进入容器的数据集:
root@node51 [~]# cd /var/snap/lxd/common/lxd/storage-pools/rpool/containers/demo/
root@node51 [/var/snap/lxd/common/lxd/storage-pools/rpool/containers/demo]#
将所有容器的数据移动到 rootfs/
夹:
root@node51 [/var/snap/lxd/common/lxd/storage-pools/rpool/containers/demo]# mv !(rootfs) rootfs/
创建容器引导所需的一些文件夹:
root@node51 [/var/snap/lxd/common/lxd/storage-pools/rpool/containers/demo]# mkdir rootfs/{dev,sys,proc}
将之前备份的元数据移动到容器的数据集中:
root@node51 [/var/snap/lxd/common/lxd/storage-pools/rpool/containers/demo]# mv /tmp/demo/* .
从元数据备份中删除空的临时目录:
root@node51 [/var/snap/lxd/common/lxd/storage-pools/rpool/containers/demo]# rm -rfv /tmp/demo
removed directory '/tmp/demo'
返回到上一个目录,以便卸载容器的数据集:
root@node51 [/var/snap/lxd/common/lxd/storage-pools/rpool/containers/demo]# cd -
/root
卸载容器的数据集,以便LXC可以将其接管:
root@node51 [~]# zfs umount rpool/lxd/containers/demo
要告诉LXC在下次启动时将容器的文件转换为非特权,请运行
lxc config edit demo
并更改读取的行
volatile.last_state.idmap: '[{"Isuid":true,"Isgid":true,"Hostid":1000000,"Nsid":0,"Maprange":1000000000}]'
至
volatile.last_state.idmap: '[{"Isuid":true,"Isgid":true,"Hostid":1,"Nsid":0,"Maprange":1000000000}]'
启动容器。
这将需要一段时间,因为容器的rootfs中的每个文件都被转换为非特权。 没有进度指标。
root@node51 [~]# lxc start demo
输入容器:
root@node51 [~]# lxc exec demo -- bash
从这里,您可以配置网络,系统启动顺序和/或使主机的LXC容器克隆启动并运行所需的其他内容。