标签:
我们在很多的语言开发中都少不了使用"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
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 clean
$ sudo snap install hello-xiaoguo_1.0_all.snap
$ 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!
$ 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
#!/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
#!/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
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
标签:
原文地址:http://blog.csdn.net/ubuntutouch/article/details/51939464