/** * ostree_sysroot_cleanup: * @self: Sysroot * @cancellable: Cancellable * @error: Error * * Delete any state that resulted from a partially completed * transaction, such as incomplete deployments. */ gboolean ostree_sysroot_cleanup (OstreeSysroot *self, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; gs_unref_object OstreeRepo *repo = NULL; g_return_val_if_fail (self->loaded, FALSE); if (!cleanup_other_bootversions (self, cancellable, error)) goto out; if (!cleanup_old_deployments (self, cancellable, error)) goto out; if (self->deployments->len > 0) { if (!ostree_sysroot_get_repo (self, &repo, cancellable, error)) goto out; if (!generate_deployment_refs_and_prune (self, repo, self->bootversion, self->subbootversion, self->deployments, cancellable, error)) goto out; } ret = TRUE; out: return ret; }
/* Used by the remote builtins which are special in taking --sysroot or --repo */ gboolean ostree_parse_sysroot_or_repo_option (GOptionContext *context, const char *sysroot_path, const char *repo_path, OstreeSysroot **out_sysroot, OstreeRepo **out_repo, GCancellable *cancellable, GError **error) { g_autoptr(OstreeSysroot) sysroot = NULL; g_autoptr(OstreeRepo) repo = NULL; if (sysroot_path) { g_autoptr(GFile) sysroot_file = g_file_new_for_path (sysroot_path); sysroot = ostree_sysroot_new (sysroot_file); if (!ostree_sysroot_load (sysroot, cancellable, error)) return FALSE; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) return FALSE; } else { repo = parse_repo_option (context, repo_path, FALSE, cancellable, error); if (!repo) return FALSE; } ot_transfer_out_value (out_sysroot, &sysroot); ot_transfer_out_value (out_repo, &repo); return TRUE; }
gboolean rpmostree_db_option_context_parse (GOptionContext *context, const GOptionEntry *main_entries, int *argc, char ***argv, OstreeRepo **out_repo, GCancellable *cancellable, GError **error) { gs_unref_object OstreeRepo *repo = NULL; gboolean success = FALSE; /* Entries are listed in --help output in the order added. We add the * main entries ourselves so that we can add the --repo entry first. */ g_option_context_add_main_entries (context, global_entries, NULL); if (!rpmostree_option_context_parse (context, main_entries, argc, argv, RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD, cancellable, NULL, error)) goto out; if (opt_repo == NULL) { gs_unref_object OstreeSysroot *sysroot = NULL; sysroot = ostree_sysroot_new_default (); if (!ostree_sysroot_load (sysroot, cancellable, error)) goto out; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) goto out; } else { gs_unref_object GFile *repo_file = g_file_new_for_path (opt_repo); repo = ostree_repo_new (repo_file); if (!ostree_repo_open (repo, cancellable, error)) goto out; } if (rpmReadConfigFiles (NULL, NULL)) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "rpm failed to init: %s", rpmlogMessage ()); goto out; } gs_transfer_out_value (out_repo, &repo); success = TRUE; out: return success; }
/** * rpmostreed_sysroot_populate : * * loads internals and starts monitoring * * Returns: True on success */ gboolean rpmostreed_sysroot_populate (RpmostreedSysroot *self, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; g_autoptr(GFile) sysroot_file = NULL; const char *sysroot_path; g_return_val_if_fail (self != NULL, FALSE); sysroot_path = rpmostree_sysroot_get_path (RPMOSTREE_SYSROOT (self)); sysroot_file = g_file_new_for_path (sysroot_path); self->ot_sysroot = ostree_sysroot_new (sysroot_file); /* This creates and caches an OstreeRepo instance inside * OstreeSysroot to ensure subsequent ostree_sysroot_get_repo() * calls won't fail. */ if (!ostree_sysroot_get_repo (self->ot_sysroot, &self->repo, cancellable, error)) goto out; if (!sysroot_populate_deployments_unlocked (self, NULL, error)) goto out; if (self->monitor == NULL) { const char *sysroot_path = gs_file_get_path_cached (ostree_sysroot_get_path (self->ot_sysroot)); g_autofree char *sysroot_deploy_path = g_build_filename (sysroot_path, "ostree/deploy", NULL); g_autoptr(GFile) sysroot_deploy = g_file_new_for_path (sysroot_deploy_path); self->monitor = g_file_monitor (sysroot_deploy, 0, NULL, error); if (self->monitor == NULL) goto out; self->sig_changed = g_signal_connect (self->monitor, "changed", G_CALLBACK (on_deploy_changed), self); } ret = TRUE; out: return ret; }
gboolean ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error) { const int is_tty = isatty (1); const char *red_bold_prefix = is_tty ? "\x1b[31m\x1b[1m" : ""; const char *red_bold_suffix = is_tty ? "\x1b[22m\x1b[0m" : ""; g_autoptr(GOptionContext) context = g_option_context_new (""); g_autoptr(OstreeSysroot) sysroot = NULL; if (!ostree_admin_option_context_parse (context, options, &argc, &argv, OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED, invocation, &sysroot, cancellable, error)) return FALSE; g_autoptr(OstreeRepo) repo = NULL; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) return FALSE; g_autoptr(GPtrArray) deployments = ostree_sysroot_get_deployments (sysroot); OstreeDeployment *booted_deployment = ostree_sysroot_get_booted_deployment (sysroot); g_autoptr(OstreeDeployment) pending_deployment = NULL; g_autoptr(OstreeDeployment) rollback_deployment = NULL; if (booted_deployment) ostree_sysroot_query_deployments_for (sysroot, NULL, &pending_deployment, &rollback_deployment); if (deployments->len == 0) { g_print ("No deployments.\n"); } else { for (guint i = 0; i < deployments->len; i++) { OstreeDeployment *deployment = deployments->pdata[i]; const char *ref = ostree_deployment_get_csum (deployment); /* Load the backing commit; shouldn't normally fail, but if it does, * we stumble on. */ g_autoptr(GVariant) commit = NULL; (void)ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT, ref, &commit, NULL); g_autoptr(GVariant) commit_metadata = NULL; if (commit) commit_metadata = g_variant_get_child_value (commit, 0); const char *version = NULL; const char *source_title = NULL; if (commit_metadata) { (void) g_variant_lookup (commit_metadata, OSTREE_COMMIT_META_KEY_VERSION, "&s", &version); (void) g_variant_lookup (commit_metadata, OSTREE_COMMIT_META_KEY_SOURCE_TITLE, "&s", &source_title); } GKeyFile *origin = ostree_deployment_get_origin (deployment); const char *deployment_status = ""; if (deployment == pending_deployment) deployment_status = " (pending)"; else if (deployment == rollback_deployment) deployment_status = " (rollback)"; g_print ("%c %s %s.%d%s\n", deployment == booted_deployment ? '*' : ' ', ostree_deployment_get_osname (deployment), ostree_deployment_get_csum (deployment), ostree_deployment_get_deployserial (deployment), deployment_status); if (version) g_print (" Version: %s\n", version); OstreeDeploymentUnlockedState unlocked = ostree_deployment_get_unlocked (deployment); switch (unlocked) { case OSTREE_DEPLOYMENT_UNLOCKED_NONE: break; default: g_print (" %sUnlocked: %s%s\n", red_bold_prefix, ostree_deployment_unlocked_state_to_string (unlocked), red_bold_suffix); } if (!origin) g_print (" origin: none\n"); else { g_autofree char *origin_refspec = g_key_file_get_string (origin, "origin", "refspec", NULL); if (!origin_refspec) g_print (" origin: <unknown origin type>\n"); else g_print (" origin refspec: %s\n", origin_refspec); if (source_title) g_print (" `- %s\n", source_title); } if (deployment_get_gpg_verify (deployment, repo)) { g_autoptr(GString) output_buffer = g_string_sized_new (256); /* Print any digital signatures on this commit. */ g_autoptr(GError) local_error = NULL; g_autoptr(OstreeGpgVerifyResult) result = ostree_repo_verify_commit_ext (repo, ref, NULL, NULL, cancellable, &local_error); /* G_IO_ERROR_NOT_FOUND just means the commit is not signed. */ if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) { g_clear_error (&local_error); continue; } else if (local_error != NULL) { g_propagate_error (error, g_steal_pointer (&local_error)); return FALSE; } const guint n_signatures = ostree_gpg_verify_result_count_all (result); for (guint jj = 0; jj < n_signatures; jj++) { ostree_gpg_verify_result_describe (result, jj, output_buffer, " GPG: ", OSTREE_GPG_SIGNATURE_FORMAT_DEFAULT); } g_print ("%s", output_buffer->str); } } } return TRUE; }
gboolean rpmostree_builtin_upgrade (int argc, char **argv, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; GOptionContext *context = g_option_context_new ("- Perform a system upgrade"); glnx_unref_object RPMOSTreeOS *os_proxy = NULL; glnx_unref_object RPMOSTreeSysroot *sysroot_proxy = NULL; g_autoptr(GVariant) default_deployment = NULL; g_autofree char *transaction_address = NULL; if (!rpmostree_option_context_parse (context, option_entries, &argc, &argv, RPM_OSTREE_BUILTIN_FLAG_NONE, cancellable, &sysroot_proxy, error)) goto out; if (opt_check_diff && opt_reboot) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "cannot specify both --reboot and --check-diff"); goto out; } if (!rpmostree_load_os_proxy (sysroot_proxy, opt_osname, cancellable, &os_proxy, error)) goto out; if (opt_check_diff) { if (!rpmostree_os_call_download_update_rpm_diff_sync (os_proxy, &transaction_address, cancellable, error)) goto out; } else { g_signal_connect (os_proxy, "notify::default-deployment", G_CALLBACK (default_changed_callback), &default_deployment); if (!rpmostree_os_call_upgrade_sync (os_proxy, get_args_variant (), &transaction_address, cancellable, error)) goto out; } if (!rpmostree_transaction_get_response_sync (sysroot_proxy, transaction_address, cancellable, error)) goto out; if (opt_check_diff) { /* yes, doing this without using dbus */ gs_unref_object OstreeSysroot *sysroot = NULL; gs_unref_object OstreeRepo *repo = NULL; gs_unref_object GFile *rpmdbdir = NULL; gs_unref_object GFile *sysroot_file = NULL; g_autofree char *origin_description = NULL; g_autoptr(GVariant) cached_update = NULL; const char *sysroot_path; GVariantDict upgrade_dict; _cleanup_rpmrev_ struct RpmRevisionData *rpmrev1 = NULL; _cleanup_rpmrev_ struct RpmRevisionData *rpmrev2 = NULL; gs_free char *ref = NULL; /* location of this rev */ gs_free char *remote = NULL; if (!rpmostree_os_get_has_cached_update_rpm_diff (os_proxy)) goto out; sysroot_path = rpmostree_sysroot_get_path (sysroot_proxy); sysroot_file = g_file_new_for_path (sysroot_path); sysroot = ostree_sysroot_new (sysroot_file); if (!ostree_sysroot_load (sysroot, cancellable, error)) goto out; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) goto out; cached_update = rpmostree_os_dup_cached_update(os_proxy); g_variant_dict_init (&upgrade_dict, cached_update); if (!g_variant_dict_lookup (&upgrade_dict, "origin", "s", &origin_description)) goto out; if (!ostree_parse_refspec (origin_description, &remote, &ref, error)) goto out; if (rpmReadConfigFiles (NULL, NULL)) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "rpm failed to init: %s", rpmlogMessage ()); goto out; } if (!(rpmrev1 = rpmrev_new (repo, ostree_deployment_get_csum (ostree_sysroot_get_booted_deployment (sysroot)), NULL, cancellable, error))) goto out; if (!(rpmrev2 = rpmrev_new (repo, ref, NULL, cancellable, error))) goto out; rpmhdrs_diff_prnt_diff (rpmhdrs_diff (rpmrev_get_headers (rpmrev1), rpmrev_get_headers (rpmrev2))); } else { /* nothing changed */ if (default_deployment == NULL) { goto out; } if (!opt_reboot) { const char *sysroot_path; sysroot_path = rpmostree_sysroot_get_path (sysroot_proxy); if (!rpmostree_print_treepkg_diff_from_sysroot_path (sysroot_path, cancellable, error)) goto out; g_print ("Run \"systemctl reboot\" to start a reboot\n"); } } ret = TRUE; out: /* Does nothing if using the message bus. */ rpmostree_cleanup_peer (); return ret; }
gboolean rpmostree_builtin_upgrade (int argc, char **argv, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; GOptionContext *context = g_option_context_new ("- Perform a system upgrade"); gs_unref_object GFile *sysroot_path = NULL; gs_unref_object OstreeSysroot *sysroot = NULL; gs_unref_object OstreeSysrootUpgrader *upgrader = NULL; gs_unref_object OstreeAsyncProgress *progress = NULL; GSConsole *console = NULL; gboolean changed; OstreeSysrootUpgraderPullFlags upgraderpullflags = 0; gs_free char *origin_description = NULL; gs_unref_object OstreeRepo *repo = NULL; g_option_context_add_main_entries (context, option_entries, NULL); if (!g_option_context_parse (context, &argc, &argv, error)) goto out; sysroot_path = g_file_new_for_path (opt_sysroot); sysroot = ostree_sysroot_new (sysroot_path); if (!ostree_sysroot_load (sysroot, cancellable, error)) goto out; upgrader = ostree_sysroot_upgrader_new_for_os (sysroot, opt_osname, cancellable, error); if (!upgrader) goto out; origin_description = ostree_sysroot_upgrader_get_origin_description (upgrader); if (origin_description) g_print ("Updating from: %s\n", origin_description); if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) goto out; console = gs_console_get (); if (console) { gs_console_begin_status_line (console, "", NULL, NULL); progress = ostree_async_progress_new_and_connect (_rpmostree_pull_progress, console); } if (opt_allow_downgrade) upgraderpullflags |= OSTREE_SYSROOT_UPGRADER_PULL_FLAGS_ALLOW_OLDER; if (opt_check_diff) { if (!ostree_sysroot_upgrader_pull_one_dir (upgrader, "/usr/share/rpm", 0, 0, progress, &changed, cancellable, error)) goto out; } else { if (!ostree_sysroot_upgrader_pull (upgrader, 0, 0, progress, &changed, cancellable, error)) goto out; } if (console) { if (!gs_console_end_status_line (console, cancellable, error)) { console = NULL; goto out; } console = NULL; } if (!changed) { g_print ("No updates available.\n"); } else { if (!opt_check_diff) { if (!ostree_sysroot_upgrader_deploy (upgrader, cancellable, error)) goto out; if (opt_reboot) gs_subprocess_simple_run_sync (NULL, GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT, cancellable, error, "systemctl", "reboot", NULL); else { #ifdef HAVE_PATCHED_HAWKEY_AND_LIBSOLV if (!rpmostree_print_treepkg_diff (sysroot, cancellable, error)) goto out; #endif g_print ("Updates prepared for next boot; run \"systemctl reboot\" to start a reboot\n"); } } else { gs_unref_object GFile *rpmdbdir = NULL; _cleanup_rpmrev_ struct RpmRevisionData *rpmrev1 = NULL; _cleanup_rpmrev_ struct RpmRevisionData *rpmrev2 = NULL; gs_free char *tmpd = g_mkdtemp (g_strdup ("/tmp/rpm-ostree.XXXXXX")); gs_free char *ref = NULL; // location of this rev gs_free char *remote = NULL; if (!ostree_parse_refspec (origin_description, &remote, &ref, error)) goto out; if (rpmReadConfigFiles (NULL, NULL)) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "rpm failed to init: %s", rpmlogMessage()); goto out; } rpmdbdir = g_file_new_for_path (tmpd); if (!(rpmrev1 = rpmrev_new (repo, rpmdbdir, ostree_deployment_get_csum (ostree_sysroot_get_booted_deployment (sysroot)), NULL, cancellable, error))) goto out; if (!(rpmrev2 = rpmrev_new (repo, rpmdbdir, ref, NULL, cancellable, error))) goto out; rpmhdrs_diff_prnt_diff (rpmrev1->root, rpmrev2->root, rpmhdrs_diff (rpmrev1->rpmdb, rpmrev2->rpmdb), cancellable, error); } } ret = TRUE; out: if (console) (void) gs_console_end_status_line (console, NULL, NULL); return ret; }
gboolean rpmostree_db_option_context_parse (GOptionContext *context, const GOptionEntry *main_entries, int *argc, char ***argv, OstreeRepo **out_repo, GFile **out_rpmdbdir, gboolean *out_rpmdbdir_is_tmp, GCancellable *cancellable, GError **error) { gs_unref_object OstreeRepo *repo = NULL; gs_unref_object GFile *rpmdbdir = NULL; gboolean rpmdbdir_is_tmp = FALSE; gboolean success = FALSE; /* Entries are listed in --help output in the order added. We add the * main entries ourselves so that we can add the --repo entry first. */ g_option_context_add_main_entries (context, global_entries, NULL); if (!rpmostree_option_context_parse (context, main_entries, argc, argv, error)) goto out; if (opt_repo == NULL) { gs_unref_object OstreeSysroot *sysroot = NULL; sysroot = ostree_sysroot_new_default (); if (!ostree_sysroot_load (sysroot, cancellable, error)) goto out; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) goto out; } else { gs_unref_object GFile *repo_file = g_file_new_for_path (opt_repo); repo = ostree_repo_new (repo_file); if (!ostree_repo_open (repo, cancellable, error)) goto out; } if (rpmReadConfigFiles (NULL, NULL)) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "rpm failed to init: %s", rpmlogMessage ()); goto out; } if (opt_rpmdbdir != NULL) { rpmdbdir = g_file_new_for_path (opt_rpmdbdir); } else { /* tmp on tmpfs is much faster than /var/tmp, * and the rpmdb itself shouldn't be too big. */ gs_free char *tmpd = g_mkdtemp (g_strdup ("/tmp/rpm-ostree.XXXXXX")); rpmdbdir = g_file_new_for_path (tmpd); rpmdbdir_is_tmp = TRUE; ostree_repo_set_disable_fsync (repo, TRUE); } gs_transfer_out_value (out_repo, &repo); gs_transfer_out_value (out_rpmdbdir, &rpmdbdir); if (out_rpmdbdir_is_tmp != NULL) *out_rpmdbdir_is_tmp = rpmdbdir_is_tmp; success = TRUE; out: return success; }
gboolean rpmostree_builtin_rebase (int argc, char **argv, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; GOptionContext *context = g_option_context_new ("REFSPEC - Switch to a different tree"); const char *new_provided_refspec; gs_unref_object OstreeSysroot *sysroot = NULL; gs_unref_object OstreeRepo *repo = NULL; gs_free char *origin_refspec = NULL; gs_free char *origin_remote = NULL; gs_free char *origin_ref = NULL; gs_free char *new_remote = NULL; gs_free char *new_ref = NULL; gs_free char *new_refspec = NULL; gs_unref_object GFile *sysroot_path = NULL; gs_unref_object OstreeSysrootUpgrader *upgrader = NULL; gs_unref_object OstreeAsyncProgress *progress = NULL; gboolean changed; GSConsole *console = NULL; gs_unref_keyfile GKeyFile *old_origin = NULL; gs_unref_keyfile GKeyFile *new_origin = NULL; if (!rpmostree_option_context_parse (context, option_entries, &argc, &argv, error)) goto out; if (argc < 2) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "REFSPEC must be specified"); goto out; } new_provided_refspec = argv[1]; sysroot_path = g_file_new_for_path (opt_sysroot); sysroot = ostree_sysroot_new (sysroot_path); if (!ostree_sysroot_load (sysroot, cancellable, error)) goto out; upgrader = ostree_sysroot_upgrader_new_for_os_with_flags (sysroot, opt_osname, OSTREE_SYSROOT_UPGRADER_FLAGS_IGNORE_UNCONFIGURED, cancellable, error); if (!upgrader) goto out; old_origin = ostree_sysroot_upgrader_get_origin (upgrader); origin_refspec = g_key_file_get_string (old_origin, "origin", "refspec", NULL); if (!ostree_parse_refspec (origin_refspec, &origin_remote, &origin_ref, error)) goto out; /* Allow just switching remotes */ if (g_str_has_suffix (new_provided_refspec, ":")) { new_remote = g_strdup (new_provided_refspec); new_remote[strlen(new_remote)-1] = '\0'; new_ref = g_strdup (origin_ref); } else { if (!ostree_parse_refspec (new_provided_refspec, &new_remote, &new_ref, error)) goto out; } if (!new_remote) new_refspec = g_strconcat (origin_remote, ":", new_ref, NULL); else new_refspec = g_strconcat (new_remote, ":", new_ref, NULL); if (strcmp (origin_refspec, new_refspec) == 0) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Old and new refs are equal: %s", new_refspec); goto out; } new_origin = ostree_sysroot_origin_new_from_refspec (sysroot, new_refspec); if (!ostree_sysroot_upgrader_set_origin (upgrader, new_origin, cancellable, error)) goto out; console = gs_console_get (); if (console) { gs_console_begin_status_line (console, "", NULL, NULL); progress = ostree_async_progress_new_and_connect (ostree_repo_pull_default_console_progress_changed, console); } /* Always allow older...there's not going to be a chronological * relationship necessarily. */ if (!ostree_sysroot_upgrader_pull (upgrader, 0, OSTREE_SYSROOT_UPGRADER_PULL_FLAGS_ALLOW_OLDER, progress, &changed, cancellable, error)) goto out; if (console) { if (!gs_console_end_status_line (console, cancellable, error)) { console = NULL; goto out; } console = NULL; } if (!ostree_sysroot_upgrader_deploy (upgrader, cancellable, error)) goto out; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) goto out; if (!ostree_repo_prepare_transaction (repo, NULL, cancellable, error)) goto out; g_print ("Deleting ref '%s:%s'\n", origin_remote, origin_ref); ostree_repo_transaction_set_ref (repo, origin_remote, origin_ref, NULL); if (!ostree_repo_commit_transaction (repo, NULL, cancellable, error)) goto out; if (!rpmostree_print_treepkg_diff (sysroot, cancellable, error)) goto out; ret = TRUE; out: if (console) (void) gs_console_end_status_line (console, NULL, NULL); return ret; }
gboolean ot_admin_builtin_set_origin (int argc, char **argv, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; GOptionContext *context; const char *remotename = NULL; const char *url = NULL; const char *branch = NULL; gs_unref_object OstreeRepo *repo = NULL; gs_unref_object OstreeSysroot *sysroot = NULL; OstreeDeployment *target_deployment = NULL; context = g_option_context_new ("REMOTENAME URL [BRANCH]"); if (!ostree_admin_option_context_parse (context, options, &argc, &argv, OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER, &sysroot, cancellable, error)) goto out; if (argc < 3) { ot_util_usage_error (context, "REMOTENAME and URL must be specified", error); goto out; } remotename = argv[1]; url = argv[2]; if (argc > 3) branch = argv[3]; if (!ostree_sysroot_load (sysroot, cancellable, error)) goto out; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) goto out; if (opt_index == -1) { target_deployment = ostree_sysroot_get_booted_deployment (sysroot); if (target_deployment == NULL) { g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not currently booted into an OSTree system"); goto out; } } else { target_deployment = ot_admin_get_indexed_deployment (sysroot, opt_index, error); if (!target_deployment) goto out; } { char **iter; gs_unref_variant_builder GVariantBuilder *optbuilder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}")); for (iter = opt_set; iter && *iter; iter++) { const char *keyvalue = *iter; gs_free char *subkey = NULL; gs_free char *subvalue = NULL; if (!ot_parse_keyvalue (keyvalue, &subkey, &subvalue, error)) goto out; g_variant_builder_add (optbuilder, "{s@v}", subkey, g_variant_new_variant (g_variant_new_string (subvalue))); } if (!ostree_repo_remote_change (repo, NULL, OSTREE_REPO_REMOTE_CHANGE_ADD_IF_NOT_EXISTS, remotename, url, g_variant_builder_end (optbuilder), cancellable, error)) goto out; } { GKeyFile *old_origin = ostree_deployment_get_origin (target_deployment); gs_free char *origin_refspec = g_key_file_get_string (old_origin, "origin", "refspec", NULL); gs_free char *new_refspec = NULL; gs_free char *origin_remote = NULL; gs_free char *origin_ref = NULL; if (!ostree_parse_refspec (origin_refspec, &origin_remote, &origin_ref, error)) goto out; { gs_free char *new_refspec = g_strconcat (remotename, ":", branch ? branch : origin_ref, NULL); gs_unref_keyfile GKeyFile *new_origin = NULL; gs_unref_object GFile *origin_path = NULL; new_origin = ostree_sysroot_origin_new_from_refspec (sysroot, new_refspec); if (!ostree_sysroot_write_origin_file (sysroot, target_deployment, new_origin, cancellable, error)) goto out; } } ret = TRUE; out: if (context) g_option_context_free (context); return ret; }
gboolean ot_admin_builtin_deploy (int argc, char **argv, OstreeSysroot *sysroot, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; const char *refspec; GOptionContext *context; GKeyFile *origin = NULL; gs_unref_object OstreeRepo *repo = NULL; gs_unref_ptrarray GPtrArray *new_deployments = NULL; gs_unref_object OstreeDeployment *new_deployment = NULL; gs_unref_object OstreeDeployment *merge_deployment = NULL; gs_free char *revision = NULL; __attribute__((cleanup(_ostree_kernel_args_cleanup))) OstreeKernelArgs *kargs = NULL; context = g_option_context_new ("REFSPEC - Checkout revision REFSPEC as the new default deployment"); g_option_context_add_main_entries (context, options, NULL); if (!g_option_context_parse (context, &argc, &argv, error)) goto out; if (argc < 2) { ot_util_usage_error (context, "REF/REV must be specified", error); goto out; } refspec = argv[1]; if (!ostree_sysroot_load (sysroot, cancellable, error)) goto out; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) goto out; /* Find the currently booted deployment, if any; we will ensure it * is present in the new deployment list. */ if (!ot_admin_require_booted_deployment_or_osname (sysroot, opt_osname, cancellable, error)) { g_prefix_error (error, "Looking for booted deployment: "); goto out; } if (opt_origin_path) { origin = g_key_file_new (); if (!g_key_file_load_from_file (origin, opt_origin_path, 0, error)) goto out; } else { origin = ostree_sysroot_origin_new_from_refspec (sysroot, refspec); } if (!ostree_repo_resolve_rev (repo, refspec, FALSE, &revision, error)) goto out; merge_deployment = ostree_sysroot_get_merge_deployment (sysroot, opt_osname); /* 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. * * TODO: Add /ostree/transaction file, and only do this cleanup if * we find it. */ if (!ostree_sysroot_cleanup (sysroot, cancellable, error)) { g_prefix_error (error, "Performing initial cleanup: "); goto out; } kargs = _ostree_kernel_args_new (); /* If they want the current kernel's args, they very likely don't * want the ones from the merge. */ if (opt_kernel_proc_cmdline) { gs_unref_object GFile *proc_cmdline_path = g_file_new_for_path ("/proc/cmdline"); gs_free char *proc_cmdline = NULL; gsize proc_cmdline_len = 0; gs_strfreev char **proc_cmdline_args = NULL; if (!g_file_load_contents (proc_cmdline_path, cancellable, &proc_cmdline, &proc_cmdline_len, NULL, error)) goto out; g_strchomp (proc_cmdline); proc_cmdline_args = g_strsplit (proc_cmdline, " ", -1); _ostree_kernel_args_replace_argv (kargs, proc_cmdline_args); } else if (merge_deployment) { OstreeBootconfigParser *bootconfig = ostree_deployment_get_bootconfig (merge_deployment); gs_strfreev char **previous_args = g_strsplit (ostree_bootconfig_parser_get (bootconfig, "options"), " ", -1); _ostree_kernel_args_replace_argv (kargs, previous_args); } if (opt_kernel_argv) { _ostree_kernel_args_replace_argv (kargs, opt_kernel_argv); } if (opt_kernel_argv_append) { _ostree_kernel_args_append_argv (kargs, opt_kernel_argv_append); } { gs_strfreev char **kargs_strv = _ostree_kernel_args_to_strv (kargs); if (!ostree_sysroot_deploy_tree (sysroot, opt_osname, revision, origin, merge_deployment, kargs_strv, &new_deployment, cancellable, error)) goto out; } if (!ostree_sysroot_simple_write_deployment (sysroot, opt_osname, new_deployment, merge_deployment, opt_retain ? OSTREE_SYSROOT_SIMPLE_WRITE_DEPLOYMENT_FLAGS_RETAIN : 0, cancellable, error)) goto out; ret = TRUE; out: if (origin) g_key_file_unref (origin); if (context) g_option_context_free (context); return ret; }
gboolean ot_admin_builtin_switch (int argc, char **argv, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; g_autoptr(GOptionContext) context = NULL; glnx_unref_object OstreeSysroot *sysroot = NULL; const char *new_provided_refspec = NULL; glnx_unref_object OstreeRepo *repo = NULL; g_autofree char *origin_refspec = NULL; g_autofree char *origin_remote = NULL; g_autofree char *origin_ref = NULL; g_autofree char *new_remote = NULL; g_autofree char *new_ref = NULL; g_autofree char *new_refspec = NULL; glnx_unref_object OstreeSysrootUpgrader *upgrader = NULL; glnx_unref_object OstreeAsyncProgress *progress = NULL; gboolean changed; GKeyFile *old_origin; GKeyFile *new_origin = NULL; context = g_option_context_new ("REF - Construct new tree from REF and deploy it"); if (!ostree_admin_option_context_parse (context, options, &argc, &argv, OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER, &sysroot, cancellable, error)) goto out; if (argc < 2) { ot_util_usage_error (context, "REF must be specified", error); goto out; } new_provided_refspec = argv[1]; if (!ostree_sysroot_load (sysroot, cancellable, error)) goto out; upgrader = ostree_sysroot_upgrader_new_for_os_with_flags (sysroot, opt_osname, OSTREE_SYSROOT_UPGRADER_FLAGS_IGNORE_UNCONFIGURED, cancellable, error); if (!upgrader) goto out; old_origin = ostree_sysroot_upgrader_get_origin (upgrader); origin_refspec = g_key_file_get_string (old_origin, "origin", "refspec", NULL); if (!ostree_parse_refspec (origin_refspec, &origin_remote, &origin_ref, error)) goto out; /* Allow just switching remotes */ if (g_str_has_suffix (new_provided_refspec, ":")) { new_remote = g_strdup (new_provided_refspec); new_remote[strlen(new_remote)-1] = '\0'; new_ref = g_strdup (origin_ref); } else { if (!ostree_parse_refspec (new_provided_refspec, &new_remote, &new_ref, error)) goto out; } if (!new_remote) new_refspec = g_strconcat (origin_remote, ":", new_ref, NULL); else new_refspec = g_strconcat (new_remote, ":", new_ref, NULL); if (strcmp (origin_refspec, new_refspec) == 0) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Old and new refs are equal: %s", new_refspec); goto out; } new_origin = ostree_sysroot_origin_new_from_refspec (sysroot, new_refspec); if (!ostree_sysroot_upgrader_set_origin (upgrader, new_origin, cancellable, error)) goto out; { g_auto(GLnxConsoleRef) console = { 0, }; glnx_console_lock (&console); if (console.is_tty) progress = ostree_async_progress_new_and_connect (ostree_repo_pull_default_console_progress_changed, &console); /* Always allow older...there's not going to be a chronological * relationship necessarily. */ if (!ostree_sysroot_upgrader_pull (upgrader, 0, OSTREE_SYSROOT_UPGRADER_PULL_FLAGS_ALLOW_OLDER, progress, &changed, cancellable, error)) goto out; if (progress) ostree_async_progress_finish (progress); } if (!ostree_sysroot_upgrader_deploy (upgrader, cancellable, error)) goto out; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) goto out; if (!ostree_repo_prepare_transaction (repo, NULL, cancellable, error)) goto out; g_print ("Deleting ref '%s:%s'\n", origin_remote, origin_ref); ostree_repo_transaction_set_ref (repo, origin_remote, origin_ref, NULL); if (!ostree_repo_commit_transaction (repo, NULL, cancellable, error)) goto out; if (opt_reboot) { if (!ot_admin_execve_reboot (sysroot, error)) goto out; } ret = TRUE; out: if (new_origin) g_key_file_unref (new_origin); return ret; }
gboolean ot_admin_builtin_switch (int argc, char **argv, OstreeSysroot *sysroot, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; GOptionContext *context; const char *new_provided_refspec = NULL; gs_unref_object OstreeRepo *repo = NULL; gs_free char *origin_refspec = NULL; gs_free char *origin_remote = NULL; gs_free char *origin_ref = NULL; gs_free char *new_remote = NULL; gs_free char *new_ref = NULL; gs_free char *new_refspec = NULL; gs_free char *new_revision = NULL; gs_unref_object GFile *deployment_path = NULL; gs_unref_object GFile *deployment_origin_path = NULL; gs_unref_object OstreeDeployment *merge_deployment = NULL; gs_unref_object OstreeDeployment *new_deployment = NULL; gs_unref_object OstreeSysrootUpgrader *upgrader = NULL; gs_unref_object OstreeAsyncProgress *progress = NULL; gboolean changed; GSConsole *console = NULL; gboolean in_status_line = FALSE; GKeyFile *old_origin; GKeyFile *new_origin = NULL; context = g_option_context_new ("REF - Construct new tree from current origin and deploy it, if it changed"); g_option_context_add_main_entries (context, options, NULL); if (!g_option_context_parse (context, &argc, &argv, error)) goto out; if (argc < 2) { ot_util_usage_error (context, "REF must be specified", error); goto out; } new_provided_refspec = argv[1]; if (!ostree_sysroot_load (sysroot, cancellable, error)) goto out; upgrader = ostree_sysroot_upgrader_new_for_os (sysroot, opt_osname, cancellable, error); if (!upgrader) goto out; old_origin = ostree_sysroot_upgrader_get_origin (upgrader); origin_refspec = g_key_file_get_string (old_origin, "origin", "refspec", NULL); if (!ostree_parse_refspec (origin_refspec, &origin_remote, &origin_ref, error)) goto out; /* Allow just switching remotes */ if (g_str_has_suffix (new_provided_refspec, ":")) { new_remote = g_strdup (new_provided_refspec); new_remote[strlen(new_remote)-1] = '\0'; new_ref = g_strdup (origin_ref); } else { if (!ostree_parse_refspec (new_provided_refspec, &new_remote, &new_ref, error)) goto out; } if (!new_remote) new_refspec = g_strconcat (origin_remote, ":", new_ref, NULL); else new_refspec = g_strconcat (new_remote, ":", new_ref, NULL); if (strcmp (origin_refspec, new_refspec) == 0) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Old and new refs are equal: %s", new_refspec); goto out; } new_origin = ostree_sysroot_origin_new_from_refspec (sysroot, new_refspec); if (!ostree_sysroot_upgrader_set_origin (upgrader, new_origin, cancellable, error)) goto out; console = gs_console_get (); if (console) { gs_console_begin_status_line (console, "", NULL, NULL); in_status_line = TRUE; progress = ostree_async_progress_new_and_connect (ot_common_pull_progress, console); } /* Always allow older...there's not going to be a chronological * relationship necessarily. */ if (!ostree_sysroot_upgrader_pull (upgrader, 0, OSTREE_SYSROOT_UPGRADER_PULL_FLAGS_ALLOW_OLDER, progress, &changed, cancellable, error)) goto out; if (in_status_line) { gs_console_end_status_line (console, NULL, NULL); in_status_line = FALSE; } if (!ostree_sysroot_upgrader_deploy (upgrader, cancellable, error)) goto out; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) goto out; if (!ostree_repo_prepare_transaction (repo, NULL, cancellable, error)) goto out; g_print ("Deleting ref '%s:%s'\n", origin_remote, origin_ref); ostree_repo_transaction_set_ref (repo, origin_remote, origin_ref, NULL); if (!ostree_repo_commit_transaction (repo, NULL, cancellable, error)) goto out; { gs_unref_object GFile *real_sysroot = g_file_new_for_path ("/"); if (opt_reboot && g_file_equal (ostree_sysroot_get_path (sysroot), real_sysroot)) { gs_subprocess_simple_run_sync (NULL, GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT, cancellable, error, "systemctl", "reboot", NULL); } } ret = TRUE; out: if (in_status_line) gs_console_end_status_line (console, NULL, NULL); if (new_origin) g_key_file_unref (new_origin); if (context) g_option_context_free (context); return ret; }
gboolean ot_admin_builtin_status (int argc, char **argv, GCancellable *cancellable, GError **error) { g_autoptr(GOptionContext) context = NULL; glnx_unref_object OstreeSysroot *sysroot = NULL; gboolean ret = FALSE; glnx_unref_object OstreeRepo *repo = NULL; OstreeDeployment *booted_deployment = NULL; g_autoptr(GPtrArray) deployments = NULL; const int is_tty = isatty (1); const char *red_bold_prefix = is_tty ? "\x1b[31m\x1b[1m" : ""; const char *red_bold_suffix = is_tty ? "\x1b[22m\x1b[0m" : ""; guint i; context = g_option_context_new ("List deployments"); if (!ostree_admin_option_context_parse (context, options, &argc, &argv, OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED, &sysroot, cancellable, error)) goto out; if (!ostree_sysroot_load (sysroot, cancellable, error)) goto out; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) goto out; deployments = ostree_sysroot_get_deployments (sysroot); booted_deployment = ostree_sysroot_get_booted_deployment (sysroot); if (deployments->len == 0) { g_print ("No deployments.\n"); } else { for (i = 0; i < deployments->len; i++) { OstreeDeployment *deployment = deployments->pdata[i]; GKeyFile *origin; const char *ref = ostree_deployment_get_csum (deployment); OstreeDeploymentUnlockedState unlocked = ostree_deployment_get_unlocked (deployment); g_autofree char *version = version_of_commit (repo, ref); glnx_unref_object OstreeGpgVerifyResult *result = NULL; GString *output_buffer; guint jj, n_signatures; GError *local_error = NULL; origin = ostree_deployment_get_origin (deployment); g_print ("%c %s %s.%d\n", deployment == booted_deployment ? '*' : ' ', ostree_deployment_get_osname (deployment), ostree_deployment_get_csum (deployment), ostree_deployment_get_deployserial (deployment)); if (version) g_print (" Version: %s\n", version); switch (unlocked) { case OSTREE_DEPLOYMENT_UNLOCKED_NONE: break; default: g_print (" %sUnlocked: %s%s\n", red_bold_prefix, ostree_deployment_unlocked_state_to_string (unlocked), red_bold_suffix); } if (!origin) g_print (" origin: none\n"); else { g_autofree char *origin_refspec = g_key_file_get_string (origin, "origin", "refspec", NULL); if (!origin_refspec) g_print (" origin: <unknown origin type>\n"); else g_print (" origin refspec: %s\n", origin_refspec); } if (deployment_get_gpg_verify (deployment, repo)) { /* Print any digital signatures on this commit. */ result = ostree_repo_verify_commit_ext (repo, ref, NULL, NULL, cancellable, &local_error); /* G_IO_ERROR_NOT_FOUND just means the commit is not signed. */ if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) { g_clear_error (&local_error); continue; } else if (local_error != NULL) { g_propagate_error (error, local_error); goto out; } output_buffer = g_string_sized_new (256); n_signatures = ostree_gpg_verify_result_count_all (result); for (jj = 0; jj < n_signatures; jj++) { ostree_gpg_verify_result_describe (result, jj, output_buffer, " GPG: ", OSTREE_GPG_SIGNATURE_FORMAT_DEFAULT); } g_print ("%s", output_buffer->str); g_string_free (output_buffer, TRUE); } } } ret = TRUE; out: return ret; }
static gboolean package_diff_transaction_execute (RpmostreedTransaction *transaction, GCancellable *cancellable, GError **error) { PackageDiffTransaction *self; OstreeSysroot *sysroot; glnx_unref_object OstreeSysrootUpgrader *upgrader = NULL; glnx_unref_object OstreeAsyncProgress *progress = NULL; glnx_unref_object OstreeRepo *repo = NULL; g_autoptr(GKeyFile) origin = NULL; g_autofree gchar *origin_description = NULL; OstreeSysrootUpgraderPullFlags upgrader_flags = 0; gboolean upgrading = FALSE; gboolean changed = FALSE; gboolean ret = FALSE; self = (PackageDiffTransaction *) transaction; sysroot = rpmostreed_transaction_get_sysroot (transaction); upgrader = ostree_sysroot_upgrader_new_for_os (sysroot, self->osname, cancellable, error); if (upgrader == NULL) goto out; if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) goto out; origin = ostree_sysroot_upgrader_dup_origin (upgrader); /* Determine if we're upgrading before we set the refspec. */ upgrading = (self->refspec == NULL && self->revision == NULL); if (self->refspec != NULL) { if (!change_upgrader_refspec (sysroot, upgrader, self->refspec, cancellable, NULL, NULL, error)) goto out; } else if (origin != NULL) { self->refspec = g_key_file_get_string (origin, "origin", "refspec", NULL); if (self->refspec == NULL) { g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Could not find refspec for booted deployment"); } } else { g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Booted deployment has no origin"); goto out; } progress = ostree_async_progress_new (); rpmostreed_transaction_connect_download_progress (transaction, progress); rpmostreed_transaction_connect_signature_progress (transaction, repo); if (self->revision != NULL) { g_autofree char *checksum = NULL; g_autofree char *version = NULL; upgrader_flags |= OSTREE_SYSROOT_UPGRADER_PULL_FLAGS_ALLOW_OLDER; if (!rpmostreed_parse_revision (self->revision, &checksum, &version, error)) goto out; if (version != NULL) { rpmostreed_transaction_emit_message_printf (transaction, "Resolving version '%s'", version); if (!rpmostreed_repo_lookup_version (repo, self->refspec, version, progress, cancellable, &checksum, error)) goto out; } g_key_file_set_string (origin, "origin", "override-commit", checksum); if (!ostree_sysroot_upgrader_set_origin (upgrader, origin, cancellable, error)) goto out; } else if (upgrading) { if (g_key_file_remove_key (origin, "origin", "override-commit", NULL)) { if (!ostree_sysroot_upgrader_set_origin (upgrader, origin, cancellable, error)) goto out; } } origin_description = ostree_sysroot_upgrader_get_origin_description (upgrader); if (origin_description != NULL) rpmostreed_transaction_emit_message_printf (transaction, "Updating from: %s", origin_description); if (!ostree_sysroot_upgrader_pull_one_dir (upgrader, "/usr/share/rpm", 0, upgrader_flags, progress, &changed, cancellable, error)) goto out; rpmostree_transaction_emit_progress_end (RPMOSTREE_TRANSACTION (transaction)); if (!changed) { if (upgrading) rpmostreed_transaction_emit_message_printf (transaction, "No upgrade available."); else rpmostreed_transaction_emit_message_printf (transaction, "No change."); } ret = TRUE; out: return ret; }