[ostree/wip/bootloader] [wip/bootloader] Work on implementing Deployment Model 2.0



commit 9190b011fe17219a69452f675156de6951b1679e
Author: Colin Walters <walters verbum org>
Date:   Tue Jun 4 18:05:35 2013 -0400

    [wip/bootloader] Work on implementing Deployment Model 2.0
    
    See https://live.gnome.org/OSTree/DeploymentModel2

 Makefile-ostree.am                          |    7 +-
 Makefile-tests.am                           |    1 +
 src/libotutil/ot-gio-utils.c                |  177 +++++
 src/libotutil/ot-gio-utils.h                |   32 +
 src/ostree/DEPLOY2.0                        |  239 ++++++
 src/ostree/ot-admin-builtin-deploy.c        | 1122 ++++++++++++++++++---------
 src/ostree/ot-admin-builtin-diff.c          |   44 +-
 src/ostree/ot-admin-builtin-init-fs.c       |    3 +-
 src/ostree/ot-admin-builtin-install.c       |   26 +-
 src/ostree/ot-admin-builtin-os-init.c       |    5 +-
 src/ostree/ot-admin-builtin-prune.c         |   42 +-
 src/ostree/ot-admin-builtin-pull-deploy.c   |   55 +-
 src/ostree/ot-admin-builtin-update-kernel.c |  296 -------
 src/ostree/ot-admin-builtin-upgrade.c       |   65 +-
 src/ostree/ot-admin-builtins.h              |    4 +-
 src/ostree/ot-admin-functions.c             |  984 +++++++++++++++++-------
 src/ostree/ot-admin-functions.h             |   60 +-
 src/ostree/ot-bootloader-syslinux.c         |  225 ++++++
 src/ostree/ot-bootloader-syslinux.h         |   40 +
 src/ostree/ot-bootloader.c                  |   53 ++
 src/ostree/ot-bootloader.h                  |   66 ++
 src/ostree/ot-builtin-admin.c               |   23 +-
 src/ostree/ot-deployment.c                  |  155 ++++
 src/ostree/ot-deployment.h                  |   61 ++
 tests/libtest.sh                            |   65 ++
 tests/t0015-admin-deploy.sh                 |   80 ++
 26 files changed, 2761 insertions(+), 1169 deletions(-)
---
diff --git a/Makefile-ostree.am b/Makefile-ostree.am
index 7825db9..1a241d5 100644
--- a/Makefile-ostree.am
+++ b/Makefile-ostree.am
@@ -57,10 +57,15 @@ ostree_SOURCES += \
        src/ostree/ot-admin-builtin-install.c \
        src/ostree/ot-admin-builtin-run-triggers.c \
        src/ostree/ot-admin-builtin-upgrade.c \
-       src/ostree/ot-admin-builtin-update-kernel.c \
        src/ostree/ot-admin-builtins.h \
        src/ostree/ot-admin-functions.h \
        src/ostree/ot-admin-functions.c \
+       src/ostree/ot-bootloader.h \
+       src/ostree/ot-bootloader.c \
+       src/ostree/ot-bootloader-syslinux.h \
+       src/ostree/ot-bootloader-syslinux.c \
+       src/ostree/ot-deployment.h \
+       src/ostree/ot-deployment.c \
        $(NULL)
 
 ostree_bin_shared_cflags = $(AM_CFLAGS) -I$(srcdir)/src/libgsystem -I$(srcdir)/src/libotutil 
-I$(srcdir)/src/libostree -I$(srcdir)/src/ostree  -DLOCALEDIR=\"$(datadir)/locale\"
diff --git a/Makefile-tests.am b/Makefile-tests.am
index cd22ab2..b81866b 100644
--- a/Makefile-tests.am
+++ b/Makefile-tests.am
@@ -28,6 +28,7 @@ testfiles = t0000-basic \
        t0005-corruption \
        t0006-libarchive \
        t0011-pull-archive-z \
+       t0015-admin-deploy \
        $(NULL)
 insttest_SCRIPTS = $(addprefix tests/,$(testfiles:=.sh))
 
diff --git a/src/libotutil/ot-gio-utils.c b/src/libotutil/ot-gio-utils.c
index d37beeb..d58b682 100644
--- a/src/libotutil/ot-gio-utils.c
+++ b/src/libotutil/ot-gio-utils.c
@@ -132,3 +132,180 @@ ot_gfile_get_child_build_path (GFile      *parent,
 
   return g_file_resolve_relative_path (parent, path);
 }
+
+GFile *
+ot_gfile_resolve_path_printf (GFile       *path,
+                              const char  *format,
+                              ...)
+{
+  va_list args;
+  gs_free char *relpath = NULL;
+
+  va_start (args, format);
+  relpath = g_strdup_vprintf (format, args);
+  va_end (args);
+
+  return g_file_resolve_relative_path (path, relpath);
+}
+
+
+gboolean
+ot_gfile_get_symlink_target_from_info (GFile             *path,
+                                       GFileInfo         *file_info,
+                                       GFile            **out_target,
+                                       GCancellable      *cancellable,
+                                       GError           **error)
+{
+  gboolean ret = FALSE;
+  const char *target;
+  gs_unref_object GFile *path_parent = NULL;
+  gs_unref_object GFile *ret_target = NULL;
+
+  if (g_file_info_get_file_type (file_info) != G_FILE_TYPE_SYMBOLIC_LINK)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "Not a symbolic link");
+      goto out;
+    }
+
+  path_parent = g_file_get_parent (path);
+  target = g_file_info_get_symlink_target (file_info);
+  g_assert (target);
+  ret_target = g_file_resolve_relative_path (path_parent, target);
+
+  ret = TRUE;
+ out:
+  ot_transfer_out_value (out_target, &ret_target);
+  return ret;
+}
+
+gboolean
+ot_gfile_query_info_allow_noent (GFile                *path,
+                                 const char           *queryopts,
+                                 GFileQueryInfoFlags   flags,
+                                 GFileInfo           **out_info,
+                                 GCancellable         *cancellable,
+                                 GError              **error)
+{
+  gboolean ret = FALSE;
+  gs_unref_object GFileInfo *ret_file_info = NULL;
+  GError *temp_error = NULL;
+
+  ret_file_info = g_file_query_info (path, queryopts, flags,
+                                     cancellable, &temp_error);
+  if (!ret_file_info)
+    {
+      if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+        {
+          g_clear_error (&temp_error);
+        }
+      else
+        {
+          g_propagate_error (error, temp_error);
+          goto out;
+        }
+    }
+
+  ret = TRUE;
+  ot_transfer_out_value (out_info, &ret_file_info);
+ out:
+  return ret;
+}
+
+gboolean
+ot_gfile_query_symlink_target_allow_noent (GFile          *path,
+                                           GFile         **out_target,
+                                           GCancellable   *cancellable,
+                                           GError        **error)
+{
+  gboolean ret = FALSE;
+  gs_unref_object GFileInfo *file_info = NULL;
+  gs_unref_object GFile *ret_target = NULL;
+
+  if (!ot_gfile_query_info_allow_noent (path, OSTREE_GIO_FAST_QUERYINFO,
+                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                        &file_info,
+                                        cancellable, error))
+    goto out;
+
+  if (file_info != NULL)
+    {
+      if (!ot_gfile_get_symlink_target_from_info (path, file_info, &ret_target,
+                                                  cancellable, error))
+        goto out;
+    }
+  
+  ret = TRUE;
+  ot_transfer_out_value (out_target, &ret_target);
+ out:
+  return ret;
+}
+
+/**
+ * ot_gfile_ensure_unlinked:
+ *
+ * Like gs_file_unlink(), but return successfully if the file doesn't
+ * exist.
+ */
+gboolean
+ot_gfile_ensure_unlinked (GFile         *path,
+                          GCancellable  *cancellable,
+                          GError       **error)
+{
+  gboolean ret = FALSE;
+  GError *temp_error = NULL;
+
+  if (!gs_file_unlink (path, cancellable, &temp_error))
+    {
+      if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+        {
+          g_clear_error (&temp_error);
+        }
+      else
+        {
+          g_propagate_error (error, temp_error);
+          goto out;
+        }
+    }
+  
+  ret = TRUE;
+ out:
+  return ret;
+}
+
+/**
+ * ot_gfile_atomic_symlink_swap:
+ * @path: Replace the contents of this symbolic link
+ * @target: New symbolic link target
+ * @cancellable:
+ * @error
+ *
+ * Create a new temporary symbolic link, then use the Unix rename()
+ * function to atomically replace @path with the new symbolic link.
+ * Do not use this function inside directories such as /tmp as it uses
+ * a predicatable file name.
+ */
+gboolean
+ot_gfile_atomic_symlink_swap (GFile          *path,
+                              const char     *target,
+                              GCancellable   *cancellable,
+                              GError        **error)
+{
+  gboolean ret = FALSE;
+  gs_unref_object GFile *parent = g_file_get_parent (path);
+  gs_free char *tmpname = g_strconcat (gs_file_get_basename_cached (path), ".tmp", NULL);
+  gs_unref_object GFile *tmppath = g_file_get_child (parent, tmpname);
+
+  if (!ot_gfile_ensure_unlinked (tmppath, cancellable, error))
+    goto out;
+  
+  if (!g_file_make_symbolic_link (tmppath, target, cancellable, error))
+    goto out;
+
+  if (!gs_file_rename (tmppath, path, cancellable, error))
+    goto out;
+
+  ret = TRUE;
+ out:
+  return ret;
+}
diff --git a/src/libotutil/ot-gio-utils.h b/src/libotutil/ot-gio-utils.h
index 802602d..aa8cd1e 100644
--- a/src/libotutil/ot-gio-utils.h
+++ b/src/libotutil/ot-gio-utils.h
@@ -42,6 +42,38 @@ GFile *ot_gfile_get_child_strconcat (GFile *parent, const char *first, ...) G_GN
 
 GFile *ot_gfile_get_child_build_path (GFile *parent, const char *first, ...) G_GNUC_NULL_TERMINATED;
 
+GFile * ot_gfile_resolve_path_printf (GFile       *path,
+                                      const char  *format,
+                                      ...) G_GNUC_PRINTF(2, 3);
+
+
+gboolean ot_gfile_get_symlink_target_from_info (GFile             *path,
+                                                GFileInfo         *file_info,
+                                                GFile            **out_target,
+                                                GCancellable      *cancellable,
+                                                GError           **error);
+
+gboolean ot_gfile_query_info_allow_noent (GFile                *path,
+                                          const char           *queryopts,
+                                          GFileQueryInfoFlags   flags,
+                                          GFileInfo           **out_info,
+                                          GCancellable         *cancellable,
+                                          GError              **error);
+  
+gboolean ot_gfile_query_symlink_target_allow_noent (GFile          *path,
+                                                    GFile         **out_target,
+                                                    GCancellable   *cancellable,
+                                                    GError        **error);
+
+gboolean ot_gfile_ensure_unlinked (GFile         *path,
+                                   GCancellable  *cancellable,
+                                   GError       **error);
+
+gboolean ot_gfile_atomic_symlink_swap (GFile          *path,
+                                       const char     *target,
+                                       GCancellable   *cancellable,
+                                       GError        **error);
+
 G_END_DECLS
 
 #endif
diff --git a/src/ostree/DEPLOY2.0 b/src/ostree/DEPLOY2.0
new file mode 100644
index 0000000..ab94fa6
--- /dev/null
+++ b/src/ostree/DEPLOY2.0
@@ -0,0 +1,239 @@
+This should be migrated to https://live.gnome.org/OSTree/DeploymentModel2
+
+/** TODO:
+ *
+ * Drop the bind mount for /etc; instead check out the writable copy
+ * inside the deployment, and keep the lookaside copy inside the tree
+ * as etc.default ?  But that would need its own read-only bind mount,
+ * so maybe /usr/etc.default or even just /usr/etc ?
+ *
+ * For /var, change it to be a systemd generator; then it could
+ * be overriden by an admin.
+ *
+ */
+
+/* ATOMIC DIRECTORY:
+ *
+ * name -> name.l0
+ * name.d:
+ *   foo/
+ *   bar
+ *   baz
+ *   ... and more files
+ * name.l0:
+ *   ../name.d/foo foo
+ *   ../name.d/bar bar
+ *   ../name.d/baz baz
+ *
+ * To update:
+ *  0) Perform cleanup
+ *  1) Add new contents to name.d.  If necessary, rename.
+ *  2) Create new name.l1, with symlinks to contents in name.d
+ *  3) Atomic swap name -> name.l1
+ * Cleanup:
+ *  1) Read value of name, e.g. name.l0
+ *  2) for each (linkname, target) i name.l0 not also in name.l1:
+ *    rm -rf ${linkname} ${target}
+ *  3) rm -rf name.l0
+ */ 
+
+/* 
+ * Terms
+ * -----
+ * We have an ordered list of deployments:
+ * "new": The new tree
+ * "current/deploy0": The currently deployed tree, set as the bootloader default
+ * "previous/deploy1": (optional) The previous tree relative to "current"
+ * "deploy2"...:  Any number of previous deployments
+ *
+ * Also in the mix is the special "running" deployment, the one we're
+ * currently running.  We want to support generating an arbitrary set
+ * of new deployments, atomically.  But the normal case is to make
+ * "new" be "current", shift everything down by one, and "previous"
+ * would drop off.
+ *
+ * Because we want to support deploying the same tree twice,
+ * e.g. going from foo -> bar -> foo, the deployments have a serial
+ * number.  A deployment list looks like:
+ *
+ * deploy0:   da39a538021.1  (tag:current, ref:x86_64-runtime, running)
+ * deploy1:   1ca9d348d67.0  (tag:previous, ref:x86_64-devel)
+ * deploy2:   da39a538021.0  (ref:x86_64-runtime)
+ * deploy3:   587e2144109.0  (ref:x86_64-runtime)
+ *
+ * The very common case of course is for the deployment serial to be
+ * 0, so we should omit it from the list.
+ *
+ * Also, exactly one deployment can be "running".  Let's say I just ran:
+ * $ ostree admin pull-deploy gnome-ostree/buildmaster/x86_64-runtime
+ * but I haven't rebooted yet.  Then:
+ *
+ * deploy0:   da39a538021.1  (tag:current, ref:x86_64-runtime)
+ * deploy1:   1ca9d348d67.0  (tag:previous, ref:x86_64-devel, running)
+ * deploy2:   da39a538021.0  (ref:x86_64-runtime)
+ * deploy3:   587e2144109.0  (ref:x86_64-runtime)
+ *
+ * In this scenario, we expect doing:
+ * $ ostree admin upgrade -r
+ * would delete the deploy0, and configuration changes are merged based on deploy1.
+ *
+ * It's also possible for deploy2/3 to be running, if the user
+ * explicitly rebooted back into them.  We should have a command:
+ * $ ostree admin reset-current
+ * to make deploy2 current, and delete the old current/previous.
+ *
+ * Each deployment has a few variables:
+ *
+ * struct deployment {
+ *  int index;  // Global offset
+ *  char *ref;  // originating OSTree refname (e.g. gnome-ostree/buildmaster/x86_64-runtime)
+ *  char *csum;     // OSTree checksum of tree
+ *  int bootserial; // An integer assigned to this tree per its ${bootcsum}
+ *  char *bootcsum;   //Checksum of kernel+initramfs
+ * }
+ *
+ * Global variables:
+ *
+ * bootversion: Either 0 or 1, as expressed by /boot/loader.${bootversion}.
+ *
+ * Constraints
+ * -----------
+ *
+ * We want fully atomic upgrades.  Some constraints though:
+ *
+ *   1) We don't want to update /boot unless the kernel actually
+ *   changed; it might be on flash storage for example, and constant
+ *   wear on that would be bad.
+ *
+ *   2) /boot may be on a separate partition, so we can't just swap
+ *      one symbolic link and have it take effect for everything.
+ *
+ * Implementation
+ * --------------
+ *
+ * We have two deployment lists: OLD and NEW.  We need to preserve any
+ * entries from OLD in NEW.  Let's take the case above:  
+ * 
+ * deploy0:   da39a538021.1  (tag:current, ref:x86_64-runtime, bootcsum:c90d227a60, running)
+ * deploy1:   1ca9d348d67.0  (tag:previous, ref:x86_64-devel, bootcsum:648a9123b4)
+ * deploy2:   da39a538021.0  (ref:x86_64-runtime, bootcsum:c90d227a60)
+ * deploy3:   587e2144109.0  (ref:x86_64-runtime, bootcsum:c90d227a60)
+ *
+ * And we want to generate the new deployment list:
+ *
+ * deploy0:   afd96ee01b1.0  (tag:current, ref:x86_64-runtime, bootcsum:c90d227a60)
+ * deploy1:   da39a538021.1  (tag:current, ref:x86_64-runtime, bootcsum:c90d227a60, running)
+ * deploy2:   1ca9d348d67.0  (tag:previous, ref:x86_64-devel, bootcsum:648a9123b4)
+ * deploy3:   da39a538021.0  (ref:x86_64-runtime, bootcsum:c90d227a60)
+ * deploy4:   587e2144109.0  (ref:x86_64-runtime, bootcsum:c90d227a60)
+ *
+ * Filesystem layout
+ * -----------------
+ *
+ * If we need to change kernel configuration, we do so atomically via
+ * the "swapped directory pattern".  In this case, that's
+ * /boot/loader.{0,1}, and /boot/loader is a symbolic link to one or
+ * the other.
+ *
+ * A tree deployment is represented by these directories:
+ *  /boot/loader/entries/${osname}-${bootcsum}
+ *    Contains kernel+initramfs files and configuration
+ *  /ostree/boot.${bootversion}/${osname}/${bootcsum}/${treebootserial}
+ *    Where ${treebootserial} is a symbolic link 0..N, pointing to 
../../../deploy/${osname}/${treecsum}.${treeserial}
+ *
+ * And these directories:
+ *  /ostree/deploy/${osname}/${treecsum}.${serial}
+ *    Contains "OS/" - this is the chroot target
+ * And the following files:
+ *  /ostree/deploy/${osname}/deploy/${treecsum}.${serial}/usr/share/ostree/ref
+ *    containing ${treeref}
+ *  /ostree/deploy/${osname}/deploy/${treecsum}.${serial}/usr/share/ostree/rev
+ *    containing the full ${treecsum} (it's shortened elsewhere to a minimum unique length, at least 8)
+ *
+ * In order to implement upgrades without touching /boot,
+ * /ostree/boot.${bootversion}/${osname}/${bootcsum} is also a
+ * swapped directory.  So the real target is e.g. ${bootcsum}.[01]
+ *
+ * Booting
+ * -------
+ *
+ * The kernel commandline contains:
+ *   ostree=/ostree/boot.${bootversion}/${osname}/${bootcsum}/${treeserial}
+ * Inside the initramfs (dracut), the ostree-prepare-root tool parses this, and
+ * sets up /sysroot.
+ *
+ * Redeploying
+ * -----------
+ *
+ * We have two deployment lists, call them CURRENT and NEW.  First,
+ * determine if we need to generate a new bootloader configuration;
+ * this will be true if NEW adds or removes "bootcsum" entries.
+ *
+ * New bootloader configuration
+ * ----------------------------
+ *
+ * Let's assume the current bootversion is 0; the new bootversion will
+ * therefore be 1.
+ *
+ * 0) Compute KEEPSET, which is the set of deployments in both OLD and NEW.
+ *    For each deployment (osname, treecsum, bootcsum, serial=0) in KEEPSET, check out
+ *      - Copy symbolic links in /ostree/boot.0/${osname}/${bootcsum} to the
+ *        corresponding .1
+ *      - Clone /boot/loader.0/${osname}-${bootcsum} to /boot/loader.1/${osname}-${bootcsum}
+ * 1) Compute NEWSET, which is the set of deployments that are only in NEW.
+ *    For each deployment (osname, treecsum, bootcsum, serial=0) in NEWSET:
+ *      - Check out /ostree/deploy/${osname}/${treecsum}.${serial}
+ *      - Do the merge into /etc in that directory, and create usr/share/ostree/{ref,rev}.
+ *      - Look in its boot/ directory, and copy the kernel/initramfs to
+ *        /boot/loader.1/entries/${osname}-${bootcsum} (unless the directory already exists).
+ *      -  Allocate an unused treebootserial in /ostree/boot.1/${osname}/${bootcsum}
+ * 2) Generate new bootloader configuration in /boot/loader.1; for example, if we're
+ *    using syslinux, /boot/loader.1/syslinux/syslinux.cfg
+ * 3) fsync() all the above, all files and directories
+ * 4) Atomically swap /boot/loader to point to /boot/loader.1
+ *
+ * Cleanup
+ * -------
+ *
+ * Read value of /boot/loader.  If it's 1, then the cleanup value is
+ * 0, and vice versa.  Assume here that it's 1.
+ *
+ * 0) rm -rf /boot/loader.0 /ostree/boot.0
+ * 1) Generate set of deployments from symbolic links in /ostree/boot.1; this is CURRENTSET
+ * 2) Generate set of all deployments from /ostree/deploy; this is DISKSET
+ * 3) Delete all deployments in DISKSET - CURRENTSET
+ *    
+ * Reusing existing bootloader configuration
+ * -----------------------------------------
+ *
+ * This only applies when we changing the deployment list only for a
+ * given ${bootcsum}, AND the new and old number of entries is the
+ * same.  However it's worth optimizing for this case because the
+ * default OSTree configuration is to keep only "current" and
+ * "previous", and most updates are not going to change the kernel.
+ *
+ * 0) Read the value of /boot/loader; this is ${bootversion}
+ * 1) Read the value of /ostree/deploy/${osname}/deploy.${bootversion}; this is ${osdeployversion};
+ *    Here we assume ${osdeployversion} is 0, the new version will be 1, and vice versa.
+ * 2) Create the directory /ostree/deploy/${osname}/deploy.${bootversion}.1
+ * 3) Compute KEEPSET, which is the set of deployments in both OLD and NEW.
+ *      Each of these will have a symbolic link number, referenced by the bootloader entry,
+ *      that needs to be preserved.
+ *      for each (treecsum, bootsum, serial, boottag) in KEEPSET:
+ *        ln -s ../deploy.${bootversion}.0/${treecsum} 
/ostree/deploy/${osname}/deploy.${bootversion}.1/${boottag}
+ * 4) Compute NEWSET, which is the set of deployments in only in NEW.
+ *    Assign each entry in NEWSET a boottag that has not yet been used.  The normal case is
+ *    removing previous, so that would be boottag 1.
+ *    for each deployment (treecsum, bootsum, serial) in NEWSET:
+ *      Check out /ostree/deploy/${osname}/deploy.${boottag}.1/${bootcsum}/${treecsum}.0
+ *    then create a corresponding -etc directory, merging from the running tree.
+ *      cp -al /ostree/deploy/${osname}/deploy.0/${bootcsum}/${treecsum}.${serial}-etc to corresponding .1
+ *      if !exists(/boot/loader.1/${osname}-${bootcsum}) then
+ *        cp -al /boot/loader.0/${osname}-${bootcsum} to corresponding .1
+ *    Note we hardlink even the writable copy of -etc; only one of these can be active
+ *    at a time, so it's fine if mutating one affects another.  We also don't make
+ *    any guarantees about whether modifications to /etc during or after a deployment
+ *    will be visible on reboot.
+ * 
+ */
+
diff --git a/src/ostree/ot-admin-builtin-deploy.c b/src/ostree/ot-admin-builtin-deploy.c
index 4569761..f04b10b 100644
--- a/src/ostree/ot-admin-builtin-deploy.c
+++ b/src/ostree/ot-admin-builtin-deploy.c
@@ -1,6 +1,6 @@
 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  *
