|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 Red Hat, Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LGPL-2.0+ |
| 5 | + * |
| 6 | + * This library is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 2 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with this library; if not, write to the |
| 18 | + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | + * Boston, MA 02111-1307, USA. |
| 20 | + * |
| 21 | + * Author: Javier Martinez Canillas <javierm@redhat.com> |
| 22 | + */ |
| 23 | + |
| 24 | +#include "config.h" |
| 25 | + |
| 26 | +#include "ostree-sysroot-private.h" |
| 27 | +#include "ot-main.h" |
| 28 | +#include "ot-admin-builtins.h" |
| 29 | +#include "ot-admin-functions.h" |
| 30 | +#include "otutil.h" |
| 31 | + |
| 32 | +static GOptionEntry options[] = { |
| 33 | + { NULL } |
| 34 | +}; |
| 35 | + |
| 36 | +gboolean |
| 37 | +ot_admin_builtin_esp_upgrade (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error) |
| 38 | +{ |
| 39 | + g_autoptr(GOptionContext) context = g_option_context_new (""); |
| 40 | + |
| 41 | + g_autoptr(OstreeSysroot) sysroot = NULL; |
| 42 | + if (!ostree_admin_option_context_parse (context, options, &argc, &argv, |
| 43 | + OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED, |
| 44 | + invocation, &sysroot, cancellable, error)) |
| 45 | + return FALSE; |
| 46 | + |
| 47 | + g_autoptr(OstreeRepo) repo = NULL; |
| 48 | + if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error)) |
| 49 | + return FALSE; |
| 50 | + |
| 51 | + g_autoptr(GPtrArray) deployments = ostree_sysroot_get_deployments (sysroot); |
| 52 | + |
| 53 | + if (deployments->len == 0) |
| 54 | + { |
| 55 | + g_print ("No deployments.\n"); |
| 56 | + return TRUE; |
| 57 | + } |
| 58 | + |
| 59 | + OstreeDeployment *deployment = ostree_sysroot_get_booted_deployment (sysroot); |
| 60 | + |
| 61 | + if (!deployment) |
| 62 | + deployment = ot_admin_get_indexed_deployment (sysroot, 0, error); |
| 63 | + |
| 64 | + struct stat stbuf; |
| 65 | + |
| 66 | + if (!glnx_fstatat_allow_noent (sysroot->sysroot_fd, "sys/firmware/efi", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 67 | + return FALSE; |
| 68 | + |
| 69 | + if (errno == ENOENT) |
| 70 | + { |
| 71 | + g_print ("Not an EFI system.\n"); |
| 72 | + return TRUE; |
| 73 | + } |
| 74 | + |
| 75 | + if (!ot_is_rw_mount ("/boot/efi")) |
| 76 | + { |
| 77 | + if (ot_is_ro_mount ("/boot/efi")) |
| 78 | + g_print ("The ESP can't be updated because /boot/efi is a read-only mountpoint.\n"); |
| 79 | + else |
| 80 | + g_print ("Only ESP mounted in /boot/efi is supported.\n"); |
| 81 | + return TRUE; |
| 82 | + } |
| 83 | + |
| 84 | + g_autofree char *deployment_path = ostree_sysroot_get_deployment_dirpath (sysroot, deployment); |
| 85 | + |
| 86 | + g_autofree char *new_esp_path = g_strdup_printf ("%s/usr/lib/ostree-boot", deployment_path); |
| 87 | + |
| 88 | + GLNX_AUTO_PREFIX_ERROR ("During copy files to the ESP", error); |
| 89 | + glnx_autofd int old_esp_fd = -1; |
| 90 | + if (!glnx_opendirat (sysroot->sysroot_fd, "boot", TRUE, &old_esp_fd, error)) |
| 91 | + return FALSE; |
| 92 | + |
| 93 | + glnx_autofd int new_esp_fd = -1; |
| 94 | + if (!glnx_opendirat (sysroot->sysroot_fd, new_esp_path, TRUE, &new_esp_fd, error)) |
| 95 | + return FALSE; |
| 96 | + |
| 97 | + /* The ESP filesystem is vfat so don't attempt to copy ownership, mode, and xattrs */ |
| 98 | + const OstreeSysrootDebugFlags flags = sysroot->debug_flags | OSTREE_SYSROOT_DEBUG_NO_XATTRS; |
| 99 | + |
| 100 | + if (!ot_copy_dir_recurse (new_esp_fd, old_esp_fd, "efi", flags , cancellable, error)) |
| 101 | + return FALSE; |
| 102 | + |
| 103 | + return TRUE; |
| 104 | +} |
0 commit comments