~ubuntu-branches/ubuntu/intrepid/schroot/intrepid

« back to all changes in this revision

Viewing changes to schroot/sbuild-chroot-lvm-snapshot.cc

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2006-07-08 18:33:28 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060708183328-rlo4mpldmyoda55q
Tags: 0.99.2-2ubuntu1
* remerge ubuntu changes:
  + debian/control: libpam-dev (>> 0.79-3ubuntu6)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright © 2005-2006  Roger Leigh <rleigh@debian.org>
2
 
 *
3
 
 * schroot is free software; you can redistribute it and/or modify it
4
 
 * under the terms of the GNU General Public License as published by
5
 
 * the Free Software Foundation; either version 2 of the License, or
6
 
 * (at your option) any later version.
7
 
 *
8
 
 * schroot is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
 
 * General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program; if not, write to the Free Software
15
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
16
 
 * MA  02111-1307  USA
17
 
 *
18
 
 *********************************************************************/
19
 
 
20
 
#include <config.h>
21
 
 
22
 
#include "sbuild.h"
23
 
 
24
 
#include <cerrno>
25
 
#include <iostream>
26
 
 
27
 
#include <sys/types.h>
28
 
#include <sys/stat.h>
29
 
#include <sys/sysmacros.h>
30
 
#include <unistd.h>
31
 
 
32
 
#include <boost/format.hpp>
33
 
 
34
 
using std::endl;
35
 
using boost::format;
36
 
using namespace sbuild;
37
 
 
38
 
chroot_lvm_snapshot::chroot_lvm_snapshot ():
39
 
  chroot_block_device(),
40
 
  chroot_source(),
41
 
  snapshot_device(),
42
 
  snapshot_options()
43
 
{
44
 
  set_run_setup_scripts(true);
45
 
  set_run_exec_scripts(true);
46
 
}
47
 
 
48
 
chroot_lvm_snapshot::~chroot_lvm_snapshot ()
49
 
{
50
 
}
51
 
 
52
 
sbuild::chroot::ptr
53
 
chroot_lvm_snapshot::clone () const
54
 
{
55
 
  return ptr(new chroot_lvm_snapshot(*this));
56
 
}
57
 
 
58
 
sbuild::chroot::ptr
59
 
chroot_lvm_snapshot::clone_source () const
60
 
{
61
 
  ptr clone(new chroot_block_device(*this));
62
 
 
63
 
  chroot_source::clone_source_setup(clone);
64
 
 
65
 
  return clone;
66
 
}
67
 
 
68
 
std::string const&
69
 
chroot_lvm_snapshot::get_snapshot_device () const
70
 
{
71
 
  return this->snapshot_device;
72
 
}
73
 
 
74
 
void
75
 
chroot_lvm_snapshot::set_snapshot_device (std::string const& snapshot_device)
76
 
{
77
 
  this->snapshot_device = snapshot_device;
78
 
}
79
 
 
80
 
std::string const&
81
 
chroot_lvm_snapshot::get_mount_device () const
82
 
{
83
 
  return this->snapshot_device;
84
 
}
85
 
 
86
 
std::string const&
87
 
chroot_lvm_snapshot::get_snapshot_options () const
88
 
{
89
 
  return this->snapshot_options;
90
 
}
91
 
 
92
 
void
93
 
chroot_lvm_snapshot::set_snapshot_options (std::string const& snapshot_options)
94
 
{
95
 
  this->snapshot_options = snapshot_options;
96
 
}
97
 
 
98
 
std::string const&
99
 
chroot_lvm_snapshot::get_chroot_type () const
100
 
{
101
 
  static const std::string type("lvm-snapshot");
102
 
 
103
 
  return type;
104
 
}
105
 
 
106
 
void
107
 
chroot_lvm_snapshot::setup_env (environment& env)
108
 
{
109
 
  chroot_block_device::setup_env(env);
110
 
  chroot_source::setup_env(env);
111
 
 
112
 
  env.add("CHROOT_LVM_SNAPSHOT_NAME", sbuild::basename(get_snapshot_device()));
113
 
  env.add("CHROOT_LVM_SNAPSHOT_DEVICE", get_snapshot_device());
114
 
  env.add("CHROOT_LVM_SNAPSHOT_OPTIONS", get_snapshot_options());
115
 
}
116
 
 
117
 
void
118
 
chroot_lvm_snapshot::setup_lock (setup_type type,
119
 
                                 bool       lock,
120
 
                                 int        status)
121
 
