00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include "sbuild-chroot-lvm-snapshot.h"
00023 #include "sbuild-format-detail.h"
00024 #include "sbuild-lock.h"
00025
00026 #include <cerrno>
00027 #include <iostream>
00028
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <sys/sysmacros.h>
00032 #include <unistd.h>
00033
00034 #include <boost/format.hpp>
00035
00036 using std::endl;
00037 using boost::format;
00038 using namespace sbuild;
00039
00040 chroot_lvm_snapshot::chroot_lvm_snapshot ():
00041 chroot_block_device(),
00042 chroot_source(),
00043 snapshot_device(),
00044 snapshot_options()
00045 {
00046 set_run_setup_scripts(true);
00047 set_run_exec_scripts(true);
00048 }
00049
00050 chroot_lvm_snapshot::~chroot_lvm_snapshot ()
00051 {
00052 }
00053
00054 sbuild::chroot::ptr
00055 chroot_lvm_snapshot::clone () const
00056 {
00057 return ptr(new chroot_lvm_snapshot(*this));
00058 }
00059
00060 sbuild::chroot::ptr
00061 chroot_lvm_snapshot::clone_source () const
00062 {
00063 ptr clone(new chroot_block_device(*this));
00064
00065 chroot_source::clone_source_setup(clone);
00066
00067 return clone;
00068 }
00069
00070 std::string const&
00071 chroot_lvm_snapshot::get_snapshot_device () const
00072 {
00073 return this->snapshot_device;
00074 }
00075
00076 void
00077 chroot_lvm_snapshot::set_snapshot_device (std::string const& snapshot_device)
00078 {
00079 if (!is_absname(snapshot_device))
00080 throw error(snapshot_device, DEVICE_ABS);
00081
00082 this->snapshot_device = snapshot_device;
00083 }
00084
00085 std::string const&
00086 chroot_lvm_snapshot::get_mount_device () const
00087 {
00088 return this->snapshot_device;
00089 }
00090
00091 std::string const&
00092 chroot_lvm_snapshot::get_snapshot_options () const
00093 {
00094 return this->snapshot_options;
00095 }
00096
00097 void
00098 chroot_lvm_snapshot::set_snapshot_options (std::string const& snapshot_options)
00099 {
00100 this->snapshot_options = snapshot_options;
00101 }
00102
00103 std::string const&
00104 chroot_lvm_snapshot::get_chroot_type () const
00105 {
00106 static const std::string type("lvm-snapshot");
00107
00108 return type;
00109 }
00110
00111 void
00112 chroot_lvm_snapshot::setup_env (environment& env)
00113 {
00114 chroot_block_device::setup_env(env);
00115 chroot_source::setup_env(env);
00116
00117 env.add("CHROOT_LVM_SNAPSHOT_NAME", sbuild::basename(get_snapshot_device()));
00118 env.add("CHROOT_LVM_SNAPSHOT_DEVICE", get_snapshot_device());
00119 env.add("CHROOT_LVM_SNAPSHOT_OPTIONS", get_snapshot_options());
00120 }
00121
00122 void
00123 chroot_lvm_snapshot::setup_lock (chroot::setup_type type,
00124 bool lock,
00125 int status)
00126 {
00127 std::string device;
00128 struct stat statbuf;
00129
00130
00131
00132 if (!(type == SETUP_STOP && lock == false))
00133 {
00134 if (type == SETUP_START)
00135 device = get_device();
00136 else
00137 device = get_snapshot_device();
00138
00139 if (device.empty())
00140 {
00141 throw error(CHROOT_DEVICE);
00142 }
00143 else if (stat(device.c_str(), &statbuf) == -1)
00144 {
00145 throw error(get_device(), DEVICE_STAT, strerror(errno));
00146 }
00147 else if (!S_ISBLK(statbuf.st_mode))
00148 {
00149 throw error(get_device(), DEVICE_NOTBLOCK);
00150 }
00151 else
00152 {
00153
00154 if ((type == EXEC_START && lock == false) ||
00155 (type == EXEC_STOP && lock == true))
00156 return;
00157
00158 sbuild::device_lock dlock(device);
00159 if (lock)
00160 {
00161 try
00162 {
00163 dlock.set_lock(lock::LOCK_EXCLUSIVE, 15);
00164 }
00165 catch (sbuild::lock::error const& e)
00166 {
00167 throw error(get_device(), DEVICE_LOCK, e);
00168 }
00169 }
00170 else
00171 {
00172 try
00173 {
00174 dlock.unset_lock();
00175 }
00176 catch (sbuild::lock::error const& e)
00177 {
00178 throw error(get_device(), DEVICE_UNLOCK, e);
00179 }
00180 }
00181 }
00182 }
00183
00184
00185 if ((type == SETUP_START && lock == true) ||
00186 (type == SETUP_STOP && lock == false && status == 0))
00187 {
00188 bool start = (type == SETUP_START);
00189 setup_session_info(start);
00190 }
00191 }
00192
00193 sbuild::chroot::session_flags
00194 chroot_lvm_snapshot::get_session_flags () const
00195 {
00196 return SESSION_CREATE;
00197 }
00198
00199 void
00200 chroot_lvm_snapshot::get_details (format_detail& detail) const
00201 {
00202 chroot_block_device::get_details(detail);
00203 chroot_source::get_details(detail);
00204
00205 if (!this->snapshot_device.empty())
00206 detail.add(_("LVM Snapshot Device"), get_snapshot_device());
00207 if (!this->snapshot_options.empty())
00208 detail.add(_("LVM Snapshot Options"), get_snapshot_options());
00209 }
00210
00211 void
00212 chroot_lvm_snapshot::get_keyfile (keyfile& keyfile) const
00213 {
00214 chroot_block_device::get_keyfile(keyfile);
00215 chroot_source::get_keyfile(keyfile);
00216
00217 if (get_active())
00218 keyfile::set_object_value(*this, &chroot_lvm_snapshot::get_snapshot_device,
00219 keyfile, get_name(), "lvm-snapshot-device");
00220
00221 keyfile::set_object_value(*this, &chroot_lvm_snapshot::get_snapshot_options,
00222 keyfile, get_name(), "lvm-snapshot-options");
00223 }
00224
00225 void
00226 chroot_lvm_snapshot::set_keyfile (keyfile const& keyfile)
00227 {
00228 chroot_block_device::set_keyfile(keyfile);
00229 chroot_source::set_keyfile(keyfile);
00230
00231 keyfile::get_object_value(*this, &chroot_lvm_snapshot::set_snapshot_device,
00232 keyfile, get_name(), "lvm-snapshot-device",
00233 get_active() ?
00234 keyfile::PRIORITY_REQUIRED :
00235 keyfile::PRIORITY_DISALLOWED);
00236
00237 keyfile::get_object_value(*this, &chroot_lvm_snapshot::set_snapshot_options,
00238 keyfile, get_name(), "lvm-snapshot-options",
00239 keyfile::PRIORITY_REQUIRED);
00240 }