summaryrefslogtreecommitdiff
path: root/lib/sbuild/chroot/chroot.cc
blob: 2582cf16d980058af65bb5daa8a0255de2b487a2 (plain)
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
/* Copyright © 2005-2013  Roger Leigh <rleigh@debian.org>
 *
 * schroot is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * schroot 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 *********************************************************************/

#include <config.h>

#include <sbuild/chroot/chroot.h>
#include <sbuild/chroot/config.h>
#include <sbuild/chroot/directory.h>
#include <sbuild/chroot/plain.h>
#include <sbuild/chroot/custom.h>
#include <sbuild/chroot/file.h>
#ifdef SBUILD_FEATURE_BLOCKDEV
#include <sbuild/chroot/block-device.h>
#endif // SBUILD_FEATURE_BLOCKDEV
#ifdef SBUILD_FEATURE_LOOPBACK
#include <sbuild/chroot/loopback.h>
#endif // SBUILD_FEATURE_LOOPBACK
#ifdef SBUILD_FEATURE_LVMSNAP
#include <sbuild/chroot/lvm-snapshot.h>
#endif // SBUILD_FEATURE_LVMSNAP
#ifdef SBUILD_FEATURE_BTRFSSNAP
#include <sbuild/chroot/btrfs-snapshot.h>
#endif // SBUILD_FEATURE_BTRFSSNAP
#include <sbuild/chroot/facet/facet.h>
#include <sbuild/chroot/facet/personality.h>
#include <sbuild/chroot/facet/plain.h>
#include <sbuild/chroot/facet/session.h>
#include <sbuild/chroot/facet/session-clonable.h>
#include <sbuild/chroot/facet/source.h>
#include <sbuild/chroot/facet/storage.h>
#include <sbuild/chroot/facet/userdata.h>
#ifdef SBUILD_FEATURE_UNSHARE
#include <sbuild/chroot/facet/unshare.h>
#endif // SBUILD_FEATURE_UNSHARE
#include "fdstream.h"

#include <cerrno>
#include <map>
#include <utility>

#include <boost/format.hpp>

using boost::format;

namespace sbuild
{
  namespace chroot
  {

    template<>
    error<chroot::error_code>::map_type
    error<chroot::error_code>::error_strings =
      {
        {chroot::chroot::CHROOT_CREATE,     N_("Chroot creation failed")},
        {chroot::chroot::CHROOT_DEVICE,     N_("Device name not set")},
        // TRANSLATORS: %1% = chroot type name
        {chroot::chroot::CHROOT_TYPE,       N_("Unknown chroot type ‘%1%’")},
        {chroot::chroot::DEVICE_ABS,        N_("Device must have an absolute path")},
        {chroot::chroot::DEVICE_LOCK,       N_("Failed to lock device")},
        {chroot::chroot::DEVICE_NOTBLOCK,   N_("File is not a block device")},
        {chroot::chroot::DEVICE_UNLOCK,     N_("Failed to unlock device")},
        {chroot::chroot::DIRECTORY_ABS,     N_("Directory must have an absolute path")},
        {chroot::chroot::FACET_ABSENT,      N_("Attempt to use facet ‘%1%’ which is not present")},
        {chroot::chroot::FACET_INVALID,     N_("Attempt to add object ‘%1%’ which is not a facet")},
        {chroot::chroot::FACET_PRESENT,     N_("Attempt to add facet ‘%1%’ which is already in use")},
        {chroot::chroot::FILE_ABS,          N_("File must have an absolute path")},
        {chroot::chroot::FILE_LOCK,         N_("Failed to acquire file lock")},
        {chroot::chroot::FILE_NOTREG,       N_("File is not a regular file")},
        {chroot::chroot::FILE_OWNER,        N_("File is not owned by user root")},
        {chroot::chroot::FILE_PERMS,        N_("File has write permissions for others")},
        {chroot::chroot::FILE_UNLOCK,       N_("Failed to discard file lock")},
        {chroot::chroot::LOCATION_ABS,      N_("Location must have an absolute path")},
        {chroot::chroot::NAME_INVALID,      N_("Invalid name")},
        {chroot::chroot::SCRIPT_CONFIG_CV,  N_("Could not set profile name from script configuration path ‘%1%’")},

        // TRANSLATORS: unlink refers to the C function which removes a file
        {chroot::chroot::SESSION_UNLINK,    N_("Failed to unlink session file")},
        {chroot::chroot::SESSION_WRITE,     N_("Failed to write session file")},
        {chroot::chroot::VERBOSITY_INVALID, N_("Message verbosity is invalid")}
      };

