Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #161 from snapcore/core-or-ubuntu-core
Browse files Browse the repository at this point in the history
Prefer the "core" snap is one is available.
  • Loading branch information
zyga authored Sep 30, 2016
2 parents 0fbe059 + 487b5a4 commit 82da3ac
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
10 changes: 10 additions & 0 deletions spread-tests/main/core-is-preferred/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
summary: The snap named 'core' is preferred to the snap 'ubuntu-core'
prepare: |
snap install --devmode snapd-hacker-toolbelt
snap install core
execute: |
snapd-hacker-toolbelt.busybox cat /meta/snap.yaml | grep -q -F 'name: core'
restore: |
snap remove snapd-hacker-toolbelt
# XXX: the core snap cannot be removed, we should use a trick to remove it
# in some other way but this can wait.
28 changes: 27 additions & 1 deletion src/mount-support.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@
**/
#define SC_VOID_DIR "/var/lib/snapd/void"

/**
* Get the path to the mounted core snap on the host distribution.
*
* The core snap may be named just "core" (preferred) or "ubuntu-core"
* (legacy). The mount point dependes on build-time configuration and may
* differ from distribution to distribution.
**/
static const char *sc_get_outer_core_mount_point()
{
const char *core_path = SNAP_MOUNT_DIR "/core/current/";
const char *ubuntu_core_path = SNAP_MOUNT_DIR "/ubuntu-core/current/";
static const char *result = NULL;
if (result == NULL) {
if (access(core_path, F_OK) == 0) {
// Use the "core" snap if available.
result = core_path;
} else if (access(ubuntu_core_path, F_OK) == 0) {
// If not try to fall back to the "ubuntu-core" snap.
result = ubuntu_core_path;
} else {
die("cannot locate the core snap");
}
}
return result;
}

static void setup_private_mount(const char *security_tag)
{
uid_t uid = getuid();
Expand Down Expand Up @@ -254,7 +280,7 @@ static void setup_snappy_os_mounts()
die("cannot create temporary directory for the root file system");
}
// Bind mount the OS snap into the rootfs directory.
const char *core_snap_dir = SNAP_MOUNT_DIR "/ubuntu-core/current";
const char *core_snap_dir = sc_get_outer_core_mount_point();
debug("bind mounting core snap: %s -> %s", core_snap_dir, rootfs_dir);
if (mount(core_snap_dir, rootfs_dir, NULL, MS_BIND, NULL) != 0) {
die("cannot bind mount core snap: %s to %s", core_snap_dir,
Expand Down
32 changes: 30 additions & 2 deletions src/quirks.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@
// XXX: for smaller patch, this should be in utils.h later
#include "user-support.h"

/**
* Get the path to the mounted core snap in the execution environment.
*
* The core snap may be named just "core" (preferred) or "ubuntu-core"
* (legacy). The mount point does not depend on build-time configuration and
* does not differ from distribution to distribution.
**/
static const char *sc_get_inner_core_mount_point()
{
const char *core_path = "/snap/core/current/";
const char *ubuntu_core_path = "/snap/ubuntu-core/current/";
static const char *result = NULL;
if (result == NULL) {
if (access(core_path, F_OK) == 0) {
// Use the "core" snap if available.
result = core_path;
} else if (access(ubuntu_core_path, F_OK) == 0) {
// If not try to fall back to the "ubuntu-core" snap.
result = ubuntu_core_path;
} else {
die("cannot locate the core snap");
}
}
return result;
}

/**
* Mount a tmpfs at a given directory.
*
Expand Down Expand Up @@ -155,8 +181,10 @@ void sc_setup_quirks()
die("cannot move /var/lib/snapd to %s", snapd_tmp);
}
// now let's make /var/lib the vanilla /var/lib from the core snap
sc_quirk_create_writable_mimic("/var/lib",
"/snap/ubuntu-core/current/var/lib",
char buf[PATH_MAX];
must_snprintf(buf, sizeof buf, "%s/var/lib",
sc_get_inner_core_mount_point());
sc_quirk_create_writable_mimic("/var/lib", buf,
MS_RDONLY | MS_REC | MS_SLAVE | MS_NODEV
| MS_NOSUID);
// now let's move /var/lib/snapd (that was originally there) back
Expand Down
6 changes: 3 additions & 3 deletions src/snap-confine.apparmor.in
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@

# mount calls to setup the pivot_root based chroot with the core snap as
# the root filesystem.
mount options=(rw bind) @SNAP_MOUNT_DIR@/ubuntu-core/*/ -> /tmp/snap.rootfs_*/,
mount options=(rw bind) @SNAP_MOUNT_DIR@/{,ubuntu-}core/*/ -> /tmp/snap.rootfs_*/,

mount options=(rw rbind) /dev/ -> /tmp/snap.rootfs_*/dev/,
mount options=(rw rbind) /etc/ -> /tmp/snap.rootfs_*/etc/,
Expand All @@ -125,7 +125,7 @@
mount options=(rw rbind) {/usr,}/lib/modules/ -> /tmp/snap.rootfs_*/lib/modules/,
mount options=(rw rbind) /var/log/ -> /tmp/snap.rootfs_*/var/log/,
mount options=(rw rbind) /usr/src/ -> /tmp/snap.rootfs_*/usr/src/,
mount options=(rw bind) @SNAP_MOUNT_DIR@/ubuntu-core/*/etc/alternatives/ -> /tmp/snap.rootfs_*/etc/alternatives/,
mount options=(rw bind) @SNAP_MOUNT_DIR@/{,ubuntu-}core/*/etc/alternatives/ -> /tmp/snap.rootfs_*/etc/alternatives/,

# Allow to mkdir /var/lib/snapd/hostfs
/var/lib/snapd/hostfs/ rw,
Expand Down Expand Up @@ -189,7 +189,7 @@
/tmp/snapd.quirks_*/ rw,
mount options=(move) /var/lib/snapd/ -> /tmp/snapd.quirks_*/,
mount fstype=tmpfs options=(rw nodev nosuid) none -> /var/lib/,
mount options=(ro rbind) /snap/ubuntu-core/*/var/lib/** -> /var/lib/**,
mount options=(ro rbind) /snap/{,ubuntu-}core/*/var/lib/** -> /var/lib/**,
umount /var/lib/snapd/,
mount options=(move) /tmp/snapd.quirks_*/ -> /var/lib/snapd/,

Expand Down

0 comments on commit 82da3ac

Please sign in to comment.