LXD 是 ubuntu 基于 LXC 技术的重构,LXD容器天然支持非特权和分布式。
LXD 与 Docker 的思路不同,Docker 是 PAAS,LXD 是 IAAS。
Ubuntu上可以直接通过软件包安装,centos上没有,只能编译安装或通过snap安装,以下是编译安装的方法。

安装依赖:

yum install acl autoconf dnsmasq git libacl libacl-devel \
            libtool make rsync squashfs-tools tar tcl ebtables \ 
            xz xz-libs flex bison systemd-devel libcap-devel

编译libcap

wget https://www.tcpdump.org/release/libpcap-1.10.0.tar.gz
cd libpcap-1.10.0
./configure
make
make install

编译lxc

wget https://linuxcontainers.org/downloads/lxc/lxc-4.0.7.tar.gz
tar xvf lxc-4.0.7.tar.gz
cd lxc-4.0.7
./autogen.sh
./configure
make
make install

编译libuv

wget https://dist.libuv.org/dist/v1.41.0/libuv-v1.41.0.tar.gz
cd /root/hbdl/libuv-v1.41.0
./autogen.sh 
./configure
make
make install

编译sqlite3

wget https://www.sqlite.org/2021/sqlite-autoconf-3350500.tar.gz
tar xvf sqlite-autoconf-3350500.tar.gz
cd sqlite-autoconf-3350500
./configure
make
make install

安装golang

wget https://golang.org/dl/go1.16.3.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.16.3.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
export GO111MODULE=on
export GOPROXY=https://goproxy.io,direct

编译lxd

wget https://linuxcontainers.org/downloads/lxd/lxd-4.13.tar.gz
tar xvf lxd-4.13.tar.gz

编译依赖前,需要修改默认的Makefile,不然会出错,报找不到上面编译安装好的sqlite3
在Makefile中找到dqlite的PKG_CONFIG_PATH,在其后面追加:/usr/local/lib/pkgconfig,如下所示:

PKG_CONFIG_PATH="$(GOPATH)/deps/raft/:/usr/local/lib/pkgconfig"

编译依赖

export GOPATH=$(pwd)/_dist
make deps

编译好后,终端上会提示你设置环境变量,直接拷贝执行。

export CGO_CFLAGS="-I/root/lxd/lxd-4.13/_dist/deps/raft/include/ -I/root/lxd/lxd-4.13/_dist/deps/dqlite/include/"
export CGO_LDFLAGS="-L/root/lxd/lxd-4.13/_dist/deps/raft/.libs -L/root/lxd/lxd-4.13/_dist/deps/dqlite/.libs/"
export LD_LIBRARY_PATH="/root/lxd/lxd-4.13/_dist/deps/raft/.libs/:/root/lxd/lxd-4.13/_dist/deps/dqlite/.libs/"
export CGO_LDFLAGS_ALLOW="-Wl,-wrap,pthread_create"

接下来是编译LXD,直接编译会报错,需要做以下几个步骤:

# 解决 fatal error: sys/capability.h: No such file or director
ln -s /usr/include/linux/capability.h /usr/include/sys/capability.h 

# 解决lib库找不到
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/lib64/pkgconfig

编辑 lxd/include/memory_utils.h 57行左右,将for循环修改为如下,提前声明int i,解决错误`
error: 'for' loop initial declarations are only allowed in C99 mode`

static inline void free_string_list(char **list)
 {
     if (list) {
         int i;
         for (i = 0; list[i]; i++)
             free(list[i]);
             free_disarm(list);
     }
 }

开始编译

export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
make

编译完后不能直接使用,需要配置以下环境变量

# for golang
export GO111MODULE=on
export GOPROXY=https://goproxy.io,direct
export GOPATH=~/go
export PATH="$PATH:$GOPATH/bin:/usr/local/go/bin"

# for lxd
export LXD_PATH=/root/lxd/lxd-4.13/_dist
export LXD_BIN=$LXD_PATH/bin
export PATH=$PATH:$LXD_BIN
export LD_LIBRARY_PATH="$LXD_PATH/deps/raft/.libs/:$LXD_PATH/deps/dqlite/.libs/:/usr/local/lib/"

单独编译安装lxd的依赖

上述是通过make deps编译的xraft和dqlite,也可以独立编译安装。
编译xraft

cd lxd-4.2
cd _dist/deps/raft
autoreconf -i
./configure
make
make install

编译dqlite

cd lxd-4.2
cd _dist/deps/dqlite
autoreconf -i
./configure
make
make install

参考:

https://www.cnblogs.com/lyh0301/p/13951592.html+&cd=9&hl=zh-CN&ct=clnk
http://www.chenlianfu.com/?p=1490
https://www.reddit.com/r/LXD/comments/8b4q9a/how_to_build_the_lxdp2c_toolapplication_to
https://discuss.linuxcontainers.org/t/howto-use-lxd-p2c/3574