    chroot::chroot ():
      name(),
      description(),
      users(),
      groups(),
      root_users(),
      root_groups(),
      aliases(),
      preserve_environment(false),
      default_shell(),
      environment_filter(SBUILD_DEFAULT_ENVIRONMENT_FILTER),
      mount_location(),
      original(true),
      script_config(),
      profile("default"),
      command_prefix(),
      message_verbosity(VERBOSITY_NORMAL),
      facets()
    {
      add_facet(facet::personality::create());
#ifdef SBUILD_FEATURE_UNSHARE
      add_facet(facet::unshare::create());
#endif // SBUILD_FEATURE_UNSHARE
      add_facet(facet::session_clonable::create());
      add_facet(facet::userdata::create());

      set_profile(get_profile());
    }

    chroot::chroot (const chroot& rhs):
      name(rhs.name),
      description(rhs.description),
      users(rhs.users),
      groups(rhs.groups),
      root_users(rhs.root_users),
      root_groups(rhs.root_groups),
      aliases(rhs.aliases),
      preserve_environment(rhs.preserve_environment),
      default_shell(rhs.default_shell),
      environment_filter(rhs.environment_filter),
      mount_location(rhs.mount_location),
      original(rhs.original),
      script_config(rhs.script_config),
      profile(rhs.profile),
      command_prefix(rhs.command_prefix),
      message_verbosity(rhs.message_verbosity),
      facets()
    {
      /// @todo Use internal version of add_facet to add chroot pointer.
      for (const auto& facet : rhs.facets)
        {
          facet_ptr fp = facet->clone();
          fp->set_chroot(*this);
          facets.push_back(fp);
        }
    }

    chroot::~chroot ()
    {
    }

    chroot::ptr
    chroot::create (std::string const& type)
    {
      chroot *new_chroot = 0;

      if (type == "directory")
        new_chroot = new directory();
      else if (type == "plain")
        new_chroot = new plain();
      else if (type == "custom")
        new_chroot = new custom();
      else if (type == "file")
        new_chroot = new file();
#ifdef SBUILD_FEATURE_BLOCKDEV
      else if (type == "block-device")
        new_chroot = new block_device();
#endif // SBUILD_FEATURE_BLOCKDEV
#ifdef SBUILD_FEATURE_LOOPBACK
      else if (type == "loopback")
        new_chroot = new loopback();
#endif // SBUILD_FEATURE_LOOPBACK
#ifdef SBUILD_FEATURE_LVMSNAP
      else if (type == "lvm-snapshot")
        new_chroot = new lvm_snapshot();
#endif // SBUILD_FEATURE_LVMSNAP
#ifdef SBUILD_FEATURE_BTRFSSNAP
      else if (type == "btrfs-snapshot")
        new_chroot = new btrfs_snapshot();
#endif // SBUILD_FEATURE_BTRFSSNAP
      else
        throw error(type, CHROOT_TYPE);

      if (new_chroot == 0)
        throw error(CHROOT_CREATE);

      return ptr(new_chroot);
    }

    std::string const&
    chroot::get_name () const
    {
      return this->name;
    }

