Canonical recently released a new Ubuntu 16.04 system in April 2016, and this system is LongTerm Support-LTS. It supports debian packages as always, but it also supports the latest Snap packages. The Snap package is a new package format released by Canonical and can even be installed on other Linux distributions. More information is available on our official developer web site: developer.ubuntu.com/en/desktop/ or snapcraft. IO/view. Canonical also released its Ubuntu Core 16 operating system for the Internet of Things (IoT) on November 3, 2016. In today’s article, we’ll focus on some of the features of the Ubuntu Core system and how to use it.

\

1) What is snap?

\

A Snap package:

  • Is a file based on the squashFS file system. It contains the application code and contains an application-specific metadata file called snap.yaml. It contains a read-only file system. Once installed, it creates an application-specific writable area that no other application can access
  • It’s completely independent of the system. In the SNAP package, it contains all the libraries and runtime it needs to run (such as Python or Java, etc.), and it can be updated over the network, and it can be rolled back to the previous version without affecting the rest of the system
  • It’s limited. Through security mechanisms, it has the properties of a sandbox, cannot access external resources at will, and is isolated from the rest of the system. It can interact with other Snap’s through well-designed security policies.

Ubuntu Core features:

\

\

\

\

\

– Minimum Ubuntu server image: Due to the smaller kernel, it can be applied to more application scenarios. The application is provided through a simple.snap package. The Snap package differs from the previous Debian and click packages on mobile phones.

Transactional updates: Updates automatically through the mall’s channels. Transactional updates provide a more reliable system. There are two root filesystem’s in Ubuntu Core: A and B (each containing A system image). When A is started, it can be used to update B. Only after B is updated completely and without errors can you switch over to B, otherwise you will always be in A. Conversely, we can update area A in the same way. Of course, at any time, we can also rollback, that is, go back to the previous startup. This ensures that our system is complete with Ubuntu Core and application updates at all times. This is very important for many systems that require a stable working environment, such as updating a Webcam that is located in a corner of the road. In addition, when updating an application or system, only the changed part (Delta) needs to be updated, so less data is transferred.

– Stronger application security: Ubuntu Core has industry-leading security. The Ubuntu Core OS system image is only readable. No application can change it. In this way, the system image will not be modified due to the installation or upgrade of an application, and some installed applications will not run properly. Each application has its own read-write space, and they are not mutually accessible. Every application operates in a limited sandbox. Access to the API is enforced by Apparmor and Seccomp. It provides strict ISOLATION based on MAC (Mandatory-access-Control). Traditional Linux applications can access any directory and read or write files in them. This is different from Snappy. If an application needs a special library, you can use Snapcraft to install the corresponding library into your application’s private directory.

If you want to learn more about Ubuntu Core, you can also check out my video “Introduction to Ubuntu Core.”

\

2) 16.04 Desktop support

\

If you don’t already have your own 16.04 desktop, you can download the latest 16.04 desktop from the url.

\

\

From the figure above, we can see that there are two formats of installation packages supported on the 16.04 desktop: Snap and Debian. In addition, we can see that each snap application is self-contained. Each SNAP application contains any dependencies needed to run it. At the same time, we can see that each SNAP application is isolated from each other (note that OS is also a SNAP). Compared to debian packages, we can see that each Debian application installation depends on the installation of other packages; Debian applications can access each other without restriction, causing security problems; Removing one debian application or package may cause other applications to fail. In contrast, different SNAP applications can install different versions of the same software (such as Python 2.7 for one application and Python 3.3 for another) without causing any interference. In theory, a SNAP application can be installed on any Linux distribution because it is independent of the operating system and its distribution. This is great for application maintenance.

Installation of the Snap package is currently supported on the following Linux distributions. If you’re interested, you can try it. You can even compile the Snapd environment running on it directly from source code.

   \

\

Canonical is currently calling on the community to port apps to snap packages and eventually ubuntu Core for the most secure operating system and good app maintenance.

For an all-Snap Ubuntu Core system (shown on the right above), it can be divided into two logical parts:

  • Read – only basic system

    • This section includes configuration files, standard directories, libraries, tools, and core services (such as Network Services, LIBC, Systemd, and others). This part of the system is read-only, and each element in it cannot be updated individually. This is called a “system-image”. In a system, there can be two or more of these images. These most basic systems appear as a root filesystem. They can roll back each other after startup, which means that if a system has a startup problem, it can automatically switch to the previous or specified system image. This is also done through snap packaging.
  • Writable SNAP applications and the framework on top of them. They are achieved using the services provided by the system above.

\

3) install

\

In order to be able to run the Snap application on Ubuntu 16.04, we had to do some installation. Use Ctrl+Alt+T to open terminal:

\

$ sudo apt update
$ sudo apt install snapd
$ sudo apt install snapcraft build-essential
Copy the code

\

On our Ubuntu 16.04 system, we had to open Universe so that we could install the Snapcraft tool for later development. Snapcraft is a tool that is necessary for us to be able to compile a SNAP project, even though it is not needed at run time. It is located in the Universe Channel shown below. This can be selected from the Settings in our Ubuntu system:

\

  

\