{
122
 
  std::string device;
123
 
  struct stat statbuf;
124
 
 
125
 
  /* Lock is removed by setup script on setup stop.  Unlocking here
126
 
     would fail: the LVM snapshot device no longer exists. */
127
 
  if (!(type == SETUP_STOP && lock == false))
128
 
    {
129
 
      if (type == SETUP_START)
130
 
        device = get_device();
131
 
      else
132
 
        device = get_snapshot_device();
133
 
 
134
 
      if (device.empty())
135
 
        {
136
 
          format fmt(_("%1% chroot: device name not set"));
137
 
          fmt % get_name();
138
 
          throw error(fmt);
139
 
        }
140
 
      else if (stat(device.c_str(), &statbuf) == -1)
141
 
        {
142
 
          format fmt(_("%1% chroot: failed to stat device %2%: %3%"));
143
 
          fmt % get_name() % device % strerror(errno);
144
 
          throw error(fmt);
145
 
        }
146
 
      else if (!S_ISBLK(statbuf.st_mode))
147
 
        {
148
 
          format fmt(_("%1% chroot: %2% is not a block device\n"));
149
 
          fmt % get_name() % device;
150
 
          throw error(fmt);
151
 
        }
152
 
      else
153
 
        {
154
 
          /* Lock is preserved while running a command. */
155
 
          if ((type == EXEC_START && lock == false) ||
156
 
              (type == EXEC_STOP && lock == true))
157
 
            return;
158
 
 
159
 
          sbuild::device_lock dlock(device);
160
 
          if (lock)
161
 
            {
162
 
              try
163
 
                {
164
 
                  dlock.set_lock(lock::LOCK_EXCLUSIVE, 15);
165
 
                }
166
 
              catch (sbuild::lock::error const& e)
167
 
                {
168
 
                  format fmt(_("%1%: failed to lock device: %2%"));
169
 
                  fmt % device % e.what();
170
 
                  throw error(fmt);
171
 
                }
172
 
            }
173
 
          else
174
 
            {
175
 
              try
176
 
                {
177
 
                  dlock.unset_lock();
178
 
                }
179
 
              catch (sbuild::lock::error const& e)
180
 
                {
181
 
                  format fmt(_("%1%: failed to unlock device: %2%"));
182
 
                  fmt % device % e.what();
183
 
                  throw error(fmt);
184
 
                }
185
 
            }
186
 
        }
187
 
    }
188
 
 
189
 
  /* Create or unlink session information. */
190
 
  if ((type == SETUP_START && lock == true) ||
191
 
      (type == SETUP_STOP && lock == false && status == 0))
192
 
    {
193
 
      bool start = (type == SETUP_START);
194
 
      setup_session_info(start);
195
 
    }
196
 
}
197
 
 
198
 
sbuild::chroot::session_flags
199
 
chroot_lvm_snapshot::get_session_flags () const
200
 
{
201
 
  return SESSION_CREATE;
202
 
}
203
 
 
204
 
void
205
 
chroot_lvm_snapshot::print_details (std::ostream& stream) const
206
 
{
207
 
  chroot_block_device::print_details(stream);
208
 
  chroot_source::print_details(stream);
209
 
 
210
 
  if (!this->snapshot_device.empty())
211
 
    stream << format_details(_("LVM Snapshot Device"),
212
 
                             get_snapshot_device());
213
 
  if (!this->snapshot_options.empty())
214
 
    stream << format_details(_("LVM Snapshot Options"),
215
 
                             get_snapshot_options());
216
 
  stream << std::flush;
217
 
}
218
 
 
219
 
void
220
 
chroot_lvm_snapshot::get_keyfile (keyfile& keyfile) const
221
 
{
222
 
  chroot_block_device::get_keyfile(keyfile);
223
 
  chroot_source::get_keyfile(keyfile);
224
 
 
225
 
  keyfile.set_value(get_name(), "lvm-snapshot-device",
226
 
                    get_snapshot_device());
227
 
 
228
 
  keyfile.set_value(get_name(), "lvm-snapshot-options",
229
 
                    get_snapshot_options());
230
 
}
231
 
 
232
 
void
233
 
chroot_lvm_snapshot::set_keyfile (keyfile const& keyfile)
234
 
{
235
 
  chroot_block_device::set_keyfile(keyfile);
236
 
  chroot_source::set_keyfile(keyfile);
237
 
 
238
 
  std::string snapshot_device;
239
 
  if (keyfile.get_value(get_name(), "lvm-snapshot-device",
240
 
                        get_active() ?
241
 
                        keyfile::PRIORITY_REQUIRED :
242
 
                        keyfile::PRIORITY_DISALLOWED,
243
 
                        snapshot_device))
244
 
    set_snapshot_device(snapshot_device);
245
 
 
246
 
  std::string snapshot_options;
247
 
  if (keyfile.get_value(get_name(), "lvm-snapshot-options",
248
 
                        keyfile::PRIORITY_REQUIRED, snapshot_options))
249
 
    set_snapshot_options(snapshot_options);
250
 
}
251
 
 
252
 
/*
253
 
 * Local Variables:
254
 
 * mode:C++
255
 
 * End:
256
 
 */