diff --git a/Makefile-ostadmin.am b/Makefile-ostadmin.am deleted file mode 100644 index 45cb2d0b..00000000 --- a/Makefile-ostadmin.am +++ /dev/null @@ -1,48 +0,0 @@ -# Copyright (C) 2012 Colin Walters -# -# 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. - -if !TRIGGERS_ONLY -bin_PROGRAMS += ostadmin -endif - -ostadmin_SOURCES = src/ostadmin/main.c \ - src/ostadmin/ot-admin-builtins.h \ - src/ostadmin/ot-admin-builtin-init.c \ - src/ostadmin/ot-admin-builtin-diff.c \ - src/ostadmin/ot-admin-builtin-deploy.c \ - src/ostadmin/ot-admin-builtin-update-kernel.c \ - src/ostadmin/ot-admin-functions.h \ - src/ostadmin/ot-admin-functions.c \ - src/ostadmin/ot-admin-main.h \ - src/ostadmin/ot-admin-main.c \ - $(NULL) - -ostadmin_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/src/libgsystem -I$(srcdir)/src/libotutil -I$(srcdir)/src/libostree -I$(srcdir)/src/ostadmin -DLOCALEDIR=\"$(datadir)/locale\" $(OT_INTERNAL_GIO_UNIX_CFLAGS) -ostadmin_LDADD = libgsystem.la libotutil.la libostree.la $(OT_INTERNAL_GIO_UNIX_LIBS) - -if ENABLE_KERNEL_UPDATES - -grub2dir = $(sysconfdir)/grub.d -grub2_SCRIPTS = src/ostadmin/grub2/15_ostree - -kernelpostinstdir = $(sysconfdir)/kernel/postinst.d -kernelpostinst_SCRIPTS = src/ostadmin/kernel/15_ostree_update - -kernelprermdir = $(sysconfdir)/kernel/prerm.d -kernelprerm_SCRIPTS = src/ostadmin/kernel/15_ostree_remove - -endif diff --git a/Makefile-ostree.am b/Makefile-ostree.am index 571b07f0..59b4775e 100644 --- a/Makefile-ostree.am +++ b/Makefile-ostree.am @@ -23,6 +23,7 @@ endif ostree_SOURCES = src/ostree/main.c \ src/ostree/ot-builtins.h \ + src/ostree/ot-builtin-admin.c \ src/ostree/ot-builtin-cat.c \ src/ostree/ot-builtin-config.c \ src/ostree/ot-builtin-checkout.c \ @@ -45,6 +46,16 @@ ostree_SOURCES = src/ostree/main.c \ src/ostree/ot-main.c \ $(NULL) +# Admin subcommand +ostree_SOURCES += \ + src/ostree/ot-admin-builtin-init.c \ + src/ostree/ot-admin-builtin-diff.c \ + src/ostree/ot-admin-builtin-deploy.c \ + src/ostree/ot-admin-builtin-update-kernel.c \ + src/ostree/ot-admin-functions.h \ + src/ostree/ot-admin-functions.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\" ostree_bin_shared_ldadd = libotutil.la libostree.la @@ -64,3 +75,16 @@ ostree_pull_LDADD = $(ostree_bin_shared_ldadd) $(OT_INTERNAL_SOUP_LIBS) endif MANPAGES += doc/ostree.1 + +if ENABLE_KERNEL_UPDATES + +grub2dir = $(sysconfdir)/grub.d +grub2_SCRIPTS = src/ostree/grub2/15_ostree + +kernelpostinstdir = $(sysconfdir)/kernel/postinst.d +kernelpostinst_SCRIPTS = src/ostree/kernel/15_ostree_update + +kernelprermdir = $(sysconfdir)/kernel/prerm.d +kernelprerm_SCRIPTS = src/ostree/kernel/15_ostree_remove + +endif diff --git a/Makefile.am b/Makefile.am index b8a99b49..55b04b18 100644 --- a/Makefile.am +++ b/Makefile.am @@ -78,7 +78,6 @@ include Makefile-daemon.am include Makefile-otutil.am include Makefile-libostree.am include Makefile-ostree.am -include Makefile-ostadmin.am include Makefile-switchroot.am include Makefile-triggers.am diff --git a/src/ostadmin/main.c b/src/ostadmin/main.c deleted file mode 100644 index ca061f78..00000000 --- a/src/ostadmin/main.c +++ /dev/null @@ -1,45 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- - * - * Copyright (C) 2012 Colin Walters - * - * 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. - * - * Author: Colin Walters - */ - -#include "config.h" - -#include - -#include - -#include "ot-admin-main.h" -#include "ot-admin-builtins.h" - -static OtAdminBuiltin builtins[] = { - { "deploy", ot_admin_builtin_deploy, 0 }, - { "diff", ot_admin_builtin_diff, 0 }, - { "init", ot_admin_builtin_init, 0 }, - { "update-kernel", ot_admin_builtin_update_kernel, 0 }, - { NULL } -}; - -int -main (int argc, - char **argv) -{ - return ot_admin_main (argc, argv, builtins); -} diff --git a/src/ostadmin/ot-admin-main.c b/src/ostadmin/ot-admin-main.c deleted file mode 100644 index 6052d901..00000000 --- a/src/ostadmin/ot-admin-main.c +++ /dev/null @@ -1,141 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- - * - * Copyright (C) 2011 Colin Walters - * - * 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. - * - * Author: Colin Walters - */ - -#include "config.h" - -#include - -#include - -#include "ot-admin-main.h" -#include "otutil.h" - -static int -usage (char **argv, OtAdminBuiltin *builtins, gboolean is_error) -{ - OtAdminBuiltin *builtin = builtins; - void (*print_func) (const gchar *format, ...); - - if (is_error) - print_func = g_printerr; - else - print_func = g_print; - - print_func ("usage: %s COMMAND [options]\n", - argv[0]); - print_func ("Builtin commands:\n"); - - while (builtin->name) - { - print_func (" %s\n", builtin->name); - builtin++; - } - return (is_error ? 1 : 0); -} - -static void -prep_builtin_argv (const char *builtin, - int argc, - char **argv, - int *out_argc, - char ***out_argv) -{ - int i; - char **cmd_argv; - - /* Should be argc - 1 + 1, to account for - the first argument (removed) and for NULL pointer */ - cmd_argv = g_new0 (char *, argc); - - for (i = 0; i < argc-1; i++) - cmd_argv[i] = argv[i+1]; - cmd_argv[i] = NULL; - *out_argc = argc-1; - *out_argv = cmd_argv; -} - -static void -set_error_print_usage (GError **error, OtAdminBuiltin *builtins, const char *msg, char **argv) -{ - g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, msg); - usage (argv, builtins, TRUE); -} - -int -ot_admin_main (int argc, - char **argv, - OtAdminBuiltin *builtins) -{ - OtAdminBuiltin *builtin; - GError *error = NULL; - int cmd_argc; - char **cmd_argv = NULL; - const char *cmd = NULL; - - /* avoid gvfs (http://bugzilla.gnome.org/show_bug.cgi?id=526454) */ - g_setenv ("GIO_USE_VFS", "local", TRUE); - - g_type_init (); - - g_set_prgname (argv[0]); - - if (argc < 2) - return usage (argv, builtins, 1); - - if (geteuid () != 0) - { - g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED, - "ostadmin: Can only be run as root"); - goto out; - } - - cmd = argv[1]; - - builtin = builtins; - while (builtin->name) - { - if (g_strcmp0 (cmd, builtin->name) == 0) - break; - builtin++; - } - - if (!builtin->name) - { - set_error_print_usage (&error, builtins, "Unknown command", argv); - goto out; - } - - prep_builtin_argv (cmd, argc, argv, &cmd_argc, &cmd_argv); - - if (!builtin->fn (cmd_argc, cmd_argv, &error)) - goto out; - - out: - g_free (cmd_argv); - if (error) - { - g_printerr ("%s\n", error->message); - g_clear_error (&error); - return 1; - } - return 0; -} diff --git a/src/ostadmin/ot-admin-main.h b/src/ostadmin/ot-admin-main.h deleted file mode 100644 index 8f0e6378..00000000 --- a/src/ostadmin/ot-admin-main.h +++ /dev/null @@ -1,32 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- - * - * Copyright (C) 2011 Colin Walters - * - * 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. - * - * Author: Colin Walters - */ - -#include - -typedef struct { - const char *name; - gboolean (*fn) (int argc, char **argv, GError **error); - int flags; -} OtAdminBuiltin; - -int ot_admin_main (int argc, char **argv, OtAdminBuiltin *builtins); - diff --git a/src/ostadmin/grub2/15_ostree b/src/ostree/grub2/15_ostree similarity index 100% rename from src/ostadmin/grub2/15_ostree rename to src/ostree/grub2/15_ostree diff --git a/src/ostadmin/kernel/15_ostree_remove b/src/ostree/kernel/15_ostree_remove similarity index 100% rename from src/ostadmin/kernel/15_ostree_remove rename to src/ostree/kernel/15_ostree_remove diff --git a/src/ostadmin/kernel/15_ostree_update b/src/ostree/kernel/15_ostree_update similarity index 100% rename from src/ostadmin/kernel/15_ostree_update rename to src/ostree/kernel/15_ostree_update diff --git a/src/ostree/main.c b/src/ostree/main.c index 72231ab3..994a8373 100644 --- a/src/ostree/main.c +++ b/src/ostree/main.c @@ -31,6 +31,7 @@ #include "ot-builtins.h" static OstreeCommand commands[] = { + { "admin", ostree_builtin_admin, OSTREE_BUILTIN_FLAG_NO_REPO }, { "cat", ostree_builtin_cat, 0 }, { "commit", ostree_builtin_commit, 0 }, { "config", ostree_builtin_config, 0 }, diff --git a/src/ostadmin/ot-admin-builtin-deploy.c b/src/ostree/ot-admin-builtin-deploy.c similarity index 92% rename from src/ostadmin/ot-admin-builtin-deploy.c rename to src/ostree/ot-admin-builtin-deploy.c index 6322f2c4..f81404f7 100644 --- a/src/ostadmin/ot-admin-builtin-deploy.c +++ b/src/ostree/ot-admin-builtin-deploy.c @@ -35,10 +35,8 @@ typedef struct { static gboolean opt_no_kernel; static gboolean opt_force; -static char *opt_ostree_dir = "/ostree"; static GOptionEntry options[] = { - { "ostree-dir", 0, 0, G_OPTION_ARG_STRING, &opt_ostree_dir, "Path to OSTree root directory (default: /ostree)", NULL }, { "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 }, { NULL } @@ -195,27 +193,30 @@ ensure_unlinked (GFile *path, */ static gboolean copy_one_config_file (OtAdminDeploy *self, + GFile *orig_etc, GFile *modified_etc, GFile *new_etc, - GFile *file, + GFile *src, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; - ot_lobj GFile *target_file = NULL; - ot_lobj GFile *target_parent = NULL; - ot_lfree char *path = NULL; + ot_lobj GFile *dest = NULL; + ot_lobj GFile *parent = NULL; + ot_lfree char *relative_path = NULL; + ot_lobj GFile *modified_path = NULL; + + relative_path = g_file_get_relative_path (orig_etc, src); + modified_path = g_file_resolve_relative_path (modified_etc, relative_path); + dest = g_file_resolve_relative_path (new_etc, relative_path); - path = g_file_get_relative_path (modified_etc, file); - g_assert (path); - target_file = g_file_resolve_relative_path (new_etc, path); - target_parent = g_file_get_parent (target_file); + parent = g_file_get_parent (dest); /* FIXME actually we need to copy permissions and xattrs */ - if (!ot_gfile_ensure_directory (target_parent, TRUE, error)) + if (!ot_gfile_ensure_directory (parent, TRUE, error)) goto out; - if (!g_file_copy (file, target_file, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA, + if (!g_file_copy (src, dest, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA, cancellable, NULL, NULL, error)) goto out; @@ -251,7 +252,7 @@ merge_etc_changes (OtAdminDeploy *self, ot_lptrarray GPtrArray *added = NULL; guint i; - modified = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + 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); added = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); @@ -287,14 +288,23 @@ merge_etc_changes (OtAdminDeploy *self, for (i = 0; i < modified->len; i++) { OstreeDiffItem *diff = modified->pdata[i]; - if (!copy_one_config_file (self, modified_etc, new_etc, diff->src, + ot_lfree char *relative_path = NULL; + ot_lobj GFile *modified_path = NULL; + ot_lobj GFile *target_path = NULL; + + relative_path = g_file_get_relative_path (orig_etc, diff->src); + modified_path = g_file_resolve_relative_path (modified_etc, relative_path); + target_path = g_file_resolve_relative_path (new_etc, relative_path); + + if (!copy_one_config_file (self, orig_etc, modified_etc, new_etc, diff->src, cancellable, error)) goto out; } for (i = 0; i < added->len; i++) { GFile *file = added->pdata[i]; - if (!copy_one_config_file (self, modified_etc, new_etc, file, + + if (!copy_one_config_file (self, orig_etc, modified_etc, new_etc, file, cancellable, error)) goto out; } @@ -505,8 +515,9 @@ do_update_kernel (OtAdminDeploy *self, ot_lptrarray GPtrArray *args = NULL; args = g_ptr_array_new (); - ot_ptrarray_add_many (args, "ostadmin", "update-kernel", + ot_ptrarray_add_many (args, "ostree", "admin", "--ostree-dir", ot_gfile_get_path_cached (self->ostree_dir), + "update-kernel", ot_gfile_get_path_cached (deploy_path), NULL); g_ptr_array_add (args, NULL); @@ -522,7 +533,7 @@ do_update_kernel (OtAdminDeploy *self, gboolean -ot_admin_builtin_deploy (int argc, char **argv, GError **error) +ot_admin_builtin_deploy (int argc, char **argv, GFile *ostree_dir, GError **error) { GOptionContext *context; OtAdminDeploy self_data; @@ -549,7 +560,7 @@ ot_admin_builtin_deploy (int argc, char **argv, GError **error) goto out; } - self->ostree_dir = g_file_new_for_path (opt_ostree_dir); + self->ostree_dir = g_object_ref (ostree_dir); if (!ot_admin_ensure_initialized (self->ostree_dir, cancellable, error)) goto out; diff --git a/src/ostadmin/ot-admin-builtin-diff.c b/src/ostree/ot-admin-builtin-diff.c similarity index 87% rename from src/ostadmin/ot-admin-builtin-diff.c rename to src/ostree/ot-admin-builtin-diff.c index c828f8e2..a5c347dc 100644 --- a/src/ostadmin/ot-admin-builtin-diff.c +++ b/src/ostree/ot-admin-builtin-diff.c @@ -28,19 +28,15 @@ #include -static char *opt_ostree_dir = "/ostree"; - static GOptionEntry options[] = { - { "ostree-dir", 0, 0, G_OPTION_ARG_STRING, &opt_ostree_dir, "Path to OSTree root directory (default: /ostree)", NULL }, { NULL } }; gboolean -ot_admin_builtin_diff (int argc, char **argv, GError **error) +ot_admin_builtin_diff (int argc, char **argv, GFile *ostree_dir, GError **error) { GOptionContext *context; gboolean ret = FALSE; - ot_lobj GFile *ostree_dir = NULL; ot_lobj GFile *repo_path = NULL; ot_lobj GFile *deployment = NULL; ot_lobj GFile *deploy_parent = NULL; @@ -58,7 +54,6 @@ ot_admin_builtin_diff (int argc, char **argv, GError **error) if (!g_option_context_parse (context, &argc, &argv, error)) goto out; - ostree_dir = g_file_new_for_path (opt_ostree_dir); repo_path = g_file_get_child (ostree_dir, "repo"); if (argc > 1) @@ -84,7 +79,7 @@ ot_admin_builtin_diff (int argc, char **argv, GError **error) ot_gfile_get_basename_cached (deployment), "-etc", NULL); - modified = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + 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); added = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); if (!ostree_diff_dirs (orig_etc_path, new_etc_path, modified, removed, added, @@ -95,7 +90,6 @@ ot_admin_builtin_diff (int argc, char **argv, GError **error) ret = TRUE; out: - g_clear_object (&ostree_dir); if (context) g_option_context_free (context); return ret; diff --git a/src/ostadmin/ot-admin-builtin-init.c b/src/ostree/ot-admin-builtin-init.c similarity index 79% rename from src/ostadmin/ot-admin-builtin-init.c rename to src/ostree/ot-admin-builtin-init.c index 703e14fc..f4331768 100644 --- a/src/ostadmin/ot-admin-builtin-init.c +++ b/src/ostree/ot-admin-builtin-init.c @@ -28,16 +28,12 @@ #include -static char *opt_ostree_dir = "/ostree"; - static GOptionEntry options[] = { - { "ostree-dir", 0, 0, G_OPTION_ARG_STRING, &opt_ostree_dir, "Path to OSTree root directory (default: /ostree)", NULL }, { NULL } }; - gboolean -ot_admin_builtin_init (int argc, char **argv, GError **error) +ot_admin_builtin_init (int argc, char **argv, GFile *ostree_dir, GError **error) { GOptionContext *context; gboolean ret = FALSE; @@ -50,12 +46,10 @@ ot_admin_builtin_init (int argc, char **argv, GError **error) if (!g_option_context_parse (context, &argc, &argv, error)) goto out; - dir = g_file_new_for_path (opt_ostree_dir); - - if (!ot_admin_ensure_initialized (dir, cancellable, error)) + if (!ot_admin_ensure_initialized (ostree_dir, cancellable, error)) goto out; - g_print ("%s initialized as OSTree root\n", opt_ostree_dir); + g_print ("%s initialized as OSTree root\n", ot_gfile_get_path_cached (ostree_dir)); ret = TRUE; out: diff --git a/src/ostadmin/ot-admin-builtin-update-kernel.c b/src/ostree/ot-admin-builtin-update-kernel.c similarity index 81% rename from src/ostadmin/ot-admin-builtin-update-kernel.c rename to src/ostree/ot-admin-builtin-update-kernel.c index d8edad5c..d69b57bd 100644 --- a/src/ostadmin/ot-admin-builtin-update-kernel.c +++ b/src/ostree/ot-admin-builtin-update-kernel.c @@ -29,20 +29,18 @@ #include typedef struct { - OstreeRepo *repo; + GFile *ostree_dir; } OtAdminUpdateKernel; -static char *opt_ostree_dir = "/ostree"; - static GOptionEntry options[] = { - { "ostree-dir", 0, 0, G_OPTION_ARG_STRING, &opt_ostree_dir, "Path to OSTree root directory", NULL }, { NULL } }; static gboolean -copy_modules (const char *release, - GCancellable *cancellable, - GError **error) +copy_modules (OtAdminUpdateKernel *self, + const char *release, + GCancellable *cancellable, + GError **error) { gboolean ret = FALSE; ot_lobj GFile *src_modules_file = NULL; @@ -50,7 +48,7 @@ copy_modules (const char *release, ot_lobj GFile *dest_modules_file = NULL; src_modules_file = ot_gfile_from_build_path ("/lib/modules", release, NULL); - dest_modules_file = ot_gfile_from_build_path (opt_ostree_dir, "modules", release, NULL); + dest_modules_file = ot_gfile_get_child_build_path (self->ostree_dir, "modules", release, NULL); dest_modules_parent = g_file_get_parent (dest_modules_file); if (!ot_gfile_ensure_directory (dest_modules_parent, FALSE, error)) goto out; @@ -69,10 +67,11 @@ copy_modules (const char *release, } static gboolean -update_initramfs (const char *release, - const char *deploy_path, - GCancellable *cancellable, - GError **error) +update_initramfs (OtAdminUpdateKernel *self, + const char *release, + const char *deploy_path, + GCancellable *cancellable, + GError **error) { gboolean ret = FALSE; ot_lfree char *initramfs_name = NULL; @@ -85,8 +84,8 @@ update_initramfs (const char *release, ot_lptrarray GPtrArray *mkinitramfs_args = NULL; ot_lobj GFile *tmpdir = NULL; ot_lfree char *initramfs_tmp_path = NULL; - ot_lfree char *ostree_vardir = NULL; - ot_lfree char *ostree_moduledir = NULL; + ot_lfree GFile *ostree_vardir = NULL; + ot_lfree GFile *ostree_moduledir = NULL; ot_lobj GFile *initramfs_tmp_file = NULL; ot_lobj GFileInfo *initramfs_tmp_info = NULL; @@ -94,8 +93,8 @@ update_initramfs (const char *release, cancellable, error)) goto out; - ostree_vardir = g_build_filename (opt_ostree_dir, "var", NULL); - ostree_moduledir = g_build_filename (opt_ostree_dir, "modules", NULL); + ostree_vardir = g_file_get_child (self->ostree_dir, "var"); + ostree_moduledir = g_file_get_child (self->ostree_dir, "modules"); mkinitramfs_args = g_ptr_array_new (); /* Note: the hardcoded /tmp path below is not actually a @@ -107,9 +106,9 @@ update_initramfs (const char *release, "--mount-readonly", "/", "--mount-proc", "/proc", "--mount-bind", "/dev", "/dev", - "--mount-bind", ostree_vardir, "/var", + "--mount-bind", ot_gfile_get_path_cached (ostree_vardir), "/var", "--mount-bind", ot_gfile_get_path_cached (tmpdir), "/tmp", - "--mount-bind", ostree_moduledir, "/lib/modules", + "--mount-bind", ot_gfile_get_path_cached (ostree_moduledir), "/lib/modules", deploy_path, "dracut", "-f", "/tmp/initramfs-ostree.img", release, NULL); @@ -188,10 +187,11 @@ grep_literal (GFile *f, } static gboolean -get_kernel_path_from_release (const char *release, - GFile **out_path, - GCancellable *cancellable, - GError **error) +get_kernel_path_from_release (OtAdminUpdateKernel *self, + const char *release, + GFile **out_path, + GCancellable *cancellable, + GError **error) { gboolean ret = FALSE; ot_lfree char *name = NULL; @@ -211,9 +211,10 @@ get_kernel_path_from_release (const char *release, } static gboolean -update_grub (const char *release, - GCancellable *cancellable, - GError **error) +update_grub (OtAdminUpdateKernel *self, + const char *release, + GCancellable *cancellable, + GError **error) { gboolean ret = FALSE; ot_lobj GFile *grub_path = g_file_new_for_path ("/boot/grub/grub.conf"); @@ -232,7 +233,8 @@ update_grub (const char *release, ot_lfree char *initramfs_arg = NULL; ot_lobj GFile *kernel_path = NULL; - if (!get_kernel_path_from_release (release, &kernel_path, cancellable, error)) + if (!get_kernel_path_from_release (self, release, &kernel_path, + cancellable, error)) goto out; if (kernel_path == NULL) @@ -268,7 +270,7 @@ update_grub (const char *release, } gboolean -ot_admin_builtin_update_kernel (int argc, char **argv, GError **error) +ot_admin_builtin_update_kernel (int argc, char **argv, GFile *ostree_dir, GError **error) { GOptionContext *context; OtAdminUpdateKernel self_data; @@ -304,18 +306,21 @@ ot_admin_builtin_update_kernel (int argc, char **argv, GError **error) release = utsname.release; if (argc > 2) release = argv[2]; + + self->ostree_dir = g_object_ref (ostree_dir); - if (!copy_modules (release, cancellable, error)) + if (!copy_modules (self, release, cancellable, error)) goto out; - if (!update_initramfs (release, deploy_path, cancellable, error)) + if (!update_initramfs (self, release, deploy_path, cancellable, error)) goto out; - if (!update_grub (release, cancellable, error)) + if (!update_grub (self, release, cancellable, error)) goto out; ret = TRUE; out: + g_clear_object (&self->ostree_dir); if (context) g_option_context_free (context); return ret; diff --git a/src/ostadmin/ot-admin-builtins.h b/src/ostree/ot-admin-builtins.h similarity index 77% rename from src/ostadmin/ot-admin-builtins.h rename to src/ostree/ot-admin-builtins.h index 4ceca2a5..f9e60d71 100644 --- a/src/ostadmin/ot-admin-builtins.h +++ b/src/ostree/ot-admin-builtins.h @@ -27,10 +27,10 @@ G_BEGIN_DECLS -gboolean ot_admin_builtin_init (int argc, char **argv, GError **error); -gboolean ot_admin_builtin_deploy (int argc, char **argv, GError **error); -gboolean ot_admin_builtin_diff (int argc, char **argv, GError **error); -gboolean ot_admin_builtin_update_kernel (int argc, char **argv, GError **error); +gboolean ot_admin_builtin_init (int argc, char **argv, GFile *ostree_dir, GError **error); +gboolean ot_admin_builtin_deploy (int argc, char **argv, GFile *ostree_dir, GError **error); +gboolean ot_admin_builtin_diff (int argc, char **argv, GFile *ostree_dir, GError **error); +gboolean ot_admin_builtin_update_kernel (int argc, char **argv, GFile *ostree_dir, GError **error); G_END_DECLS diff --git a/src/ostadmin/ot-admin-functions.c b/src/ostree/ot-admin-functions.c similarity index 100% rename from src/ostadmin/ot-admin-functions.c rename to src/ostree/ot-admin-functions.c diff --git a/src/ostadmin/ot-admin-functions.h b/src/ostree/ot-admin-functions.h similarity index 100% rename from src/ostadmin/ot-admin-functions.h rename to src/ostree/ot-admin-functions.h diff --git a/src/ostree/ot-builtin-admin.c b/src/ostree/ot-builtin-admin.c new file mode 100644 index 00000000..f8aa8f9f --- /dev/null +++ b/src/ostree/ot-builtin-admin.c @@ -0,0 +1,105 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters + * + * 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. + * + * Author: Colin Walters + */ + +#include "config.h" + +#include "ot-builtins.h" +#include "ot-admin-builtins.h" +#include "ot-main.h" +#include "ostree.h" +#include "ostree-repo-file.h" + +#include + +static char *opt_ostree_dir = "/ostree"; + +static GOptionEntry options[] = { + { "ostree-dir", 0, 0, G_OPTION_ARG_STRING, &opt_ostree_dir, "Path to OSTree root directory (default: /ostree)", NULL }, + { NULL } +}; + +typedef struct { + const char *name; + gboolean (*fn) (int argc, char **argv, GFile *ostree_dir, GError **error); +} OstreeAdminCommand; + +static OstreeAdminCommand admin_subcommands[] = { + { "init", ot_admin_builtin_init }, + { "deploy", ot_admin_builtin_deploy }, + { "update-kernel", ot_admin_builtin_update_kernel }, + { "config-diff", ot_admin_builtin_diff }, + { NULL, NULL } +}; + +gboolean +ostree_builtin_admin (int argc, char **argv, GFile *repo_path, GError **error) +{ + GOptionContext *context; + gboolean ret = FALSE; + __attribute__((unused)) GCancellable *cancellable = NULL; + const char *subcommand_name; + OstreeAdminCommand *subcommand; + int subcmd_argc; + char **subcmd_argv = NULL; + ot_lobj GFile *ostree_dir = NULL; + + context = g_option_context_new ("[OPTIONS] SUBCOMMAND - Run an administrative subcommand"); + g_option_context_add_main_entries (context, options, NULL); + + if (!g_option_context_parse (context, &argc, &argv, error)) + goto out; + + if (argc <= 1) + { + ot_util_usage_error (context, "A valid SUBCOMMAND is required", error); + goto out; + } + subcommand_name = argv[1]; + + subcommand = admin_subcommands; + while (subcommand->name) + { + if (g_strcmp0 (subcommand_name, subcommand->name) == 0) + break; + subcommand++; + } + + if (!subcommand->name) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, + "Unknown command '%s'", subcommand_name); + goto out; + } + + ostree_dir = g_file_new_for_path (opt_ostree_dir); + + ostree_prep_builtin_argv (subcommand_name, argc-2, argv+2, &subcmd_argc, &subcmd_argv); + + if (!subcommand->fn (subcmd_argc, subcmd_argv, ostree_dir, error)) + goto out; + + ret = TRUE; + out: + if (context) + g_option_context_free (context); + return ret; +} diff --git a/src/ostree/ot-builtins.h b/src/ostree/ot-builtins.h index b380190c..aa7a645d 100644 --- a/src/ostree/ot-builtins.h +++ b/src/ostree/ot-builtins.h @@ -27,6 +27,7 @@ G_BEGIN_DECLS +gboolean ostree_builtin_admin (int argc, char **argv, GFile *repo_path, GError **error); gboolean ostree_builtin_cat (int argc, char **argv, GFile *repo_path, GError **error); gboolean ostree_builtin_config (int argc, char **argv, GFile *repo_path, GError **error); gboolean ostree_builtin_checkout (int argc, char **argv, GFile *repo_path, GError **error); diff --git a/src/ostree/ot-main.c b/src/ostree/ot-main.c index 90f3209b..554307f3 100644 --- a/src/ostree/ot-main.c +++ b/src/ostree/ot-main.c @@ -54,12 +54,12 @@ ostree_usage (char **argv, return (is_error ? 1 : 0); } -static void -prep_builtin_argv (const char *builtin, - int argc, - char **argv, - int *out_argc, - char ***out_argv) +void +ostree_prep_builtin_argv (const char *builtin, + int argc, + char **argv, + int *out_argc, + char ***out_argv) { int i; char **cmd_argv; @@ -165,7 +165,7 @@ ostree_run (int argc, goto out; } - prep_builtin_argv (cmd, argc-arg_off, argv+arg_off, &cmd_argc, &cmd_argv); + ostree_prep_builtin_argv (cmd, argc-arg_off, argv+arg_off, &cmd_argc, &cmd_argv); if (!command->fn (cmd_argc, cmd_argv, repo_file, &error)) goto out; diff --git a/src/ostree/ot-main.h b/src/ostree/ot-main.h index 4deb086a..f7e632b0 100644 --- a/src/ostree/ot-main.h +++ b/src/ostree/ot-main.h @@ -33,6 +33,13 @@ typedef struct { int flags; /* OstreeBuiltinFlags */ } OstreeCommand; +void +ostree_prep_builtin_argv (const char *builtin, + int argc, + char **argv, + int *out_argc, + char ***out_argv); + int ostree_main (int argc, char **argv, OstreeCommand *commands); int ostree_run (int argc, char **argv, OstreeCommand *commands, GError **error);