You can also add the repository of universe from the command line. For those eager to get their hands on the latest snapcraft and Snapd, see the EnableProposed article for the latest updates.

Here, we briefly describe the terminology used:

  • Snapd: An environment that helps you manage snap installation, uninstallation, and transactional Update. It also helped me with the garbage collection of my old snap. Snapd is located in /usr/lib/snapd/snapd on our Ubuntu Core system
  • Snapcraft: This is a tool that helps us package a Snap app. Snapcraft. Yaml is a YAML file format that defines how to package an application as a Snap package. The Snapcraft tool takes advantage of it for packaging.

\

Then, you can install and use one of the required applications in our Terminal:

\

$ sudo snap install ss-qt
$ ss-qt
Copy the code

We can run our installed application directly from dash on our computer:

\

\

\

\

\

If you want to install more apps, you can just go to the app Store on your desktop:

\

\

\

When an application is successfully installed, you can also run the following command to view it:

\

Liuxg @ liuxg: ~ / after/desktop/rssreader $snap list Name Version Rev 27 canonical hello world - 6.3 - the Developer Notes Hello -world- CLI 0.1 x1 - Hello -xiaoguo 1.0 x2 - rssReader -app 1.0 x2 - snappy-debug 0.23 22 Canonical - Telegram - Sergiusens 0.9.50 x1 - Test - License 0.1 x1 - Ubuntu - Calculator-app 2.1+ Snap3 5 Ubuntucoredev - Ubuntu - Core Canonical - Webcam -webui 1.0 X1 -Copy the code

You can see all the applications that have been successfully installed. After each application is installed, it has its own Version number, as well as a Rev number. For apps installed from the Ubuntu Store, the Rev is a number, such as the Rev number 5 for the Ubuntu-Calculator-app above, which is not a number compared to other apps that are not installed from the Store.

\

We can use the snap info command to get the details of a SNAP:

$ snap info core
name:      core
summary:   "snapd runtime environment"
publisher: canonical
description: |
  The core runtime environment for snapd
type:        core
tracking:    stable
installed:   16-2 (1337) 78MB -
refreshed:   2017-02-25 06:41:22 +0800 CST
channels:                
  stable:    16-2 (1337) 78MB -
  candidate: 16-2 (1393) 79MB -
  beta:      16-2 (1393) 79MB -
  edge:      16-2 (1421) 82MB -
Copy the code

\

In general, when we install the SNAP application, by default, we install it from Stable Channel. We can install it from beta/edge channel with the following command:

\

$ snap install hello --channel-beta
Copy the code

\

Or:

\

$ snap install <snap_name> --beta
Copy the code

\

Or:

\

$Snap Refresh Hello --channel=beta Name Version Rev Developer Notes Hello 2.10.1 29 Canonical - Hello (beta) installedCopy the code

\

And as we can see from above, the Calculator application is also in there. If you want to know how this application is implemented, please refer to the source code:

\

https://code.launchpad.net/~dpm/ubuntu-calendar-app/snap-all-things
Copy the code

Careful developers may notice that this application actually uses the same code as Ubuntu phones. No changes have been made. In a sense, Ubuntu has achieved a real Convergence of application design. In order to do this, we only need a Snap package for an app, which can run directly on different screen sizes and automatically adapt the screen size to get the best display. For example, in our other tutorial, “How to Package a Qmake Ubuntu Mobile app as a Snap App”, it shows how to turn a mobile app into a Snap app that can run on the desktop.

\

On the other hand, the Snap application can be deployed on any Linux distribution that supports the SNAP package installation, as long as it supports the Snap package, and it will not be dependent on the operating system release. Maintainability should be very good.

\

Typically, a Snap app checks the latest snap version in the background every day and installs it automatically. Of course, we can also update our SNAP app with the following command:

$ snap refresh <snap name>
Copy the code

We can also rollback to the previous version (supported from snapd 2.11) by using the following command

$ snap revert <snap name>
Copy the code

\

How do we find the Snap application we need from the command line?

\

$ snap find ss-qt
$ snap find shadowsocks
Copy the code

\

\

\

Currently, the find command only supports searching applications in stable Channel. We can use the command above to find the application in the store whose name contains Shadowsocks. Starting with Snap 2.18, if snap Find doesn’t have any parameters after it, it will show some of the featured Snap we recommend:

\

liu-xiao-guo@localhost:~$snap find Name Version Developer Notes Summary Docker 1.11.2-9 Canonical - The Docker App Deployment Mechanism LXD 2.6.2 Canonical - LXD - The Container LighervisorCopy the code

\

You can also use the — Section option to show the areas you care most about. Currently these sections are: Featured, Database, Internet-of-Things, Media, Messaging and OPS

\

liu-xiao-guo@localhost:~$ snap find --section=internet-of-things Name Version Developer Notes Summary openhab B4-5 - Offline Canonical - OpenHab 2.0 Smart Home Server, Offline versions NextCloud 10.0.2 SNAP1 NextCloud - NextCloud ServerCopy the code

\

We can also find the snap app we need at uappexplorer.com/apps:

\

\

\

