在最新的snapd 2.20中,它开始支持一个叫做classic模式的snap 应用开发.这种classic可以使得我们的应用开发者能够快速地开发我们所需要的应用,这是因为我们不必要对我们的现有的应用做太多的改变.在classic模式下的应用,它可以看见host系统的所有的位于"/"下的文件,就像我们目前正常的应用一样.但是在安装我们的应用后,它的所有文件将位于/snap/foo/current下.它的执行文件将位于/snap/bin目录下,就像我们目前的所有其它的snap应用一样.
当我们安装我们的classic模式下的snap应用时,我们需要使用--classic选项.在上传我们的应用到Ubuntu Core商店时,也需要人工检查.它可以看见位于/snap/core/current下的所有的文件,同时也可以对host里的任何位置的文件进行操作.这样做的目的是为了能够使得开发者快速地发布自己的以snap包为格式的应用,并在以后的开发中逐渐运用Ubuntu Core的confinement以得到完善.在目前看来,classic模式下的应用在可以遇见的将来不能够安装到all-snap系统中,比如Ubuntu Core 16.
对于classic模式的应用来说,它的"/"目录对应于host系统的"/".更多的信息可以参阅地址:http://snapcraft.io/docs/reference/confinement
安装
在开发之前,我们在desktop上安装core而不是ubuntu-core.我们可以用snap list命令来查看:
liuxg@liuxg:~$ snap list
Name Version Rev Developer Notes
core 16.04.1 714 canonical -
firefox-snap 0.1 x1 classic
hello 1.0 x1 devmode
hello-world 6.3 27 canonical -
如果你的系统里是安装的ubuntu-core的话,建议大家使用devtool中的reset-state来使得我们的系统恢复到最初的状态(没有任何安装的snap).在以后的snapd发布中,我们将不再有ubuntu-core这个snap了.我们也可以适用如下的方法来删除ubuntu-core snap并安装上core snap:
$ sudo apt purge -y snapd
$ sudo apt install snapd
$ sudo snap install core
另外对于有的开发者来说从stable channel得不到最新的snap 2.20,我们可以在我们的Ubuntu Destkop中,打开"System Settings"/"Software & Updates"/"Developer Options":
我们可以打开上面所示的开关,就可以得到最新的所有关于我们Ubuntu桌面系统的发布的软件.snap 2.20版本目前就在这个xenial-proposed之中.
在今天的教程中,我们来做一个例程来进行将讲解:
https://github.com/liu-xiao-guo/helloworld-classic
在上面的例程中,它的snapcraft.yaml的文件如下:
snapcraft.yaml
name: hello
version: "1.0"
summary: The 'hello-world' of snaps
description: |
This is a simple snap example that includes a few interesting binaries
to demonstrate snaps and their confinement.
* hello-world.env - dump the env of commands run inside app sandbox
* hello-world.evil - show how snappy sandboxes binaries
* hello-world.sh - enter interactive shell that runs in app sandbox
* hello-world - simply output text
grade: stable
confinement: classic
type: app #it can be gadget or framework
apps:
env:
command: bin/env
evil:
command: bin/evil
sh:
command: bin/sh
hello-world:
command: bin/echo
createfile:
command: bin/createfile
createfiletohome:
command: bin/createfiletohome
listhome:
command: bin/listhome
showroot:
command: bin/showroot
parts:
hello:
plugin: dump
source: .
confinement: classic
$ sudo snap install hello_1.0_amd64.snap --classic --dangerous
#!/bin/bash
cd /
echo "list all of the content in the root:"
ls
echo "show the home content:"
cd home
ls
当我们运行我们的应用showroot时,我们可以看到:
liuxg@liuxg:~/snappy/desktop/helloworld-classic$ hello.showroot
list all of the content in the root:
bin core home lib media proc sbin sys var
boot dev initrd.img lib64 mnt root snap tmp vmlinuz
cdrom etc initrd.img.old lost+found opt run srv usr vmlinuz.old
show the home content:
liuxg root.ini
liuxg@liuxg:~/snappy/desktop/helloworld-classic$ ls /
bin core home lib media proc sbin sys var
boot dev initrd.img lib64 mnt root snap tmp vmlinuz
cdrom etc initrd.img.old lost+found opt run srv usr vmlinuz.old
#!/bin/sh
set -e
echo "Hello Evil World!"
echo "This example demonstrates the app confinement"
echo "You should see a permission denied error next"
echo "Haha" > /var/tmp/myevil.txt
echo "If you see this line the confinement is not working correctly, please file a bug"
运行结果如下:
liuxg@liuxg:~/snappy/desktop/helloworld-classic$ hello.evil
Hello Evil World!
This example demonstrates the app confinement
You should see a permission denied error next
If you see this line the confinement is not working correctly, please file a bug
显然在我们没有使用interface的情况下,我们可以想其它的任何目录进行操作,并写入我们想要的数据.confinement在classic模式下不起任何的作用.对于我们开发者来说,我们只需要快速地把我的应用打包为snap即可.
Firefox snapcraft.yaml
name: firefox-snap
version: '0.1'
summary: "A Firefox snap"
description: "Firefox in a classic confined snap"
grade: devel
confinement: classic
apps:
firefox-snap:
command: firefox
aliases: [firefox]
parts:
firefox:
plugin: dump
source: https://download.mozilla.org/?product=firefox-50.1.0-SSL&os=linux64&lang=en-US
source-type: tar