我们在很多的语言开发中都少不了使用"Hello, the world!"来展示一个开发环境的成功与否,在今天的教程中,我们也毫不例外地利用这个例子来展示snap应用是如何构建的.虽然这个例子很简单,但是在我们进入例程的过程中,我们会慢慢地发现有关snap系统的一些特性,这样可以更好地帮助我们来了解这个系统.如果大家想了解16.04的桌面对snap的支持,请参阅文章"安装snap应用到Ubuntu 16.4桌面系统".
$ sudo snap install hello-world
liuxg@liuxg:/snap/hello-world/current/meta$ ls
gui snap.yaml
name: hello-world
version: 6.3
architectures: [ all ]
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
apps:
env:
command: bin/env
evil:
command: bin/evil
sh:
command: bin/sh
hello-world:
command: bin/echo
liuxg@liuxg:/snap/hello-world/current/bin$ ls
echo env evil sh
liuxg@liuxg:/snap/hello-world/current$ tree -L 3
.
├── bin
│ ├── echo
│ ├── env
│ ├── evil
│ └── sh
└── meta
├── gui
│ └── icon.png
└── snap.yaml
$ snapcraft init
name: my-snap # the name of the snap
version: 0 # the version of the snap
summary: This is my-snap's summary # 79 char long summary
description: This is my-snap's description # a longer description for the snap
confinement: devmode # use "strict" to enforce system access only via declared interfaces
parts:
my-part: # Replace with a part name of your liking
# Get more information about plugins by running
# snapcraft help plugins
# and more information about the available plugins
# by running
# snapcraft list-plugins
plugin: nil
liuxg@liuxg:~/snappy/desktop/helloworld$ tree -L 3
.
├── bin
│ ├── echo
│ ├── env
│ ├── evil
│ └── sh
├── setup
│ └── gui
│ └── icon.png
└── snapcraft.yaml
name: hello-xiaoguo
version: 1.0
architectures: [ all ]
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
confinement: strict
apps:
env:
command: bin/env
evil:
command: bin/evil
sh:
command: bin/sh
hello-world:
command: bin/echo
parts:
hello:
plugin: copy
files:
./bin: bin
liuxg@liuxg:~/snappy/desktop/helloworld$ snapcraft list-plugins
ant catkin copy gulp kbuild make nil python2 qmake tar-content
autotools cmake go jdk kernel maven nodejs python3 scons
liuxg@liuxg:~/snappy/desktop/helloworld/bin$ ls -al
total 24
drwxrwxr-x 2 liuxg liuxg 4096 7月 13 00:31 .
drwxrwxr-x 4 liuxg liuxg 4096 7月 18 10:31 ..
-rwxrwxr-x 1 liuxg liuxg 31 7月 12 05:20 echo
-rwxrwxr-x 1 liuxg liuxg 27 7月 12 05:20 env
-rwxrwxr-x 1 liuxg liuxg 274 7月 12 05:20 evil
-rwxrwxr-x 1 liuxg liuxg 209 7月 12 05:20 sh
$ chmod a+x echo
$ snapcraft
liuxg@liuxg:~/snappy/desktop/helloworld$ tree -L 2
.
├── bin
│ ├── echo
│ ├── env
│ ├── evil
│ └── sh
├── hello-xiaoguo_1.0_all.snap
├── parts
│ └── hello
├── prime
│ ├── bin
│ ├── command-env.wrapper
│ ├── command-evil.wrapper
│ ├── command-hello-world.wrapper
│ ├── command-sh.wrapper
│ └── meta
├── setup
│ └── gui
├── snapcraft.yaml
└── stage
└── bin
liuxg@liuxg:~/snappy/desktop/helloworld$ snapcraft --help
...
The available lifecycle commands are:
clean Remove content - cleans downloads, builds or install artifacts.
cleanbuild Create a snap using a clean environment managed by lxd.
pull Download or retrieve artifacts defined for a part.
build Build artifacts defined for a part. Build systems capable of
running parallel build jobs will do so unless
"--no-parallel-build" is specified.
stage Stage the part's built artifacts into the common staging area.
prime Final copy and preparation for the snap.
snap Create a snap.
Parts ecosystem commands
update Updates the parts listing from the cloud.
define Shows the definition for the cloud part.
search Searches the remotes part cache for matching parts.
Calling snapcraft without a COMMAND will default to 'snap'
...
$ snapcraft cleanbuild
$ snapcraft clean
$ sudo snap install hello-xiaoguo_1.0_all.snap --force-dangerous
$ sudo snap install hello-xiaoguo_1.0_all.snap --dangerous
$ snapcraft clean
$ snapcraft pull
$ snapcraft build
$ snapcraft stage
$ snapcraft prime
$ snapcraft snap prime/
$ sudo snap try prime/
liuxg@liuxg:~/snappy/desktop/helloworld$ snap list
Name Version Rev Developer Notes
hello-world 6.3 27 canonical -
hello-xiaoguo 1.0 x2 -
ubuntu-core 16.04+20160531.11-56 122 canonical -
$ hello-xiaoguo.env
$ hello-xiaoguo.evil
$ hello-xiaoguo.sh
$ hello-xiaoguo.hello-world
liuxg@liuxg:~/snappy/desktop/helloworld$ hello-xiaoguo.hello-world
Hello World!
$ unsquashfs -l hello-xiaoguo_1.0_all.snap
$ unsquashfs hello-xiaoguo_1.0_all.snap
$ cd cd squashfs-root
# Hack hack hack
$ snapcraft snap
这里我们就留给开发者们自己做练习.
liuxg@liuxg:/snap/bin$ ls -l
total 52
-rwxr-xr-x 1 root root 708 7月 20 15:37 hello-world
-rwxr-xr-x 1 root root 783 7月 21 15:13 hello-world-cli
-rwxr-xr-x 1 root root 683 7月 20 15:37 hello-world.env
-rwxr-xr-x 1 root root 687 7月 20 15:37 hello-world.evil
-rwxr-xr-x 1 root root 679 7月 20 15:37 hello-world.sh
-rwxr-xr-x 1 root root 743 7月 22 15:30 hello-xiaoguo.createfile
-rwxr-xr-x 1 root root 767 7月 22 15:30 hello-xiaoguo.createfiletohome
-rwxr-xr-x 1 root root 715 7月 22 15:30 hello-xiaoguo.env
-rwxr-xr-x 1 root root 719 7月 22 15:30 hello-xiaoguo.evil
-rwxr-xr-x 1 root root 747 7月 22 15:30 hello-xiaoguo.hello-world
-rwxr-xr-x 1 root root 711 7月 22 15:30 hello-xiaoguo.sh
-rwxr-xr-x 1 root root 726 7月 22 11:32 snappy-debug.security
-rwxr-xr-x 1 root root 798 7月 20 10:44 telegram-sergiusens.telegram
$ hello-xiaoguo.env | grep SNAP
liuxg@liuxg:~$ hello-xiaoguo.env | grep SNAP
SNAP_USER_COMMON=/home/liuxg/snap/hello-xiaoguo/common
SNAP_LIBRARY_PATH=/var/lib/snapd/lib/gl:
SNAP_COMMON=/var/snap/hello-xiaoguo/common
SNAP_USER_DATA=/home/liuxg/snap/hello-xiaoguo/x2
SNAP_DATA=/var/snap/hello-xiaoguo/x2
SNAP_REVISION=x2
SNAP_NAME=hello-xiaoguo
SNAP_ARCH=amd64
SNAP_VERSION=1.0
SNAP=/snap/hello-xiaoguo/x2
SNAP_DATA=/var/snap/hello-xiaoguo/x2
#!/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:~$ hello-xiaoguo.evil
Hello Evil World!
This example demonstrates the app confinement
You should see a permission denied error next
/snap/hello-xiaoguo/x2/bin/evil: 9: /snap/hello-xiaoguo/x2/bin/evil: cannot create /var/tmp/myevil.txt: Permission denied
8307 comm="fswebcam" requested_mask="r" denied_mask="r" fsuid=0 ouid=0
Jul 19 13:27:18 liuxg kernel: [19665.330053] audit: type=1400 audit(1468906038.378:4309): apparmor="DENIED" operation="open" profile="snap.webcam-webui.webcam-webui" name="/etc/fonts/fonts.conf" pid=18307 comm="fswebcam" requested_mask="r" denied_mask="r" fsuid=0 ouid=0
Jul 19 13:27:25 liuxg gnome-session[2151]: (nm-applet:2584): nm-applet-WARNING **: ModemManager is not available for modem at /hfp/org/bluez/hci0/dev_F4_B7_E2_CC_F0_56
Jul 19 13:27:26 liuxg kernel: [19673.182647] audit: type=1400 audit(1468906046.230:4310): apparmor="DENIED" operation="mknod" profile="snap.hello-xiaoguo.evil" name="/var/tmp/myevil.txt" pid=18314 comm="evil" requested_mask="c" denied_mask="c" fsuid=1000 ouid=1000
liuxg@liuxg:~/snappy/desktop/helloworld$ cat /var/log/syslog | grep DENIED | grep hello-xiaoguo
Jul 19 13:25:25 liuxg kernel: [19552.926619] audit: type=1400 audit(1468905925.975:4276): apparmor="DENIED" operation="mknod" profile="snap.hello-xiaoguo.evil" name="/var/tmp/myevil.txt" pid=18273 comm="evil" requested_mask="c" denied_mask="c" fsuid=1000 ouid=1000
Jul 19 13:27:26 liuxg kernel: [19673.182647] audit: type=1400 audit(1468906046.230:4310): apparmor="DENIED" operation="mknod" profile="snap.hello-xiaoguo.evil" name="/var/tmp/myevil.txt" pid=18314 comm="evil" requested_mask="c" denied_mask="c" fsuid=1000 ouid=1000
$ sudo grep audit /var/log/syslog
audit: type=1326 audit(1430766107.122:16): auid=1000 uid=1000 gid=1000 ses=15 pid=1491 comm="env" exe="/bin/bash" sig=31 arch=40000028 syscall=983045 compat=0 ip=0xb6fb0bd6 code=0x0
$ scmp_sys_resolver 983045
set_tls
#!/bin/sh
set -e
echo "Hello a nice World!"
echo "This example demonstrates the app confinement"
echo "This app tries to write to its own user directory"
echo "Haha" > $HOME/test.txt
echo "Succeeded! Please find a file created at $HOME/test.txt"
echo "If do not see this, please file a bug"
createfile:
command: bin/createfile
liuxg@liuxg:~/snappy/desktop/helloworld$ hello-xiaoguo.createfile
Hello a nice World!
This example demonstrates the app confinement
This app tries to write to its own user directory
Succeeded! Please find a file created at /home/liuxg/snap/hello-xiaoguo/x3/test.txt
If do not see this, please file a bug
liuxg@liuxg:~/snappy/desktop/helloworld$ hello-xiaoguo.env | grep home
GPG_AGENT_INFO=/home/liuxg/.gnupg/S.gpg-agent:0:1
SNAP_USER_COMMON=/home/liuxg/snap/hello-xiaoguo/common
ANDROID_NDK_ROOT=/home/liuxg/android-ndk-r10e
SNAP_USER_DATA=/home/liuxg/snap/hello-xiaoguo/x3
PWD=/home/liuxg/snappy/desktop/helloworld
HOME=/home/liuxg/snap/hello-xiaoguo/x3
XAUTHORITY=/home/liuxg/.Xauthority
liuxg@liuxg:~/snappy/desktop/helloworld$ hello-xiaoguo.env | grep SNAP
SNAP_USER_COMMON=/home/liuxg/snap/hello-xiaoguo/common
SNAP_LIBRARY_PATH=/var/lib/snapd/lib/gl:
SNAP_COMMON=/var/snap/hello-xiaoguo/common
SNAP_USER_DATA=/home/liuxg/snap/hello-xiaoguo/x3
SNAP_DATA=/var/snap/hello-xiaoguo/x3
SNAP_REVISION=x3
SNAP_NAME=hello-xiaoguo
SNAP_ARCH=amd64
SNAP_VERSION=1.0
SNAP=/snap/hello-xiaoguo/x3
$ sudo snap install snappy-debug
$ sudo /snap/bin/snappy-debug.security scanlog foo
#!/bin/sh
set -e
echo "Hello a nice World!"
echo "This example demonstrates the app confinement"
echo "This app tries to write to its own user directory"
echo "Haha" > /home/$USER/test.txt
echo "Succeeded! Please find a file created at $HOME/test.txt"
echo "If do not see this, please file a bug"
liuxg@liuxg:~/snappy/desktop/helloworld$ hello-xiaoguo.createfiletohome
Hello a nice World!
This example demonstrates the app confinement
This app tries to write to its own user directory
/snap/hello-xiaoguo/x1/bin/createfiletohome: 9: /snap/hello-xiaoguo/x1/bin/createfiletohome: cannot create /home/liuxg/test.txt: Permission denied
$ sudo snap install hello-xiaoguo_1.0_all.snap --devmode --force-dangerous
Snaps can be uploaded to the edge and beta channels only
name: hello-xiaoguo
version: 1.0
architectures: [ all ]
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
confinement: strict
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
plugs: [home]
parts:
hello:
plugin: copy
files:
./bin: bin
liuxg@liuxg:~$ snap interfaces
Slot Plug
:camera -
:cups-control -
:firewall-control -
:gsettings -
:home -
:locale-control -
:log-observe -
:modem-manager -
:mount-observe -
:network -
:network-bind -
:network-control -
:network-manager -
:network-observe -
:opengl -
:optical-drive -
:ppp -
:pulseaudio -
:snapd-control -
:system-observe -
:timeserver-control -
:timezone-control -
:unity7 -
:x11 -
liuxg@liuxg:~/snappy/desktop/helloworld$ hello-xiaoguo.createfiletohome
Hello a nice World!
This example demonstrates the app confinement
This app tries to write to its own user directory
Succeeded! Please find a file created at /home/liuxg/snap/hello-xiaoguo/x1/test.txt
If do not see this, please file a bug
$ hello-xiaoguo.sh
liuxg@liuxg:~$ hello-xiaoguo.sh
Launching a shell inside the default app confinement. Navigate to your
app-specific directories with:
$ cd $SNAP
$ cd $SNAP_DATA
$ cd $SNAP_USER_DATA
bash-4.3$ env | grep snap
SNAP_USER_COMMON=/home/liuxg/snap/hello-xiaoguo/common
SNAP_LIBRARY_PATH=/var/lib/snapd/lib/gl:
SNAP_COMMON=/var/snap/hello-xiaoguo/common
SNAP_USER_DATA=/home/liuxg/snap/hello-xiaoguo/x4
SNAP_DATA=/var/snap/hello-xiaoguo/x4
PATH=/snap/hello-xiaoguo/x4/bin:/snap/hello-xiaoguo/x4/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
HOME=/home/liuxg/snap/hello-xiaoguo/x4
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/share/gnome:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
SNAP=/snap/hello-xiaoguo/x4
bash-4.3$
我们可以通过这个shell来完成我们想要的任务.比如:
bash-4.3$ cd /home/liuxg
bash-4.3$ pwd
/home/liuxg
bash-4.3$ touch hello.text
touch: cannot touch 'hello.text': Permission denied
$ snapcraft tour
Snapcraft tour initialized in ./snapcraft-tour/
Instructions are in the README, or http://snapcraft.io/create/#tour