For those who don’t like the command line, you can also use the Ubuntu Core store to install your favorite applications. See the article “Managing Our Ubuntu Core application with SnapWeb” for detailed steps.

\

When we install our application, we may also get the following error message:

\

liuxg@liuxg:~$ sudo snap install oxide-digitalsignage --devmode --channel=beta
error: cannot install "oxide-digitalsignage": snap "oxide-digitalsignage" has changes in progress
Copy the code

This is usually the case if we interrupt an application while it is being installed. So how do we deal with this problem?

\

liuxg@liuxg:~$ snap changes ID Status Spawn Ready Summary 219 Done 2016-12-08T05:43:09Z 2016-12-08T05:43:09Z Refresh all  snaps in the system 220 Done 2016-12-08T13:06:21Z 2016-12-08T13:06:23Z Remove "livevideo" snap 221 Done 2016-12-08T13:06:32Z 2016-12-08T13:06:32Z Refresh all snaps in the system 222 Done 2016-12-08T13:06:36Z 2016-12-08T13:06:38z Install "liveVideo" snap from file "liveVideo_0.1_amd64. snap" 223 Done 2016-12-08T13:06:54z 2016-12-08T13:06:57Z Remove "livevideo" snap 224 Done 2016-12-08T22:54:32Z 2016-12-08T22:54:32Z Refresh all snaps in the  system 225 Done 2016-12-08T23:58:11Z 2016-12-08T23:58:28Z Install "snaplint" snap 226 Doing 2016-12-09T00:54:06Z - Install "oxide-digitalsignage" snap from "beta" channelCopy the code

\

We can see what is going on with the snap changes command, and then we can abort our current operation with the following command:

\

liuxg@liuxg:~$ snap abort 226
error: access denied (try with sudo)
liuxg@liuxg:~$ sudo snap abort 226
Copy the code

\

\

\

\

4) Delete a Snap app

\

We have just installed a SNAP application on our desktop system. We can now remove the application with the following command. We first display the installed application on the command line:

\

liuxg@liuxg:~$snap List Name Version Rev Developer Notes Hello-world 6.1 26 Canonical - RssReader 1.0 x1 devMode 1 x3 devMode Ubuntu-calculator-app 2.1+ Snap3 5 Ubuntucoredev - Ubuntu - Core 16.04+20160531.11-56 122 Canonical - Webcam -webui 1 X1 -Copy the code

\

Above, we see that the Ubuntu-Calculator-app application has been installed. We can delete it as follows.

liuxg@liuxg:~$ sudo snap remove ubuntu-calculator-app
[sudo] password for liuxg: 

Done
Copy the code

\

Redisplay the list of Snap apps we’ve installed:

\

liuxg@liuxg:~$snap List Name Version Rev Developer Notes Hello-world 6.1 26 Canonical - RssReader 1.0 x1 devMode Rssreader -app 1.0 x2 - Snaptest 1 x1 devMode Snaptest -app 1 x3 devMode Ubuntu - Core 16.04+20160531.11-56 122 Canonical - webcam-webui 1 x1Copy the code

Apparently we can’t find the Ubuntu-Calculator-app anymore.

\

\

5) Where do I find the installation files

\

After we apply a snap application to our system, we can view all snap applications installed on our system by using the following command:

\

liuxg@liuxg:~$snap List Name Version Rev Developer Notes Hello-world 6.1 26 Canonical - RssReader 1.0 x1 devMode 1 x3 devMode Ubuntu-calculator-app 2.1+ Snap3 5 Ubuntucoredev - Ubuntu - Core 16.04+20160531.11-56 122 Canonical - Webcam -webui 1 X1 -Copy the code

Some of snap’s commands may be unfamiliar to some developers. We can get help in the following ways:

\

$ snap --help      # Or use 'snap <command> --help' for help on a specific command
Copy the code

\

After installing our app, we can find our Snap installation file in the following path:

\

liuxg@liuxg:/var/lib/snapd/snaps$ ls
hello-world_26.snap    rssreader_x1.snap     snaptest-app_x3.snap          ubuntu-core_122.snap
rssreader-app_x1.snap  snaptest-app_x1.snap  snaptest_x1.snap              webcam-webui_x1.snap
rssreader-app_x2.snap  snaptest-app_x2.snap  ubuntu-calculator-app_5.snap
Copy the code

We can check the mount status in the system by using the following method:

liuxg@liuxg:~$ mount | grep calculator
/var/lib/snapd/snaps/ubuntu-calculator-app_5.snap on /snap/ubuntu-calculator-app/5 type squashfs (ro,relatime)
Copy the code

\

When we delete a Snap app, all we really need to do is delete the.snap file on the system. As you can see from above, We are actually the/var/lib/snapd/snaps/ubuntu – calculator – app_5. Snap file can make it by using the method of the mount at/snap/ubuntu – calculator – app / 5 directory can be seen. Generally, after a snap application is successfully installed, it is in the /snap/$name/$version/ directory.

\

