如何利用LXD来在不同的Ubuntu桌面环境中编译我们的snap

LXD作为一容器的hypervisor,它对LXC提供了更多的新的用户体验.在今天的教程中,我们来介绍如何利用LXD来在不同的Ubuntu Desktop版本下编译我们的snap应用.


1)安装LXD及命令行工具


我们可以参照链接来安装我们的LXD: https://linuxcontainers.org/lxd/getting-started-cli/.为了方便,我们可以利用已经做好的Ubuntu Image:

liuxg@liuxg:~$ lxc launch ubuntu:yakkety
Creating flying-snake
Starting flying-snake

在这里,我们创建了一个叫做 flying-snake的容器.这个名字是自动生产的.它是基于Ubuntu 16.10的yakkety.
如果你想有一个自己的容器的名称,你也可以使用如下的命令来生产:

$ lxc launch ubuntu:yakkety foobar

这里的 foobar将是我们生成的容器的名称而不是像上面自动生成的flying-snake.

我们可以利用如下的命令来查看:

liuxg@liuxg:~$ lxc list
+----------------------+---------+-------------------+------+------------+-----------+
|         NAME         |  STATE  |       IPV4        | IPV6 |    TYPE    | SNAPSHOTS |
+----------------------+---------+-------------------+------+------------+-----------+
| flying-snake         | RUNNING | 10.0.1.143 (eth0) |      | PERSISTENT | 0         |
+----------------------+---------+-------------------+------+------------+-----------+
| immortal-feline      | STOPPED |                   |      | PERSISTENT | 0         |
+----------------------+---------+-------------------+------+------------+-----------+
| vivid-x86-armhf      | STOPPED |                   |      | PERSISTENT | 0         |
+----------------------+---------+-------------------+------+------------+-----------+
| xenial-desktop-amd64 | STOPPED |                   |      | PERSISTENT | 0         |
+----------------------+---------+-------------------+------+------------+-----------+

2)创建一个用户


我们可以利用如下的命令来创建一个属于自己的用户:

liuxg@liuxg:~$ lxc exec flying-snake -- adduser liuxg
Adding user `liuxg' ...
Adding new group `liuxg' (1001) ...
Adding new user `liuxg' (1001) with group `liuxg' ...
Creating home directory `/home/liuxg' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for liuxg
Enter the new value, or press ENTER for the default
	Full Name []: liuxg
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] y

请注意这里的 flying-snake为我们刚才创建的container的名称.开发者必须根据自己的名称进行选择.我为这个container创建了一个叫做 liuxg的用户.为用户添加管理员权限:

liuxg@liuxg:~$ lxc exec flying-snake -- adduser liuxg sudo
Adding user `liuxg' to group `sudo' ...
Adding user liuxg to group sudo
Done.

$ lxc exec flying-snake -- visudo
通过上面的命令,启动编辑器,并在文件的最后,加入:

<username>   ALL=(ALL) NOPASSWD: ALL



注意这里的 liuxg是我们刚才创建的用户名.开发者需要替换为自己的用户名.

更新系统并安装所需要的工具:

$ lxc exec flying-snake -- apt update -qq
$ lxc exec flying-snake -- apt upgrade -qq
$ lxc exec flying-snake -- apt install -qq -y snapcraft build-essential


3)登陆并编译我们的应用


我们可以通过如下的命令来登陆:

$ lxc exec flying-snake -- sudo -iu liuxg

注意这里的liuxg是我们之前创建的用户.

liuxg@liuxg:~$ lxc exec flying-snake -- sudo -iu liuxg
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

liuxg@flying-snake:~$ 
liuxg@flying-snake:~$ ls -al
total 20
drwxr-xr-x 2 liuxg liuxg 4096 Jan  4 02:52 .
drwxr-xr-x 4 root  root  4096 Jan  4 02:52 ..
-rw-r--r-- 1 liuxg liuxg  220 Jan  4 02:52 .bash_logout
-rw-r--r-- 1 liuxg liuxg 3771 Jan  4 02:52 .bashrc
-rw-r--r-- 1 liuxg liuxg  655 Jan  4 02:52 .profile
liuxg@flying-snake:~$ mkdir apps
liuxg@flying-snake:~$ cd apps/
liuxg@flying-snake:~/apps$ git clone https://github.com/liu-xiao-guo/alias
Cloning into 'alias'...
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
Checking connectivity... done.
liuxg@flying-snake:~/apps$ ls
alias
liuxg@flying-snake:~/apps$ cd alias/
liuxg@flying-snake:~/apps/alias$ ls
hello.sh  snapcraft.yaml
liuxg@flying-snake:~/apps/alias$ snapcraft 
Preparing to pull aliases 
Pulling aliases 
Preparing to build aliases 
Building aliases 
Staging aliases 
Priming aliases 
Snapping 'my-alias' |                                                                
Snapped my-alias_0.1_amd64.snap

我们可以看到我们已经在yakkety (16.10)的环境中把我们的应用打包为一个snap.

我们可以利用 lxc file pull命令来把我们的容器里的文件拷入到我们的host:

lxc file pull flying-snake/etc/hosts .

我们可以通过 lxc file push命令来把我们所需要的文件拷入到我们的container中:

$ lxc file push hosts first/tmp/

我们可以利用:

$ lxc stop flying-snake

来停止我们的container.

你也可以利用:

$ lxc start flying-snake

来启动一个已经停止的container。


liuxg@liuxg:~/tmp$ lxc stop flying-snake
liuxg@liuxg:~/tmp$ lxc list
+----------------------+---------+------+------+------------+-----------+
|         NAME         |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+----------------------+---------+------+------+------------+-----------+
| flying-snake         | STOPPED |      |      | PERSISTENT | 0         |
+----------------------+---------+------+------+------------+-----------+
| immortal-feline      | STOPPED |      |      | PERSISTENT | 0         |
+----------------------+---------+------+------+------------+-----------+
| vivid-x86-armhf      | STOPPED |      |      | PERSISTENT | 0         |
+----------------------+---------+------+------+------------+-----------+
| xenial-desktop-amd64 | STOPPED |      |      | PERSISTENT | 0         |
+----------------------+---------+------+------+------------+-----------+

你可以通过下面的命令来删除一个container:

$ lxc delete flying-snake

具体的操作可以参阅文章: https://linuxcontainers.org/lxd/getting-started-cli/








相关推荐