    void
    chroot::set_name (std::string const& name)
    {
      std::string::size_type pos = name.find_first_of(config::namespace_separator);
      if (pos != std::string::npos)
        {
          error e(name, NAME_INVALID);
          format fmt(_("Namespace separator ‘%1%’ may not be used in a chroot name"));
          fmt % config::namespace_separator;
          e.set_reason(fmt.str());
          throw e;
        }

      if (!is_valid_sessionname(name))
        {
          error e(name, NAME_INVALID);
          e.set_reason(_("Naming restrictions are documented in schroot.conf(5)"));
          throw e;
        }

      this->name = name;
    }


    std::string const&
    chroot::get_description () const
    {
      return this->description;
    }

    void
    chroot::set_description (std::string const& description)
    {
      this->description = description;
    }

    std::string const&
    chroot::get_mount_location () const
    {
      return this->mount_location;
    }

    void
    chroot::set_mount_location (std::string const& location)
    {
      if (!location.empty() && !is_absname(location))
        throw error(location, LOCATION_ABS);
      this->mount_location = location;
    }

    std::string
    chroot::get_path () const
    {
      return get_facet_strict<facet::storage>()->get_path();
    }

    string_list const&
    chroot::get_users () const
    {
      return this->users;
    }

    void
    chroot::set_users (string_list const& users)
    {
      this->users = users;
    }

    string_list const&
    chroot::get_groups () const
    {
      return this->groups;
    }

    void
    chroot::set_groups (string_list const& groups)
    {
      this->groups = groups;
    }

    string_list const&
    chroot::get_root_users () const
    {
      return this->root_users;
    }

    void
    chroot::set_root_users (string_list const& users)
    {
      this->root_users = users;
    }

    string_list const&
    chroot::get_root_groups () const
    {
      return this->root_groups;
    }

    void
    chroot::set_root_groups (string_list const& groups)
    {
      this->root_groups = groups;
    }

    string_list const&
    chroot::get_aliases () const
    {
      return this->aliases;
    }

    void
    chroot::set_aliases (string_list const& aliases)
    {
      for (const auto& alias : aliases)
        {
          std::string::size_type found = alias.find_first_of(config::namespace_separator);
          if (found != std::string::npos)
            {
              error e(alias, NAME_INVALID);
              format fmt(_("Namespace separator ‘%1%’ may not be used in an alias name"));
              fmt % config::namespace_separator;
              e.set_reason(fmt.str());
              throw e;
            }

          if (!is_valid_sessionname(alias))
            {
              error e(alias, NAME_INVALID);
              e.set_reason(_("Naming restrictions are documented in schroot.conf(5)"));
              throw e;
            }
        }

      this->aliases = aliases;
    }

    bool
    chroot::get_preserve_environment () const
    {
      return this->preserve_environment;
    }

    void
    chroot::set_preserve_environment (bool preserve_environment)
    {
      this->preserve_environment = preserve_environment;
    }

    std::string const&
    chroot::get_default_shell () const
    {
      return this->default_shell;
    }

    void
    chroot::set_default_shell (std::string const& default_shell)
    {
      this->default_shell = default_shell;
    }

    regex const&
    chroot::get_environment_filter () const
    {
      return this->environment_filter;
    }

    void
    chroot::set_environment_filter (regex const& environment_filter)
    {
      this->environment_filter = environment_filter;
    }

    bool
    chroot::get_original () const
    {
      return this->original;
    }

    void
    chroot::set_original (bool original)
    {
      this->original = original;
    }

    bool
    chroot::get_run_setup_scripts () const
    {
      facet::plain::const_ptr plain = get_facet<facet::plain>();

      return !static_cast<bool>(plain);
    }

    std::string const&
    chroot::get_script_config () const
    {
      return this->script_config;
    }

    void
    chroot::set_script_config (std::string const& script_config)
    {
      this->script_config = script_config;

      // Undo work of set_profile, so profile is completely unset.
      this->profile.clear();
      facet::userdata::ptr userdata =
        get_facet<facet::userdata>();
      if (userdata)
        {
          userdata->remove_data("setup.config");
          userdata->remove_data("setup.copyfiles");
          userdata->remove_data("setup.nssdatabases");
          userdata->remove_data("setup.fstab");
        }

    }