liuxg@liuxg:/snap/ubuntu-calculator-app/ 2 $tree - 2. ├─ bin │ ├─ build │ ├─ ubuntu-calculator-app ├ ─ ─ the command - calculator. Wrapper ├ ─ ─ etc │ ├ ─ ─ apparmor. D │ ├ ─ ─ dbus - 1 │ ├ ─ ─ the default │ ├ ─ ─ drirc │ ├ ─ ─ fonts │ ├ ─ ─ GPS. Conf │ ├ ─ ─ GSS │ ├ ─ ─ init │ ├ ─ ─ init. D │ ├ ─ ─ the ldap │ ├ ─ ─ pki │ ├ ─ ─ pulse │ ├ ─ ─ ucf. Conf │ ├ ─ ─ X11 │ └ ─ ─ XDG ├ ─ ─ lib │ ├ ─ ─ systemd │ └ ─ ─ x86_64 - Linux - gnu ├ ─ ─ meta │ ├ ─ ─ GUI │ └ ─ ─ the snap. The yaml ├ ─ ─ usr │ ├ ─ ─ bin │ ├ ─ ─ lib │ └ ─ ─ share └ ─ ─ var └ ─ ─ libCopy the code

Careful readers may have noticed that the files in the mounted directory are just another Linux installation file structure. It essentially installs all the required files needed by the Calculator into the same root directory, eliminating any need for system files. In theory, our application will not be unable to run due to system upgrades or version changes. This is undoubtedly a great boon for some software developers! The software we design today will not need to be changed when Ubuntu is upgraded to future version 20.4 or whatever. One of the big benefits of doing this is that the design of our app is completely distro free. Of course, we could have deployed our application to any other Linux distribution that supported the Snap package installation, and it would have worked just fine. We don’t have to worry about what version of Linux it is running and what version of distribution it is.

\

A SNAP system consists of a series of SNAP applications. Each application is independent and read-only. Each SNAP application communicates through the interface described in the next section.

\

\

The SNAP system presents all snap applications to us in /snap/bin. Your system $PATH contains this PATH, so you can start your SNAP app anywhere:

\

liuxg@liuxg:/snap/bin$ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Copy the code

Of course, for a Snap application, the size of its Snap file package can also be very large.

-rw-r--r-- 1 liuxg liuxg 122M 7月 12 12:00 Ubuntu - Calculator-app_2.1+ SNAP3_amd64. SnapCopy the code

As you can see from the above, our SNAP package is 122M in size. If we want to view the contents of our package, we can do so by using the following command:

\

Ubuntu $unsquashfs - l - calculator - app_2. 1 + snap3_amd64. Snap | lessCopy the code
squashfs-root squashfs-root/bin squashfs-root/bin/calculator squashfs-root/command-calculator.wrapper squashfs-root/etc squashfs-root/etc/X11 squashfs-root/etc/X11/Xreset squashfs-root/etc/X11/Xreset.d squashfs-root/etc/X11/Xreset.d/README squashfs-root/etc/X11/Xresources squashfs-root/etc/X11/Xresources/x11-common squashfs-root/etc/X11/Xsession squashfs-root/etc/X11/Xsession.d squashfs-root/etc/X11/Xsession.d/20x11-common_process-args squashfs-root/etc/X11/Xsession.d/30x11-common_xresources squashfs-root/etc/X11/Xsession.d/35x11-common_xhost-local squashfs-root/etc/X11/Xsession.d/40x11-common_xsessionrc squashfs-root/etc/X11/Xsession.d/50x11-common_determine-startup  squashfs-root/etc/X11/Xsession.d/60x11-common_localhost squashfs-root/etc/X11/Xsession.d/60x11-common_xdg_path ...Copy the code

\

We can also get all the files in the snap package directly with the following command:

\

$unsquashfs Ubuntu - Calculator-app_2.1+ SNAP3_amd64. snap $CD squashfs-root # Hack Hack Hack Hack and go back to the previous dir $ snapcraft snap squashfs-rootCopy the code

\

We can repackage our app with the final name snapcraft Snap. For details, see “How to hack a published Ubuntu Core application”.

\

Let’s take a look at how much space our installed application takes up: \

375M	./ubuntu-calculator-app/
Copy the code

\

This means that the installed space of an application is 350M. It also depends on the type of application we have installed. For our Ubuntu-Calculator-app, we put all the Qt libraries we needed and whatever else we needed into the package. As with any other Python application, we can dump any library we need for the Python version into our package. We don’t need to worry about how it affects other applications.

\

In future designs, we could make specific libraries (such as the Qt library) available to each application through content sharing, which would greatly reduce the size of applications in our store. In addition, for the same publisher, we can use the same method without having to copy multiple shared libraries across multiple applications.

\

We can download an app’s Snap file from the store with the following command:

$ snap download <snap-name>
Copy the code

Then hacking is done using the above method. Of course, when we use it, we still need to respect the developer’s originality.


\

\

\

6) Release our app to the store

\

We can easily publish our developed Apps to our app store through “My Apps”. When uploading our application, we must remember to select “Ubuntu Core” as the store to upload.

\

\

\

\

To upload a new Snap app to the store, we simply fill in the required metadata information and upload the Snap file we developed:

\

\

\

If you are a device manufacturer or operator, you can even create your own store. A simple snap app for manufacturing stores can be found at the address.