- * Copyright (C) 2012 Colin Walters <walters verbum org>
+ * Copyright (C) 2012,2013 Colin Walters <walters verbum org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -28,131 +28,17 @@
 
 #include <glib/gi18n.h>
 
-typedef struct {
-  OstreeRepo  *repo;
-  OtAdminBuiltinOpts *admin_opts;
-  GFile *ostree_dir;
-  char  *osname;
-  GFile *osname_dir;
-
-  char        *current_deployment_ref;
-  char        *previous_deployment_ref;
-  char        *resolved_commit;
-  char        *resolved_previous_commit;
-  
-  char        *previous_deployment_revision;
-  GFile       *deploy_target_path;
-  GFile       *previous_deployment;
-} OtAdminDeploy;
-
-static gboolean opt_no_kernel;
-static gboolean opt_force;
+static gboolean opt_no_bootloader;
+static gboolean opt_retain;
+static char **opt_kernel_argv;
 
 static GOptionEntry options[] = {
-  { "no-kernel", 0, 0, G_OPTION_ARG_NONE, &opt_no_kernel, "Don't update kernel related config (initramfs, 
bootloader)", NULL },
-  { "force", 0, 0, G_OPTION_ARG_NONE, &opt_force, "Overwrite any existing deployment", NULL },
+  { "no-bootloader", 0, 0, G_OPTION_ARG_NONE, &opt_no_bootloader, "Don't update bootloader", NULL },
+  { "retain", 0, 0, G_OPTION_ARG_NONE, &opt_retain, "Do not delete previous deployment", NULL },
+  { "karg", 0, 0, G_OPTION_ARG_STRING, &opt_kernel_argv, "Set kernel argument, like --karg=root=/dev/sda1", 
NULL },
   { NULL }
 };
 
