forked from coreos/rpm-ostree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpmostree-core.c
4717 lines (4044 loc) · 171 KB
/
rpmostree-core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2014 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 <glib-unix.h>
#include <rpm/rpmsq.h>
#include <rpm/rpmlib.h>
#include <rpm/rpmlog.h>
#include <rpm/rpmfi.h>
#include <rpm/rpmmacro.h>
#include <rpm/rpmts.h>
#include <gio/gunixoutputstream.h>
#include <systemd/sd-journal.h>
#include <libdnf/libdnf.h>
#include <librepo/librepo.h>
#include "rpmostree-core-private.h"
#include "rpmostree-rojig-core.h"
#include "rpmostree-postprocess.h"
#include "rpmostree-rpm-util.h"
#include "rpmostree-passwd-util.h"
#include "rpmostree-scripts.h"
#include "rpmostree-kernel.h"
#include "rpmostree-importer.h"
#include "rpmostree-output.h"
#define RPMOSTREE_MESSAGE_COMMIT_STATS SD_ID128_MAKE(e6,37,2e,38,41,21,42,a9,bc,13,b6,32,b3,f8,93,44)
#define RPMOSTREE_MESSAGE_SELINUX_RELABEL SD_ID128_MAKE(5a,e0,56,34,f2,d7,49,3b,b1,58,79,b7,0c,02,e6,5d)
#define RPMOSTREE_MESSAGE_PKG_REPOS SD_ID128_MAKE(0e,ea,67,9b,bf,a3,4d,43,80,2d,ec,99,b2,74,eb,e7)
#define RPMOSTREE_MESSAGE_PKG_IMPORT SD_ID128_MAKE(df,8b,b5,4f,04,fa,47,08,ac,16,11,1b,bf,4b,a3,52)
static OstreeRepo * get_pkgcache_repo (RpmOstreeContext *self);
/* Given a string, look for ostree:// or rojig:// prefix and
* return its type and the remainder of the string. Note
* that ostree:// can be either a refspec (TYPE_OSTREE) or
* a bare commit (TYPE_COMMIT).
*/
gboolean
rpmostree_refspec_classify (const char *refspec,
RpmOstreeRefspecType *out_type,
const char **out_remainder,
GError **error)
{
if (g_str_has_prefix (refspec, RPMOSTREE_REFSPEC_ROJIG_PREFIX))
{
*out_type = RPMOSTREE_REFSPEC_TYPE_ROJIG;
if (out_remainder)
*out_remainder = refspec + strlen (RPMOSTREE_REFSPEC_ROJIG_PREFIX);
return TRUE;
}
/* Add any other prefixes here */
/* For compatibility, fall back to ostree:// when we have no prefix. */
const char *remainder;
if (g_str_has_prefix (refspec, RPMOSTREE_REFSPEC_OSTREE_PREFIX))
remainder = refspec + strlen (RPMOSTREE_REFSPEC_OSTREE_PREFIX);
else
remainder = refspec;
if (ostree_validate_checksum_string (remainder, NULL))
*out_type = RPMOSTREE_REFSPEC_TYPE_CHECKSUM;
else
*out_type = RPMOSTREE_REFSPEC_TYPE_OSTREE;
if (out_remainder)
*out_remainder = remainder;
return TRUE;
}
char*
rpmostree_refspec_to_string (RpmOstreeRefspecType reftype,
const char *data)
{
const char *prefix = NULL;
switch (reftype)
{
case RPMOSTREE_REFSPEC_TYPE_OSTREE:
case RPMOSTREE_REFSPEC_TYPE_CHECKSUM:
prefix = RPMOSTREE_REFSPEC_OSTREE_PREFIX;
break;
case RPMOSTREE_REFSPEC_TYPE_ROJIG:
prefix = RPMOSTREE_REFSPEC_ROJIG_PREFIX;
break;
}
g_assert (prefix);
return g_strconcat (prefix, data, NULL);
}
/* Primarily this makes sure that ostree refspecs start with `ostree://`.
*/
char*
rpmostree_refspec_canonicalize (const char *orig_refspec,
GError **error)
{
RpmOstreeRefspecType refspectype;
const char *refspec_data;
if (!rpmostree_refspec_classify (orig_refspec, &refspectype, &refspec_data, error))
return NULL;
return rpmostree_refspec_to_string (refspectype, refspec_data);
}
static int
compare_pkgs (gconstpointer ap,
gconstpointer bp)
{
DnfPackage **a = (gpointer)ap;
DnfPackage **b = (gpointer)bp;
return dnf_package_cmp (*a, *b);
}
/***********************************************************
* RpmOstreeTreespec *
***********************************************************
*/
struct _RpmOstreeTreespec {
GObject parent;
GVariant *spec;
GVariantDict *dict;
};
G_DEFINE_TYPE (RpmOstreeTreespec, rpmostree_treespec, G_TYPE_OBJECT)
static void
rpmostree_treespec_finalize (GObject *object)
{
RpmOstreeTreespec *self = RPMOSTREE_TREESPEC (object);
g_clear_pointer (&self->spec, g_variant_unref);
g_clear_pointer (&self->dict, g_variant_dict_unref);
G_OBJECT_CLASS (rpmostree_treespec_parent_class)->finalize (object);
}
static void
rpmostree_treespec_class_init (RpmOstreeTreespecClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = rpmostree_treespec_finalize;
}
static void
rpmostree_treespec_init (RpmOstreeTreespec *self)
{
}
static int
qsort_cmpstr (const void*ap, const void *bp, gpointer data)
{
const char *a = *((const char *const*) ap);
const char *b = *((const char *const*) bp);
return strcmp (a, b);
}
/* Handle converting "treespec" bits to GVariant.
* Look for @key in @keyfile (under the section "tree"), and
* if found, strip leading/trailing whitespace from each entry,
* sort them, and add it to @builder under the same key name.
* If there are no entries for it, and @notfound_key is provided,
* set @notfound_key in the variant to TRUE.
*/
static void
add_canonicalized_string_array (GVariantBuilder *builder,
const char *key,
const char *notfound_key,
GKeyFile *keyfile)
{
g_autoptr(GHashTable) set = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
g_auto(GStrv) input = g_key_file_get_string_list (keyfile, "tree", key, NULL, NULL);
if (!(input && *input))
{
if (notfound_key)
{
g_variant_builder_add (builder, "{sv}", notfound_key, g_variant_new_boolean (TRUE));
return;
}
}
/* Iterate over strings, strip leading/trailing whitespace */
for (char ** iter = input; iter && *iter; iter++)
{
g_autofree char *stripped = g_strdup (*iter);
g_strchug (g_strchomp (stripped));
g_hash_table_add (set, g_steal_pointer (&stripped));
}
/* Ensure sorted */
guint count;
g_autofree char **sorted = (char**)g_hash_table_get_keys_as_array (set, &count);
if (count > 1)
g_qsort_with_data (sorted, count, sizeof (void*), qsort_cmpstr, NULL);
g_variant_builder_add (builder, "{sv}", key,
g_variant_new_strv ((const char*const*)sorted, count));
}
/* Get a bool from @keyfile, adding it to @builder */
static void
tf_bind_boolean (GKeyFile *keyfile,
GVariantBuilder *builder,
const char *name,
gboolean default_value)
{
gboolean v = default_value;
if (g_key_file_has_key (keyfile, "tree", name, NULL))
v = g_key_file_get_boolean (keyfile, "tree", name, NULL);
g_variant_builder_add (builder, "{sv}", name, g_variant_new_boolean (v));
}
RpmOstreeTreespec *
rpmostree_treespec_new_from_keyfile (GKeyFile *keyfile,
GError **error)
{
g_autoptr(RpmOstreeTreespec) ret = g_object_new (RPMOSTREE_TYPE_TREESPEC, NULL);
g_auto(GVariantBuilder) builder;
g_variant_builder_init (&builder, (GVariantType*)"a{sv}");
/* We allow the "ref" key to be missing for cases where we don't need one.
* This is abusing the Treespec a bit, but oh well... */
{ g_autofree char *ref = g_key_file_get_string (keyfile, "tree", "ref", NULL);
if (ref)
g_variant_builder_add (&builder, "{sv}", "ref", g_variant_new_string (ref));
}
#define BIND_STRING(k) \
{ g_autofree char *v = g_key_file_get_string (keyfile, "tree", k, NULL); \
if (v) \
g_variant_builder_add (&builder, "{sv}", k, g_variant_new_string (v)); \
}
BIND_STRING("rojig");
BIND_STRING("rojig-version");
BIND_STRING("releasever");
#undef BIND_STRING
add_canonicalized_string_array (&builder, "packages", NULL, keyfile);
add_canonicalized_string_array (&builder, "cached-packages", NULL, keyfile);
add_canonicalized_string_array (&builder, "removed-base-packages", NULL, keyfile);
add_canonicalized_string_array (&builder, "cached-replaced-base-packages", NULL, keyfile);
/* We allow the "repo" key to be missing. This means that we rely on hif's
* normal behaviour (i.e. look at repos in repodir with enabled=1). */
{ g_auto(GStrv) val = g_key_file_get_string_list (keyfile, "tree", "repos", NULL, NULL);
if (val && *val)
add_canonicalized_string_array (&builder, "repos", NULL, keyfile);
}
add_canonicalized_string_array (&builder, "instlangs", "instlangs-all", keyfile);
if (g_key_file_get_boolean (keyfile, "tree", "skip-sanity-check", NULL))
g_variant_builder_add (&builder, "{sv}", "skip-sanity-check", g_variant_new_boolean (TRUE));
tf_bind_boolean (keyfile, &builder, "documentation", TRUE);
tf_bind_boolean (keyfile, &builder, "recommends", TRUE);
tf_bind_boolean (keyfile, &builder, "selinux", TRUE);
ret->spec = g_variant_builder_end (&builder);
ret->dict = g_variant_dict_new (ret->spec);
return g_steal_pointer (&ret);
}
RpmOstreeTreespec *
rpmostree_treespec_new_from_path (const char *path, GError **error)
{
g_autoptr(GKeyFile) specdata = g_key_file_new();
if (!g_key_file_load_from_file (specdata, path, 0, error))
return NULL;
return rpmostree_treespec_new_from_keyfile (specdata, error);
}
RpmOstreeTreespec *
rpmostree_treespec_new (GVariant *variant)
{
g_autoptr(RpmOstreeTreespec) ret = g_object_new (RPMOSTREE_TYPE_TREESPEC, NULL);
ret->spec = g_variant_ref (variant);
ret->dict = g_variant_dict_new (ret->spec);
return g_steal_pointer (&ret);
}
const char *
rpmostree_treespec_get_ref (RpmOstreeTreespec *spec)
{
const char *r = NULL;
g_variant_dict_lookup (spec->dict, "ref", "&s", &r);
return r;
}
GVariant *
rpmostree_treespec_to_variant (RpmOstreeTreespec *spec)
{
return g_variant_ref (spec->spec);
}
/***********************************************************
* RpmOstreeContext *
***********************************************************
*/
G_DEFINE_TYPE (RpmOstreeContext, rpmostree_context, G_TYPE_OBJECT)
static void
rpmostree_context_finalize (GObject *object)
{
RpmOstreeContext *rctx = RPMOSTREE_CONTEXT (object);
g_clear_object (&rctx->spec);
g_clear_object (&rctx->dnfctx);
g_clear_object (&rctx->rojig_pkg);
g_free (rctx->rojig_checksum);
g_free (rctx->rojig_inputhash);
g_clear_object (&rctx->pkgcache_repo);
g_clear_object (&rctx->ostreerepo);
g_clear_pointer (&rctx->devino_cache, (GDestroyNotify)ostree_repo_devino_cache_unref);
g_clear_object (&rctx->sepolicy);
g_clear_pointer (&rctx->passwd_dir, g_free);
g_clear_pointer (&rctx->pkgs, g_ptr_array_unref);
g_clear_pointer (&rctx->pkgs_to_download, g_ptr_array_unref);
g_clear_pointer (&rctx->pkgs_to_import, g_ptr_array_unref);
g_clear_pointer (&rctx->pkgs_to_relabel, g_ptr_array_unref);
g_clear_pointer (&rctx->pkgs_to_remove, g_hash_table_unref);
g_clear_pointer (&rctx->pkgs_to_replace, g_hash_table_unref);
g_clear_pointer (&rctx->vlockmap, g_hash_table_unref);
(void)glnx_tmpdir_delete (&rctx->tmpdir, NULL, NULL);
(void)glnx_tmpdir_delete (&rctx->repo_tmpdir, NULL, NULL);
g_clear_pointer (&rctx->rootfs_usrlinks, g_hash_table_unref);
G_OBJECT_CLASS (rpmostree_context_parent_class)->finalize (object);
}
static void
rpmostree_context_class_init (RpmOstreeContextClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = rpmostree_context_finalize;
}
static void
rpmostree_context_init (RpmOstreeContext *self)
{
self->tmprootfs_dfd = -1;
self->dnf_cache_policy = RPMOSTREE_CONTEXT_DNF_CACHE_DEFAULT;
self->enable_rofiles = TRUE;
}
static void
set_rpm_macro_define (const char *key, const char *value)
{
g_autofree char *buf = g_strconcat ("%define ", key, " ", value, NULL);
/* Calling expand with %define (ignoring the return
* value) is apparently the way to change the global
* macro context.
*/
free (rpmExpand (buf, NULL));
}
RpmOstreeContext *
rpmostree_context_new_system (OstreeRepo *repo,
GCancellable *cancellable,
GError **error)
{
g_return_val_if_fail (repo != NULL, FALSE);
RpmOstreeContext *self = g_object_new (RPMOSTREE_TYPE_CONTEXT, NULL);
self->is_system = TRUE;
self->ostreerepo = g_object_ref (repo);
/* We can always be control-c'd at any time; this is new API,
* otherwise we keep calling _rpmostree_reset_rpm_sighandlers() in
* various places.
*/
rpmsqSetInterruptSafety (FALSE);
self->dnfctx = dnf_context_new ();
DECLARE_RPMSIGHANDLER_RESET;
dnf_context_set_http_proxy (self->dnfctx, g_getenv ("http_proxy"));
dnf_context_set_repo_dir (self->dnfctx, "/etc/yum.repos.d");
dnf_context_set_cache_dir (self->dnfctx, RPMOSTREE_CORE_CACHEDIR RPMOSTREE_DIR_CACHE_REPOMD);
dnf_context_set_solv_dir (self->dnfctx, RPMOSTREE_CORE_CACHEDIR RPMOSTREE_DIR_CACHE_SOLV);
dnf_context_set_lock_dir (self->dnfctx, "/run/rpm-ostree/" RPMOSTREE_DIR_LOCK);
dnf_context_set_user_agent (self->dnfctx, PACKAGE_NAME "/" PACKAGE_VERSION);
/* don't need SWDB: https://github.com/rpm-software-management/libdnf/issues/645 */
dnf_context_set_write_history (self->dnfctx, FALSE);
dnf_context_set_check_disk_space (self->dnfctx, FALSE);
dnf_context_set_check_transaction (self->dnfctx, FALSE);
/* Hack until libdnf+librepo know how to better negotaiate zchunk.
* see also the bits in configure.ac that define HAVE_ZCHUNK
**/
#ifndef HAVE_ZCHUNK
dnf_context_set_zchunk (self->dnfctx, FALSE);
#endif
return self;
}
/* Create a context that assembles a new filesystem tree, possibly without root
* privileges. Some behavioral distinctions are made by looking at the undelying
* OstreeRepoMode. In particular, an OSTREE_REPO_MODE_BARE_USER_ONLY repo
* is assumed to be for unprivileged containers/buildroots.
*/
RpmOstreeContext *
rpmostree_context_new_tree (int userroot_dfd,
OstreeRepo *repo,
GCancellable *cancellable,
GError **error)
{
g_autoptr(RpmOstreeContext) ret = rpmostree_context_new_system (repo, cancellable, error);
if (!ret)
return NULL;
{ g_autofree char *reposdir = glnx_fdrel_abspath (userroot_dfd, "rpmmd.repos.d");
dnf_context_set_repo_dir (ret->dnfctx, reposdir);
}
{ const char *cache_rpmmd = glnx_strjoina ("cache/", RPMOSTREE_DIR_CACHE_REPOMD);
g_autofree char *cachedir = glnx_fdrel_abspath (userroot_dfd, cache_rpmmd);
dnf_context_set_cache_dir (ret->dnfctx, cachedir);
}
{ const char *cache_solv = glnx_strjoina ("cache/", RPMOSTREE_DIR_CACHE_SOLV);
g_autofree char *cachedir = glnx_fdrel_abspath (userroot_dfd, cache_solv);
dnf_context_set_solv_dir (ret->dnfctx, cachedir);
}
{ const char *lock = glnx_strjoina ("cache/", RPMOSTREE_DIR_LOCK);
g_autofree char *lockdir = glnx_fdrel_abspath (userroot_dfd, lock);
dnf_context_set_lock_dir (ret->dnfctx, lockdir);
}
return g_steal_pointer (&ret);
}
static gboolean
rpmostree_context_ensure_tmpdir (RpmOstreeContext *self,
const char *subdir,
GError **error)
{
if (!self->tmpdir.initialized)
{
if (!glnx_mkdtemp ("rpmostree-core-XXXXXX", 0700, &self->tmpdir, error))
return FALSE;
}
g_assert (self->tmpdir.initialized);
if (!glnx_shutil_mkdir_p_at (self->tmpdir.fd, subdir, 0755, NULL, error))
return FALSE;
return TRUE;
}
void
rpmostree_context_set_pkgcache_only (RpmOstreeContext *self,
gboolean pkgcache_only)
{
/* if called, must always be before setup() */
g_assert (!self->spec);
self->pkgcache_only = pkgcache_only;
}
void
rpmostree_context_set_dnf_caching (RpmOstreeContext *self,
RpmOstreeContextDnfCachePolicy policy)
{
self->dnf_cache_policy = policy;
}
/* Pick up repos dir and passwd from @cfg_deployment. */
void
rpmostree_context_configure_from_deployment (RpmOstreeContext *self,
OstreeSysroot *sysroot,
OstreeDeployment *cfg_deployment)
{
g_autofree char *cfg_deployment_root =
rpmostree_get_deployment_root (sysroot, cfg_deployment);
g_autofree char *reposdir =
g_build_filename (cfg_deployment_root, "etc/yum.repos.d", NULL);
/* point libhif to the yum.repos.d and os-release of the merge deployment */
dnf_context_set_repo_dir (self->dnfctx, reposdir);
/* point the core to the passwd & group of the merge deployment */
g_assert (!self->passwd_dir);
self->passwd_dir = g_build_filename (cfg_deployment_root, "etc", NULL);
}
/* By default, we use a "treespec" however, reflecting everything from
* treefile -> treespec is annoying. Long term we want to unify those. This
* is a temporary escape hatch.
*/
void
rpmostree_context_set_treefile (RpmOstreeContext *self, RORTreefile *treefile_rs)
{
self->treefile_rs = treefile_rs;
}
/* Use this if no packages will be installed, and we just want a "dummy" run.
*/
void
rpmostree_context_set_is_empty (RpmOstreeContext *self)
{
self->empty = TRUE;
}
/* XXX: or put this in new_system() instead? */
void
rpmostree_context_set_repos (RpmOstreeContext *self,
OstreeRepo *base_repo,
OstreeRepo *pkgcache_repo)
{
g_set_object (&self->ostreerepo, base_repo);
g_set_object (&self->pkgcache_repo, pkgcache_repo);
}
static OstreeRepo *
get_pkgcache_repo (RpmOstreeContext *self)
{
return self->pkgcache_repo ?: self->ostreerepo;
}
/* I debated making this part of the treespec. Overall, I think it makes more
* sense to define it outside since the policy to use depends on the context in
* which the RpmOstreeContext is used, not something we can always guess on our
* own correctly. */
void
rpmostree_context_set_sepolicy (RpmOstreeContext *self,
OstreeSePolicy *sepolicy)
{
g_set_object (&self->sepolicy, sepolicy);
}
void
rpmostree_context_set_devino_cache (RpmOstreeContext *self,
OstreeRepoDevInoCache *devino_cache)
{
g_assert (self->enable_rofiles);
if (self->devino_cache)
ostree_repo_devino_cache_unref (self->devino_cache);
self->devino_cache = devino_cache ? ostree_repo_devino_cache_ref (devino_cache) : NULL;
}
void
rpmostree_context_disable_rofiles (RpmOstreeContext *self)
{
g_assert (!self->devino_cache);
self->enable_rofiles = FALSE;
}
DnfContext *
rpmostree_context_get_dnf (RpmOstreeContext *self)
{
return self->dnfctx;
}
/* Add rpmmd repo information, since it's very useful for determining
* state. See also:
*
* - https://github.com/projectatomic/rpm-ostree/issues/774
* - The repo_metadata_for_package() function in rpmostree-unpacker.c
* This returns a value of key of type aa{sv} - the key should be
* `rpmostree.rpmmd-repos`.
*/
GVariant *
rpmostree_context_get_rpmmd_repo_commit_metadata (RpmOstreeContext *self)
{
g_auto(GVariantBuilder) repo_list_builder;
g_variant_builder_init (&repo_list_builder, (GVariantType*)"aa{sv}");
g_autoptr(GPtrArray) repos =
rpmostree_get_enabled_rpmmd_repos (self->dnfctx, DNF_REPO_ENABLED_PACKAGES);
for (guint i = 0; i < repos->len; i++)
{
g_auto(GVariantBuilder) repo_builder;
g_variant_builder_init (&repo_builder, (GVariantType*)"a{sv}");
DnfRepo *repo = repos->pdata[i];
const char *id = dnf_repo_get_id (repo);
g_variant_builder_add (&repo_builder, "{sv}", "id", g_variant_new_string (id));
guint64 ts = dnf_repo_get_timestamp_generated (repo);
g_variant_builder_add (&repo_builder, "{sv}", "timestamp", g_variant_new_uint64 (ts));
g_variant_builder_add (&repo_list_builder, "@a{sv}", g_variant_builder_end (&repo_builder));
}
return g_variant_ref_sink (g_variant_builder_end (&repo_list_builder));
}
GHashTable *
rpmostree_dnfcontext_get_varsubsts (DnfContext *context)
{
GHashTable *r = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert (r, g_strdup ("basearch"), g_strdup (dnf_context_get_base_arch (context)));
return r;
}
/* Look for a repo named @reponame, and ensure it is enabled for package
* downloads.
*/
static gboolean
enable_one_repo (GPtrArray *sources,
const char *reponame,
GError **error)
{
for (guint i = 0; i < sources->len; i++)
{
DnfRepo *src = sources->pdata[i];
const char *id = dnf_repo_get_id (src);
if (strcmp (reponame, id) != 0)
continue;
dnf_repo_set_enabled (src, DNF_REPO_ENABLED_PACKAGES);
return TRUE;
}
return glnx_throw (error, "Unknown rpm-md repository: %s", reponame);
}
/* Enable all repos in @enabled_repos, and disable everything else */
static gboolean
context_repos_enable_only (RpmOstreeContext *context,
const char *const *enabled_repos,
GError **error)
{
GPtrArray *sources = dnf_context_get_repos (context->dnfctx);
for (guint i = 0; i < sources->len; i++)
{
DnfRepo *src = sources->pdata[i];
dnf_repo_set_enabled (src, DNF_REPO_ENABLED_NONE);
}
for (const char *const *iter = enabled_repos; iter && *iter; iter++)
{
if (!enable_one_repo (sources, *iter, error))
return FALSE;
}
return TRUE;
}
/* Wraps `dnf_context_setup()`, and initializes state based on the treespec
* @spec. Another way to say it is we pair `DnfContext` with an
* `RpmOstreeTreespec`. For example, we handle "instlangs", set the rpmdb root
* to `/usr/share/rpm`, and handle the required rpm-md repos. The "packages"
* entry from @spec is processed later.
*
* If either @install_root or @source_root are `NULL`, `/usr/share/empty` is
* used. Typically @source_root being `NULL` is for "from scratch" root
* filesystems.
*/
gboolean
rpmostree_context_setup (RpmOstreeContext *self,
const char *install_root,
const char *source_root,
RpmOstreeTreespec *spec,
GCancellable *cancellable,
GError **error)
{
const char *releasever = NULL;
g_autofree char **enabled_repos = NULL;
g_autofree char **instlangs = NULL;
/* This exists (as a canonically empty dir) at least on RHEL7+ */
static const char emptydir_path[] = "/usr/share/empty";
/* allow NULL for treespec, but canonicalize to an empty keyfile for cleaner queries */
if (!spec)
{
g_autoptr(GKeyFile) kf = g_key_file_new ();
self->spec = rpmostree_treespec_new_from_keyfile (kf, NULL);
}
else
self->spec = g_object_ref (spec);
g_variant_dict_lookup (self->spec->dict, "releasever", "&s", &releasever);
if (!install_root)
install_root = emptydir_path;
if (!source_root)
{
source_root = emptydir_path;
/* Hackaround libdnf's auto-introspection semantics. For e.g.
* treecompose/container, we currently require using .repo files which
* don't reference $releasever.
*/
dnf_context_set_release_ver (self->dnfctx, releasever ?: "rpmostree-unset-releasever");
}
else if (releasever)
dnf_context_set_release_ver (self->dnfctx, releasever);
dnf_context_set_install_root (self->dnfctx, install_root);
dnf_context_set_source_root (self->dnfctx, source_root);
/* Set the RPM _install_langs macro, which gets processed by librpm; this is
* currently only referenced in the traditional or non-"unified core" code.
*/
if (g_variant_dict_lookup (self->spec->dict, "instlangs", "^a&s", &instlangs))
{
g_autoptr(GString) opt = g_string_new ("");
gboolean first = TRUE;
for (char **iter = instlangs; iter && *iter; iter++)
{
const char *v = *iter;
if (!first)
g_string_append_c (opt, ':');
else
first = FALSE;
g_string_append (opt, v);
}
dnf_context_set_rpm_macro (self->dnfctx, "_install_langs", opt->str);
}
/* This is what we use as default. */
dnf_context_set_rpm_macro (self->dnfctx, "_dbpath", "/" RPMOSTREE_RPMDB_LOCATION);
if (!dnf_context_setup (self->dnfctx, cancellable, error))
return FALSE;
/* disable all repos in pkgcache-only mode, otherwise obey "repos" key */
if (self->pkgcache_only)
{
if (!context_repos_enable_only (self, NULL, error))
return FALSE;
}
else
{
/* NB: missing "repos" --> let hif figure it out for itself */
if (g_variant_dict_lookup (self->spec->dict, "repos", "^a&s", &enabled_repos))
if (!context_repos_enable_only (self, (const char *const*)enabled_repos, error))
return FALSE;
}
g_autoptr(GPtrArray) repos =
rpmostree_get_enabled_rpmmd_repos (self->dnfctx, DNF_REPO_ENABLED_PACKAGES);
if (repos->len == 0 && !self->pkgcache_only)
{
/* To be nice, let's only make this fatal if "packages" is empty (e.g. if
* we're only installing local RPMs. Missing deps will cause the regular
* 'not found' error from libdnf. */
g_autofree char **pkgs = NULL;
if (g_variant_dict_lookup (self->spec->dict, "packages", "^a&s", &pkgs) &&
g_strv_length (pkgs) > 0)
return glnx_throw (error, "No enabled repositories");
}
/* Keep a handy pointer to the rojig source if specified, since it influences
* a lot of things here.
*/
g_variant_dict_lookup (self->spec->dict, "rojig", "&s", &self->rojig_spec);
/* Ensure that each repo that's enabled is marked as required; this should be
* the default, but we make sure. This is a bit of a messy topic, but for
* rpm-ostree we're being more strict about requiring repos.
*/
for (guint i = 0; i < repos->len; i++)
dnf_repo_set_required (repos->pdata[i], TRUE);
{ gboolean docs;
g_assert (g_variant_dict_lookup (self->spec->dict, "documentation", "b", &docs));
if (!docs)
dnf_transaction_set_flags (dnf_context_get_transaction (self->dnfctx),
DNF_TRANSACTION_FLAG_NODOCS);
}
/* We could likely delete this, but I'm keeping a log message just in case */
if (g_variant_dict_contains (self->spec->dict, "ignore-scripts"))
sd_journal_print (LOG_INFO, "ignore-scripts is no longer supported");
gboolean selinux;
g_assert (g_variant_dict_lookup (self->spec->dict, "selinux", "b", &selinux));
/* Load policy from / if SELinux is enabled, and we haven't already loaded
* a policy. This is mostly for the "compose tree" case.
*/
if (selinux && !self->sepolicy)
{
glnx_autofd int host_rootfs_dfd = -1;
/* Ensure that the imported packages are labeled with *a* policy if
* possible, even if it's not the final one. This helps avoid duplicating
* all of the content.
*/
if (!glnx_opendirat (AT_FDCWD, "/", TRUE, &host_rootfs_dfd, error))
return FALSE;
g_autoptr(OstreeSePolicy) sepolicy = ostree_sepolicy_new_at (host_rootfs_dfd, cancellable, error);
if (!sepolicy)
return FALSE;
/* For system contexts, whether SELinux required is dynamic based on the
* filesystem state. If the base compose doesn't have a policy package,
* then that's OK. It'd be cleaner if we loaded the no-SELinux state from
* the origin, but that'd be a backwards-incompatible change.
*/
if (!self->is_system && ostree_sepolicy_get_name (sepolicy) == NULL)
return glnx_throw (error, "Unable to load SELinux policy from /");
rpmostree_context_set_sepolicy (self, sepolicy);
}
return TRUE;
}
static void
on_hifstate_percentage_changed (DnfState *hifstate,
guint percentage,
gpointer user_data)
{
rpmostree_output_progress_percent (percentage);
}
static gboolean
get_commit_metadata_string (GVariant *commit,
const char *key,
char **out_str,
GError **error)
{
g_autoptr(GVariant) meta = g_variant_get_child_value (commit, 0);
g_autoptr(GVariantDict) meta_dict = g_variant_dict_new (meta);
g_autoptr(GVariant) str_variant = NULL;
str_variant =
_rpmostree_vardict_lookup_value_required (meta_dict, key,
G_VARIANT_TYPE_STRING, error);
if (!str_variant)
return FALSE;
g_assert (g_variant_is_of_type (str_variant, G_VARIANT_TYPE_STRING));
*out_str = g_strdup (g_variant_get_string (str_variant, NULL));
return TRUE;
}
static gboolean
get_commit_sepolicy_csum (GVariant *commit,
char **out_csum,
GError **error)
{
return get_commit_metadata_string (commit, "rpmostree.sepolicy",
out_csum, error);
}
static gboolean
get_commit_header_sha256 (GVariant *commit,
char **out_csum,
GError **error)
{
return get_commit_metadata_string (commit, "rpmostree.metadata_sha256",
out_csum, error);
}
static gboolean
get_commit_repodata_chksum_repr (GVariant *commit,
char **out_csum,
GError **error)
{
return get_commit_metadata_string (commit, "rpmostree.repodata_checksum",
out_csum, error);
}
static char *
get_nevra_relpath (const char *nevra)
{
return g_strdup_printf ("metarpm/%s.rpm", nevra);
}
static char *
get_package_relpath (DnfPackage *pkg)
{
return get_nevra_relpath (dnf_package_get_nevra (pkg));
}
/* We maintain a temporary copy on disk of the RPM header value; librpm will
* load this dynamically as the txn progresses.
*/
static gboolean
checkout_pkg_metadata (RpmOstreeContext *self,
const char *nevra,
GVariant *header,
GCancellable *cancellable,
GError **error)
{
if (!rpmostree_context_ensure_tmpdir (self, "metarpm", error))
return FALSE;
/* give it a .rpm extension so we can fool the libdnf stack */
g_autofree char *path = get_nevra_relpath (nevra);
if (!glnx_fstatat_allow_noent (self->tmpdir.fd, path, NULL, 0, error))
return FALSE;
/* we may have already written the header out for this one */
if (errno == 0)
return TRUE;
return glnx_file_replace_contents_at (self->tmpdir.fd, path,
g_variant_get_data (header),
g_variant_get_size (header),
GLNX_FILE_REPLACE_NODATASYNC,
cancellable, error);
}
static gboolean
get_header_variant (OstreeRepo *repo,
const char *cachebranch,
GVariant **out_header,
GCancellable *cancellable,
GError **error)
{
g_autofree char *cached_rev = NULL;
if (!ostree_repo_resolve_rev (repo, cachebranch, FALSE,
&cached_rev, error))
return FALSE;
g_autoptr(GVariant) pkg_commit = NULL;
if (!ostree_repo_load_commit (repo, cached_rev, &pkg_commit, NULL, error))
return FALSE;
g_autoptr(GVariant) pkg_meta = g_variant_get_child_value (pkg_commit, 0);
g_autoptr(GVariantDict) pkg_meta_dict = g_variant_dict_new (pkg_meta);
g_autoptr(GVariant) header =
_rpmostree_vardict_lookup_value_required (pkg_meta_dict, "rpmostree.metadata",
(GVariantType*)"ay", error);
if (!header)
{
g_autofree char *nevra = rpmostree_cache_branch_to_nevra (cachebranch);
g_prefix_error (error, "In commit %s of %s: ", cached_rev, nevra);
return FALSE;
}
*out_header = g_steal_pointer (&header);
return TRUE;
}
static gboolean
checkout_pkg_metadata_by_dnfpkg (RpmOstreeContext *self,
DnfPackage *pkg,
GCancellable *cancellable,
GError **error)
{
const char *nevra = dnf_package_get_nevra (pkg);
g_autofree char *cachebranch = rpmostree_get_cache_branch_pkg (pkg);
OstreeRepo *pkgcache_repo = get_pkgcache_repo (self);
g_autoptr(GVariant) header = NULL;
if (!get_header_variant (pkgcache_repo, cachebranch, &header,
cancellable, error))
return FALSE;
return checkout_pkg_metadata (self, nevra, header, cancellable, error);
}
gboolean
rpmostree_pkgcache_find_pkg_header (OstreeRepo *pkgcache,
const char *nevra,
const char *expected_sha256,
GVariant **out_header,
GCancellable *cancellable,
GError **error)
{
g_autofree char *cache_branch = NULL;
if (!rpmostree_nevra_to_cache_branch (nevra, &cache_branch, error))
return FALSE;
if (expected_sha256 != NULL)
{
g_autofree char *commit_csum = NULL;
g_autoptr(GVariant) commit = NULL;
g_autofree char *actual_sha256 = NULL;
if (!ostree_repo_resolve_rev (pkgcache, cache_branch, TRUE, &commit_csum, error))
return FALSE;