We can also publish our application from the command line:

$ snapcraft login
Copy the code

We first login through the above command, then use the following command to upload the application:

$ snapcraft register
$ snapcraft upload
Copy the code

Before we upload an application, we must first register a Snap package name. See the Store section of the article “Learn to Make a Snap” for details.

Finally we exit with the following command:

$ snapcraft logout
Copy the code

\

Starting with snapcraft 2.24, we can use the list-registered command to query the applications we have published:

\

liuxg@liuxg:/snap/my-alias/current$ snapcraft login Enter your Ubuntu One SSO credentials. Email: [email protected] Password: Second-factor auth: 063065 Login successful. liuxg@liuxg:/snap/my-alias/current$ snapcraft list-registered Name Since Visibility Price Notes  chrome-browser 2016-11-07T23:58:12Z public - - my-chrome 2016-11-07T23:47:36Z public - - mychrome 2016-11-08T00:14:14Z public - - piglow-music 2016-12-15T08:24:12Z public - - ss-qt 2016-11-02T05:56:55Z public - - ssocks 2016-11-22T02:49:50Z public - - v-top 2016-12-20T03:03:32Z public - -Copy the code

\

For more on how to use the Ubuntu Core Store to publish and install our apps, see my article “Managing our Ubuntu Core Apps with SnapWeb” and “How to Snap Shadowsocks and publish to the Ubuntu Store”. You can also check out how a minimal Ubuntu Store is implemented github.com/noise/snaps… .

\

\

7) Restricted SNAP apps

\

\

When a SNAP application is installed, it is placed in a limited security sandbox at run time, and each application is isolated from the other. By default, each application in each SNAP package can access each other and work together. However, if it has access to other applications or other resources that are not in its own package, it will be limited.

\

The technologies that implement this security sandbox are called AppArmor, Seccomp, and Device CGroups. Each application has its own/TMP directory, devpts, etc. This restricted setting is defined by a project file called snapcraft. Yaml. In this file, SNAP declares the resources it wants to access, and the system will produce the appropriate limits for it. We will introduce snapcraft. Yaml in more detail in future articles.

\

Each SNAP app has its own restricted directory of files that can be accessed. We can view these directories using the Hello-world app installed in the Store:

\

$ sudo snap install hello-world
$ hello-world.env | grep SNAP
Copy the code

\

liuxg@liuxg:~$ hello-world.env | grep SNAP SNAP_USER_COMMON=/home/liuxg/snap/hello-world/common SNAP_LIBRARY_PATH=/var/lib/snapd/lib/gl: SNAP_COMMON=/var/snap/hello-world/common SNAP_USER_DATA=/home/liuxg/snap/hello-world/27 SNAP_DATA=/var/snap/hello-world/27 SNAP_REVISION=27 SNAP_NAME=hello-world SNAP_ARCH= AMd64 SNAP_VERSION=6.3 SNAP=/snap/hello-world/27Copy the code

As you can see, our app has access to the $SNAP_COMMON directory above, which is the same for all versions of a SNAP app. The data in the $SNAP_USER_DATA directory is accessible to our application. $SNAP_DATA can be accessed using sudo. Normally, snap applications that are daemons’ services can access data in this directory because they have sudo permissions.

\

These snap related environment variables can be referenced in our application. These variables are described as follows: \

\

\

\

In SNAP system, the operation of each application is limited. Every SNAP app that wants to access resources outside of the sandbox (or expose its resources to other Snaps) must use interface. Interface allows us to share one Snap’s resources, interact with other Snaps, and access desired hardware resources. An interface defines the rules of interaction between two ends. The two ends are called plug and slot. Slot is a provider and plug is a consumer. Plug and slot can be linked together either automatically or manually. Of course, we can also delete this connection. See my article “WebCam Snap Application Examples” on how to manually establish this connection. We can use commands such as the following to establish a manual interface plug and slot connection:

\

$ sudo snap connect webcam-webui:camera ubuntu-core:camera  
Copy the code

\

\

\

With Interface, we can connect two SNAP applications (note that Ubuntu Core is a Snap itself). We can connect the system OS by interface to share common resources or services such as OpenGL. As an example, when we use Snapcraft to produce the Snap files we want, we want our Snap app to eventually be able to access our user’s $HOME file directory. We can view our existing plug and slot with the following command.

\

liuxg@liuxg:~$ snap interfaces
Slot                 Plug
:camera              -
:cups-control        -
:firewall-control    -
:gsettings           -
:home                rssreader-app,snaptest-app
:locale-control      -
:log-observe         -
:modem-manager       -
:mount-observe       -
:network             -
:network-bind        webcam-webui
:network-control     -
:network-manager     -
:network-observe     -
:opengl              rssreader-app,snaptest-app,ubuntu-calculator-app
:optical-drive       -
:ppp                 -
:pulseaudio          -
:snapd-control       -
:system-observe      -
:timeserver-control  -
:timezone-control    -
:unity7              rssreader-app,snaptest-app,ubuntu-calculator-app
:x11                 -
Copy the code

\