-/**
- * update_current:
- *
- * Atomically swap the /ostree/current symbolic link to point to a new
- * path.  If successful, the old current will be saved as
- * /ostree/previous, and /ostree/current-etc will be a link to the
- * current /etc subdirectory.
- *
- * Unless the new-current equals current, in which case, do nothing.
- */
-static gboolean
-update_current (OtAdminDeploy      *self,
-                GFile              *current_deployment,
-                GFile              *deploy_target,
-                GCancellable       *cancellable,
-                GError            **error)
-{
-  gboolean ret = FALSE;
-  ot_lobj GFile *current_path = NULL;
-  ot_lobj GFile *current_etc_path = NULL;
-  ot_lobj GFile *previous_path = NULL;
-  ot_lobj GFile *tmp_current_path = NULL;
-  ot_lobj GFile *tmp_current_etc_path = NULL;
-  ot_lobj GFile *tmp_previous_path = NULL;
-  ot_lobj GFileInfo *previous_info = NULL;
-  ot_lfree char *relative_current = NULL;
-  ot_lfree char *relative_current_etc = NULL;
-  ot_lfree char *relative_previous = NULL;
-
-  current_path = g_file_get_child (self->osname_dir, "current");
-  current_etc_path = g_file_get_child (self->osname_dir, "current-etc");
-  previous_path = g_file_get_child (self->osname_dir, "previous");
-
-  relative_current = g_file_get_relative_path (self->osname_dir, deploy_target);
-  g_assert (relative_current);
-  relative_current_etc = g_strconcat (relative_current, "-etc", NULL);
-
-  if (current_deployment)
-    {
-      ot_lfree char *relative_previous = NULL;
-
-      if (g_file_equal (current_deployment, deploy_target))
-        {
-          g_print ("ostadmin: %s already points to %s\n", gs_file_get_path_cached (current_path),
-                   relative_current);
-          return TRUE;
-        }
-
-      tmp_previous_path = g_file_get_child (self->osname_dir, "tmp-previous");
-      (void) gs_file_unlink (tmp_previous_path, NULL, NULL);
-
-      relative_previous = g_file_get_relative_path (self->osname_dir, current_deployment);
-      g_assert (relative_previous);
-      if (symlink (relative_previous, gs_file_get_path_cached (tmp_previous_path)) < 0)
-        {
-          ot_util_set_error_from_errno (error, errno);
-          goto out;
-        }
-    }
-
-  tmp_current_path = g_file_get_child (self->osname_dir, "tmp-current");
-  (void) gs_file_unlink (tmp_current_path, NULL, NULL);
-
-  if (symlink (relative_current, gs_file_get_path_cached (tmp_current_path)) < 0)
-    {
-      ot_util_set_error_from_errno (error, errno);
-      goto out;
-    }
-
-  tmp_current_etc_path = g_file_get_child (self->osname_dir, "tmp-current-etc");
-  (void) gs_file_unlink (tmp_current_etc_path, NULL, NULL);
-  if (symlink (relative_current_etc, gs_file_get_path_cached (tmp_current_etc_path)) < 0)
-    {
-      ot_util_set_error_from_errno (error, errno);
-      goto out;
-    }
-
-  if (!gs_file_rename (tmp_current_path, current_path,
-                       cancellable, error))
-    goto out;
-  if (!gs_file_rename (tmp_current_etc_path, current_etc_path,
-                       cancellable, error))
-    goto out;
-
-  if (tmp_previous_path)
-    {
-      if (!gs_file_rename (tmp_previous_path, previous_path,
-                           cancellable, error))
-        goto out;
-    }
-
-  g_print ("ostadmin: %s set to %s\n", gs_file_get_path_cached (current_path),
-           relative_current);
-
-  ret = TRUE;
- out:
-  return ret;
-}
-
 typedef struct {
   GError **error;
   gboolean caught_error;
@@ -183,46 +69,13 @@ on_checkout_complete (GObject         *object,
 
 
 /**
- * ensure_unlinked:
- *
- * Like gs_file_unlink(), but return successfully if the file doesn't
- * exist.
- */
-static gboolean
-ensure_unlinked (GFile         *path,
-                 GCancellable  *cancellable,
-                 GError       **error)
-{
-  gboolean ret = FALSE;
-  GError *temp_error = NULL;
-
-  if (!gs_file_unlink (path, cancellable, &temp_error))
-    {
-      if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
-        {
-          g_clear_error (&temp_error);
-        }
-      else
-        {
-          g_propagate_error (error, temp_error);
-          goto out;
-        }
-    }
-  
-  ret = TRUE;
- out:
-  return ret;
-}
-
-/**
  * copy_one_config_file:
  *
  * Copy @file from @modified_etc to @new_etc, overwriting any existing
  * file there.
  */
 static gboolean
-copy_one_config_file (OtAdminDeploy      *self,
-                      GFile              *orig_etc,
+copy_one_config_file (GFile              *orig_etc,
                       GFile              *modified_etc,
                       GFile              *new_etc,
                       GFile              *src,
@@ -262,7 +115,7 @@ copy_one_config_file (OtAdminDeploy      *self,
         {
           ot_lobj GFile *child = g_file_get_child (src, g_file_info_get_name (child_info));
 
-          if (!copy_one_config_file (self, orig_etc, modified_etc, new_etc, child,
+          if (!copy_one_config_file (orig_etc, modified_etc, new_etc, child,
                                      cancellable, error))
             goto out;
         }
@@ -303,8 +156,7 @@ copy_one_config_file (OtAdminDeploy      *self,
  * changed in @new_etc, the modified version always wins.
  */
 static gboolean
-merge_etc_changes (OtAdminDeploy  *self,
-                   GFile          *orig_etc,
+merge_etc_changes (GFile          *orig_etc,
                    GFile          *modified_etc,
                    GFile          *new_etc,
                    GCancellable   *cancellable,
@@ -347,7 +199,7 @@ merge_etc_changes (OtAdminDeploy  *self,
       g_assert (path);
       target_file = g_file_resolve_relative_path (new_etc, path);
 
-      if (!ensure_unlinked (target_file, cancellable, error))
+      if (!ot_gfile_ensure_unlinked (target_file, cancellable, error))
         goto out;
     }
 
@@ -355,7 +207,7 @@ merge_etc_changes (OtAdminDeploy  *self,
     {
       OstreeDiffItem *diff = modified->pdata[i];
 
-      if (!copy_one_config_file (self, orig_etc, modified_etc, new_etc, diff->target,
+      if (!copy_one_config_file (orig_etc, modified_etc, new_etc, diff->target,
                                  cancellable, error))
         goto out;
     }
@@ -363,7 +215,7 @@ merge_etc_changes (OtAdminDeploy  *self,
     {
       GFile *file = added->pdata[i];
 
-      if (!copy_one_config_file (self, orig_etc, modified_etc, new_etc, file,
+      if (!copy_one_config_file (orig_etc, modified_etc, new_etc, file,
                                  cancellable, error))
         goto out;
     }
@@ -374,58 +226,32 @@ merge_etc_changes (OtAdminDeploy  *self,
 }
 
 /**
- * deploy_tree:
+ * checkout_deployment_tree:
  *
  * Look up @revision in the repository, and check it out in
- * OSTREE_DIR/deploy/OS/DEPLOY_TARGET.
- *
- * Merge configuration changes from the old deployment, if any.
- *
- * Update the OSTREE_DIR/current{,-etc} and OSTREE_DIR/previous symbolic
- * links.
+ * /ostree/deploy/OS/deploy/${treecsum}.${deployserial}.
  */
 static gboolean
-deploy_tree (OtAdminDeploy     *self,
-             const char        *deploy_target,
-             const char        *revision,
-             GCancellable      *cancellable,
-             GError           **error)
+checkout_deployment_tree (GFile             *sysroot,
+                          OstreeRepo        *repo,
+                          OtDeployment      *deployment,
+                          GFile            **out_deployment_path,
+                          GCancellable      *cancellable,
+                          GError           **error)
 {
   gboolean ret = FALSE;
-  ot_lfree char *deploy_target_fullname = NULL;
-  ot_lfree char *deploy_target_fullname_tmp = NULL;
-  ot_lobj GFile *deploy_target_path_tmp = NULL;
-  ot_lfree char *deploy_target_etc_name = NULL;
-  ot_lobj GFile *deploy_target_etc_path = NULL;
-  ot_lobj GFile *deploy_target_default_etc_path = NULL;
-  ot_lobj GFile *deploy_parent = NULL;
-  ot_lobj GFile *previous_deployment_etc = NULL;
-  ot_lobj GFile *previous_deployment_etc_default = NULL;
-  ot_lobj OstreeRepoFile *root = NULL;
-  ot_lobj GFileInfo *file_info = NULL;
-  ot_lobj GFileInfo *existing_checkout_info = NULL;
-  ot_lfree char *checkout_target_name = NULL;
-  ot_lfree char *checkout_target_tmp_name = NULL;
-  GError *temp_error = NULL;
-  gboolean skip_checkout;
-
-  if (!revision)
-    revision = deploy_target;
-
-  if (!g_file_query_exists (self->osname_dir, cancellable))
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                   "No OS \"%s\" found in \"%s\"", self->osname,
-                   gs_file_get_path_cached (self->osname_dir));
-      goto out;
-    }
-
-  if (!ostree_repo_resolve_rev (self->repo, revision, FALSE, &self->resolved_commit, error))
-    goto out;
-  if (!ostree_repo_resolve_rev (self->repo, revision, TRUE, &self->resolved_previous_commit, error))
-    goto out;
-
-  root = (OstreeRepoFile*)ostree_repo_file_new_root (self->repo, self->resolved_commit);
+  const char *csum = ot_deployment_get_csum (deployment);
+  gs_unref_object OstreeRepoFile *root = NULL;
+  gs_unref_object GFileInfo *file_info = NULL;
+  gs_unref_object GFileInfo *existing_checkout_info = NULL;
+  gs_free char *checkout_target_name = NULL;
+  gs_free char *checkout_target_tmp_name = NULL;
+  gs_unref_object GFile *osdeploy_path = NULL;
+  gs_unref_object GFile *deploy_target_path = NULL;
+  gs_unref_object GFile *deploy_parent = NULL;
+  ProcessOneCheckoutData checkout_data;
+
+  root = (OstreeRepoFile*)ostree_repo_file_new_root (repo, csum);
   if (!ostree_repo_file_ensure_resolved (root, error))
     goto out;
 
@@ -435,168 +261,378 @@ deploy_tree (OtAdminDeploy     *self,
   if (!file_info)
     goto out;
 
-  deploy_target_fullname = g_strconcat (deploy_target, "-", self->resolved_commit, NULL);
-  self->deploy_target_path = g_file_resolve_relative_path (self->osname_dir, deploy_target_fullname);
-
-  deploy_target_fullname_tmp = g_strconcat (deploy_target_fullname, ".tmp", NULL);
-  deploy_target_path_tmp = g_file_resolve_relative_path (self->osname_dir, deploy_target_fullname_tmp);
+  osdeploy_path = ot_gfile_get_child_build_path (sysroot, "ostree", "deploy",
+                                                 ot_deployment_get_osname (deployment),
+                                                 "deploy", NULL);
+  checkout_target_name = g_strdup_printf ("%s.%d", csum, ot_deployment_get_deployserial (deployment));
+  deploy_target_path = g_file_get_child (osdeploy_path, checkout_target_name);
 
-  deploy_parent = g_file_get_parent (self->deploy_target_path);
+  deploy_parent = g_file_get_parent (deploy_target_path);
   if (!gs_file_ensure_directory (deploy_parent, TRUE, cancellable, error))
     goto out;
+  
+  g_print ("ostadmin: Creating deployment %s\n",
+           gs_file_get_path_cached (deploy_target_path));
 
-  deploy_target_etc_name = g_strconcat (deploy_target, "-", self->resolved_commit, "-etc", NULL);
-  deploy_target_etc_path = g_file_resolve_relative_path (self->osname_dir, deploy_target_etc_name);
-
-  /* Delete any previous temporary data */
-  if (!gs_shutil_rm_rf (deploy_target_path_tmp, cancellable, error))
+  memset (&checkout_data, 0, sizeof (checkout_data));
+  checkout_data.loop = g_main_loop_new (NULL, TRUE);
+  checkout_data.error = error;
+  
+  ostree_repo_checkout_tree_async (repo, 0, 0, deploy_target_path, root,
+                                   file_info, cancellable,
+                                   on_checkout_complete, &checkout_data);
+  
+  g_main_loop_run (checkout_data.loop);
+  
+  g_main_loop_unref (checkout_data.loop);
+  
+  if (checkout_data.caught_error)
     goto out;
 
-  existing_checkout_info = g_file_query_info (self->deploy_target_path, OSTREE_GIO_FAST_QUERYINFO,
-                                              G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
-                                              cancellable, &temp_error);
-  if (existing_checkout_info)
+  ret = TRUE;
+  ot_transfer_out_value (out_deployment_path, &deploy_target_path);
+ out:
+  return ret;
+}
+
+static gboolean
+merge_etc_for_deployment (GFile             *source_etc_path,
+                          GFile             *source_etc_pristine_path,
+                          OtDeployment      *deployment,
+                          GFile             *deployment_path,
+                          GCancellable      *cancellable,
+                          GError           **error)
+{
+  gboolean ret = FALSE;
+  gs_unref_object GFile *deployment_etc_path = NULL;
+  gs_unref_object GFile *deployment_usretc_path = NULL;
+  gboolean etc_exists;
+  gboolean usretc_exists;
+
+  deployment_etc_path = g_file_get_child (deployment_path, "etc");
+  deployment_usretc_path = g_file_resolve_relative_path (deployment_path, "usr/etc");
+  
+  etc_exists = g_file_query_exists (deployment_etc_path, NULL);
+  usretc_exists = g_file_query_exists (deployment_usretc_path, NULL);
+
+  if (etc_exists && usretc_exists)
     {
-      if (opt_force)
-        {
-          if (!gs_shutil_rm_rf (self->deploy_target_path, cancellable, error))
-            goto out;
-          if (!gs_shutil_rm_rf (deploy_target_etc_path, cancellable, error))
-            goto out;
-          
-          skip_checkout = FALSE;
-        }
-      else
-        skip_checkout = TRUE;
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
+                           "Tree contains both /etc and /usr/etc");
+      goto out;
     }
-  else if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+  else if (etc_exists)
     {
-      g_clear_error (&temp_error);
-      skip_checkout = FALSE;
+      /* Compatibility hack */
+      if (!gs_file_rename (deployment_etc_path, deployment_usretc_path,
+                           cancellable, error))
+        goto out;
+      usretc_exists = TRUE;
+      etc_exists = FALSE;
+    }
+  
+  if (usretc_exists)
+    {
+      g_assert (!etc_exists);
+      if (!gs_shutil_cp_a (deployment_usretc_path, deployment_etc_path,
+                           cancellable, error))
+        goto out;
+      g_print ("ostadmin: Created %s\n", gs_file_get_path_cached (deployment_etc_path));
+    }
+
+  if (source_etc_path)
+    {
+      if (!merge_etc_changes (source_etc_path, source_etc_pristine_path, deployment_etc_path, 
+                              cancellable, error))
+        goto out;
     }
   else
     {
-      g_propagate_error (error, temp_error);
-      goto out;
+      g_print ("ostadmin: No previous configuration changes to merge\n");
     }
 
-  if (!ot_admin_get_current_deployment (self->ostree_dir, self->osname, &self->previous_deployment,
-                                        cancellable, error))
+  ret = TRUE;
+ out:
+  return ret;
+}
+
+static gboolean
+get_kernel_from_tree (GFile         *deployroot,
+                      GFile        **out_kernel,
+                      GFile        **out_initramfs,
+                      GCancellable  *cancellable,
+                      GError       **error)
+{
+  gboolean ret = FALSE;
+  gs_unref_object GFile *bootdir = g_file_get_child (deployroot, "boot");
+  gs_unref_object GFileEnumerator *dir_enum = NULL;
+  gs_unref_object GFileInfo *file_info = NULL;
+  gs_unref_object GFile *ret_kernel = NULL;
+  gs_unref_object GFile *ret_initramfs = NULL;
+
+  dir_enum = g_file_enumerate_children (bootdir, OSTREE_GIO_FAST_QUERYINFO,
+                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                        NULL, error);
+  if (!dir_enum)
     goto out;
-  if (self->previous_deployment)
+
+  while (TRUE)
     {
-      ot_lfree char *etc_name;
-      ot_lobj GFile *parent;
+      GFileInfo *file_info = NULL;
+      const char *name;
 
-      etc_name = g_strconcat (gs_file_get_basename_cached (self->previous_deployment), "-etc", NULL);
-      parent = g_file_get_parent (self->previous_deployment);
+      if (!gs_file_enumerator_iterate (dir_enum, &file_info, NULL,
+                                       cancellable, error))
+        goto out;
+      if (file_info == NULL)
+        break;
 
-      previous_deployment_etc = g_file_get_child (parent, etc_name);
+      name = g_file_info_get_name (file_info);
 
-      if (!g_file_query_exists (previous_deployment_etc, cancellable)
-          || g_file_equal (self->previous_deployment, self->deploy_target_path))
-        g_clear_object (&previous_deployment_etc);
-      else
-        previous_deployment_etc_default = g_file_get_child (self->previous_deployment, "etc");
+      if (ret_kernel == NULL && g_str_has_prefix (name, "vmlinuz-"))
+        ret_kernel = g_file_get_child (bootdir, name);
+      else if (ret_initramfs == NULL && g_str_has_prefix (name, "initramfs-"))
+        ret_initramfs = g_file_get_child (bootdir, name);
+      
+      if (ret_kernel && ret_initramfs)
+        break;
+    }
 
-      if (!ostree_repo_resolve_rev (self->repo, self->current_deployment_ref, TRUE,
-                                    &self->previous_deployment_revision, error))
-        goto out;
+  if (ret_kernel == NULL)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
+                   "Failed to find boot/vmlinuz-CHECKSUM in %s",
+                   gs_file_get_path_cached (deployroot));
+      goto out;
     }
 
+  ot_transfer_out_value (out_kernel, &ret_kernel);
+  ot_transfer_out_value (out_initramfs, &ret_initramfs);
+  ret = TRUE;
+ out:
+  return ret;
+}
 
-  if (!skip_checkout)
+static gboolean
+checksum_from_kernel_src (GFile        *src,
+                          char        **out_checksum,
+                          GError     **error)
+{
+  const char *last_dash = strrchr (gs_file_get_path_cached (src), '-');
+  if (!last_dash)
     {
-      ProcessOneCheckoutData checkout_data;
-      ot_lobj GFile *triggers_run_path = NULL;
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "Malformed initramfs name '%s', missing '-'", gs_file_get_basename_cached (src));
+      return FALSE;
+    }
+  *out_checksum = g_strdup (last_dash + 1);
+  return TRUE;
+}
 
-      g_print ("ostadmin: Creating deployment %s\n",
-               gs_file_get_path_cached (self->deploy_target_path));
+static int
+sort_by_bootserial (gconstpointer ap, gconstpointer bp)
+{
+  OtDeployment **a_loc = (OtDeployment**)ap;
+  OtDeployment *a = *a_loc;
+  OtDeployment **b_loc = (OtDeployment**)bp;
+  OtDeployment *b = *b_loc;
+
+  if (ot_deployment_get_bootserial (a) == ot_deployment_get_bootserial (b))
+    return 0;
+  else if (ot_deployment_get_bootserial (a) < ot_deployment_get_bootserial (b))
+    return -1;
+  return 1;
+}
 
-      memset (&checkout_data, 0, sizeof (checkout_data));
-      checkout_data.loop = g_main_loop_new (NULL, TRUE);
-      checkout_data.error = error;
+static GPtrArray *
+filter_deployments_by_bootcsum (GPtrArray    *deployments,
+                                const char   *osname,
+                                const char   *bootcsum)
+{
+  GPtrArray *ret = g_ptr_array_new ();
+  guint i;
 
-      ostree_repo_checkout_tree_async (self->repo, 0, 0, deploy_target_path_tmp, root,
-                                       file_info, cancellable,
-                                       on_checkout_complete, &checkout_data);
+  for (i = 0; i < deployments->len; i++)
+    {
+      OtDeployment *deployment = deployments->pdata[i];
+      
+      if (strcmp (ot_deployment_get_osname (deployment), osname) != 0)
+        continue;
+      if (strcmp (ot_deployment_get_bootcsum (deployment), bootcsum) != 0)
+        continue;
+      
+      g_ptr_array_add (ret, deployment);
+    }
+  g_ptr_array_sort (ret, sort_by_bootserial);
 
-      g_main_loop_run (checkout_data.loop);
+  return ret;
+}
 
-      g_main_loop_unref (checkout_data.loop);
+static void
+compute_new_deployment_list (int           current_bootversion,
+                             GPtrArray    *current_deployments,
+                             OtDeployment *booted_deployment,
+                             const char   *osname,
+                             const char   *revision,
+                             const char   *bootcsum,
+                             GPtrArray   **out_new_deployments,
+                             int          *out_new_bootversion)
+{
+  guint i;
+  int new_index;
+  guint new_deployserial = 0;
+  int new_bootserial = 0;
+  gs_unref_object OtDeployment *new_deployment = NULL;
+  gs_unref_ptrarray GPtrArray *matching_deployments_by_bootserial = NULL;
+  OtDeployment *last_deployment_for_this_os = NULL;
+  gs_unref_ptrarray GPtrArray *ret_new_deployments = NULL;
+  gboolean requires_new_bootversion;
+
+  for (i = 0; i < current_deployments->len; i++)
+    {
+      OtDeployment *deployment = current_deployments->pdata[i];
+      
+      if (strcmp (ot_deployment_get_osname (deployment), osname) != 0)
+        continue;
+      if (strcmp (ot_deployment_get_csum (deployment), revision) != 0)
+        continue;
+
+      if (!opt_retain)
+        last_deployment_for_this_os = deployment;
+      new_deployserial = MAX(new_deployserial, ot_deployment_get_deployserial (deployment)+1);
+    }
+  
+  /* We need to update the bootloader only if the deployment we're
+   * removing uses a different kernel.
+   */
+  requires_new_bootversion =
+    (last_deployment_for_this_os == NULL) ||
+    (strcmp (ot_deployment_get_bootcsum (last_deployment_for_this_os), bootcsum) != 0);
 
-      if (checkout_data.caught_error)
-        goto out;
+  ret_new_deployments = g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref);
 
-      triggers_run_path = g_file_resolve_relative_path (deploy_target_path_tmp, 
"usr/share/ostree/triggers-run");
+  new_deployment = ot_deployment_new (0, osname, NULL, revision, new_deployserial,
+                                      bootcsum, new_bootserial);
+  g_ptr_array_add (ret_new_deployments, g_object_ref (new_deployment));
+  new_index = 1;
+  for (i = 0; i < current_deployments->len; i++)
+    {
+      OtDeployment *orig_deployment = current_deployments->pdata[i];
+      gs_unref_object OtDeployment *deployment_clone = NULL;
 
-      if (!g_file_query_exists (triggers_run_path, NULL))
-        {
-          if (!ostree_run_triggers_in_root (deploy_target_path_tmp, cancellable, error))
-            goto out;
-        }
+      if (orig_deployment == last_deployment_for_this_os)
+        continue;
 
-      deploy_target_default_etc_path = ot_gfile_get_child_strconcat (deploy_target_path_tmp, "etc", NULL);
+      deployment_clone = ot_deployment_clone (orig_deployment);
+      ot_deployment_set_index (deployment_clone, new_index);
+      new_index++;
+      g_ptr_array_add (ret_new_deployments, g_object_ref (deployment_clone));
+    }
 
-      if (!gs_shutil_rm_rf (deploy_target_etc_path, cancellable, error))
-        goto out;
+  /* Just renumber the deployments for the OS we're adding; we don't
+   * handle anything else at the moment.
+   */
+  matching_deployments_by_bootserial = filter_deployments_by_bootcsum (ret_new_deployments,
+                                                                       osname, bootcsum);
+  for (i = 0; i < matching_deployments_by_bootserial->len; i++)
+    {
+      OtDeployment *deployment = matching_deployments_by_bootserial->pdata[i];
+      ot_deployment_set_bootserial (deployment, i);
+    }
 
-      if (!gs_shutil_cp_a (deploy_target_default_etc_path, deploy_target_etc_path,
-                           cancellable, error))
-        goto out;
+  *out_new_deployments = ret_new_deployments;
+  ret_new_deployments = NULL;
+  g_assert (current_bootversion == 0 || current_bootversion == 1);
+  if (requires_new_bootversion)
+    *out_new_bootversion = (current_bootversion == 0) ? 1 : 0;
+  else
+    *out_new_bootversion = current_bootversion;
+}
 
-      g_print ("ostadmin: Created %s\n", gs_file_get_path_cached (deploy_target_etc_path));
+static void
+get_previous_deployment_etc (GFile                  *sysroot,
+                             GPtrArray              *current_deployments,
+                             OtDeployment           *deployment,
+                             GFile                 **out_previous_etc,
+                             GFile                 **out_previous_etc_pristine)
+{
+  guint i;
+  const char *osname = ot_deployment_get_osname (deployment);
+  GFile *ret_previous_etc = NULL;
+  GFile *ret_previous_etc_pristine = NULL;
 
-      if (previous_deployment_etc)
-        {
-          if (!merge_etc_changes (self, previous_deployment_etc_default,
-                                  previous_deployment_etc, deploy_target_etc_path, 
-                                  cancellable, error))
-            goto out;
-        }
-      else
-        g_print ("ostadmin: No previous deployment; therefore, no configuration changes to merge\n");
+  for (i = 0; i < current_deployments->len; i++)
+    {
+      OtDeployment *deployment = current_deployments->pdata[i];
+      gs_unref_object GFile *path = NULL;
 
-      if (!gs_file_rename (deploy_target_path_tmp, self->deploy_target_path,
-                           cancellable, error))
-        goto out;
+      if (strcmp (ot_deployment_get_osname (deployment), osname) != 0)
+        continue;
+      
+      path = ot_admin_get_deployment_directory (sysroot, deployment);
+      ret_previous_etc = g_file_resolve_relative_path (path, "etc");
+      ret_previous_etc_pristine = g_file_resolve_relative_path (path, "usr/etc");
+      break;
     }
 
-  ret = TRUE;
- out:
-  return ret;
+  /* FIXME: Implement */
+  *out_previous_etc = ret_previous_etc;
+  *out_previous_etc_pristine = ret_previous_etc_pristine;
 }
 
-/**
- * do_update_kernel:
- *
- * Ensure we have a GRUB entry, initramfs set up, etc.
- */
 static gboolean
-do_update_kernel (OtAdminDeploy     *self,
-                  GCancellable      *cancellable,
-                  GError           **error)
+swap_bootlinks (GFile        *sysroot,
+                int           current_bootversion,
+                GPtrArray    *new_deployments,
+                GCancellable *cancellable,
+                GError      **error)
 {
   gboolean ret = FALSE;
-  gs_unref_object GSSubprocess *proc = NULL;
-  gs_unref_ptrarray GPtrArray *args = NULL;
-
-  args = g_ptr_array_new ();
-  ot_ptrarray_add_many (args, "ostree", "admin",
-                        "--ostree-dir", gs_file_get_path_cached (self->ostree_dir),
-                        "--boot-dir", gs_file_get_path_cached (self->admin_opts->boot_dir),
-                        "update-kernel",
-                        self->osname,
-                        gs_file_get_path_cached (self->deploy_target_path), NULL);
-  g_ptr_array_add (args, NULL);
-
-  proc = gs_subprocess_new_simple_argv ((char**)args->pdata,
-                                        GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
-                                        GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
-                                        cancellable, error);
-  if (!proc)
+  guint i;
+  int old_subbootversion, new_subbootversion;
+  gs_unref_object GFile *ostree_dir = g_file_get_child (sysroot, "ostree");
+  gs_free char *ostree_bootdir_name = g_strdup_printf ("boot.%d", current_bootversion);
+  gs_unref_object GFile *ostree_bootdir = g_file_resolve_relative_path (ostree_dir, ostree_bootdir_name);
+  gs_free char *ostree_subbootdir_name = NULL;
+  gs_unref_object GFile *ostree_subbootdir = NULL;
+  gs_unref_ptrarray GPtrArray *deployments_to_swap = NULL;
+
+  if (!ot_admin_read_current_subbootversion (sysroot, current_bootversion,
+                                             &old_subbootversion,
+                                             cancellable, error))
+    goto out;
+
+  new_subbootversion = old_subbootversion == 0 ? 1 : 0;
+
+  ostree_subbootdir_name = g_strdup_printf ("boot.%d.%d", current_bootversion, new_subbootversion);
+  ostree_subbootdir = g_file_resolve_relative_path (ostree_dir, ostree_subbootdir_name);
+
+  if (!gs_file_ensure_directory (ostree_subbootdir, TRUE, cancellable, error))
     goto out;
-  if (!gs_subprocess_wait_sync_check (proc, cancellable, error))
+
+  for (i = 0; i < new_deployments->len; i++)
+    {
+      OtDeployment *deployment = new_deployments->pdata[i];
+      gs_free char *bootlink_pathname = g_strdup_printf ("%s/%s/%d",
+                                                         ot_deployment_get_osname (deployment),
+                                                         ot_deployment_get_bootcsum (deployment),
+                                                         ot_deployment_get_bootserial (deployment));
+      gs_free char *bootlink_target = g_strdup_printf ("../../../deploy/%s/deploy/%s.%d",
+                                                       ot_deployment_get_osname (deployment),
+                                                       ot_deployment_get_csum (deployment),
+                                                       ot_deployment_get_deployserial (deployment));
+      gs_unref_object GFile *linkname = g_file_get_child (ostree_subbootdir, bootlink_pathname);
+      gs_unref_object GFile *linkname_parent = g_file_get_parent (linkname);
+
+      if (!gs_file_ensure_directory (linkname_parent, TRUE, cancellable, error))
+        goto out;
+
+      if (!g_file_make_symbolic_link (linkname, bootlink_target, cancellable, error))
+        goto out;
+    }
+
+  if (!ot_gfile_atomic_symlink_swap (ostree_bootdir, ostree_subbootdir_name,
+                                     cancellable, error))
     goto out;
 
   ret = TRUE;
@@ -604,31 +640,240 @@ do_update_kernel (OtAdminDeploy     *self,
   return ret;
 }
 
+static char *
+remove_checksum_from_kernel_name (const char *name,
+                                  const char *csum)
+{
+  const char *p = strrchr (name, '-');
+  g_assert_cmpstr (p+1, ==, csum);
+  return g_strndup (name, p-name);
+}
+
+static GHashTable *
+parse_keyvalue_contents (const char *contents,
+                         const char *split)
+{
+  char **lines = g_strsplit (contents, split, -1);
+  char **iter;
+  GHashTable *ret = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+  for (iter = lines; *iter; iter++)
+    {
+      char *line = *iter;
+      char *eq = strchr (line, '=');
+      
+      if (!eq)
+        continue;
+      
+      *eq = '\0';
+      eq++;
+      
+      g_hash_table_insert (ret, line, eq);
+    }
+
+  return ret;
+}
+
+static char *
+set_kernel_arg (const char *options,
+                const char *optkey,
+                const char *optval)
+{
+  char **args = NULL;
+  char **iter;
+  char *opteq = g_strconcat (optkey, "=", NULL);
+  GString *ret = g_string_new ("");
+  gboolean first = TRUE;
+  gboolean set_option = FALSE;
+
+  if (options)
+    args = g_strsplit (options, " ", -1);
+  
+  for (iter = args; iter && *iter; iter++)
+    {
+      const char *arg = *iter;
+      if (!first)
+        {
+          g_string_append_c (ret, ' ');
+        }
+      else
+        first = FALSE;
+      if (g_str_has_prefix (arg, opteq))
+        {
+          set_option = TRUE;
+          g_string_append_printf (ret, "%s=%s", optkey, optval);
+        }
+      else
+        {
+          g_string_append (ret, arg);
+        }
+    }
+
+  if (!set_option)
+    g_string_append_printf (ret, "%s=%s", optkey, optval);
+
+  return g_string_free (ret, FALSE);
+}
+
 static gboolean
-complete_deployment (OtAdminDeploy     *self,
-                     GCancellable      *cancellable,
-                     GError           **error)
+install_deployment_kernel (GFile          *sysroot,
+                           int             new_bootversion,
+                           OtDeployment   *deployment,
+                           GCancellable   *cancellable,
+                           GError        **error)
+
 {
   gboolean ret = FALSE;
+  const char *osname = ot_deployment_get_osname (deployment);
+  const char *bootcsum = ot_deployment_get_bootcsum (deployment);
+  gs_unref_object GFile *bootdir = NULL;
+  gs_unref_object GFile *bootcsumdir = NULL;
+  gs_unref_object GFile *bootconfpath = NULL;
+  gs_unref_object GFile *bootconfpath_parent = NULL;
+  gs_free char *dest_kernel_name = NULL;
+  gs_unref_object GFile *dest_kernel_path = NULL;
+  gs_unref_object GFile *dest_initramfs_path = NULL;
+  gs_unref_object GFile *tree_kernel_path = NULL;
+  gs_unref_object GFile *tree_initramfs_path = NULL;
+  gs_unref_object GFile *etc_os_release = NULL;
+  gs_unref_object GFile *deployment_dir = NULL;
+  gs_free char *contents = NULL;
+  gs_unref_hashtable GHashTable *osrelease_values = NULL;
+  gs_unref_hashtable GHashTable *bootconfig_overrides = NULL;
+  gs_free char *linux_relpath = NULL;
+  gs_free char *linux_key = NULL;
+  gs_free char *initramfs_relpath = NULL;
+  gs_free char *initrd_key = NULL;
+  gs_free char *version_key = NULL;
+  gs_free char *ostree_kernel_arg = NULL;
+  gs_free char *options_key = NULL;
+  const char *val;
+  gsize len;
+
+  deployment_dir = ot_admin_get_deployment_directory (sysroot, deployment);
+
+  if (!get_kernel_from_tree (deployment_dir, &tree_kernel_path, &tree_initramfs_path,
+                             cancellable, error))
+    goto out;
 
-  /* Write out a ref so that any "ostree prune" on the raw repo
-   * doesn't GC the currently deployed tree.
-   */
-  if (!ostree_repo_write_ref (self->repo, NULL, self->current_deployment_ref,
-                              self->resolved_commit, error))
+  bootdir = g_file_get_child (sysroot, "boot");
+  bootcsumdir = ot_gfile_resolve_path_printf (bootdir, "ostree/%s-%s",
+                                              osname,
+                                              bootcsum);
+  bootconfpath = ot_gfile_resolve_path_printf (bootdir, "loader.%d/entries/ostree-%s-%s-%d.conf",
+                                               new_bootversion, osname, 
+                                               ot_deployment_get_csum (deployment),
+                                               ot_deployment_get_bootserial (deployment));
+
+  if (!gs_file_ensure_directory (bootcsumdir, TRUE, cancellable, error))
     goto out;
-  /* Only overwrite previous if it's different from what we're deploying now.
-   */
-  if (self->resolved_previous_commit != NULL
-      && strcmp (self->resolved_previous_commit, self->resolved_commit) != 0)
+  bootconfpath_parent = g_file_get_parent (bootconfpath);
+  if (!gs_file_ensure_directory (bootconfpath_parent, TRUE, cancellable, error))
+    goto out;
+
+  dest_kernel_name = remove_checksum_from_kernel_name (gs_file_get_basename_cached (tree_kernel_path),
+                                                       bootcsum);
+  dest_kernel_path = g_file_get_child (bootcsumdir, dest_kernel_name);
+  if (!g_file_query_exists (dest_kernel_path, NULL))
     {
-      if (!ostree_repo_write_ref (self->repo, NULL, self->previous_deployment_ref,
-                                  self->previous_deployment_revision, error))
+      if (!gs_file_linkcopy_sync_data (tree_kernel_path, dest_kernel_path, G_FILE_COPY_OVERWRITE | 
G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
+                                       cancellable, error))
         goto out;
     }
 
-  if (!update_current (self, self->previous_deployment, self->deploy_target_path,
-                       cancellable, error))
+  if (tree_initramfs_path)
+    {
+      gs_free char *dest_initramfs_name = remove_checksum_from_kernel_name (gs_file_get_basename_cached 
(tree_initramfs_path),
+                                                                       bootcsum);
+      dest_initramfs_path = g_file_get_child (bootcsumdir, dest_initramfs_name);
+
+      if (!g_file_query_exists (dest_initramfs_path, NULL))
+        {
+          if (!gs_file_linkcopy_sync_data (tree_initramfs_path, dest_initramfs_path, G_FILE_COPY_OVERWRITE | 
G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
+                                           cancellable, error))
+            goto out;
+        }
+    }
+
+  etc_os_release = g_file_resolve_relative_path (deployment_dir, "etc/os-release");
+
+  if (!g_file_load_contents (etc_os_release, cancellable,
+                             &contents, &len, NULL, error))
+    {
+      g_prefix_error (error, "Reading /etc/os-release: ");
+      goto out;
+    }
+
+  osrelease_values = parse_keyvalue_contents (contents, "\n");
+  bootconfig_overrides = g_hash_table_new (g_str_hash, g_str_equal);
+
+  /* title */
+  if ((val = g_hash_table_lookup (osrelease_values, "PRETTY_NAME")) != NULL)
+    {
+      g_hash_table_insert (bootconfig_overrides, (gpointer)"title", (gpointer)val);
+    }
+  else if ((val = g_hash_table_lookup (osrelease_values, "ID")) != NULL)
+    {
+      g_hash_table_insert (bootconfig_overrides, (gpointer)"title", (gpointer)val);
+    }
+  else
+    {
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                           "No PRETTY_NAME or ID in /etc/os-release");
+      goto out;
+    }
+
+  version_key = g_strdup_printf ("%d", ot_deployment_get_bootserial (deployment));
+  g_hash_table_insert (bootconfig_overrides, (gpointer)"version", version_key);
+
+  linux_relpath = g_file_get_relative_path (bootdir, dest_kernel_path);
+  linux_key = g_strconcat ("/", linux_relpath, NULL);
+  g_hash_table_insert (bootconfig_overrides, (gpointer)"linux", linux_key);
+
+  if (dest_initramfs_path)
+    {
+      initramfs_relpath = g_file_get_relative_path (bootdir, dest_initramfs_path);
+      initrd_key = g_strconcat ("/", initramfs_relpath, NULL);
+      g_hash_table_insert (bootconfig_overrides, "initrd", initrd_key);
+    }
+
+  val = ot_admin_bootconfig_get_key (ot_deployment_get_bootconfig (deployment), "options");
+  ostree_kernel_arg = g_strdup_printf ("/ostree/boot.%d/%s/%s/%d",
+                                       new_bootversion, osname, bootcsum,
+                                       ot_deployment_get_bootserial (deployment));
+  options_key = set_kernel_arg (val, "ostree", ostree_kernel_arg);
+  g_hash_table_insert (bootconfig_overrides, "options", options_key);
+
+  if (!ot_admin_write_bootconfig_with_overrides (bootconfpath,
+                                                 ot_deployment_get_bootconfig (deployment),
+                                                 bootconfig_overrides,
+                                                 cancellable, error))
+    goto out;
+
+  ret = TRUE;
+ out:
+  return ret;
+}
+
+static gboolean
+swap_bootloader (GFile          *sysroot,
+                 int             current_bootversion,
+                 int             new_bootversion,
+                 GCancellable   *cancellable,
+                 GError        **error)
+{
+  gboolean ret = FALSE;
+  gs_unref_object GFile *boot_loader_link = NULL;
+  gs_free char *new_target = NULL;
+
+  g_assert ((current_bootversion == 0 && new_bootversion == 1) ||
+            (current_bootversion == 1 && new_bootversion == 0));
+
+  boot_loader_link = g_file_resolve_relative_path (sysroot, "boot/loader");
+  new_target = g_strdup_printf ("loader.%d", new_bootversion);
+
+  if (!ot_gfile_atomic_symlink_swap (boot_loader_link, new_target,
+                                     cancellable, error))
     goto out;
 
   ret = TRUE;
@@ -639,20 +884,36 @@ complete_deployment (OtAdminDeploy     *self,
 gboolean
 ot_admin_builtin_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GError **error)
 {
+  __attribute__((unused)) GCancellable *cancellable = NULL;
   GOptionContext *context;
-  OtAdminDeploy self_data;
-  OtAdminDeploy *self = &self_data;
   gboolean ret = FALSE;
-  ot_lobj GFile *repo_path = NULL;
-  ot_lobj GFile *deploy_path = NULL;
+  OtDeployment *new_deployment;
   const char *osname = NULL;
-  const char *deploy_target = NULL;
+  const char *ref = NULL;
   const char *revision = NULL;
-  __attribute__((unused)) GCancellable *cancellable = NULL;
-
-  memset (self, 0, sizeof (*self));
-
-  context = g_option_context_new ("OSNAME TREENAME [REVISION] - In operating system OS, check out revision 
TREENAME (or REVISION as TREENAME)");
+  GFile *sysroot = admin_opts->sysroot;
+  gs_unref_object GFile *rootfs = NULL;
+  gs_unref_object OstreeRepo *repo = NULL;
+  gs_unref_object GFile *commit_root = NULL;
+  gs_unref_object GFile *tree_kernel_path = NULL;
+  gs_unref_object GFile *tree_initramfs_path = NULL;
+  gs_unref_object GFile *new_deployment_path = NULL;
+  gs_unref_object GFile *repo_path = NULL;
+  gs_unref_object GFile *deploy_path = NULL;
+  gs_free char *new_bootcsum = NULL;
+  gs_free char *resolved_revision = NULL;
+  gs_free char *booted_osname = NULL;
+  gs_unref_object OtDeployment *booted_deployment = NULL;
+  gs_unref_object GFile *booted_deployment_path = NULL;
+  gs_unref_object GFile *source_etc_path = NULL;
+  gs_unref_object GFile *source_etc_pristine_path = NULL;
+  gs_unref_ptrarray GPtrArray *current_deployments = NULL;
+  gs_unref_ptrarray GPtrArray *new_deployments = NULL;
+  int current_bootversion;
+  int new_bootversion;
+  int i;
+
+  context = g_option_context_new ("OSNAME REF [REVISION] - In operating system OS, check out revision REF 
(or REVISION as REF)");
 
   g_option_context_add_main_entries (context, options, NULL);
 
@@ -665,51 +926,162 @@ ot_admin_builtin_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
       goto out;
     }
 
-  self->admin_opts = admin_opts;
-  self->ostree_dir = g_object_ref (admin_opts->ostree_dir);
-
-  if (!ot_admin_ensure_initialized (self->ostree_dir, cancellable, error))
+  if (!ot_admin_ensure_initialized (sysroot, cancellable, error))
     goto out;
 
-  repo_path = g_file_get_child (self->ostree_dir, "repo");
-  self->repo = ostree_repo_new (repo_path);
-  if (!ostree_repo_check (self->repo, error))
+  repo_path = g_file_resolve_relative_path (sysroot, "ostree/repo");
+  repo = ostree_repo_new (repo_path);
+  if (!ostree_repo_check (repo, error))
     goto out;
 
   osname = argv[1];
-  deploy_target = argv[2];
+  ref = argv[2];
   if (argc > 3)
     revision = argv[3];
 
-  self->osname = g_strdup (osname);
-  self->osname_dir = ot_gfile_get_child_build_path (self->ostree_dir, "deploy", osname, NULL);
-  self->current_deployment_ref = g_strdup_printf ("deployment/%s/current", self->osname);
-  self->previous_deployment_ref = g_strdup_printf ("deployment/%s/previous", self->osname);
+  if (revision == NULL)
+    {
+      if (!ostree_repo_resolve_rev (repo, ref, FALSE, &resolved_revision,
+                                    error))
+        goto out;
+      revision = resolved_revision;
+    }
 
-  if (!deploy_tree (self, deploy_target, revision, cancellable, error))
+  if (!ostree_repo_read_commit (repo, resolved_revision, &commit_root, cancellable, error))
     goto out;
 
-  if (!opt_no_kernel)
+  if (!get_kernel_from_tree (commit_root, &tree_kernel_path, &tree_initramfs_path,
+                             cancellable, error))
+    goto out;
+  
+  if (tree_initramfs_path != NULL)
     {
-      if (!do_update_kernel (self, cancellable, error))
+      if (!checksum_from_kernel_src (tree_initramfs_path, &new_bootcsum, error))
         goto out;
     }
+  else
+    {
+      if (!checksum_from_kernel_src (tree_kernel_path, &new_bootcsum, error))
+        goto out;
+    }
+
+  /* Ok, this tree looks reasonable to deploy.  Here we perform
+   * cleanup of any leftover data from previous partial failures.
+   * This avoids having to call gs_shutil_rm_rf() at random points
+   * throughout the process.
+   */
+  if (!ot_admin_cleanup (sysroot, cancellable, error))
+    {
+      g_prefix_error (error, "Performing initial cleanup: ");
+      goto out;
+    }
 
-  if (!complete_deployment (self, cancellable, error))
+  /* As a special case, the currently running deployment must always
+   * be present in the deployment list.
+   */
+  if (!ot_admin_get_booted_deployment (&booted_deployment,
+                                       cancellable, error))
     goto out;
 
+  if (booted_deployment != NULL)
+    {
+      gs_unref_object GFile *rootfs = g_file_new_for_path ("/");
+      if (g_file_equal (sysroot, rootfs))
+        booted_deployment_path = ot_admin_get_deployment_directory (sysroot, booted_deployment);
+      else
+        g_clear_object (&booted_deployment);
+    }
+
+  if (!ot_admin_list_deployments (sysroot, &current_bootversion, &current_deployments,
+                                  cancellable, error))
+    {
+      g_prefix_error (error, "While listing deployments: ");
+      goto out;
+    }
+
+  compute_new_deployment_list (current_bootversion,
+                               current_deployments, booted_deployment,
+                               osname, revision, new_bootcsum,
+                               &new_deployments,
+                               &new_bootversion);
+  new_deployment = new_deployments->pdata[0];
+
+  if (!checkout_deployment_tree (sysroot, repo, new_deployment, &new_deployment_path,
+                                 cancellable, error))
+    {
+      g_prefix_error (error, "Checking out tree: ");
+      goto out;
+    }
+
+  if (g_strcmp0 (booted_osname, osname) == 0)
+    {
+      source_etc_path = g_file_get_child (booted_deployment_path, "etc");
+      source_etc_pristine_path = g_file_resolve_relative_path (booted_deployment_path, "usr/etc");
+    }
+  else
+    {
+      get_previous_deployment_etc (sysroot, current_deployments, new_deployment,
+                                   &source_etc_path, &source_etc_pristine_path);
+    }
+
+  if (!merge_etc_for_deployment (source_etc_path, source_etc_pristine_path,
+                                 new_deployment,
+                                 new_deployment_path,
+                                 cancellable, error))
+    {
+      g_prefix_error (error, "During /etc merge: ");
+      goto out;
+    }
+
+  if (current_bootversion == new_bootversion)
+    {
+      if (!swap_bootlinks (sysroot, current_bootversion,
+                           new_deployments,
+                           cancellable, error))
+        {
+          g_prefix_error (error, "Swapping current bootlinks: ");
+          goto out;
+        }
+    }
+  else
+    {
+      for (i = 0; i < new_deployments->len; i++)
+        {
+          OtDeployment *deployment = new_deployments->pdata[i];
+          if (!install_deployment_kernel (sysroot, new_bootversion, deployment,
+                                          cancellable, error))
+            {
+              g_prefix_error (error, "Installing kernel: ");
+              goto out;
+            }
+        }
+
+      /* Swap bootlinks for *new* version */
+      if (!swap_bootlinks (sysroot, new_bootversion, new_deployments,
+                           cancellable, error))
+        {
+          g_prefix_error (error, "Generating new bootlinks: ");
+          goto out;
+        }
+
+      if (!swap_bootloader (sysroot, current_bootversion, new_bootversion,
+                            cancellable, error))
+        {
+          g_prefix_error (error, "Final bootloader swap: ");
+          goto out;
+        }
+    }
+
+  /* And finally, cleanup of any leftover data.
+   */
+  if (!ot_admin_cleanup (sysroot, cancellable, error))
+    {
+      g_prefix_error (error, "Performing final cleanup: ");
+      goto out;
+    }
+
   ret = TRUE;
  out:
-  g_clear_object (&self->repo);
-  g_free (self->osname);
-  g_free (self->current_deployment_ref);
-  g_free (self->previous_deployment_ref);
-  g_free (self->resolved_commit);
-  g_free (self->resolved_previous_commit);
-  g_free (self->previous_deployment_revision);
-  g_clear_object (&self->previous_deployment);
-  g_clear_object (&self->ostree_dir);
-  g_clear_object (&self->osname_dir);
   if (context)
     g_option_context_free (context);
   return ret;
diff --git a/src/ostree/ot-admin-builtin-diff.c b/src/ostree/ot-admin-builtin-diff.c
index 7792db3..b425d6d 100644
--- a/src/ostree/ot-admin-builtin-diff.c
+++ b/src/ostree/ot-admin-builtin-diff.c
@@ -37,10 +37,10 @@ ot_admin_builtin_diff (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GE
 {
   GOptionContext *context;
   gboolean ret = FALSE;
-  const char *osname;
-  GFile *ostree_dir = admin_opts->ostree_dir;
+  gs_free char *booted_osname = NULL;
   ot_lobj GFile *repo_path = NULL;
-  ot_lobj GFile *deployment = NULL;
+  gs_unref_object OtDeployment *deployment = NULL;
+  gs_unref_object GFile *deployment_dir = NULL;
   ot_lobj GFile *deploy_parent = NULL;
   ot_lptrarray GPtrArray *modified = NULL;
   ot_lptrarray GPtrArray *removed = NULL;
@@ -49,45 +49,27 @@ ot_admin_builtin_diff (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GE
   ot_lobj GFile *new_etc_path = NULL;
   __attribute__((unused)) GCancellable *cancellable = NULL;
 
-  context = g_option_context_new ("OSNAME [REVISION] - Diff configuration for OSNAME");
+  context = g_option_context_new ("Diff current /etc configuration versus default");
 
   g_option_context_add_main_entries (context, options, NULL);
 
   if (!g_option_context_parse (context, &argc, &argv, error))
     goto out;
   
-  repo_path = g_file_get_child (ostree_dir, "repo");
+  repo_path = g_file_resolve_relative_path (admin_opts->sysroot, "ostree/repo");
 
-  if (argc < 2)
+  if (!ot_admin_get_booted_deployment (&deployment, cancellable, error))
+    goto out;
+  if (deployment == NULL)
     {
-      ot_util_usage_error (context, "OSNAME must be specified", error);
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                           "Not currently booted into an OSTree system");
       goto out;
     }
+  deployment_dir = ot_admin_get_deployment_directory (admin_opts->sysroot, deployment);
 
-  osname = argv[1];
-
-  if (argc > 2)
-    {
-      deployment = ot_gfile_get_child_build_path (ostree_dir, "deploy", osname, argv[2], NULL);
-      if (!g_file_query_exists (deployment, NULL))
-        {
-          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                       "Deployment %s doesn't exist", gs_file_get_path_cached (deployment));
-          goto out;
-        }
-    }
-  else
-    {
-      if (!ot_admin_get_current_deployment (ostree_dir, osname, &deployment,
-                                            cancellable, error))
-        goto out;
-    }
-
-  orig_etc_path = g_file_resolve_relative_path (deployment, "etc");
-  deploy_parent = g_file_get_parent (deployment);
-  new_etc_path = ot_gfile_get_child_strconcat (deploy_parent,
-                                               gs_file_get_basename_cached (deployment),
-                                               "-etc", NULL);
+  orig_etc_path = g_file_resolve_relative_path (deployment_dir, "usr/etc");
+  new_etc_path = g_file_resolve_relative_path (deployment_dir, "etc");
   
   modified = g_ptr_array_new_with_free_func ((GDestroyNotify) ostree_diff_item_unref);
   removed = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
diff --git a/src/ostree/ot-admin-builtin-init-fs.c b/src/ostree/ot-admin-builtin-init-fs.c
index d8a5355..dc1a45a 100644
--- a/src/ostree/ot-admin-builtin-init-fs.c
+++ b/src/ostree/ot-admin-builtin-init-fs.c
@@ -75,8 +75,7 @@ ot_admin_builtin_init_fs (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
     goto out;
   g_clear_object (&child);
 
-  child = g_file_get_child (dir, "ostree");
-  if (!ot_admin_ensure_initialized (child, cancellable, error))
+  if (!ot_admin_ensure_initialized (dir, cancellable, error))
     goto out;
 
   ret = TRUE;
diff --git a/src/ostree/ot-admin-builtin-install.c b/src/ostree/ot-admin-builtin-install.c
index 5f5fb3d..675f52c 100644
--- a/src/ostree/ot-admin-builtin-install.c
+++ b/src/ostree/ot-admin-builtin-install.c
@@ -70,7 +70,6 @@ ot_admin_builtin_install (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
   gboolean ret = FALSE;
   const char *keyfile_arg = NULL;
   const char *treename_arg = NULL;
-  GFile *ostree_dir = admin_opts->ostree_dir;
   ot_lobj GFile *deploy_dir = NULL;
   ot_lobj GFile *osdir = NULL;
   ot_lobj GFile *dest_osconfig_path = NULL;
@@ -96,14 +95,7 @@ ot_admin_builtin_install (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
       goto out;
     }
 
-  if (admin_opts->ostree_dir == NULL)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                   "No existing /ostree found; use --ostree-dir");
-      goto out;
-    }
-
-  if (!ot_admin_ensure_initialized (admin_opts->ostree_dir, cancellable, error))
+  if (!ot_admin_ensure_initialized (admin_opts->sysroot, cancellable, error))
     goto out;
 
   self->loop = g_main_loop_new (NULL, TRUE);
@@ -136,11 +128,11 @@ ot_admin_builtin_install (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
 
   osname = g_key_file_get_string (keyfile, "os", "Name", error);
 
-  ostree_dir_arg = g_strconcat ("--ostree-dir=",
-                                gs_file_get_path_cached (ostree_dir),
+  ostree_dir_arg = g_strconcat ("--sysroot=",
+                                gs_file_get_path_cached (admin_opts->sysroot),
                                 NULL);
 
-  if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
+  if (!gs_subprocess_simple_run_sync (NULL,
                                       GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                       cancellable, error,
                                       "ostree", "admin", ostree_dir_arg, "os-init", osname, NULL))
@@ -157,7 +149,7 @@ ot_admin_builtin_install (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
         goto out;
     }
 
-  osdir = ot_gfile_get_child_build_path (ostree_dir, "deploy", osname, NULL);
+  osdir = ot_gfile_get_child_build_path (admin_opts->sysroot, "ostree", "deploy", osname, NULL);
   dest_osconfig_path = ot_gfile_get_child_strconcat (osdir, osname, ".cfg", NULL);
 
   if (!g_file_copy (self->osconfig_path, dest_osconfig_path, G_FILE_COPY_OVERWRITE | 
G_FILE_COPY_TARGET_DEFAULT_PERMS,
@@ -168,7 +160,7 @@ ot_admin_builtin_install (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
     goto out;
 
   repoarg = g_strconcat ("--repo=",
-                         gs_file_get_path_cached (ostree_dir), "/repo",
+                         gs_file_get_path_cached (admin_opts->sysroot), "/ostree/repo",
                          NULL);
 
   {
@@ -178,7 +170,7 @@ ot_admin_builtin_install (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
     if (!repourl)
       goto out;
     
-    if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
+    if (!gs_subprocess_simple_run_sync (NULL,
                                         GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                         cancellable, error,
                                         "ostree", repoarg, "remote", "add",
@@ -186,13 +178,13 @@ ot_admin_builtin_install (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
       goto out;
   }
 
-  if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
+  if (!gs_subprocess_simple_run_sync (NULL,
                                       GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                       cancellable, error,
                                         "ostree", "pull", repoarg, osname, NULL))
     goto out;
 
-  if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
+  if (!gs_subprocess_simple_run_sync (NULL,
                                       GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                       cancellable, error,
                                       "ostree", "admin", ostree_dir_arg, "deploy", osname,
diff --git a/src/ostree/ot-admin-builtin-os-init.c b/src/ostree/ot-admin-builtin-os-init.c
index 0536167..8d725fd 100644
--- a/src/ostree/ot-admin-builtin-os-init.c
+++ b/src/ostree/ot-admin-builtin-os-init.c
@@ -38,7 +38,6 @@ ot_admin_builtin_os_init (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
   GOptionContext *context;
   gboolean ret = FALSE;
   const char *osname = NULL;
-  GFile *ostree_dir = admin_opts->ostree_dir;
   ot_lobj GFile *deploy_dir = NULL;
   ot_lobj GFile *dir = NULL;
   __attribute__((unused)) GCancellable *cancellable = NULL;
@@ -49,7 +48,7 @@ ot_admin_builtin_os_init (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
   if (!g_option_context_parse (context, &argc, &argv, error))
     goto out;
 
-  if (!ot_admin_ensure_initialized (ostree_dir, cancellable, error))
+  if (!ot_admin_ensure_initialized (admin_opts->sysroot, cancellable, error))
     goto out;
 
   if (argc < 2)
@@ -60,7 +59,7 @@ ot_admin_builtin_os_init (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
 
   osname = argv[1];
 
-  deploy_dir = ot_gfile_get_child_build_path (ostree_dir, "deploy", osname, NULL);
+  deploy_dir = ot_gfile_get_child_build_path (admin_opts->sysroot, "ostree", "deploy", osname, NULL);
 
   /* Ensure core subdirectories of /var exist, since we need them for
    * dracut generation, and the host will want them too.  Note that at
diff --git a/src/ostree/ot-admin-builtin-prune.c b/src/ostree/ot-admin-builtin-prune.c
index 4d60099..024abd2 100644
--- a/src/ostree/ot-admin-builtin-prune.c
+++ b/src/ostree/ot-admin-builtin-prune.c
@@ -41,15 +41,12 @@ ot_admin_builtin_prune (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, G
 {
   GOptionContext *context;
   gboolean ret = FALSE;
-  guint i;
   const char *osname;
-  GFile *ostree_dir = admin_opts->ostree_dir;
   ot_lobj GFile *repo_path = NULL;
   ot_lobj GFile *deploy_dir = NULL;
   ot_lobj GFile *current_deployment = NULL;
   ot_lobj GFile *previous_deployment = NULL;
   ot_lobj GFile *active_deployment = NULL;
-  ot_lptrarray GPtrArray *deployments = NULL;
   gs_free char *active_osname = NULL;
   __attribute__((unused)) GCancellable *cancellable = NULL;
 
@@ -68,43 +65,10 @@ ot_admin_builtin_prune (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, G
 
   osname = argv[1];
 
-  if (!ot_admin_list_deployments (ostree_dir, osname, &deployments,
-                                  cancellable, error))
+  if (!ot_admin_cleanup (admin_opts->sysroot, cancellable, error))
     goto out;
 
-  if (!ot_admin_get_current_deployment (ostree_dir, osname, &current_deployment,
-                                        cancellable, error));
-  if (!ot_admin_get_previous_deployment (ostree_dir, osname, &previous_deployment,
-                                         cancellable, error));
-  if (!ot_admin_get_active_deployment (ostree_dir, &active_osname, &active_deployment,
-                                       cancellable, error));
-
-  for (i = 0; i < deployments->len; i++)
-    {
-      GFile *deployment = deployments->pdata[i];
-      ot_lobj GFile *deployment_etc = NULL;
-      ot_lobj GFile *parent = NULL;
-
-      if ((current_deployment && g_file_equal (deployment, current_deployment))
-          || (previous_deployment && g_file_equal (deployment, previous_deployment))
-          || (active_deployment && g_file_equal (deployment, active_deployment)))
-        continue;
-
-      parent = g_file_get_parent (deployment);
-      deployment_etc = ot_gfile_get_child_strconcat (parent, gs_file_get_basename_cached (deployment),
-                                                     "-etc", NULL);
-      
-      g_print ("Deleting deployment %s\n", gs_file_get_path_cached (deployment));
-      if (!gs_shutil_rm_rf (deployment, cancellable, error))
-        goto out;
-      /* Note - not atomic; we may be leaving the -etc directory around
-       * if this fails in the middle =/
-       */
-      if (!gs_shutil_rm_rf (deployment_etc, cancellable, error))
-        goto out;
-    }
-  
-  repo_path = g_file_get_child (ostree_dir, "repo");
+  repo_path = g_file_resolve_relative_path (admin_opts->sysroot, "ostree/repo");
 
   if (!opt_no_repo_prune)
     {
@@ -112,7 +76,7 @@ ot_admin_builtin_prune (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, G
 
       repo_arg = g_strconcat ("--repo=", gs_file_get_path_cached (repo_path), NULL);
       
-      if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
+      if (!gs_subprocess_simple_run_sync (NULL,
                                           GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                           cancellable, error,
                                           "ostree", repo_arg, "prune", "--refs-only",
diff --git a/src/ostree/ot-admin-builtin-pull-deploy.c b/src/ostree/ot-admin-builtin-pull-deploy.c
index cfdd9e6..15b0ef1 100644
--- a/src/ostree/ot-admin-builtin-pull-deploy.c
+++ b/src/ostree/ot-admin-builtin-pull-deploy.c
@@ -95,7 +95,6 @@ ot_admin_builtin_pull_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_o
   gboolean ret = FALSE;
   const char *osname;
   const char *target;
-  GFile *ostree_dir = admin_opts->ostree_dir;
   ot_lobj OstreeRepo *repo = NULL;
   ot_lobj GFile *repo_path = NULL;
   ot_lobj GFile *current_deployment = NULL;
@@ -105,67 +104,45 @@ ot_admin_builtin_pull_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_o
   ot_lptrarray GPtrArray *subproc_args = NULL;
   __attribute__((unused)) GCancellable *cancellable = NULL;
 
-  context = g_option_context_new ("OSNAME [TREE] - Ensure TREE (default current) is in list of remotes, then 
download and deploy");
+  context = g_option_context_new ("OSNAME TREE - Ensure TREE (default current) is in list of remotes, then 
download and deploy");
 
   g_option_context_add_main_entries (context, options, NULL);
 
   if (!g_option_context_parse (context, &argc, &argv, error))
     goto out;
 
-  if (argc < 2)
+  if (argc < 3)
     {
-      ot_util_usage_error (context, "OSNAME must be specified", error);
+      ot_util_usage_error (context, "OSNAME and TREE must be specified", error);
       goto out;
     }
 
   osname = argv[1];
+  target = argv[2];
 
-  repo_path = g_file_get_child (ostree_dir, "repo");
+  repo_path = g_file_resolve_relative_path (admin_opts->sysroot, "ostree/repo");
 
   repo = ostree_repo_new (repo_path);
   if (!ostree_repo_check (repo, error))
     goto out;
 
-  if (argc > 2)
-    {
-      target = argv[2];
-      if (!ensure_remote_branch (repo, osname, target,
-                                cancellable, error))
-        goto out;
-      
-      deploy_name = g_strdup (target);
-    }
-  else
-    {
-      if (!ot_admin_get_current_deployment (ostree_dir, osname, &current_deployment,
-                                            cancellable, error))
-        goto out;
-      
-      if (!current_deployment)
-        {
-          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                       "No current deployment");
-          goto out;
-        }
-      
-      ot_admin_parse_deploy_name (ostree_dir, osname, current_deployment,
-                                  &deploy_name, NULL);
-    }
-
-  if (!ot_admin_pull (ostree_dir, osname, cancellable, error))
+  if (!ensure_remote_branch (repo, osname, target,
+                             cancellable, error))
+    goto out;
+  
+  deploy_name = g_strdup (target);
+  
+  if (!ot_admin_pull (admin_opts->sysroot, osname, cancellable, error))
     goto out;
 
   {
-    ot_lfree char *opt_ostree_dir_arg = g_strconcat ("--ostree-dir=",
-                                                     gs_file_get_path_cached (ostree_dir),
-                                                     NULL);
-    ot_lfree char *opt_boot_dir_arg = g_strconcat ("--boot-dir=",
-                                                     gs_file_get_path_cached (admin_opts->boot_dir),
+    ot_lfree char *opt_ostree_dir_arg = g_strconcat ("--sysroot=",
+                                                     gs_file_get_path_cached (admin_opts->sysroot),
                                                      NULL);
-    if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
+    if (!gs_subprocess_simple_run_sync (NULL,
                                         GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                         cancellable, error,
-                                        "ostree", "admin", opt_ostree_dir_arg, opt_boot_dir_arg, "deploy", 
osname,
+                                        "ostree", "admin", opt_ostree_dir_arg, "deploy", osname,
                                         deploy_name, NULL))
       goto out;
   }
diff --git a/src/ostree/ot-admin-builtin-upgrade.c b/src/ostree/ot-admin-builtin-upgrade.c
index f13ce21..99219d1 100644
--- a/src/ostree/ot-admin-builtin-upgrade.c
+++ b/src/ostree/ot-admin-builtin-upgrade.c
@@ -43,15 +43,16 @@ ot_admin_builtin_upgrade (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
 {
   GOptionContext *context;
   gboolean ret = FALSE;
-  GFile *ostree_dir = admin_opts->ostree_dir;
   gs_free char *booted_osname = NULL;
   const char *osname = NULL;
   gs_unref_object GFile *deployment = NULL;
   gs_unref_object GFile *repo_path = NULL;
   gs_unref_object OstreeRepo *repo = NULL;
-  gs_free char *deploy_name = NULL;
-  gs_free char *current_rev = NULL;
-  gs_free char *new_rev = NULL;
+  gs_unref_object OtDeployment *booted_deployment = NULL;
+  int current_bootversion;
+  gs_unref_ptrarray GPtrArray *current_deployments = NULL;
+  int new_bootversion;
+  gs_unref_ptrarray GPtrArray *new_deployments = NULL;
   gs_free char *ostree_dir_arg = NULL;
   __attribute__((unused)) GCancellable *cancellable = NULL;
 
@@ -67,35 +68,37 @@ ot_admin_builtin_upgrade (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
     }
   else
     {
-      if (!ot_admin_get_booted_os (&booted_osname, NULL,
-                                   cancellable, error))
+      if (!ot_admin_get_booted_deployment (&booted_deployment, cancellable, error))
         goto out;
-      if (booted_osname == NULL)
+      
+      if (booted_deployment == NULL)
         {
           g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                        "Not in an active OSTree system; must specify OSNAME");
           goto out;
         }
-      osname = booted_osname;
+      osname = ot_deployment_get_osname (booted_deployment);
     }
   
-  if (!ot_admin_get_current_deployment (ostree_dir, osname, &deployment,
-                                        cancellable, error))
-    goto out;
-
-  ot_admin_parse_deploy_name (ostree_dir, osname, deployment, &deploy_name, &current_rev);
+  if (opt_reboot)
+    {
+      if (!ot_admin_list_deployments (admin_opts->sysroot,
+                                      &current_bootversion, &current_deployments,
+                                      cancellable, error))
+        goto out;
+    }
 
-  ostree_dir_arg = g_strconcat ("--ostree-dir=",
-                                gs_file_get_path_cached (ostree_dir),
+  ostree_dir_arg = g_strconcat ("--sysroot=",
+                                gs_file_get_path_cached (admin_opts->sysroot),
                                 NULL);
   
-  if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
+  if (!gs_subprocess_simple_run_sync (NULL,
                                       GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                       cancellable, error,
                                       "ostree", "admin", ostree_dir_arg, "pull-deploy", osname, NULL))
     goto out;
 
-  if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
+  if (!gs_subprocess_simple_run_sync (NULL,
                                       GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                       cancellable, error,
                                       "ostree", "admin", ostree_dir_arg, "prune", osname, NULL))
@@ -103,17 +106,29 @@ ot_admin_builtin_upgrade (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
 
   if (opt_reboot)
     {
-      repo_path = g_file_get_child (ostree_dir, "repo");
-
-      repo = ostree_repo_new (repo_path);
-      if (!ostree_repo_check (repo, error))
+      gboolean need_reboot = FALSE;
+      if (!ot_admin_list_deployments (admin_opts->sysroot, &new_bootversion, &new_deployments,
+                                      cancellable, error))
         goto out;
 
-      if (!ostree_repo_resolve_rev (repo, deploy_name, TRUE, &new_rev,
-                                    error))
-        goto out;
+      if (current_deployments->len == new_deployments->len)
+        {
+          if (current_deployments->len > 0)
+            {
+              OtDeployment *previous_current = current_deployments->pdata[0];
+              OtDeployment *new_current = new_deployments->pdata[0];
+
+              if (strcmp (ot_deployment_get_csum (previous_current),
+                          ot_deployment_get_csum (new_current)) != 0)
+                need_reboot = TRUE;
+            }
+        }
+      else
+        {
+          need_reboot = TRUE;
+        }
 
-      if (strcmp (current_rev, new_rev) != 0 && opt_reboot)
+      if (need_reboot)
         {
           gs_subprocess_simple_run_sync (NULL, GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                          cancellable, error,
diff --git a/src/ostree/ot-admin-builtins.h b/src/ostree/ot-admin-builtins.h
index 5fbdacd..5305170 100644
--- a/src/ostree/ot-admin-builtins.h
+++ b/src/ostree/ot-admin-builtins.h
@@ -28,8 +28,7 @@
 G_BEGIN_DECLS
 
 typedef struct {
-  GFile *ostree_dir;
-  GFile *boot_dir;
+  GFile *sysroot;
 } OtAdminBuiltinOpts;
 
 gboolean ot_admin_builtin_os_init (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GError **error);
@@ -40,7 +39,6 @@ gboolean ot_admin_builtin_prune (int argc, char **argv, OtAdminBuiltinOpts *admi
 gboolean ot_admin_builtin_pull_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GError 
**error);
 gboolean ot_admin_builtin_diff (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GError **error);
 gboolean ot_admin_builtin_run_triggers (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GError 
**error);
-gboolean ot_admin_builtin_update_kernel (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GError 
**error);
 gboolean ot_admin_builtin_upgrade (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GError **error);
 
 G_END_DECLS
diff --git a/src/ostree/ot-admin-functions.c b/src/ostree/ot-admin-functions.c
index 28eb077..555d86d 100644
--- a/src/ostree/ot-admin-functions.c
+++ b/src/ostree/ot-admin-functions.c
@@ -23,16 +23,28 @@
 #include "config.h"
 
 #include "ot-admin-functions.h"
+#include "ot-deployment.h"
 #include "otutil.h"
 #include "ostree-core.h"
+#include "libgsystem.h"
+
+static void
+match_info_cleanup (void *loc)
+{
+  GMatchInfo **match = (GMatchInfo**)loc;
+  if (*match) g_match_info_unref (*match);
+}
 
 gboolean
-ot_admin_ensure_initialized (GFile         *ostree_dir,
+ot_admin_ensure_initialized (GFile         *sysroot,
                              GCancellable  *cancellable,
                              GError       **error)
 {
   gboolean ret = FALSE;
-  ot_lobj GFile *dir = NULL;
+  gs_unref_object GFile *dir = NULL;
+  gs_unref_object GFile *ostree_dir = NULL;
+
+  ostree_dir = g_file_get_child (sysroot, "ostree");
 
   g_clear_object (&dir);
   dir = g_file_get_child (ostree_dir, "repo");
@@ -66,444 +78,832 @@ ot_admin_ensure_initialized (GFile         *ostree_dir,
 }
 
 static gboolean
-query_file_info_allow_noent (GFile         *path,
-                             GFileInfo    **out_info,
-                             GCancellable  *cancellable,
-                             GError       **error)
+parse_bootlink (const char    *bootlink,
+                int           *out_entry_bootversion,
+                char         **out_osname,
+                char         **out_bootcsum,
+                int           *out_treebootserial,
+                GError       **error)
 {
   gboolean ret = FALSE;
-  ot_lobj GFileInfo *ret_file_info = NULL;
-  GError *temp_error = NULL;
+  __attribute__((cleanup(match_info_cleanup))) GMatchInfo *match = NULL;
+  gs_free char *bootversion_str = NULL;
+  gs_free char *treebootserial_str = NULL;
+
+  static gsize regex_initialized;
+  static GRegex *regex;
 
-  ret_file_info = g_file_query_info (path, OSTREE_GIO_FAST_QUERYINFO,
-                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
-                                     cancellable, &temp_error);
-  if (!ret_file_info)
+  if (g_once_init_enter (&regex_initialized))
     {
-      if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
-        {
-          g_clear_error (&temp_error);
-        }
-      else
-        {
-          g_propagate_error (error, temp_error);
-          goto out;
-        }
+      regex = g_regex_new ("^/ostree/boot.([01])/([^/]+)/([^/]+)/([0-9]+)$", 0, 0, NULL);
+      g_assert (regex);
+      g_once_init_leave (&regex_initialized, 1);
     }
 
+  if (!g_regex_match (regex, bootlink, 0, &match))
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "Invalid ostree= argument '%s', expected 
ostree=/ostree/boot.BOOTVERSION/OSNAME/BOOTCSUM/TREESERIAL", bootlink);
+      goto out;
+    }
+    
+  bootversion_str = g_match_info_fetch (match, 1);
+  *out_entry_bootversion = (int)g_ascii_strtoll (bootversion_str, NULL, 10);
+  *out_osname = g_match_info_fetch (match, 2);
+  *out_bootcsum = g_match_info_fetch (match, 3);
+  treebootserial_str = g_match_info_fetch (match, 4);
+  *out_treebootserial = (int)g_ascii_strtoll (treebootserial_str, NULL, 10);
+  
   ret = TRUE;
-  ot_transfer_out_value (out_info, &ret_file_info);
  out:
   return ret;
 }
 
 static gboolean
-query_symlink_target_allow_noent (GFile         *path,
-                                  GFile        **out_target,
-                                  GCancellable  *cancellable,
-                                  GError       **error)
+parse_deploy_path_name (const char *name,
+                        char      **out_csum,
+                        int        *out_serial,
+                        GError    **error)
 {
   gboolean ret = FALSE;
-  ot_lobj GFileInfo *file_info = NULL;
-  ot_lobj GFile *ret_target = NULL;
-  ot_lobj GFile *path_parent = NULL;
+  __attribute__((cleanup(match_info_cleanup))) GMatchInfo *match = NULL;
+  gs_free char *serial_str = NULL;
 
-  if (!query_file_info_allow_noent (path, &file_info,
-                                    cancellable, error))
-    goto out;
+  static gsize regex_initialized;
+  static GRegex *regex;
 
-  path_parent = g_file_get_parent (path);
+  if (g_once_init_enter (&regex_initialized))
+    {
+      regex = g_regex_new ("^([0-9a-f]+)\\.([0-9]+)$", 0, 0, NULL);
+      g_assert (regex);
+      g_once_init_leave (&regex_initialized, 1);
+    }
 
-  if (file_info != NULL)
+  if (!g_regex_match (regex, name, 0, &match))
     {
-      const char *target;
-      
-      if (g_file_info_get_file_type (file_info) != G_FILE_TYPE_SYMBOLIC_LINK)
-        {
-          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                       "Not a symbolic link");
-          goto out;
-        }
-      target = g_file_info_get_symlink_target (file_info);
-      g_assert (target);
-      ret_target = g_file_resolve_relative_path (path_parent, target);
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "Invalid deploy name '%s', expected CHECKSUM.TREESERIAL", name);
+      goto out;
     }
 
+  *out_csum = g_match_info_fetch (match, 1);
+  serial_str = g_match_info_fetch (match, 2);
+  *out_serial = (int)g_ascii_strtoll (serial_str, NULL, 10);
+
   ret = TRUE;
-  ot_transfer_out_value (out_target, &ret_target);
  out:
   return ret;
 }
 
-/**
- * ot_admin_get_current_deployment:
- * 
- * Returns in @out_deployment the full file path of the current
- * deployment that the /ostree/current symbolic link points to, or
- * %NULL if none.
- */
-gboolean
-ot_admin_get_current_deployment (GFile           *ostree_dir,
-                                 const char      *osname,
-                                 GFile          **out_deployment,
-                                 GCancellable    *cancellable,
-                                 GError         **error)
+static gboolean
+parse_deployment (GFile           *sysroot,
+                  const char      *boot_link,
+                  OtDeployment   **out_deployment,
+                  GCancellable    *cancellable,
+                  GError         **error)
 {
-  ot_lobj GFile *current_path = NULL;
-
-  current_path = ot_gfile_get_child_build_path (ostree_dir, "deploy", osname,
-                                                "current", NULL);
+  gboolean ret = FALSE;
+  const char *relative_boot_link;
+  gs_unref_object OtDeployment *ret_deployment = NULL;
+  int entry_boot_version;
+  int treebootserial;
+  int deployserial;
+  gs_free char *osname = NULL;
+  gs_free char *bootcsum = NULL;
+  gs_free char *treecsum = NULL;
+  gs_unref_object GFile *treebootserial_link = NULL;
+  gs_unref_object GFileInfo *treebootserial_info = NULL;
+  gs_unref_object GFile *treebootserial_target = NULL;
+      
+  if (!parse_bootlink (boot_link, &entry_boot_version,
+                       &osname, &bootcsum, &treebootserial,
+                       error))
+    goto out;
 
-  return query_symlink_target_allow_noent (current_path, out_deployment,
+  relative_boot_link = boot_link;
+  if (*relative_boot_link == '/')
+    relative_boot_link++;
+  treebootserial_link = g_file_resolve_relative_path (sysroot, relative_boot_link);
+  treebootserial_info = g_file_query_info (treebootserial_link, OSTREE_GIO_FAST_QUERYINFO,
+                                           G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                            cancellable, error);
+  if (!treebootserial_info)
+    goto out;
+
+  if (!ot_gfile_get_symlink_target_from_info (treebootserial_link, treebootserial_info,
+                                              &treebootserial_target, cancellable, error))
+    goto out;
+
+  if (!parse_deploy_path_name (gs_file_get_basename_cached (treebootserial_target),
+                               &treecsum, &deployserial, error))
+    goto out;
+
+  ret_deployment = ot_deployment_new (-1, osname, NULL, treecsum, deployserial,
+                                      bootcsum, treebootserial);
+
+  ret = TRUE;
+  ot_transfer_out_value (out_deployment, &ret_deployment);
+ out:
+  return ret;
 }
 
 /**
- * ot_admin_get_previous_deployment:
+ * ot_admin_get_booted_deployment_dir:
  * 
  * Returns in @out_deployment the full file path of the current
- * deployment that the /ostree/previous symbolic link points to, or
+ * deployment that the /run/ostree/current symbolic link points to, or
  * %NULL if none.
  */
 gboolean
-ot_admin_get_previous_deployment (GFile           *ostree_dir,
-                                  const char      *osname,
-                                  GFile          **out_deployment,
-                                  GCancellable    *cancellable,
-                                  GError         **error)
+ot_admin_get_booted_deployment (OtDeployment       **out_deployment,
+                                GCancellable        *cancellable,
+                                GError             **error)
 {
-  ot_lobj GFile *previous_path = NULL;
+  gboolean ret = FALSE;
+  gs_unref_object GFile *current_path = g_file_new_for_path ("/run/ostree/current");
+  gs_unref_object GFile *bootlink_path = NULL;
+  gs_free char *osname = NULL;
+  gs_unref_object OtDeployment *ret_deployment = NULL;
 
-  previous_path = ot_gfile_get_child_build_path (ostree_dir, "deploy", osname,
-                                                 "previous", NULL);
+  if (!ot_gfile_query_symlink_target_allow_noent (current_path, &bootlink_path,
+                                                  cancellable, error))
+    goto out;
 
-  return query_symlink_target_allow_noent (previous_path, out_deployment,
-                                           cancellable, error);
+  if (bootlink_path)
+    {
+      gs_unref_object GFile *sysroot = g_file_new_for_path ("/");
+      const char *bootlink = gs_file_get_path_cached (bootlink_path);
+      
+      if (!parse_deployment (sysroot, bootlink, &ret_deployment,
+                             cancellable, error))
+        goto out;
+    }
+
+  ret = TRUE;
+  ot_transfer_out_value (out_deployment, &ret_deployment);
+ out:
+  return ret;
 }
 
-/*
 static gboolean
-ot_admin_list_osnames (GFile               *ostree_dir,
-                       GPtrArray          **out_osnames,
-                       GCancellable        *cancellable,
-                       GError             **error)
+read_current_bootversion (GFile         *sysroot,
+                          int           *out_bootversion,
+                          GCancellable  *cancellable,
+                          GError       **error)
 {
   gboolean ret = FALSE;
-  ot_lobj GFileEnumerator *dir_enum = NULL;
-  ot_lobj GFileInfo *file_info = NULL;
-  ot_lobj GFile *deploy_dir = NULL;
-  ot_lptrarray GPtrArray *ret_osnames = NULL;
-  GError *temp_error = NULL;
+  gs_unref_object GFile *boot_loader_path = g_file_resolve_relative_path (sysroot, "boot/loader");
+  gs_unref_object GFileInfo *info = NULL;
+  const char *target;
+  int ret_bootversion;
 
-  deploy_dir = g_file_get_child (ostree_dir, "deploy");
-
-  dir_enum = g_file_enumerate_children (deploy_dir, OSTREE_GIO_FAST_QUERYINFO,
+  if (!ot_gfile_query_info_allow_noent (boot_loader_path, OSTREE_GIO_FAST_QUERYINFO,
                                         G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
-                                        NULL, error);
-  if (!dir_enum)
+                                        &info,
+                                        cancellable, error))
+    goto out;
+
+  if (info == NULL)
+    ret_bootversion = 0;
+  else
+    {
+      if (g_file_info_get_file_type (info) != G_FILE_TYPE_SYMBOLIC_LINK)
+        {
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                       "Not a symbolic link: %s", gs_file_get_path_cached (boot_loader_path));
+          goto out;
+        }
+
+      target = g_file_info_get_symlink_target (info);
+      if (g_strcmp0 (target, "loader.0") == 0)
+        ret_bootversion = 0;
+      else if (g_strcmp0 (target, "loader.1") == 0)
+        ret_bootversion = 1;
+      else
+        {
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                       "Invalid target '%s' in %s", target, gs_file_get_path_cached (boot_loader_path));
+          goto out;
+        }
+    }
+
+  ret = TRUE;
+  *out_bootversion = ret_bootversion;
+ out:
+  return ret;
+}
+
+gboolean
+ot_admin_read_current_subbootversion (GFile         *sysroot,
+                                      int            bootversion,
+                                      int           *out_subbootversion,
+                                      GCancellable  *cancellable,
+                                      GError       **error)
+{
+  gboolean ret = FALSE;
+  gs_unref_object GFile *ostree_dir = g_file_get_child (sysroot, "ostree");
+  gs_free char *ostree_bootdir_name = g_strdup_printf ("boot.%d", bootversion);
+  gs_unref_object GFile *ostree_bootdir = g_file_resolve_relative_path (ostree_dir, ostree_bootdir_name);
+  gs_free char *ostree_subbootdir_name = NULL;
+  gs_unref_object GFile *ostree_subbootdir = NULL;
+  gs_unref_ptrarray GPtrArray *deployments_to_swap = NULL;
+
+  if (!ot_gfile_query_symlink_target_allow_noent (ostree_bootdir, &ostree_subbootdir,
+                                                  cancellable, error))
     goto out;
 
-  while ((file_info = g_file_enumerator_next_file (dir_enum, NULL, error)) != NULL)
+  if (ostree_subbootdir == NULL)
+    {
+      *out_subbootversion = 0;
+    }
+  else
     {
-      if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
+      const char *current_subbootdir_name = gs_file_get_basename_cached (ostree_subbootdir);
+      if (g_str_has_suffix (current_subbootdir_name, ".0"))
+        *out_subbootversion = 0;
+      else if (g_str_has_suffix (current_subbootdir_name, ".1"))
+        *out_subbootversion = 1;
+      else
         {
-          char *name = g_strdup (g_file_info_get_name (file_info));
-          g_ptr_array_add (ret_osnames, name);
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                       "Invalid target '%s' in %s",
+                       gs_file_get_path_cached (ostree_subbootdir),
+                       gs_file_get_path_cached (ostree_bootdir));
+          goto out;
         }
-      g_clear_object (&file_info);
     }
 
-  if (temp_error != NULL)
+  ret = TRUE;
+ out:
+  return ret;
+}
+
+static gboolean
+read_one_boot_loader_entry_lines (GFile           *boot_dir,
+                                  GFile           *config,
+                                  GVariant       **out_entry,
+                                  GCancellable    *cancellable,
+                                  GError         **error)
+{
+  gboolean ret = FALSE;
+  gs_unref_variant GVariant*ret_entry = NULL;
+  gboolean builder_initialized = FALSE;
+  GVariantBuilder builder;
+  gs_free char *contents = NULL;
+  gs_free char *relpath = NULL;
+  char **lines = NULL;
+  char **iter = NULL;
+
+  contents = gs_file_load_contents_utf8 (config, cancellable, error);
+  if (!contents)
+    goto out;
+
+  builder_initialized = TRUE;
+  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(sss)"));
+
+  lines = g_strsplit (contents, "\n", -1);
+  for (iter = lines; *iter; iter++)
     {
-      g_propagate_error (error, temp_error);
-      goto out;
+      const char *line = *iter;
+      char **items = NULL;
+      gboolean iskey = FALSE;
+      
+      if (g_ascii_isalpha (*line))
+        {
+          items = g_strsplit_set (line, " \t", 2);
+          if (g_strv_length (items) == 2)
+            {
+              g_variant_builder_add (&builder, "(sss)", items[0], items[1], line);
+              iskey = TRUE;
+            }
+        }
+      if (!iskey)
+        g_variant_builder_add (&builder, "(sss)", "", "",
+                               line);
+      g_strfreev (items);
     }
-  
+
+  relpath = g_file_get_relative_path (boot_dir, config);
+  *out_entry = g_variant_new ("(s a(sss))", relpath, 
+                              g_variant_builder_end (&builder));
+  builder_initialized = FALSE;
   ret = TRUE;
-  ot_transfer_out_value (out_osnames, &ret_osnames);
  out:
+  if (builder_initialized)
+    g_variant_builder_clear (&builder);
+  g_strfreev (lines);
   return ret;
 }
-*/
 
 static gboolean
-list_deployments_internal (GFile               *from_dir,
-                           GPtrArray           *inout_deployments,
-                           GCancellable        *cancellable,
-                           GError             **error)
+read_boot_loader_entry_lines (GFile         *boot_dir,
+                               GPtrArray    **out_loader_entries,
+                               GCancellable  *cancellable,
+                               GError       **error)
 {
   gboolean ret = FALSE;
+  gs_unref_object GFileEnumerator *dir_enum = NULL;
+  gs_unref_object GFile *loader_entries_dir = NULL;
+  gs_unref_ptrarray GPtrArray *ret_loader_entries = NULL;
   GError *temp_error = NULL;
-  ot_lobj GFileEnumerator *dir_enum = NULL;
-  ot_lobj GFileInfo *file_info = NULL;
 
-  dir_enum = g_file_enumerate_children (from_dir, OSTREE_GIO_FAST_QUERYINFO,
-                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
-                                        NULL, error);
+  loader_entries_dir = g_file_resolve_relative_path (boot_dir, "loader/entries");
+  ret_loader_entries = g_ptr_array_new_with_free_func ((GDestroyNotify)g_variant_unref);
+
+  dir_enum = g_file_enumerate_children (loader_entries_dir, OSTREE_GIO_FAST_QUERYINFO,
+                                        0, NULL, &temp_error);
   if (!dir_enum)
-    goto out;
+    {
+      if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+        {
+          g_clear_error (&temp_error);
+          goto done;
+        } 
+      else
+        {
+          g_propagate_error (error, temp_error);
+          goto out;
+        }
+    }
 
-  while ((file_info = g_file_enumerator_next_file (dir_enum, cancellable, error)) != NULL)
+  while (TRUE)
     {
+      GFileInfo *file_info;
+      GFile *child;
       const char *name;
-      ot_lobj GFile *child = NULL;
-      ot_lobj GFile *possible_etc = NULL;
-      ot_lobj GFile *possible_usr = NULL;
+
+      if (!gs_file_enumerator_iterate (dir_enum, &file_info, &child,
+                                       cancellable, error))
+        goto out;
+      if (file_info == NULL)
+        break;
 
       name = g_file_info_get_name (file_info);
 
-      if (g_str_has_suffix (name, "-etc"))
-        goto next;
-      if (g_file_info_get_file_type (file_info) != G_FILE_TYPE_DIRECTORY)
-        goto next;
+      if (g_str_has_prefix (name, "ostree-") &&
+          g_str_has_suffix (name, ".conf") &&
+          g_file_info_get_file_type (file_info) == G_FILE_TYPE_REGULAR)
+        {
+          GVariant *entry;
+          if (!read_one_boot_loader_entry_lines (boot_dir, child, &entry,
+                                                 cancellable, error))
+            goto out;
+          g_ptr_array_add (ret_loader_entries, entry);  /* Transfer ownership */
+        }
+    }
+
+ done:
+  ot_transfer_out_value (out_loader_entries, &ret_loader_entries);
+  ret = TRUE;
+ out:
+  return ret;
+}
 
-      child = g_file_get_child (from_dir, name);
+static gboolean
+list_deployment_dirs_for_os (GFile               *osdir,
+                             GPtrArray           *inout_deployments,
+                             GCancellable        *cancellable,
+                             GError             **error)
+{
+  gboolean ret = FALSE;
+  const char *osname = gs_file_get_basename_cached (osdir);
+  gs_unref_object GFileEnumerator *dir_enum = NULL;
+  gs_unref_object GFile *osdeploy_dir = NULL;
+  GError *temp_error = NULL;
 
-      possible_etc = ot_gfile_get_child_strconcat (from_dir, name, "-etc", NULL);
-      /* Bit of a hack... */
-      possible_usr = g_file_get_child (child, "usr");
+  osdeploy_dir = g_file_get_child (osdir, "deploy");
 
-      if (g_file_query_exists (possible_etc, cancellable))
-        g_ptr_array_add (inout_deployments, g_file_get_child (from_dir, name));
-      else if (g_file_query_exists (possible_usr, cancellable))
-        goto next;
+  dir_enum = g_file_enumerate_children (osdeploy_dir, OSTREE_GIO_FAST_QUERYINFO,
+                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                        NULL, &temp_error);
+  if (!dir_enum)
+    {
+      if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+        {
+          g_clear_error (&temp_error);
+          goto done;
+        } 
       else
         {
-          if (!list_deployments_internal (child, inout_deployments,
-                                          cancellable, error))
-            goto out;
+          g_propagate_error (error, temp_error);
+          goto out;
         }
-
-    next:
-      g_clear_object (&file_info);
     }
-  if (temp_error != NULL)
+
+  while (TRUE)
     {
-      g_propagate_error (error, temp_error);
-      goto out;
+      const char *name;
+      GFileInfo *file_info = NULL;
+      GFile *child = NULL;
+      gs_unref_object OtDeployment *deployment = NULL;
+      gs_free char *csum = NULL;
+      gint deployserial;
+
+      if (!gs_file_enumerator_iterate (dir_enum, &file_info, &child,
+                                       cancellable, error))
+        goto out;
+      if (file_info == NULL)
+        break;
+
+      name = g_file_info_get_name (file_info);
+
+      if (g_file_info_get_file_type (file_info) != G_FILE_TYPE_DIRECTORY)
+        continue;
+
+      if (!parse_deploy_path_name (name, &csum, &deployserial, error))
+        goto out;
+      
+      deployment = ot_deployment_new (-1, osname, NULL, csum, deployserial, NULL, -1);
+      g_ptr_array_add (inout_deployments, g_object_ref (deployment));
     }
 
+ done:
   ret = TRUE;
  out:
   return ret;
 }
 
-gboolean
-ot_admin_list_deployments (GFile               *ostree_dir,
-                           const char          *osname,
-                           GPtrArray          **out_deployments,
-                           GCancellable        *cancellable,
-                           GError             **error)
+static gboolean
+list_all_deployment_directories (GFile               *sysroot,
+                                 GPtrArray          **out_deployments,
+                                 GCancellable        *cancellable,
+                                 GError             **error)
 {
   gboolean ret = FALSE;
-  ot_lobj GFileEnumerator *dir_enum = NULL;
-  ot_lobj GFileInfo *file_info = NULL;
-  ot_lobj GFile *osdir = NULL;
-  ot_lptrarray GPtrArray *ret_deployments = NULL;
+  gs_unref_object GFileEnumerator *dir_enum = NULL;
+  gs_unref_object GFile *deploydir = NULL;
+  gs_unref_object GFile *osdir = NULL;
+  gs_unref_ptrarray GPtrArray *ret_deployments = NULL;
+  GError *temp_error = NULL;
+
+  deploydir = g_file_resolve_relative_path (sysroot, "ostree/deploy");
 
-  osdir = ot_gfile_get_child_build_path (ostree_dir, "deploy", osname, NULL);
   ret_deployments = g_ptr_array_new_with_free_func (g_object_unref);
 
-  if (!list_deployments_internal (osdir, ret_deployments, cancellable, error))
-    goto out;
+  dir_enum = g_file_enumerate_children (deploydir, OSTREE_GIO_FAST_QUERYINFO,
+                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                        cancellable, &temp_error);
+  if (!dir_enum)
+    {
+      if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+        {
+          g_clear_error (&temp_error);
+          goto done;
+        } 
+      else
+        {
+          g_propagate_error (error, temp_error);
+          goto out;
+        }
+    }
+
+  while (TRUE)
+    {
+      GFileInfo *file_info = NULL;
+      GFile *child = NULL;
+
+      if (!gs_file_enumerator_iterate (dir_enum, &file_info, &child,
+                                       NULL, error))
+        goto out;
+      if (file_info == NULL)
+        break;
+
+      if (g_file_info_get_file_type (file_info) != G_FILE_TYPE_DIRECTORY)
+        continue;
+      
+      if (!list_deployment_dirs_for_os (child, ret_deployments, cancellable, error))
+        goto out;
+    }
   
+ done:
   ret = TRUE;
   ot_transfer_out_value (out_deployments, &ret_deployments);
  out:
   return ret;
 }
 
-gboolean
-ot_admin_get_booted_os (char        **out_osname,
-                        char        **out_tree,
-                        GCancellable *cancellable,
-                        GError      **error)
+static char *
+get_ostree_kernel_arg_from_entry (GVariant  *boot_loader_entry)
 {
-  gboolean ret = FALSE;
-  gs_free char *ret_osname = NULL;
-  gs_free char *ret_tree = NULL;
-  gs_free char *cmdline_contents = NULL;
-  const char *iter;
-  gsize len;
-
-  if (!g_file_get_contents ("/proc/cmdline", &cmdline_contents, &len,
-                            error))
-    goto out;
-
-  iter = cmdline_contents;
-  do
+  GVariantIter iter;
+  const char *key;
+  const char *value;
+  const char *line;
+  char *ret = NULL;
+  gs_unref_variant GVariant *lines = g_variant_get_child_value (boot_loader_entry, 1);
+
+  g_variant_iter_init (&iter, lines);
+  while (g_variant_iter_loop (&iter, "(&s&s&s)", &key, &value, &line))
     {
-      const char *next = strchr (iter, ' ');
-      if (next)
-       next += 1;
-      if (g_str_has_prefix (iter, "ostree="))
+      char **opts, **iter;
+      if (g_strcmp0 (key, "options") != 0)
+        continue;
+     
+      opts = g_strsplit (value, " ", -1);
+      for (iter = opts; *iter; iter++)
         {
-          const char *slash = strchr (iter, '/');
-          if (slash)
+          const char *opt = *iter;
+          if (g_str_has_prefix (opt, "ostree="))
             {
-              const char *start = iter + strlen ("ostree=");
-              ret_osname = g_strndup (start, slash - start);
-              if (next)
-                ret_tree = g_strndup (slash + 1, next - slash - 1);
-              else
-                ret_tree = g_strdup (slash + 1);
+              ret = g_strdup (opt + strlen ("ostree="));
               break;
             }
         }
-      iter = next;
+      g_strfreev (opts);
+      if (ret)
+        break;
     }
-  while (iter != NULL);
 
-  ret = TRUE;
- out:
-  ot_transfer_out_value (out_osname, &ret_osname);
-  ot_transfer_out_value (out_tree, &ret_tree);
   return ret;
 }
 
-gboolean
-ot_admin_get_active_deployment (GFile           *ostree_dir,
-                                char           **out_osname,
-                                GFile          **out_deployment,
-                                GCancellable    *cancellable,
-                                GError         **error)
+static gboolean
+list_deployments_process_one_boot_entry (GFile               *sysroot,
+                                         guint                index,
+                                         GVariant            *entry,
+                                         GPtrArray           *inout_deployments,
+                                         GCancellable        *cancellable,
+                                         GError             **error)
 {
   gboolean ret = FALSE;
-  ot_lptrarray GPtrArray *osnames = NULL;
-  ot_lptrarray GPtrArray *deployments = NULL;
-  gs_free char *ret_osname = NULL;
-  gs_unref_object GFile *ret_deployment = NULL;
-
-  if (!ot_admin_get_booted_os (&ret_osname, NULL, cancellable, error))
-    goto out;
+  gs_free char *ostree_arg = NULL;
+  gs_unref_object OtDeployment *deployment = NULL;
+  gs_unref_variant GVariant *lines = g_variant_get_child_value (entry, 1);
 
-  if (ret_osname != NULL && out_deployment != NULL)
+  ostree_arg = get_ostree_kernel_arg_from_entry (entry);
+  if (ostree_arg == NULL)
     {
-      gs_unref_object GFile *rootfs_path = NULL;
-      gs_unref_object GFileInfo *rootfs_info = NULL;
-      guint32 root_dev;
-      guint64 root_inode;
-      guint i;
-
-      rootfs_path = g_file_new_for_path ("/");
-      rootfs_info = g_file_query_info (rootfs_path, OSTREE_GIO_FAST_QUERYINFO,
-                                       G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
-                                       cancellable, error);
-      if (!rootfs_info)
-        goto out;
-
-      root_dev = g_file_info_get_attribute_uint32 (rootfs_info, "unix::device");
-      root_inode = g_file_info_get_attribute_uint64 (rootfs_info, "unix::inode");
-
-      if (!ot_admin_list_deployments (ostree_dir, ret_osname, &deployments,
-                                      cancellable, error))
-        goto out;
-      
-      for (i = 0; i < deployments->len; i++)
-        {
-          GFile *deployment = deployments->pdata[i];
-          gs_unref_object GFileInfo *deployment_info = NULL;
-          guint32 deploy_dev;
-          guint64 deploy_inode;
-          
-          deployment_info = g_file_query_info (deployment, OSTREE_GIO_FAST_QUERYINFO,
-                                               G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
-                                               cancellable, error);
-          if (!deployment_info)
-            goto out;
-
-          deploy_dev = g_file_info_get_attribute_uint32 (deployment_info, "unix::device");
-          deploy_inode = g_file_info_get_attribute_uint64 (deployment_info, "unix::inode");
-
-          if (root_dev == deploy_dev && root_inode == deploy_inode)
-            {
-              ret_deployment = g_object_ref (deployment);
-              break;
-            }
-        }
-      
-      g_assert (ret_deployment != NULL);
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "No ostree= kernel argument found");
+      goto out;
     }
+  
+  if (!parse_deployment (sysroot, ostree_arg, &deployment,
+                         cancellable, error))
+    goto out;
+  
+  ot_deployment_set_index (deployment, index);
+  ot_deployment_set_bootconfig (deployment, lines);
 
+  g_ptr_array_add (inout_deployments, g_object_ref (deployment));
+  
   ret = TRUE;
-  ot_transfer_out_value (out_osname, &ret_osname);
-  ot_transfer_out_value (out_deployment, &ret_deployment);
  out:
   return ret;
 }
 
 gboolean
-ot_admin_get_default_ostree_dir (GFile        **out_ostree_dir,
-                                 GCancellable  *cancellable,
-                                 GError       **error)
+ot_admin_list_deployments (GFile               *sysroot,
+                           int                 *out_current_bootversion,
+                           GPtrArray          **out_deployments,
+                           GCancellable        *cancellable,
+                           GError             **error)
 {
   gboolean ret = FALSE;
-  gs_unref_object GFile *possible_ostree_dir = NULL;
-  gs_unref_object GFile *ret_ostree_dir = NULL;
-  gs_unref_object GFile *host_usr = NULL;
+  gs_unref_object GFile *boot_dir = g_file_get_child (sysroot, "boot");
+  gs_unref_ptrarray GPtrArray *boot_loader_entries = NULL;
+  gs_unref_ptrarray GPtrArray *ret_deployments = NULL;
+  guint i;
+  int bootversion;
 
-  host_usr = g_file_new_for_path ("/usr");
+  if (!read_current_bootversion (sysroot, &bootversion, cancellable, error))
+    goto out;
 
-  if (ret_ostree_dir == NULL)
-    {
-      g_clear_object (&possible_ostree_dir);
-      possible_ostree_dir = g_file_new_for_path ("/sysroot/ostree");
-      if (g_file_query_exists (possible_ostree_dir, NULL))
-        ret_ostree_dir = g_object_ref (possible_ostree_dir);
-    }
-  if (ret_ostree_dir == NULL)
+  if (!read_boot_loader_entry_lines (boot_dir, &boot_loader_entries,
+                                     cancellable, error))
+    goto out;
+
+  ret_deployments = g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref);
+
+  for (i = 0; i < boot_loader_entries->len; i++)
     {
-      g_clear_object (&possible_ostree_dir);
-      possible_ostree_dir = g_file_new_for_path ("/ostree");
-      /* If there's also /usr, we assume we're outside an ostree root
-       * and thus should use /ostree.
-       */
-      if (g_file_query_exists (possible_ostree_dir, NULL) ||
-          g_file_query_exists (host_usr, NULL))
-        ret_ostree_dir = g_object_ref (possible_ostree_dir);
+      const char *path;
+      GVariant *entry = boot_loader_entries->pdata[i];
+      g_variant_get_child (entry, 0, "&s", &path);
+
+      if (!list_deployments_process_one_boot_entry (sysroot, (guint)i,
+                                                    entry, ret_deployments,
+                                                    cancellable, error))
+        {
+          g_prefix_error (error, "Parsing bootloader entry '%s': ", path);
+          goto out;
+        }
     }
 
   ret = TRUE;
-  ot_transfer_out_value (out_ostree_dir, &ret_ostree_dir);
+  *out_current_bootversion = bootversion;
+  ot_transfer_out_value (out_deployments, &ret_deployments);
+ out:
   return ret;
 }
 
 gboolean
-ot_admin_pull (GFile         *ostree_dir,
+ot_admin_pull (GFile         *sysroot,
                const char    *osname,
                GCancellable  *cancellable,
                GError       **error)
 {
-  gs_unref_object GFile *repo_path = g_file_get_child (ostree_dir, "repo");
+  gs_unref_object GFile *repo_path = g_file_resolve_relative_path (sysroot, "ostree/repo");
   gs_free char *repo_arg = g_strconcat ("--repo=",
                                         gs_file_get_path_cached (repo_path),
                                         NULL);
 
-  return gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
+  return gs_subprocess_simple_run_sync (NULL,
                                         GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                         cancellable, error,
                                         "ostree", repo_arg, "pull", osname, NULL);
 }
 
-void
-ot_admin_parse_deploy_name (GFile       *ostree_dir,
-                            const char  *osname,
-                            GFile       *deployment,
-                            char       **out_name,
-                            char       **out_rev)
+GFile *
+ot_admin_get_deployment_directory (GFile        *sysroot,
+                                   OtDeployment *deployment)
 {
-  gs_unref_object GFile *deploy_dir = g_file_get_child (ostree_dir, "deploy");
-  gs_unref_object GFile *os_dir = g_file_get_child (deploy_dir, osname);
-  gs_free char *relpath = g_file_get_relative_path (os_dir, deployment);
-  const char *last_dash;
-
-  g_assert (relpath);
-  last_dash = strrchr (relpath, '-');
-  if (!last_dash)
-    g_error ("Failed to parse deployment name %s", relpath);
+  gs_free char *path = g_strdup_printf ("ostree/deploy/%s/deploy/%s.%d",
+                                        ot_deployment_get_osname (deployment),
+                                        ot_deployment_get_csum (deployment),
+                                        ot_deployment_get_deployserial (deployment));
+  return g_file_resolve_relative_path (sysroot, path);
+}
+
+gboolean
+ot_admin_cleanup (GFile               *sysroot,
+                  GCancellable        *cancellable,
+                  GError             **error)
+{
+  gboolean ret = FALSE;
+  gs_unref_hashtable GHashTable *active_deployment_dirs = NULL;
+  gs_unref_ptrarray GPtrArray *deployments = NULL;
+  gs_unref_ptrarray GPtrArray *all_deployment_dirs = NULL;
+  gs_free char *cleanup_boot_name = NULL;
+  gs_unref_object GFile *cleanup_boot_dir = NULL;
+  int bootversion;
+  int subbootversion;
+  int cleanup_bootversion;
+  int cleanup_subbootversion;
+  guint i;
+
+  active_deployment_dirs = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal, NULL, 
g_object_unref);
+
+  if (!ot_admin_list_deployments (sysroot, &bootversion, &deployments,
+                                  cancellable, error))
+    goto out;
+
+  if (!ot_admin_read_current_subbootversion (sysroot, bootversion, &subbootversion,
+                                             cancellable, error))
+    goto out;
+
+  cleanup_bootversion = bootversion == 0 ? 1 : 0;
+  cleanup_subbootversion = subbootversion == 0 ? 1 : 0;
+
+  cleanup_boot_dir = ot_gfile_resolve_path_printf (sysroot, "boot/loader.%d", cleanup_bootversion);
+  if (!gs_shutil_rm_rf (cleanup_boot_dir, cancellable, error))
+    goto out;
+  g_clear_object (&cleanup_boot_dir);
+
+  cleanup_boot_dir = ot_gfile_resolve_path_printf (sysroot, "ostree/boot.%d", cleanup_bootversion);
+  if (!gs_shutil_rm_rf (cleanup_boot_dir, cancellable, error))
+    goto out;
+  g_clear_object (&cleanup_boot_dir);
+
+  cleanup_boot_dir = ot_gfile_resolve_path_printf (sysroot, "ostree/boot.%d.0", cleanup_bootversion);
+  if (!gs_shutil_rm_rf (cleanup_boot_dir, cancellable, error))
+    goto out;
+  g_clear_object (&cleanup_boot_dir);
+
+  cleanup_boot_dir = ot_gfile_resolve_path_printf (sysroot, "ostree/boot.%d.1", cleanup_bootversion);
+  if (!gs_shutil_rm_rf (cleanup_boot_dir, cancellable, error))
+    goto out;
+  g_clear_object (&cleanup_boot_dir);
+
+  cleanup_boot_dir = ot_gfile_resolve_path_printf (sysroot, "ostree/boot.%d.%d", bootversion,
+                                                   cleanup_subbootversion);
+  if (!gs_shutil_rm_rf (cleanup_boot_dir, cancellable, error))
+    goto out;
+  g_clear_object (&cleanup_boot_dir);
+
+  for (i = 0; i < deployments->len; i++)
+    {
+      OtDeployment *deployment = deployments->pdata[i];
+      GFile *deployment_path = ot_admin_get_deployment_directory (sysroot, deployment);
+      /* Transfer ownership */
+      g_hash_table_insert (active_deployment_dirs, deployment_path, deployment_path);
+    }
+
+  if (!list_all_deployment_directories (sysroot, &all_deployment_dirs,
+                                        cancellable, error))
+    goto out;
   
-  if (out_name)
-    *out_name = g_strndup (relpath, last_dash - relpath);
-  if (out_rev)
-    *out_rev = g_strdup (last_dash + 1);
+  for (i = 0; i < all_deployment_dirs->len; i++)
+    {
+      OtDeployment *deployment = all_deployment_dirs->pdata[i];
+      gs_unref_object GFile *deployment_path = ot_admin_get_deployment_directory (sysroot, deployment);
+      if (!g_hash_table_lookup (active_deployment_dirs, deployment_path))
+        {
+          g_print ("ostadmin: Deleting deployment %s\n", gs_file_get_path_cached (deployment_path));
+          if (!gs_shutil_rm_rf (deployment_path, cancellable, error))
+            goto out;
+        }
+    }
+
+  ret = TRUE;
+ out:
+  return ret;
+}
+
+const char *
+ot_admin_bootconfig_get_key (GVariant   *bootconfig,
+                             const char *key)
+{
+  GVariantIter iter;
+  const char *bkey;
+  const char *value;
+  const char *line;
+
+  if (bootconfig)
+    {
+      g_variant_iter_init (&iter, bootconfig);
+      while (g_variant_iter_loop (&iter, "(&s&s&s)", &bkey, &value, &line))
+        {
+          if (strcmp (key, bkey) == 0)
+            return value;
+        }
+    }
+  return NULL;
+}
+
+gboolean
+ot_admin_write_bootconfig_with_overrides (GFile          *output,
+                                          GVariant       *bootconfig,
+                                          GHashTable     *overrides,
+                                          GCancellable   *cancellable,
+                                          GError        **error)
+{
+  GHashTableIter hashiter;
+  gpointer hashkey, hashvalue;
+  gs_unref_object GOutputStream *out = NULL;
+  gs_unref_object GDataOutputStream *dataout = NULL;
+  gboolean ret = FALSE;
+  GVariantIter iter;
+  const char *key;
+  const char *value;
+  const char *line;
+  gs_unref_hashtable GHashTable *written_overrides = NULL;
+
+  written_overrides = g_hash_table_new (g_str_hash, g_str_equal);
+
+  out = (GOutputStream*)g_file_replace (output, NULL, FALSE, 0, cancellable, error);
+  if (!out)
+    goto out;
+
+  dataout = g_data_output_stream_new (out);
+
+  if (bootconfig != NULL)
+    {
+      g_variant_iter_init (&iter, bootconfig);
+      while (g_variant_iter_loop (&iter, "(&s&s&s)", &key, &value, &line))
+        {
+          const char *override = g_hash_table_lookup (overrides, key);
+
+          if (!override)
+            {
+              if (!g_data_output_stream_put_string (dataout, line, cancellable, error))
+                goto out;
+            }
+          else
+            {
+              if (!g_data_output_stream_put_string (dataout, key, cancellable, error))
+                goto out;
+              if (!g_data_output_stream_put_byte (dataout, ' ', cancellable, error))
+                goto out;
+              if (!g_data_output_stream_put_string (dataout, override, cancellable, error))
+                goto out;
+              g_hash_table_insert (written_overrides, (gpointer)key, (gpointer)key);
+            }
+
+          if (!g_data_output_stream_put_byte (dataout, '\n', cancellable, error))
+            goto out;
+        }
+    }
+
+  g_hash_table_iter_init (&hashiter, overrides);
+  while (g_hash_table_iter_next (&hashiter, &hashkey, &hashvalue))
+    {
+      if (g_hash_table_lookup (written_overrides, hashkey))
+        continue;
+      if (!g_data_output_stream_put_string (dataout, hashkey, cancellable, error))
+        goto out;
+      if (!g_data_output_stream_put_byte (dataout, ' ', cancellable, error))
+        goto out;
+      if (!g_data_output_stream_put_string (dataout, hashvalue, cancellable, error))
+        goto out;
+      if (!g_data_output_stream_put_byte (dataout, '\n', cancellable, error))
+        goto out;
+    }
+
+  if (!g_output_stream_close ((GOutputStream*)dataout, cancellable, error))
+    goto out;
+
+  ret = TRUE;
+ out:
+  return ret;
 }
diff --git a/src/ostree/ot-admin-functions.h b/src/ostree/ot-admin-functions.h
index a03ef59..13aada5 100644
--- a/src/ostree/ot-admin-functions.h
+++ b/src/ostree/ot-admin-functions.h
@@ -24,6 +24,7 @@
 #define __OT_ADMIN_FUNCTIONS__
 
 #include <gio/gio.h>
+#include "ot-deployment.h"
 
 G_BEGIN_DECLS
 
@@ -31,34 +32,48 @@ gboolean ot_admin_ensure_initialized (GFile         *ostree_dir,
                                      GCancellable  *cancellable,
                                      GError       **error);
 
+gboolean ot_admin_read_current_subbootversion (GFile         *sysroot,
+                                               int            bootversion,
+                                               int           *out_subbootversion,
+                                               GCancellable  *cancellable,
+                                               GError       **error);
+
 gboolean ot_admin_get_booted_os (char        **out_osname,
                                  char        **out_tree,
                                  GCancellable *cancellable,
                                  GError      **error);
 
-gboolean ot_admin_get_current_deployment (GFile           *ostree_dir,
-                                          const char      *osname,
-                                          GFile          **out_deployment,
-                                          GCancellable    *cancellable,
-                                          GError         **error);
-gboolean ot_admin_get_previous_deployment (GFile           *ostree_dir,
-                                           const char      *osname,
-                                           GFile          **out_deployment,
-                                           GCancellable    *cancellable,
-                                           GError         **error);
-
-gboolean ot_admin_list_deployments (GFile               *ostree_dir,
-                                    const char          *osname,
+gboolean ot_admin_get_booted_deployment_dir (GFile           *sysroot,
+                                             char           **out_osname,
+                                             GFile          **out_deployment,
+                                             GCancellable    *cancellable,
+                                             GError         **error);
+gboolean ot_admin_get_booted_deployment (OtDeployment       **out_deployment,
+                                         GCancellable        *cancellable,
+                                         GError             **error);
+
+GFile *ot_admin_get_deployment_directory (GFile        *sysroot,
+                                          OtDeployment *deployment);
+
+const char *ot_admin_bootconfig_get_key (GVariant   *bootconfig,
+                                         const char *key);
+
+gboolean ot_admin_write_bootconfig_with_overrides (GFile          *output,
+                                                   GVariant       *bootconfig,
+                                                   GHashTable     *overrides,
+                                                   GCancellable   *cancellable,
+                                                   GError        **error);
+
+gboolean ot_admin_cleanup (GFile               *sysroot,
+                           GCancellable        *cancellable,
+                           GError             **error);
+
+gboolean ot_admin_list_deployments (GFile               *sysroot,
+                                    int                 *out_bootversion,
                                     GPtrArray          **out_deployments,
                                     GCancellable        *cancellable,
                                     GError             **error);
 
-gboolean ot_admin_get_active_deployment (GFile           *ostree_dir,
-                                         char           **out_osname,
-                                         GFile          **out_deployment,
-                                         GCancellable    *cancellable,
-                                         GError         **error);
-
 gboolean ot_admin_get_default_ostree_dir (GFile        **out_ostree_dir,
                                           GCancellable  *cancellable,
                                           GError       **error);
@@ -68,13 +83,6 @@ gboolean ot_admin_pull (GFile         *ostree_dir,
                         GCancellable  *cancellable,
                         GError       **error);
 
-void
-ot_admin_parse_deploy_name (GFile   *ostree_dir,
-                            const char *osname,
-                            GFile   *deployment,
-                            char   **out_name,
-                            char   **out_rev);
-
 G_END_DECLS
 
 #endif
diff --git a/src/ostree/ot-bootloader-syslinux.c b/src/ostree/ot-bootloader-syslinux.c
new file mode 100644
index 0000000..7687568
--- /dev/null
+++ b/src/ostree/ot-bootloader-syslinux.c
@@ -0,0 +1,225 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2013 Colin Walters <walters verbum org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include "ot-bootloader-syslinux.h"
+
+struct _OtBootloaderSyslinux
+{
+  GObject       parent_instance;
+
+  GFile        *boot_dir;
+  GFile        *config_path;
+};
+
+typedef GObjectClass OtBootloaderSyslinuxClass;
+
+static void ot_bootloader_syslinux_iface_init (OtBootloaderInterface *iface);
+G_DEFINE_TYPE_WITH_CODE (OtBootloaderSyslinux, ot_bootloader_syslinux, G_TYPE_OBJECT,
+  G_IMPLEMENT_INTERFACE (OT_TYPE_BOOTLOADER, ot_bootloader_syslinux_iface_init))
+
+static gboolean
+ot_bootloader_syslinux_query (OtBootloader *iface)
+{
+  OtBootloaderSyslinux *self = OT_BOOTLOADER_SYSLINUX (iface);
+
+  return g_file_query_exists (self->config_path, NULL);
+}
+
+#if 0
+static gboolean
+is_legacy_gnome_ostree_label (const char *osname,
+                              const char *line)
+{
+  return strcmp (osname, "gnome-ostree") == 0
+    && strcmp (line, "OSTree") == 0;
+}
+
+static GFile *
+get_target_boot_dir (OtBootloader  *self,
+                     const char    *osname,
+                     const char    *revision)
+{
+  /* TODO - adjust for systemd bootloader spec */
+  gs_free char *shortrev = g_strndup (revision, 16);
+  gs_free char *name = g_strconcat ("ostree/", osname, "/", shortrev, NULL);
+  return g_file_resolve_relative_path (self->boot_dir, name);
+}
+
+static gboolean
+ot_bootloader_syslinux_update_entry (OtBootloader  *self,
+                                     const char    *osname,
+                                     const char    *os_label,
+                                     const char    *revision,
+                                     GFile         *kernel_path,
+                                     const char    *kernel_checksum,
+                                     GFile         *initramfs_path,
+                                     const char    *initramfs_checksum,
+                                     GCancellable  *cancellable,
+                                     GError       **error)
+{
+  gboolean ret = FALSE;
+  gs_free char *config_contents = NULL;
+  gs_free char *new_config_contents = NULL;
+  gs_free char *label_prefix = NULL;
+  gs_unref_ptrarray GPtrArray *new_lines = NULL;
+  gs_unref_object GFile *target_boot_dir = NULL;
+  gs_unref_object GFile *target_kernel_path = NULL;
+  gs_unref_object GFile *target_initramfs_path = NULL;
+  gs_free char *kernel_relpath = NULL;
+  gs_free char *initramfs_relpath = NULL;
+  gboolean parsing_label = FALSE;
+  char **lines = NULL;
+  char **iter;
+  char **append_opts = NULL;
+
+  config_contents = gs_file_load_contents_utf8 (self->config_path, cancellable, error);
+  if (!config_contents)
+    goto out;
+
+  target_boot_dir = get_target_boot_dir (self, osname, revision);
+  if (!gs_file_ensure_directory (target_boot_dir, cancellable, error))
+    goto out;
+
+  target_kernel_path = g_file_get_child (target_boot_dir, gs_file_get_basename_cached (kernel_path));
+  target_initramfs_path = g_file_get_child (target_boot_dir, gs_file_get_basename_cached (initramfs_path));
+
+  if (!gs_file_linkcopy_sync_data (kernel_path, target_kernel_path, G_FILE_COPY_OVERWRITE,
+                                   cancellable, error))
+    goto out;
+  if (!gs_file_linkcopy_sync_data (initramfs_path, target_initramfs_path, G_FILE_COPY_OVERWRITE,
+                                   cancellable, error))
+    goto out;
+
+  kernel_relpath = g_file_get_relative_path (self->boot_dir, target_kernel_path);
+  initramfs_relpath = g_file_get_relative_path (self->boot_dir, target_initramfs_path);
+
+  label_prefix = g_strconcat ("LABEL [ostree:", osname, "] ", NULL);
+
+  lines = g_strsplit (config_contents, "\n", -1);
+
+  new_lines = g_ptr_array_new_with_free_func (g_free);
+  
+  for (iter = lines; *iter; iter++)
+    {
+      char *line = *iter;
+      gboolean skip = parsing_label;
+
+      if (!parsing_label &&
+          (g_str_has_prefix (line, label_prefix) ||
+           is_legacy_gnome_ostree (osname, line)))
+        {
+          skip = parsing_label = TRUE;
+          g_ptr_array_add (new_lines, g_strconcat (label_prefix, os_label, NULL));
+          g_ptr_array_add (new_lines, g_strconcat ("\tLINUX ", kernel_relpath, NULL));
+          g_ptr_array_add (new_lines, g_strconcat ("\tINITRD ", initramfs_relpath, NULL));
+        }
+      else if (parsing_label && g_str_has_prefix (line, "\tAPPEND "))
+        {
+          g_strfreev (append_opts);
+          append_opts = g_strsplit (line + strlen ("\tAPPEND "), " ");
+        }
+      else if (parsing_label && !g_str_has_prefix (line, "\t"))
+        {
+          char **append_iter;
+          char *append_joined;
+          for (append_iter = append_opts; *append_iter; append_iter++)
+            {
+              char *append_opt = *append_iter;
+              if (g_str_has_prefix (append_opt, "ostree="))
+                {
+                  g_free (append_opt);
+                  *append_opt = g_strconcat ("ostree=", osname, "/current");
+                }
+            }
+          append_joined = g_strjoinv (append_opts, " ");
+          g_ptr_array_add (new_lines, g_strconcat ("\tAPPEND ", append_joined, NULL));
+          skip = parsing_label = FALSE;
+        }
+      
+      if (!skip)
+        {
+          /* Transfer ownership */
+          g_ptr_array_add (new_lines, line);
+          *iter = NULL;
+        }
+      else
+        {
+          g_free (*iter);
+        }
+    }
+
+  g_ptr_array_add (new_lines, NULL);
+  new_config_contents = g_strjoinv ("\n", (gchar**)new_lines->pdata);
+
+  if (strcmp (new_config_contents, config_contents) != 0)
+    {
+      if (!g_file_replace_contents (self->config_path, strlen (new_config_contents), 
+                                    NULL, FALSE, G_FILE_CREATE_NONE,
+                                    cancellable, error))
+        goto out;
+      g_print ("Saved new version of %s\n", gs_file_get_path_cached (self->config_path));
+    }
+  
+  ret = TRUE;
+ out:
+  g_free (lines); /* Note we freed elements individually */
+  return ret;
+}
+#endif
+
+static void
+ot_bootloader_syslinux_finalize (GObject *object)
+{
+  OtBootloaderSyslinux *self = OT_BOOTLOADER_SYSLINUX (object);
+
+  g_clear_object (&self->boot_dir);
+
+  G_OBJECT_CLASS (ot_bootloader_syslinux_parent_class)->finalize (object);
+}
+
+void
+ot_bootloader_syslinux_init (OtBootloaderSyslinux *self)
+{
+}
+
+void
+ot_bootloader_syslinux_iface_init (OtBootloaderInterface *iface)
+{
+  iface->query = ot_bootloader_syslinux_query;
+}
+
+void
+ot_bootloader_syslinux_class_init (OtBootloaderSyslinuxClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  object_class->finalize = ot_bootloader_syslinux_finalize;
+}
+
+OtBootloaderSyslinux *
+ot_bootloader_syslinux_new (GFile *boot_dir)
+{
+  OtBootloaderSyslinux *self = g_object_new (OT_TYPE_BOOTLOADER_SYSLINUX, NULL);
+  self->boot_dir = g_object_ref (boot_dir);
+  self->config_path = g_file_resolve_relative_path (self->boot_dir, "syslinux/syslinux.cfg");
+  return self;
+}
diff --git a/src/ostree/ot-bootloader-syslinux.h b/src/ostree/ot-bootloader-syslinux.h
new file mode 100644
index 0000000..3cd1a0a
--- /dev/null
+++ b/src/ostree/ot-bootloader-syslinux.h
@@ -0,0 +1,40 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2013 Colin Walters <walters verbum org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __OT_BOOTLOADER_SYSLINUX_H__
+#define __OT_BOOTLOADER_SYSLINUX_H__
+
+#include "ot-bootloader.h"
+
+G_BEGIN_DECLS
+
+#define OT_TYPE_BOOTLOADER_SYSLINUX (ot_bootloader_syslinux_get_type ())
+#define OT_BOOTLOADER_SYSLINUX(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), OT_TYPE_BOOTLOADER_SYSLINUX, 
OtBootloaderSyslinux))
+#define G_IS_BOOTLOADER_SYSLINUX(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), OT_TYPE_BOOTLOADER_SYSLINUX))
+
+typedef struct _OtBootloaderSyslinux OtBootloaderSyslinux;
+
+GType ot_bootloader_syslinux_get_type (void) G_GNUC_CONST;
+
+OtBootloaderSyslinux * ot_bootloader_syslinux_new (GFile *boot_dir);
+
+G_END_DECLS
+
+#endif /* __OT_BOOTLOADER_SYSLINUX_H__ */
diff --git a/src/ostree/ot-bootloader.c b/src/ostree/ot-bootloader.c
new file mode 100644
index 0000000..661df1b
--- /dev/null
+++ b/src/ostree/ot-bootloader.c
@@ -0,0 +1,53 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2013 Colin Walters <walters verbum org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include "ot-bootloader.h"
+
+G_DEFINE_INTERFACE (OtBootloader, ot_bootloader, G_TYPE_OBJECT)
+
+static void
+ot_bootloader_default_init (OtBootloaderInterface *iface)
+{
+}
+
+gboolean
+ot_bootloader_query (OtBootloader  *self)
+{
+  g_return_val_if_fail (OT_IS_BOOTLOADER (self), FALSE);
+
+  return OT_BOOTLOADER_GET_IFACE (self)->query (self);
+}
+
+/*
+gboolean
+ot_bootloader_update_entry (OtBootloader  *self,
+                            const char    *osname,
+                            GFile         *kernel_path,
+                            GFile         *initramfs_path,
+                            GCancellable  *cancellable,
+                            GError       **error)
+{
+  g_return_val_if_fail (OT_IS_BOOTLOADER (self), FALSE);
+
+  return OT_BOOTLOADER_GET_IFACE (self)->update_entry (self, osname, kernel_path, initramfs_path,
+                                                       cancellable, error);
+}
+*/
diff --git a/src/ostree/ot-bootloader.h b/src/ostree/ot-bootloader.h
new file mode 100644
index 0000000..e4e3877
--- /dev/null
+++ b/src/ostree/ot-bootloader.h
@@ -0,0 +1,66 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2013 Colin Walters <walters verbum org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __OT_BOOTLOADER_H__
+#define __OT_BOOTLOADER_H__
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define OT_TYPE_BOOTLOADER (ot_bootloader_get_type ())
+#define OT_BOOTLOADER(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), OT_TYPE_BOOTLOADER, OtBootloader))
+#define OT_IS_BOOTLOADER(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), OT_TYPE_BOOTLOADER))
+#define OT_BOOTLOADER_GET_IFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), OT_TYPE_BOOTLOADER, 
OtBootloaderInterface))
+
+typedef struct _OtBootloader OtBootloader;
+typedef struct _OtBootloaderInterface                            OtBootloaderInterface;
+
+struct _OtBootloaderInterface
+{
+  GTypeInterface g_iface;
+
+  /* virtual functions */
+  gboolean             (* query)                   (OtBootloader  *self);
+};
+
+GType ot_bootloader_get_type (void) G_GNUC_CONST;
+
+gboolean ot_bootloader_query (OtBootloader *self);
+
+/*
+gboolean ot_bootloader_update_entry (OtBootloader  *self,
+                                    const char    *osname,
+                                     const char    *os_label,
+                                     const char    *revision,
+                                    GFile         *kernel_path,
+                                    GFile         *initramfs_path,
+                                    GCancellable  *cancellable,
+                                    GError       **error);
+*/
+
+gboolean ot_bootloader_prune (OtBootloader  *self,
+                             const char    *osname,
+                             GCancellable  *cancellable,
+                             GError       **error);
+
+G_END_DECLS
+
+#endif /* __OT_BOOTLOADER_H__ */
diff --git a/src/ostree/ot-builtin-admin.c b/src/ostree/ot-builtin-admin.c
index c8f60fb..fdcb570 100644
--- a/src/ostree/ot-builtin-admin.c
+++ b/src/ostree/ot-builtin-admin.c
@@ -31,12 +31,10 @@
 
 #include <glib/gi18n.h>
 
-static char *opt_ostree_dir = NULL;
-static char *opt_boot_dir = "/boot";
+static char *opt_sysroot = "/";
 
 static GOptionEntry options[] = {
-  { "ostree-dir", 0, 0, G_OPTION_ARG_STRING, &opt_ostree_dir, "Path to OSTree root directory (default: 
/ostree)", NULL },
-  { "boot-dir", 0, 0, G_OPTION_ARG_STRING, &opt_boot_dir, "Path to system boot directory (default: /boot)", 
NULL },
+  { "sysroot", 0, 0, G_OPTION_ARG_STRING, &opt_sysroot, "Path to root directory (default: /)", NULL },
   { NULL }
 };
 
@@ -53,7 +51,6 @@ static OstreeAdminCommand admin_subcommands[] = {
   { "upgrade", ot_admin_builtin_upgrade },
   { "pull-deploy", ot_admin_builtin_pull_deploy },
   { "prune", ot_admin_builtin_prune },
-  { "update-kernel", ot_admin_builtin_update_kernel },
   { "config-diff", ot_admin_builtin_diff },
   { "run-triggers", ot_admin_builtin_run_triggers },
   { NULL, NULL }
@@ -70,8 +67,6 @@ ostree_builtin_admin (int argc, char **argv, GFile *repo_path, GError **error)
   int subcmd_argc;
   OtAdminBuiltinOpts admin_opts;
   char **subcmd_argv = NULL;
-  ot_lobj GFile *ostree_dir = NULL;
-  ot_lobj GFile *boot_dir = NULL;
 
   context = g_option_context_new ("[OPTIONS] SUBCOMMAND - Run an administrative subcommand");
 
@@ -117,21 +112,9 @@ ostree_builtin_admin (int argc, char **argv, GFile *repo_path, GError **error)
       goto out;
     }
 
-  if (opt_ostree_dir != NULL)
-    {
-      ostree_dir = g_file_new_for_path (opt_ostree_dir);
-    }
-  else
-    {
-      if (!ot_admin_get_default_ostree_dir (&ostree_dir, cancellable, error))
-        goto out;
-    }
-  boot_dir = g_file_new_for_path (opt_boot_dir);
-
   ostree_prep_builtin_argv (subcommand_name, argc-2, argv+2, &subcmd_argc, &subcmd_argv);
 
-  admin_opts.ostree_dir = ostree_dir;
-  admin_opts.boot_dir = boot_dir;
+  admin_opts.sysroot = g_file_new_for_path (opt_sysroot);
   if (!subcommand->fn (subcmd_argc, subcmd_argv, &admin_opts, error))
     goto out;
  
diff --git a/src/ostree/ot-deployment.c b/src/ostree/ot-deployment.c
new file mode 100644
index 0000000..df3e49b
--- /dev/null
+++ b/src/ostree/ot-deployment.c
@@ -0,0 +1,155 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2013 Colin Walters <walters verbum org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include "ot-deployment.h"
+
+struct _OtDeployment
+{
+  GObject       parent_instance;
+
+  int index;  /* Global offset */
+  char *osname;  /* osname */
+  char *ref;  /* originating OSTree refname (e.g. gnome-ostree/buildmaster/x86_64-runtime) */
+  char *csum;  /* OSTree checksum of tree */
+  int deployserial;  /* How many times this particular csum appears in deployment list */
+  char *bootcsum;  /* Checksum of kernel+initramfs */
+  int bootserial; /* An integer assigned to this tree per its ${bootcsum} */
+  GVariant *bootconfig; /* Bootloader configuration */
+};
+
+typedef GObjectClass OtDeploymentClass;
+
+G_DEFINE_TYPE (OtDeployment, ot_deployment, G_TYPE_OBJECT)
+
+const char *
+ot_deployment_get_csum (OtDeployment *self)
+{
+  return self->csum;
+}
+
+const char *
+ot_deployment_get_bootcsum (OtDeployment *self)
+{
+  return self->bootcsum;
+}
+
+const char *
+ot_deployment_get_osname (OtDeployment *self)
+{
+  return self->osname;
+}
+
+int
+ot_deployment_get_deployserial (OtDeployment *self)
+{
+  return self->deployserial;
+}
+
+int
+ot_deployment_get_bootserial (OtDeployment *self)
+{
+  return self->bootserial;
+}
+
+GVariant *
+ot_deployment_get_bootconfig (OtDeployment *self)
+{
+  return self->bootconfig;
+}
+
+void
+ot_deployment_set_index (OtDeployment *self, int index)
+{
+  self->index = index;
+}
+
+void
+ot_deployment_set_bootserial (OtDeployment *self, int index)
+{
+  self->bootserial = index;
+}
+
+void
+ot_deployment_set_bootconfig (OtDeployment *self, GVariant *bootconfig)
+{
+  g_clear_pointer (&self->bootconfig, g_variant_unref);
+  if (bootconfig)
+    self->bootconfig = g_variant_ref (bootconfig);
+}
+
+OtDeployment *
+ot_deployment_clone (OtDeployment *self)
+{
+  OtDeployment *ret = ot_deployment_new (self->index, self->osname,
+                                         self->ref, self->csum,
+                                         self->deployserial,
+                                         self->bootcsum, self->bootserial);
+  ot_deployment_set_bootconfig (ret, self->bootconfig);
+  return ret;
+}
+
+static void
+ot_deployment_finalize (GObject *object)
+{
+  OtDeployment *self = OT_DEPLOYMENT (object);
+
+  g_free (self->ref);
+  g_free (self->osname);
+  g_free (self->csum);
+  g_free (self->bootcsum);
+  g_clear_pointer (&self->bootconfig, g_variant_unref);
+
+  G_OBJECT_CLASS (ot_deployment_parent_class)->finalize (object);
+}
+
+void
+ot_deployment_init (OtDeployment *self)
+{
+}
+
+void
+ot_deployment_class_init (OtDeploymentClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  object_class->finalize = ot_deployment_finalize;
+}
+
+OtDeployment *
+ot_deployment_new (int    index,
+                   const char  *osname,
+                   const char  *ref,
+                   const char  *csum,
+                   int    deployserial,
+                   const char  *bootcsum,
+                   int    bootserial)
+{
+  OtDeployment *self = g_object_new (OT_TYPE_DEPLOYMENT, NULL);
+  self->index = index;
+  self->osname = g_strdup (osname);
+  self->ref = g_strdup (ref);
+  self->csum = g_strdup (csum);
+  self->deployserial = deployserial;
+  self->bootcsum = g_strdup (bootcsum);
+  self->bootserial = bootserial;
+  return self;
+}
diff --git a/src/ostree/ot-deployment.h b/src/ostree/ot-deployment.h
new file mode 100644
index 0000000..8073d79
--- /dev/null
+++ b/src/ostree/ot-deployment.h
@@ -0,0 +1,61 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2013 Colin Walters <walters verbum org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __OT_DEPLOYMENT_H__
+#define __OT_DEPLOYMENT_H__
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define OT_TYPE_DEPLOYMENT (ot_deployment_get_type ())
+#define OT_DEPLOYMENT(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), OT_TYPE_DEPLOYMENT, OtDeployment))
+#define OT_IS_DEPLOYMENT(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), OT_TYPE_DEPLOYMENT))
+
+typedef struct _OtDeployment OtDeployment;
+
+GType ot_deployment_get_type (void) G_GNUC_CONST;
+
+OtDeployment * ot_deployment_new (int    index,
+                                  const char  *osname,
+                                  const char  *ref,
+                                  const char  *csum,
+                                  int    deployserial,
+                                  const char  *bootcsum,
+                                  int    bootserial);
+
+int ot_deployment_get_index (OtDeployment *self);
+const char *ot_deployment_get_osname (OtDeployment *self);
+int ot_deployment_get_deployserial (OtDeployment *self);
+const char *ot_deployment_get_csum (OtDeployment *self);
+const char *ot_deployment_get_bootcsum (OtDeployment *self);
+int ot_deployment_get_bootserial (OtDeployment *self);
+GVariant *ot_deployment_get_bootconfig (OtDeployment *self);
+
+void ot_deployment_set_index (OtDeployment *self, int index);
+void ot_deployment_set_bootserial (OtDeployment *self, int index);
+void ot_deployment_set_bootconfig (OtDeployment *self, GVariant *bootconfig);
+
+OtDeployment *ot_deployment_clone (OtDeployment *self);
+
+
+G_END_DECLS
+
+#endif /* __OT_DEPLOYMENT_H__ */
diff --git a/tests/libtest.sh b/tests/libtest.sh
index e4518f1..5e860d5 100644
--- a/tests/libtest.sh
+++ b/tests/libtest.sh
@@ -38,12 +38,22 @@ assert_has_file () {
     test -f "$1" || (echo 1>&2 "Couldn't find '$1'"; exit 1)
 }
 
+assert_has_dir () {
+    test -d "$1" || (echo 1>&2 "Couldn't find '$1'"; exit 1)
+}
+
 assert_not_has_file () {
     if test -f "$1"; then
        echo 1>&2 "File '$1' exists"; exit 1
     fi
 }
 
+assert_not_has_dir () {
+    if test -d "$1"; then
+       echo 1>&2 "Directory '$1' exists"; exit 1
+    fi
+}
+
 assert_file_has_content () {
     if ! grep -q -e "$2" "$1"; then
        echo 1>&2 "File '$1' doesn't match regexp '$2'"; exit 1
@@ -159,3 +169,58 @@ EOF
 
     export OSTREE="ostree --repo=repo"
 }
+
+setup_os_repository () {
+    mode=$1
+    shift
+
+    oldpwd=`pwd`
+
+    cd ${test_tmpdir}
+    mkdir testos-repo
+    if test -n "$mode"; then
+       ostree --repo=testos-repo init --mode=${mode}
+    else
+       ostree --repo=testos-repo init
+    fi
+
+    cd ${test_tmpdir}
+    mkdir osdata
+    cd osdata
+    mkdir -p boot usr/bin usr/lib/modules/3.6.0 usr/share usr/etc
+    echo "a kernel" > boot/vmlinuz-3.6.0
+    echo "an initramfs" > boot/initramfs-3.6.0
+    echo "a kernel module" > usr/lib/modules/3.6.0/foofs.ko
+    bootcsum=$(cat boot/vmlinuz-3-6.0 boot/initramfs-3.6.0 usr/lib/modules/3.6.0/foofs.ko | sha256sum | cut 
-f 1 -d ' ')
+    export bootcsum
+    mv boot/vmlinuz-3.6.0 boot/vmlinuz-3.6.0-${bootcsum}
+    mv boot/initramfs-3.6.0 boot/initramfs-3.6.0-${bootcsum}
+    
+    echo "an executable" > usr/bin/sh
+    echo "some shared data" > usr/share/langs.txt
+    echo "a library" > usr/lib/libfoo.so.0
+    ln -s usr/bin bin
+cat > usr/etc/os-release <<EOF
+NAME=TestOS
+VERSION=42
+ID=testos
+VERSION_ID=42
+PRETTY_NAME="TestOS 42"
+EOF
+    echo "a config file" > usr/etc/aconfigfile
+    mkdir -p usr/etc/NetworkManager
+    echo "a default daemon file" > usr/etc/NetworkManager/nm.conf
+
+    ostree --repo=${test_tmpdir}/testos-repo commit -b testos/buildmaster/x86_64-runtime -s "Build"
+    
+    echo "a new executable" > usr/bin/sh
+    ostree --repo=${test_tmpdir}/testos-repo commit -b testos/buildmaster/x86_64-runtime -s "Build"
+
+    ostree --repo=${test_tmpdir}/testos-repo fsck -q
+
+    cd ${test_tmpdir}
+    mkdir sysroot
+    ostree admin --sysroot=sysroot init-fs sysroot
+    ostree admin --sysroot=sysroot os-init testos
+
+}
diff --git a/tests/t0015-admin-deploy.sh b/tests/t0015-admin-deploy.sh
new file mode 100755
index 0000000..78c4e98
--- /dev/null
+++ b/tests/t0015-admin-deploy.sh
@@ -0,0 +1,80 @@
+#!/bin/bash
+#
+# Copyright (C) 2011 Colin Walters <walters verbum org>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+
+set -e
+
+. $(dirname $0)/libtest.sh
+
+echo "1..1"
+
+setup_os_repository "archive-z2"
+
+deployref="testos/buildmaster/x86_64-runtime^"
+
+echo "ok setup"
+
+echo "1..5"
+
+ostree --repo=sysroot/ostree/repo pull-local testos-repo testos/buildmaster/x86_64-runtime
+rev=$(ostree --repo=sysroot/ostree/repo rev-parse ${deployref})
+ostree admin --sysroot=sysroot deploy testos ${deployref}
+
+echo "ok deploy command"
+
+assert_not_has_dir sysroot/boot/loader.0
+assert_has_dir sysroot/boot/loader.1
+assert_has_dir sysroot/ostree/boot.1.1
+assert_has_file sysroot/boot/loader/entries/ostree-testos-${rev}-0.conf
+assert_file_has_content sysroot/boot/ostree/testos-${bootcsum}/vmlinuz-3.6.0 'a kernel'
+assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0/etc/os-release 'NAME=TestOS'
+assert_file_has_content sysroot/ostree/boot.1/testos/${bootcsum}/0/etc/os-release 'NAME=TestOS'
+
+echo "ok layout"
+
+ostree admin --sysroot=sysroot deploy testos ${deployref}
+# Keep the same bootversion
+assert_not_has_dir sysroot/boot/loader.0
+assert_has_dir sysroot/boot/loader.1
+# But swap subbootversion
+assert_not_has_dir sysroot/ostree/boot.1.1
+assert_has_dir sysroot/ostree/boot.1.0
+assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.1/etc/os-release 'NAME=TestOS'
+assert_file_has_content sysroot/ostree/boot.1/testos/${bootcsum}/0/etc/os-release 'NAME=TestOS'
+
+echo "ok second deploy (swap)"
+
+ostree admin --sysroot=sysroot deploy otheros ${deployref}
+assert_has_dir sysroot/boot/loader.0
+assert_not_has_dir sysroot/boot/loader.1
+assert_has_file sysroot/boot/loader/entries/ostree-testos-${rev}-0.conf
+assert_has_file sysroot/boot/loader/entries/ostree-otheros-${rev}-0.conf
+assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.1/etc/os-release 'NAME=TestOS'
+assert_file_has_content sysroot/ostree/deploy/otheros/deploy/${rev}.0/etc/os-release 'NAME=TestOS'
+
+echo "ok independent deploy"
+
+ostree admin --sysroot=sysroot deploy --retain testos ${deployref}
+assert_not_has_dir sysroot/boot/loader.0
+assert_has_dir sysroot/boot/loader.1
+assert_has_file sysroot/boot/loader/entries/ostree-testos-${rev}-0.conf
+assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.1/etc/os-release 'NAME=TestOS'
+assert_has_file sysroot/boot/loader/entries/ostree-testos-${rev}-1.conf
+assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.2/etc/os-release 'NAME=TestOS'
+
+echo "ok second deploy (retain)"


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]