    std::string const&
    chroot::get_profile () const
    {
      return this->profile;
    }

    void
    chroot::set_profile (std::string const& profile)
    {
      this->profile = profile;

      facet::userdata::ptr userdata =
        get_facet<facet::userdata>();
      if (userdata)
        {
          userdata->set_system_data("setup.config", this->profile + "/config");
          userdata->set_system_data("setup.copyfiles", this->profile + "/copyfiles");
          userdata->set_system_data("setup.nssdatabases", this->profile + "/nssdatabases");
          userdata->set_system_data("setup.fstab", this->profile + "/fstab");
        }
    }

    string_list const&
    chroot::get_command_prefix () const
    {
      return this->command_prefix;
    }

    void
    chroot::set_command_prefix (string_list const& command_prefix)
    {
      this->command_prefix = command_prefix;
    }

    chroot::verbosity
    chroot::get_verbosity () const
    {
      return this->message_verbosity;
    }

    const char *
    chroot::get_verbosity_string () const
    {
      const char *verbosity = 0;

      switch (this->message_verbosity)
        {
        case chroot::VERBOSITY_QUIET:
          verbosity = "quiet";
          break;
        case chroot::VERBOSITY_NORMAL:
          verbosity = "normal";
          break;
        case chroot::VERBOSITY_VERBOSE:
          verbosity = "verbose";
          break;
        default:
          log_debug(DEBUG_CRITICAL) << format("Invalid verbosity level: %1%, falling back to 'normal'")
            % static_cast<int>(this->message_verbosity)
                                    << std::endl;
          verbosity = "normal";
          break;
        }

      return verbosity;
    }

    void
    chroot::set_verbosity (chroot::verbosity verbosity)
    {
      this->message_verbosity = verbosity;
    }

    void
    chroot::set_verbosity (std::string const& verbosity)
    {
      if (verbosity == "quiet")
        this->message_verbosity = VERBOSITY_QUIET;
      else if (verbosity == "normal")
        this->message_verbosity = VERBOSITY_NORMAL;
      else if (verbosity == "verbose")
        this->message_verbosity = VERBOSITY_VERBOSE;
      else
        throw error(verbosity, VERBOSITY_INVALID);
    }

    string_list
    chroot::list_facets () const
    {
      string_list facet_names;

      for (const auto& facet : facets)
        facet_names.push_back(facet->get_name());

      return facet_names;
    }

    std::string const&
    chroot::get_chroot_type () const
    {
      return get_facet_strict<facet::storage>()->get_name();
    }

    void
    chroot::setup_env (environment& env) const
    {
      setup_env(*this, env);

      for (const auto& facet : facets)
        facet->setup_env(*this, env);
    }

    void
    chroot::setup_env (chroot const& chroot,
                       environment&  env) const
    {
      env.add("CHROOT_TYPE", chroot.get_chroot_type());
      env.add("CHROOT_NAME", chroot.get_name());
      env.add("SESSION_ID", chroot.get_name());
      env.add("CHROOT_DESCRIPTION", chroot.get_description());
      env.add("CHROOT_MOUNT_LOCATION", chroot.get_mount_location());
      env.add("CHROOT_PATH", chroot.get_path());
      if (!chroot.get_script_config().empty())
        env.add("CHROOT_SCRIPT_CONFIG", normalname(std::string(SCHROOT_SYSCONF_DIR) +  '/' + chroot.get_script_config()));
      if (!chroot.get_profile().empty())
        {
          env.add("CHROOT_PROFILE", chroot.get_profile());
          env.add("CHROOT_PROFILE_DIR", normalname(std::string(SCHROOT_SYSCONF_DIR) +  '/' + chroot.get_profile()));
        }
      env.add("CHROOT_SESSION_CREATE",
              static_cast<bool>(chroot.get_session_flags() & SESSION_CREATE));
      env.add("CHROOT_SESSION_CLONE",
              static_cast<bool>(chroot.get_session_flags() & SESSION_CLONE));
      env.add("CHROOT_SESSION_PURGE",
              static_cast<bool>(chroot.get_session_flags() & SESSION_PURGE));
    }