The plug defined for each SNAP application on my computer system is shown above. All of the slots shown on the left above are actually OS Snap (Ubuntu-Core) even though they are simply displayed. The right shows the plugs defined for each application.

\

A more detailed description of interfaces can be found in our document “interfaces”. In the article, it describes each plugs in detail. In our snapcraft. Yaml project file, we must declare this permission so that our application can access the resources we need. For example, for our snap, if we want to access the $HOME directory, we can define it in snapcraft. Yaml like this:

\

name: foo
apps:
  bar:
    command: bin/bar
    plugs: [ home, unity7 ] 
Copy the code

\

Here, the direct connection between home and unity7 and ubuntu core is done automatically. We can see all the interfaces and whether they are automatically connected in the document Snaps Interfaces. Now that we have these plugs set, our Snap app will have access to the $HOME directory. Otherwise we may find error message denied in /var/log/syslog. For more information on security, see the article Snap Security Policy and Sandboxing.

\

It is also worth pointing out that if we want our application to run as we used to on the Ubuntu desktop and not subject to snap security, we can install our application using the following command:

\

$ sudo snap install <package.snap> --devmode
Copy the code

\

As pointed out above, this is development in Developer Mode. It allows developers to be free of security issues when they start developing applications. When we release the application, we debug it safely. For more on this, see the article “Learn to Make a Snap” or the article “HelloWorld Snap Routine”. \

\

What we must pay attention to is: \

\

Snaps with “devmode” can be uploaded to the edge and beta channels only\

\

We’ll cover snapcraft. Yaml in more detail in a future chapter, so don’t worry!

Although each application produces a new version of the file directory during each installation (for example, during upgrade), some file data is common to different versions and does not change when running in different versions, such as the following directories:

\

/var/snap/<name>/current/  ← $SNAP_DATA is the versioned snap data directory
/var/snap/<name>/common/   ← $SNAP_COMMON will not be versioned on upgrades
Copy the code

\

To sum up, each application interacts directly with the OS and other applications in the following way:

\

\

\

In addition, we can get more information by using the following command at $snap interfaces:

\

$ snap interfaces <snap> to find the slots offered and plugs used by the specified snap.
$ snap interfaces <snap>:<slot or plug> for details of only the specified slot or plug.
$ snap interfaces -i=<interface> [<snap>] to get a filtered list of plugs and/or slots.
Copy the code

For example, we could get all the plugs for the application using the following command:

liuxg@liuxg:~$ snap interfaces telegram-sergiusens
Slot           Plug
:home          telegram-sergiusens
:network       telegram-sergiusens
:network-bind  telegram-sergiusens
:unity7        telegram-sergiusens
Copy the code

When an application is updated, all data in the writable area of the previous version (SNAP_USER_DATA and SNAP_DATA) will be automatically copied to the new version of the application and used by the new version.

\

\

\

\

\

\

\

8) How to get SNAP help

\

As we wrote above, we can get help from SNAP with the following command:

\

liuxg@liuxg:~$ snap --help
Usage:
  snap [OPTIONS] <command>

The snap tool interacts with the snapd daemon to control the snappy software platform.


Application Options:
      --version  print the version and exit

Help Options:
  -h, --help     Show this help message

Available commands:
  abort        Abort a pending change
  ack          Adds an assertion to the system
  change       List a change's tasks
  changes      List system changes
  connect      Connects a plug to a slot
  create-user  Creates a local system user
  disconnect   Disconnects a plug from a slot
  find         Finds packages to install
  help         Help
  install      Install a snap to the system
  interfaces   Lists interfaces in the system
  known        Shows known assertions of the provided type
  list         List installed snaps
  login        Authenticates on snapd and the store
  logout       Log out of the store
  refresh      Refresh a snap in the system
  remove       Remove a snap from the system
  run          Run the given snap command
  try          Try an unpacked snap in the system
Copy the code

For each snap command, we can obtain its help information in the following ways:

\

liuxg@liuxg:~$ snap install -h
Usage:
  snap [OPTIONS] install [install-OPTIONS] <snap>

The install command installs the named snap in the system.

Application Options:
      --version        print the version and exit

Help Options:
  -h, --help           Show this help message

[install command options]
          --channel=   Use this channel instead of stable
          --edge       Install from the edge channel
          --beta       Install from the beta channel
          --candidate  Install from the candidate channel
          --stable     Install from the stable channel
          --devmode    Install the snap with non-enforcing security
Copy the code

With the above approach, we can gain a deeper understanding of the SNAP command.

If you’re interested in developing a Snap app, but would like help from others, you can contribute to the discussion by sending an email to [email protected] Mailinglist. You can also participate in the discussion via the following Snappy Channel on Freenode. We have plenty of experts and community leaders to help you answer your questions. Alternatively, we can post our questions on AskUbuntu. If you want to participate in our Playpen development, please join us in our discussion on Gitter. More communication channels, please refer to our connections: developer.ubuntu.com/en/snappy/s… .

\

\

\

\

9) Such as compiling a SNAP application

\

If you already have a SNAP program, you just need to:

  • Install snapcraft. See the beginning of the article
  • In the root directory of the project, just type “snapcraft”. The root directory of the project usually contains snapcraft. Yaml or.snapcraft. Yaml files.

