summaryrefslogtreecommitdiff
path: root/lib/sbuild/chroot/facet/block-device.cc
blob: 735bd78b8cebe8752cea81dd6075b16ad5d54eed (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
/* 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/facet/block-device.h>
#include <sbuild/chroot/facet/lvm-snapshot.h>
#include <sbuild/chroot/facet/factory.h>
#include <sbuild/chroot/facet/session.h>
#include <sbuild/chroot/facet/session-clonable.h>
#include <sbuild/chroot/facet/source-clonable.h>
#ifdef SBUILD_FEATURE_UNION
#include <sbuild/chroot/facet/fsunion.h>
#endif // SBUILD_FEATURE_UNION
#include <sbuild/format-detail.h>
#include <sbuild/util.h>

#include <cassert>
#include <cerrno>
#include <cstring>

#include <boost/format.hpp>

using boost::format;
using namespace sbuild;

namespace sbuild
{
  namespace chroot
  {
    namespace facet
    {

      namespace
      {

        factory::facet_info block_device_info =
          {
            "block-device",
            N_("Support for ‘block-device’ chroots"),
            false,
            []() -> facet::ptr { return block_device::create(); }
          };

        factory block_device_register(block_device_info);

      }

      block_device::block_device ():
        block_device_base()
      {
      }

      block_device::~block_device ()
      {
      }

      block_device::block_device (const block_device& rhs):
        block_device_base(rhs)
      {
      }

#ifdef SBUILD_FEATURE_LVMSNAP
      block_device::block_device (const lvm_snapshot& rhs):
        block_device_base(rhs)
      {
      }
#endif // SBUILD_FEATURE_LVMSNAP

      void
      block_device::set_chroot (chroot& chroot,
                                bool    copy)
      {
        block_device_base::set_chroot(chroot, copy);
#ifdef SBUILD_FEATURE_UNION
        if (!copy && !owner->get_facet<fsunion>())
          owner->add_facet(fsunion::create());
#endif // SBUILD_FEATURE_UNION
      }

      std::string const&
      block_device::get_name () const
      {
        static const std::string name("block-device");

        return name;
      }

      block_device::ptr
      block_device::create ()
      {
        return ptr(new block_device());
      }

#ifdef SBUILD_FEATURE_LVMSNAP
      block_device::ptr
      block_device::create (const lvm_snapshot& rhs)
      {
        return ptr(new block_device(rhs));
      }
#endif // SBUILD_FEATURE_LVMSNAP

      facet::ptr
      block_device::clone () const
      {
        return ptr(new block_device(*this));
      }

      void
      block_device::setup_lock (chroot::setup_type type,
                                bool               lock,
                                int                status)
      {
        /* Lock is preserved through the entire session. */
        if ((type == chroot::SETUP_START && lock == false) ||
            (type == chroot::SETUP_STOP && lock == true))
          return;

        try
          {
            if (!stat
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
                (this->get_device()).is_character()
#else
                (this->get_device()).is_block()
#endif
                )
              {
                throw error(get_device(), chroot::DEVICE_NOTBLOCK);
              }
            else
              {
#ifdef SBUILD_FEATURE_UNION
                /* We don't lock the device if fsunion is configured. */
                fsunion::const_ptr puni
                  (owner->get_facet<fsunion>());
#endif // SBUILD_FEATURE_UNION
              }
          }
        catch (sbuild::stat::error const& e) // Failed to stat
          {
            // Don't throw if stopping a session and the device stat
            // failed.  This is because the setup scripts shouldn't fail
            // to be run if the block device no longer exists, which
            // would prevent the session from being ended.
            if (type != chroot::SETUP_STOP)
              throw;
          }

        /* Create or unlink session information. */
        if ((type == chroot::SETUP_START && lock == true) ||
            (type == chroot::SETUP_STOP && lock == false && status == 0))
          {
            bool start = (type == chroot::SETUP_START);
            owner->get_facet_strict<session>()->setup_session_info(start);
          }
      }

      void
      block_device::chroot_session_setup (chroot const&      parent,
                                          std::string const& session_id,
                                          std::string const& alias,
                                          std::string const& user,
                                          bool               root)
      {
        // Block devices need the mount device name specifying.
        mountable::ptr pmnt
          (owner->get_facet<mountable>());
        if (pmnt)
          pmnt->set_mount_device(get_device());
      }

    }
  }
}