Skip to content

Commit c91b660

Browse files
committed
deployment: enable adding custom metadata
1 parent 223a1af commit c91b660

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

Makefile-ostree.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ ostree_SOURCES += \
7676
src/ostree/ot-admin-builtin-set-default.c \
7777
src/ostree/ot-admin-builtin-instutil.c \
7878
src/ostree/ot-admin-builtin-kargs.c \
79+
src/ostree/ot-admin-builtin-metadata.c \
7980
src/ostree/ot-admin-builtin-cleanup.c \
8081
src/ostree/ot-admin-builtin-os-init.c \
8182
src/ostree/ot-admin-builtin-set-origin.c \

src/libostree/ostree-deployment.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "config.h"
1919

2020
#include "ostree-deployment-private.h"
21+
#include "ostree-sysroot-private.h"
2122
#include "ostree.h"
2223
#include "otutil.h"
2324

@@ -476,3 +477,60 @@ ostree_deployment_is_finalization_locked (OstreeDeployment *self)
476477
{
477478
return self->finalization_locked;
478479
}
480+
481+
/**
482+
* ostree_deployment_set_ext_metadata:
483+
* @self: Deployment
484+
* @metadata_key: extended attribute(metadata) name/key to add.
485+
* @metadata_value: extended attribute(metadata) value to add.
486+
* @error: a #GError
487+
*
488+
*/
489+
gboolean
490+
ostree_deployment_set_ext_metadata (OstreeDeployment *self, const char *metadata_key, const char *metadata_value, GError **error)
491+
{
492+
g_autofree char *backing_relpath = _ostree_sysroot_get_deployment_backing_relpath (self);
493+
if (setxattr (backing_relpath, metadata_key, metadata_value, strlen(metadata_value), 0) < 0)
494+
{
495+
g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
496+
"Failed to set deployment metadata %s on %s: %s", metadata_key, backing_relpath,
497+
g_strerror (errno));
498+
return FALSE;
499+
}
500+
return TRUE;
501+
}
502+
503+
/**
504+
* ostree_deployment_get_ext_metadata:
505+
* @self: Deployment
506+
* @metadata_key: (nullable): key of extended attribute
507+
* @error: a #GError
508+
* Returns: The value of a extended attribute(metadata) checksum given the key.
509+
*/
510+
const char *
511+
ostree_deployment_get_ext_metadata (OstreeDeployment *self, const char *metadata_key, GError **error)
512+
{
513+
g_autofree char *backing_relpath = _ostree_sysroot_get_deployment_backing_relpath (self);
514+
g_autofree char *metadata_value = NULL;
515+
g_autofree int len = getxattr (backing_relpath, metadata_key, NULL, 0);
516+
if (len < 0)
517+
{
518+
if (errno == ENODATA)
519+
return NULL;
520+
g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
521+
"Failed to get deployment metadata %s on %s: %s", metadata_key, backing_relpath,
522+
g_strerror (errno));
523+
return NULL;
524+
}
525+
metadata_value = g_malloc (len + 1);
526+
len = getxattr (backing_relpath, metadata_key, metadata_value, len);
527+
if (len < 0)
528+
{
529+
g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
530+
"Failed to get deployment metadata %s on %s: %s", metadata_key, backing_relpath,
531+
g_strerror (errno));
532+
return NULL;
533+
}
534+
metadata_value[len] = '\0';
535+
return metadata_value;
536+
}

src/libostree/ostree-deployment.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,13 @@ const char *ostree_deployment_unlocked_state_to_string (OstreeDeploymentUnlocked
107107
_OSTREE_PUBLIC
108108
OstreeDeploymentUnlockedState ostree_deployment_get_unlocked (OstreeDeployment *self);
109109

110+
111+
_OSTREE_PUBLIC
112+
gboolean ostree_deployment_set_ext_metadata (OstreeDeployment *self, const char *metadata_key,
113+
const char *metadata_value, GError **error);
114+
115+
_OSTREE_PUBLIC
116+
const char *ostree_deployment_get_ext_metadata (OstreeDeployment *self, const char *metadata_key,
117+
GError **error);
118+
110119
G_END_DECLS
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (C) 2015 Colin Walters <[email protected]>
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, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "config.h"
21+
22+
#include "ot-admin-builtins.h"
23+
#include "ot-admin-functions.h"
24+
#include "otutil.h"
25+
26+
static char **opt_set;
27+
static char **opt_get;
28+
29+
static GOptionEntry options[]
30+
= { { "set", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_set,
31+
"Set deployment metadata, like DATE=030424; this overrides any metadata with the "
32+
"same name",
33+
"KEY=VALUE" },
34+
{ "get", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_get,
35+
"Get the value of a deployment metadata.", "KEY" },
36+
{ NULL } };
37+
38+
gboolean
39+
ot_admin_builtin_metadata (int argc, char **argv, OstreeCommandInvocation *invocation,
40+
GCancellable *cancellable, GError **error)
41+
{
42+
gboolean ret = FALSE;
43+
g_autoptr (GPtrArray) deployments = NULL;
44+
OstreeDeployment *first_deployment = NULL;
45+
g_autoptr (GOptionContext) context = NULL;
46+
g_autoptr (OstreeSysroot) sysroot = NULL;
47+
48+
if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
49+
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER, invocation, &sysroot,
50+
cancellable, error))
51+
goto out;
52+
53+
if (deployments->len == 0)
54+
{
55+
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Unable to find a deployment in sysroot");
56+
goto out;
57+
}
58+
59+
first_deployment = deployments->pdata[0];
60+
61+
if (opt_set)
62+
{
63+
char *key = strtok(*opt_set, "=");
64+
char *value = strtok(NULL, "=");
65+
// ^^ This needs error checking and probably is wrong... but builds!
66+
ostree_deployment_set_ext_metadata(first_deployment, key, value, error);
67+
}
68+
69+
if (opt_get)
70+
{
71+
ostree_deployment_get_ext_metadata (first_deployment, *opt_get, error);
72+
}
73+
74+
ret = TRUE;
75+
out:
76+
return ret;
77+
}

src/ostree/ot-admin-builtins.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ BUILTINPROTO (kargs);
5151
BUILTINPROTO (post_copy);
5252
BUILTINPROTO (lock_finalization);
5353
BUILTINPROTO (state_overlay);
54+
BUILTINPROTO (metadata);
5455

5556
#undef BUILTINPROTO
5657

src/ostree/ot-builtin-admin.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ static OstreeCommand admin_subcommands[] = {
7070
{ "upgrade", OSTREE_BUILTIN_FLAG_NO_REPO, ot_admin_builtin_upgrade,
7171
"Construct new tree from current origin and deploy it, if it changed" },
7272
{ "kargs", OSTREE_BUILTIN_FLAG_NO_REPO, ot_admin_builtin_kargs, "Change kernel arguments" },
73+
{ "deployment-metadata", OSTREE_BUILTIN_FLAG_NO_REPO, ot_admin_builtin_metadata,
74+
"Set extended metadata for current the deployment" },
7375
{ NULL, 0, NULL, NULL }
7476
};
7577

0 commit comments

Comments
 (0)