1
/* Copyright © 2005-2006 Roger Leigh <rleigh@debian.org>
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.
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.
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,
18
*********************************************************************/
27
#include <sys/types.h>
29
#include <sys/sysmacros.h>
32
#include <boost/format.hpp>
36
using namespace sbuild;
38
chroot_lvm_snapshot::chroot_lvm_snapshot ():
39
chroot_block_device(),
44
set_run_setup_scripts(true);
45
set_run_exec_scripts(true);
48
chroot_lvm_snapshot::~chroot_lvm_snapshot ()
53
chroot_lvm_snapshot::clone () const
55
return ptr(new chroot_lvm_snapshot(*this));
59
chroot_lvm_snapshot::clone_source () const
61
ptr clone(new chroot_block_device(*this));
63
chroot_source::clone_source_setup(clone);
69
chroot_lvm_snapshot::get_snapshot_device () const
71
return this->snapshot_device;
75
chroot_lvm_snapshot::set_snapshot_device (std::string const& snapshot_device)
77
this->snapshot_device = snapshot_device;
81
chroot_lvm_snapshot::get_mount_device () const
83
return this->snapshot_device;
87
chroot_lvm_snapshot::get_snapshot_options () const
89
return this->snapshot_options;
93
chroot_lvm_snapshot::set_snapshot_options (std::string const& snapshot_options)
95
this->snapshot_options = snapshot_options;
99
chroot_lvm_snapshot::get_chroot_type () const
101
static const std::string type("lvm-snapshot");
107
chroot_lvm_snapshot::setup_env (environment& env)
109
chroot_block_device::setup_env(env);
110
chroot_source::setup_env(env);
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());
118
chroot_lvm_snapshot::setup_lock (setup_type type,
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))
129
if (type == SETUP_START)
130
device = get_device();
132
device = get_snapshot_device();
136
format fmt(_("%1% chroot: device name not set"));
140
else if (stat(device.c_str(), &statbuf) == -1)
142
format fmt(_("%1% chroot: failed to stat device %2%: %3%"));
143
fmt % get_name() % device % strerror(errno);
146
else if (!S_ISBLK(statbuf.st_mode))
148
format fmt(_("%1% chroot: %2% is not a block device\n"));
149
fmt % get_name() % device;
154
/* Lock is preserved while running a command. */
155
if ((type == EXEC_START && lock == false) ||
156
(type == EXEC_STOP && lock == true))
159
sbuild::device_lock dlock(device);
164
dlock.set_lock(lock::LOCK_EXCLUSIVE, 15);
166
catch (sbuild::lock::error const& e)
168
format fmt(_("%1%: failed to lock device: %2%"));
169
fmt % device % e.what();
179
catch (sbuild::lock::error const& e)
181
format fmt(_("%1%: failed to unlock device: %2%"));
182
fmt % device % e.what();
189
/* Create or unlink session information. */
190
if ((type == SETUP_START && lock == true) ||
191
(type == SETUP_STOP && lock == false && status == 0))
193
bool start = (type == SETUP_START);
194
setup_session_info(start);
198
sbuild::chroot::session_flags
199
chroot_lvm_snapshot::get_session_flags () const
201
return SESSION_CREATE;
205
chroot_lvm_snapshot::print_details (std::ostream& stream) const
207
chroot_block_device::print_details(stream);
208
chroot_source::print_details(stream);
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;
220
chroot_lvm_snapshot::get_keyfile (keyfile& keyfile) const
222
chroot_block_device::get_keyfile(keyfile);
223
chroot_source::get_keyfile(keyfile);
225
keyfile.set_value(get_name(), "lvm-snapshot-device",
226
get_snapshot_device());
228
keyfile.set_value(get_name(), "lvm-snapshot-options",
229
get_snapshot_options());
233
chroot_lvm_snapshot::set_keyfile (keyfile const& keyfile)
235
chroot_block_device::set_keyfile(keyfile);
236
chroot_source::set_keyfile(keyfile);
238
std::string snapshot_device;
239
if (keyfile.get_value(get_name(), "lvm-snapshot-device",
241
keyfile::PRIORITY_REQUIRED :
242
keyfile::PRIORITY_DISALLOWED,
244
set_snapshot_device(snapshot_device);
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);