    void
    chroot::lock (setup_type type)
    {
      setup_lock(type, true, 0);
    }

    void
    chroot::unlock (setup_type type,
                    int        status)
    {
      setup_lock(type, false, status);
    }

    void
    chroot::setup_lock(setup_type type,
                       bool       lock,
                       int        status)
    {
      get_facet_strict<facet::storage>()->setup_lock(type, lock, status);
    }

    chroot::session_flags
    chroot::get_session_flags () const
    {
      session_flags flags = get_session_flags(*this);

      for (const auto& facet : facets)
        flags = flags | facet->get_session_flags(*this);

      return flags;
    }

    chroot::session_flags
    chroot::get_session_flags (chroot const& chroot) const
    {
      return SESSION_NOFLAGS;
    }

    void
    chroot::get_details (format_detail& detail) const
    {
      get_details(*this, detail);

      for (const auto& facet : facets)
        facet->get_details(*this, detail);
    }

    void
    chroot::get_details (chroot const&  chroot,
                         format_detail& detail) const
    {
      detail.add(_("Name"), chroot.get_name());

      detail
        .add(_("Description"), chroot.get_description())
        .add(_("Type"), chroot.get_chroot_type())
        .add(_("Message Verbosity"), chroot.get_verbosity_string())
        .add(_("Users"), chroot.get_users())
        .add(_("Groups"), chroot.get_groups())
        .add(_("Root Users"), chroot.get_root_users())
        .add(_("Root Groups"), chroot.get_root_groups())
        .add(_("Aliases"), chroot.get_aliases())
        .add(_("Preserve Environment"), chroot.get_preserve_environment())
        .add(_("Default Shell"), chroot.get_default_shell())
        .add(_("Environment Filter"), chroot.get_environment_filter())
        .add(_("Run Setup Scripts"), chroot.get_run_setup_scripts())
        .add(_("Configuration Profile"), chroot.get_profile())
        .add(_("Script Configuration"), chroot.get_script_config())
        .add(_("Session Managed"),
             static_cast<bool>(chroot.get_session_flags() & chroot::SESSION_CREATE))
        .add(_("Session Cloned"),
             static_cast<bool>(chroot.get_session_flags() & chroot::SESSION_CLONE))
        .add(_("Session Purged"),
             static_cast<bool>(chroot.get_session_flags() & chroot::SESSION_PURGE));

      if (!chroot.get_command_prefix().empty())
        detail.add(_("Command Prefix"), chroot.get_command_prefix());

      /* Non user-settable properties are listed last. */
      if (!chroot.get_mount_location().empty())
        detail.add(_("Mount Location"), chroot.get_mount_location());
      if (!chroot.get_path().empty())
        detail.add(_("Path"), chroot.get_path());
    }

    void
    chroot::print_details (std::ostream& stream) const
    {
      std::string title(_("Chroot"));

      if (get_facet<facet::session>())
        title = _("Session");
      if (get_facet<facet::source>())
        title = _("Source");

      format_detail fmt(title, stream.getloc());

      get_details(fmt);

      stream << fmt;
    }

    string_list
    chroot::get_used_keys () const
    {
      string_list used_keys;

      get_used_keys(used_keys);

      for (const auto& facet : facets)
        facet->get_used_keys(used_keys);

      return used_keys;
    }

    void
    chroot::get_used_keys (string_list& used_keys) const
    {
      // Keys which are used elsewhere, but should be counted as "used".
      used_keys.push_back("type");

      used_keys.push_back("active");
      used_keys.push_back("run-setup-scripts");
      used_keys.push_back("run-session-scripts");
      used_keys.push_back("run-exec-scripts");
      used_keys.push_back("profile");
      used_keys.push_back("script-config");
      used_keys.push_back("priority");
      used_keys.push_back("aliases");
      used_keys.push_back("environment-filter");
      used_keys.push_back("description");
      used_keys.push_back("users");
      used_keys.push_back("groups");
      used_keys.push_back("root-users");
      used_keys.push_back("root-groups");
      used_keys.push_back("mount-location");
      used_keys.push_back("name");
      used_keys.push_back("command-prefix");
      used_keys.push_back("message-verbosity");
      used_keys.push_back("preserve-environment");
      used_keys.push_back("shell");
    }