Canonical has called on developers around the world to build Snap apps. We have already placed our developed applications in the following repository. If you are interested in this, please install the following commands to download these apps for reference:

\

$ git clone https://github.com/ubuntu/snappy-playpen.git
$ cd snappy-playpen
Copy the code

At present, we have the following projects for our reference:

\

atom/               idea/                openttd/         tinyproxy/
cloudfoundry-cli/   imagemagick-edge/    plank/           tyrant-unleashed-optimizer/
consul/             imagemagick-stable/  qcomicbook/      ubuntu-clock-app/
dcos-cli/           keepassx/            qdriverstation/  ubuntukylin-icon-theme/
deis-workflow-cli/  kpcli/               ristretto/       vault/
dosbox/             leafpad/             scummvm/         vlc/
ffmpeg/             minetest/            shotwell/        wallpaperdownloader/
galculator/         moon-buggy/          smplayer/        youtube-dl/
gitter-im/          mpv/                 snap-template/
heroku/             openjdk-demo/        snaptest/
Copy the code

\

We can go directly to the root directory of each project and type the following command:

$ snapcraft
Copy the code

When the project is successfully compiled, we can directly find a file with the extension.snap in the root directory of the project. This is the Snap installation file we need. We can install the application as described above.

\

To clear a snap file during compilation, type the following command:

\

$ snapcraft clean
Copy the code

\

More information about Snapcraft can be found in its help:

\

liuxg@liuxg:~$ snapcraft --help
snapcraft

Usage:
 ...

The available commands are:
  help         Obtain help for a certain plugin or topic
  init         Initialize a snapcraft project.
  list-plugins List the available plugins that handle different types of part.
  login        Authenticate session against Ubuntu One SSO.
  logout       Clear session credentials.
  register     Register the package name in the store.
  tour         Setup the snapcraft examples tour in the specified directory,
               or ./snapcraft-tour/.
  upload       Upload a snap to the Ubuntu Store.

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'
Copy the code

\

During the packaging process of Snapcraft, it goes through the following stages:

\

  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.
Copy the code

\

We can use Snapcraft to process each stage individually to see what each step does. “Snapcraft pull” and so on. Packaging is performed in the order listed above. For more information on how to package, see connecting to snapcraft. IO /create/. \

If you are interested in developing an Ubuntu desktop application, see my article “How to package a Qmake Ubuntu Mobile Application as a Snap application” or “HelloWorld Snap routine”.

\

\

\

10) How do I run a SNAP app

\

We can run our application in Dash, and we can also launch our application from the command line in Terminal. When we design our SNAP application, we usually use a project file called snapcraft. Yaml. Let’s use an example to illustrate:

\

snapcraft.yaml

name: snaptest-app
version: 1
summary: This is a summary
description: This is the description
confinement: devmode

apps:
  test:
    command: desktop-launch $SNAP/lib/x86_64-linux-gnu/bin/snaptest
    plugs: [home,unity7,opengl]

parts:
  application:
    source: ./src
    plugin: qmake
    qt-version: qt5
    build-packages:
      - cmake
      - gettext
      - intltool
      - ubuntu-touch-sounds
      - suru-icon-theme
      - qml-module-qttest
      - qml-module-qtsysteminfo
      - qml-module-qt-labs-settings
      - qtdeclarative5-u1db1.0
      - qtdeclarative5-qtmultimedia-plugin
      - qtdeclarative5-qtpositioning-plugin
      - qtdeclarative5-ubuntu-content1
      - qt5-default
      - qtbase5-dev
      - qtdeclarative5-dev
      - qtdeclarative5-dev-tools
      - qtdeclarative5-folderlistmodel-plugin
      - qtdeclarative5-ubuntu-ui-toolkit-plugin
      - xvfb
    stage-packages:
      - ubuntu-sdk-libs
      - qtubuntu-desktop
      - qml-module-qtsysteminfo
    stage:
      - -usr/share/pkgconfig/xkeyboard-config.pc       
    snap:
      - -usr/share/doc
      - -usr/include
    after: [desktop/qt5]
Copy the code

\

In the snapcraft. Yaml file above, the name of our package is “snaptest-app”. There is an application defined under apps called “test”. From the command line, we can run our final application as follows:

\

$ snaptest-app.test
Copy the code

\

That is, we can start our application with < >.<

>. If you want to learn more about Snapcraft, please check out our video “Snapcraft Demo — How to Snap an app”.

\

In particular, if the package name of a SNAP is exactly the same as the name of the app, you can just type in the package name to run the app. Such as:

\

hello-world:

Yaml name: Hello-world version: 6.1 Architectures: [all] "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/echoCopy the code

From the above we can see that both the package name and the application name are hello-world, so we can run the application using hello-world directly. For other applications, we have to use things like hhello-world.env.

\

\

11) How to get our system back to the starting state without SNAP installation

\

As we developed Snap, we found that it was often possible for one SNAP to take up too much space. In addition, if there are multiple versions installed (each installation produces a new version), our hard disk space footprint will become very large. If we were to manually remove these versions, it would be very troublesome. We may need to use umount and easily create files in the following directories:

\

/var/lib/snapd/state.json\

\

Is so broken that normal use of the snap list command does not produce the results we want. So how do we deal with this problem?

We can get the script we want at the following address:

\

git clone https://github.com/zyga/devtools/
Copy the code

\

Run the following downloaded command:

\

$ sudo ./reset-state
Copy the code

We follow the instructions given by the command. So we can delete all the Snap apps and environments THAT I’ve installed. Our system was restored to the original state without any SNAP installed! \

\

12) How do I start or cancel an installed SNAP

\

Let’s say one day I don’t want to run a Snap app I’ve installed, but I don’t want to remove it from the system either. Or MAYBE I want my service to restart (for some daemon applications). We can do this by:

\

liuxg@liuxg:~$snap list Name Version Rev Developer Notes Hello - Xiaoguo 1.0 x1 - Ubuntu - Core 16.04.1 423 canonical - Webcamhtml 0.1 x1 devMode liuxg@liuxg:~$sudo snap disable hello-xiaoguo hello-xiaoguo disabled liuxg@liuxg:~$snap list Name Version Rev Developer Notes Hello-xiaoguo 1.0 x1 Disabled Ubuntu - Core 16.04.1 423 Canonical - Webcamhtml 0.1 x1 devmode liuxg@liuxg:~$ hello-xiaoguo.env hello-xiaoguo.env: command not found liuxg@liuxg:~$ sudo snap enable hello-xiaoguo hello-xiaoguo enabled liuxg@liuxg:~$ snap list Name Version Rev Developer Notes Hello - Xiaoguo 1.0 x1 - Ubuntu - Core 16.04.1 423 Canonical - WebcamHTML 0.1 x1 devMode liuxg@liuxg:~$ hello-xiaoguo.env runtime/cgo: LC_PAPER=zh_CN.UTF-8Copy the code

\

Here, we can see that we can enable or disable our snap by using the Snap disable/enable method. \

\

13) What’s the difference between Ubuntu Core Snap and Docker

\

\

As you can see from the figure above, snap is an extension of the Linux operating system in Ubuntu Core and is closely integrated with the system. It shares the Host Linux file system and has its own separate space. It has one and only one snap instance installed. The Docker and Linux base systems are relatively separate (different nics, different file systems, etc.). All Dockers can have a Docker image at the same time, and each can have a different IP address. It can have multiple instances. Docker is suitable for mass deployment in the cloud. Snap, by comparison, is better suited for deployment on VMS and devices.

\

\

14) Where can I download Ubuntu Core Image

\

The Image of Ubuntu Core can be downloaded at:

\

Cdimage.ubuntu.com/ubuntu-snap… \

Cdimage.ubuntu.com/ubuntu-core…

Cdimage.ubuntu.com/ubuntu-core…

\

Official release:

Releases.ubuntu.com/ubuntu-core… \

\

See the article “How to install Ubuntu Core for Raspberry PI and Compile it in A Snap System” for more information on how to brush your board to the desired Ubuntu Core.

\

\

15) How to develop a SNAP app

\

After reading so much, you might be curious about how to develop a SNAP app. I would like to introduce you to the two introductory tutorials I made:

  • Helloworld Snap routine \

  • WebCam Snap application instance \

From the two examples above, developers should have an idea of how to create a SNAP application. Developing a SNAP app should no longer be a mystery.

\

\

16) More learning resources

\

We can get the routines we need by:

\

$ sudo apt update
$ sudo apt install snapd snapcraft snapcraft-examples
Copy the code

\

Of course, we can also directly download the source of Snapcraft:

\

$ git clone https://github.com/snapcore/snapcraft
Copy the code

All routines can be found in the “demos” directory of Snapcraft. We can learn from these routines how to use Snapcraft to package our application.

\

We can also get the source code of our already developed application by:

\

$ git clone https://github.com/ubuntu/snappy-playpen
Copy the code

\

snapcraft tour:

$ snapcraft tour
Snapcraft tour initialized in ./snapcraft-tour/
Instructions are in the README, or http://snapcraft.io/create/#tour
Copy the code

\

We can also get the demo routine in Snapcraft by doing the following:

$ sudo apt install snapcraft-examples
Copy the code

Finally you can also install our Snap Codelabs to follow our tutorial:

$ sudo snap install snap-codelabs
Copy the code

\

After installation, type the following address into your browser:

http://localhost:8123/ 
Copy the code

We can find the routines we need to learn in the Snapcraft-tour directory in our home.

\

We can use these resources to reference all existing applications. \

  • Github.com/snapcore/sn… \

  • Github.com/snapcore/sn… \

  • developer.ubuntu.com/en/desktop/\

  • developer.ubuntu.com/en/snappy/\

  • snapcraft.io/\

  • Developer.ubuntu.com/en/desktop/… \

  • askubuntu.com/\

  • docs.ubuntu.com/core/en/\

  • www.ubuntu.com/snappy

  • tutorials.ubuntu.com/\

If you are interested, you can also check out my video “Introduction to Ubuntu Core”.

Read more: insights.ubuntu.com/2016/12/12/…

\