    void
    chroot::get_keyfile (keyfile& keyfile) const
    {
      get_keyfile(*this, keyfile);

      for (const auto& facet : facets)
        facet->get_keyfile(*this, keyfile);
    }

    void
    chroot::get_keyfile (chroot const& chroot,
                         keyfile&      keyfile) const
    {
      keyfile.remove_group(chroot.get_name());

      bool session = static_cast<bool>(get_facet<facet::session>());

      if (session)
        keyfile::set_object_value(chroot, &chroot::get_name,
                                  keyfile, chroot.get_name(),
                                  "name");

      keyfile::set_object_value(chroot, &chroot::get_chroot_type,
                                keyfile, chroot.get_name(),
                                "type");

      keyfile::set_object_value(chroot, &chroot::get_profile,
                                keyfile, chroot.get_name(),
                                "profile");

      if (!get_script_config().empty())
        keyfile::set_object_value(chroot, &chroot::get_script_config,
                                  keyfile, chroot.get_name(),
                                  "script-config");

      keyfile::set_object_list_value(chroot, &chroot::get_aliases,
                                     keyfile, chroot.get_name(),
                                     "aliases");

      keyfile::set_object_value(chroot, &chroot::get_environment_filter,
                                keyfile, chroot.get_name(),
                                "environment-filter");

      keyfile::set_object_value(chroot, &chroot::get_description,
                                keyfile, chroot.get_name(),
                                "description");

      keyfile::set_object_list_value(chroot, &chroot::get_users,
                                     keyfile, chroot.get_name(),
                                     "users");

      keyfile::set_object_list_value(chroot, &chroot::get_groups,
                                     keyfile, chroot.get_name(),
                                     "groups");

      keyfile::set_object_list_value(chroot, &chroot::get_root_users,
                                     keyfile, chroot.get_name(),
                                     "root-users");

      keyfile::set_object_list_value(chroot, &chroot::get_root_groups,
                                     keyfile, chroot.get_name(),
                                     "root-groups");

      if (session)
        keyfile::set_object_value(chroot, &chroot::get_mount_location,
                                  keyfile, chroot.get_name(),
                                  "mount-location");

      keyfile::set_object_list_value(chroot, &chroot::get_command_prefix,
                                     keyfile, chroot.get_name(),
                                     "command-prefix");

      keyfile::set_object_value(chroot, &chroot::get_verbosity_string,
                                keyfile, chroot.get_name(),
                                "message-verbosity");

      keyfile::set_object_value(chroot, &chroot::get_preserve_environment,
                                keyfile, chroot.get_name(),
                                "preserve-environment");

      keyfile::set_object_value(chroot, &chroot::get_default_shell,
                                keyfile, chroot.get_name(),
                                "shell");
    }

    void
    chroot::set_keyfile (keyfile const& keyfile)
    {
      set_keyfile(*this, keyfile);

      for (const auto& facet : facets)
        facet->set_keyfile(*this, keyfile);
    }

    void
    chroot::set_keyfile (chroot&        chroot,
                         keyfile const& keyfile)
    {
      // Null method for obsolete keys.
      void (chroot::* nullmethod)(bool) = 0;

      bool session = static_cast<bool>(get_facet<facet::session>());

      keyfile::get_object_value(chroot, nullmethod,
                                keyfile, chroot.get_name(),
                                "active",
                                keyfile::PRIORITY_OBSOLETE);

      // Setup scripts are run depending on the chroot type in use, and is
      // no longer user-configurable.  They need to run for all types
      // except "plain".
      keyfile::get_object_value(chroot, nullmethod,
                                keyfile, chroot.get_name(),
                                "run-setup-scripts",
                                keyfile::PRIORITY_OBSOLETE);

      // Exec scripts have been removed, so these two calls do nothing
      // except to warn the user that the options are no longer used.
      keyfile::get_object_value(chroot, nullmethod,
                                keyfile, chroot.get_name(),
                                "run-session-scripts",
                                keyfile::PRIORITY_OBSOLETE);
      keyfile::get_object_value(chroot, nullmethod,
                                keyfile, chroot.get_name(),
                                "run-exec-scripts",
                                keyfile::PRIORITY_OBSOLETE);

      keyfile::get_object_value(chroot, &chroot::set_profile,
                                keyfile, chroot.get_name(),
                                "profile",
                                keyfile::PRIORITY_OPTIONAL);

      keyfile::get_object_value(chroot, &chroot::set_script_config,
                                keyfile, chroot.get_name(),
                                "script-config",
                                session ?
                                keyfile::PRIORITY_OPTIONAL :
                                keyfile::PRIORITY_DEPRECATED);

      keyfile::get_object_value(chroot, nullmethod,
                                keyfile, chroot.get_name(),
                                "priority",
                                session ?
                                keyfile::PRIORITY_OPTIONAL :
                                keyfile::PRIORITY_OBSOLETE);

      keyfile::get_object_list_value(chroot, &chroot::set_aliases,
                                     keyfile, chroot.get_name(),
                                     "aliases",
                                     keyfile::PRIORITY_OPTIONAL);

      keyfile::get_object_value(chroot, &chroot::set_environment_filter,
                                keyfile, chroot.get_name(),
                                "environment-filter",
                                keyfile::PRIORITY_OPTIONAL);

      keyfile::get_object_value(chroot, &chroot::set_description,
                                keyfile, chroot.get_name(),
                                "description",
                                keyfile::PRIORITY_OPTIONAL);

      keyfile::get_object_list_value(chroot, &chroot::set_users,
                                     keyfile, chroot.get_name(),
                                     "users",
                                     keyfile::PRIORITY_OPTIONAL);

      keyfile::get_object_list_value(chroot, &chroot::set_groups,
                                     keyfile, chroot.get_name(),
                                     "groups",
                                     keyfile::PRIORITY_OPTIONAL);

      keyfile::get_object_list_value(chroot, &chroot::set_root_users,
                                     keyfile, chroot.get_name(),
                                     "root-users",
                                     keyfile::PRIORITY_OPTIONAL);

      keyfile::get_object_list_value(chroot, &chroot::set_root_groups,
                                     keyfile, chroot.get_name(),
                                     "root-groups",
                                     keyfile::PRIORITY_OPTIONAL);

      keyfile::get_object_value(chroot, &chroot::set_mount_location,
                                keyfile, chroot.get_name(),
                                "mount-location",
                                session ?
                                keyfile::PRIORITY_REQUIRED :
                                keyfile::PRIORITY_DISALLOWED);

      keyfile::get_object_value(chroot, &chroot::set_name,
                                keyfile, chroot.get_name(),
                                "name",
                                session ?
                                keyfile::PRIORITY_OPTIONAL :
                                keyfile::PRIORITY_DISALLOWED);

      keyfile::get_object_list_value(chroot, &chroot::set_command_prefix,
                                     keyfile, chroot.get_name(),
                                     "command-prefix",
                                     keyfile::PRIORITY_OPTIONAL);

      keyfile::get_object_value(chroot, &chroot::set_verbosity,
                                keyfile, chroot.get_name(),
                                "message-verbosity",
                                keyfile::PRIORITY_OPTIONAL);

      keyfile::get_object_value(chroot, &chroot::set_preserve_environment,
                                keyfile, chroot.get_name(),
                                "preserve-environment",
                                keyfile::PRIORITY_OPTIONAL);

      keyfile::get_object_value(chroot, &chroot::set_default_shell,
                                keyfile, chroot.get_name(),
                                "shell",
                                keyfile::PRIORITY_OPTIONAL);
    }

  }
}