[24167] | 1 | /* Copyright © 2005-2008 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 3 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, see |
---|
| 15 | * <http://www.gnu.org/licenses/>. |
---|
| 16 | * |
---|
| 17 | *********************************************************************/ |
---|
| 18 | |
---|
| 19 | #include <config.h> |
---|
| 20 | |
---|
| 21 | #include "sbuild-chroot-block-device.h" |
---|
| 22 | #include "sbuild-chroot-facet-session-clonable.h" |
---|
| 23 | #ifdef SBUILD_FEATURE_UNION |
---|
| 24 | #include "sbuild-chroot-facet-union.h" |
---|
| 25 | #endif // SBUILD_FEATURE_UNION |
---|
| 26 | #include "sbuild-format-detail.h" |
---|
| 27 | #include "sbuild-lock.h" |
---|
| 28 | #include "sbuild-util.h" |
---|
| 29 | |
---|
| 30 | #include <cassert> |
---|
| 31 | #include <cerrno> |
---|
| 32 | #include <cstring> |
---|
| 33 | |
---|
| 34 | #include <boost/format.hpp> |
---|
| 35 | |
---|
| 36 | using boost::format; |
---|
| 37 | using namespace sbuild; |
---|
| 38 | |
---|
| 39 | chroot_block_device::chroot_block_device (): |
---|
| 40 | chroot_block_device_base() |
---|
| 41 | { |
---|
| 42 | #ifdef SBUILD_FEATURE_UNION |
---|
| 43 | add_facet(sbuild::chroot_facet_union::create()); |
---|
| 44 | #endif // SBUILD_FEATURE_UNION |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | chroot_block_device::~chroot_block_device () |
---|
| 48 | { |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | chroot_block_device::chroot_block_device (const chroot_block_device& rhs): |
---|
| 52 | chroot_block_device_base(rhs) |
---|
| 53 | { |
---|
| 54 | } |
---|
| 55 | |
---|
[24314] | 56 | #ifdef SBUILD_FEATURE_LVMSNAP |
---|
[24167] | 57 | chroot_block_device::chroot_block_device (const chroot_lvm_snapshot& rhs): |
---|
| 58 | chroot_block_device_base(rhs) |
---|
| 59 | { |
---|
| 60 | #ifdef SBUILD_FEATURE_UNION |
---|
| 61 | if (!get_facet<sbuild::chroot_facet_union>()) |
---|
| 62 | add_facet(sbuild::chroot_facet_union::create()); |
---|
| 63 | #endif // SBUILD_FEATURE_UNION |
---|
| 64 | } |
---|
[24314] | 65 | #endif // SBUILD_FEATURE_LVMSNAP |
---|
[24167] | 66 | |
---|
| 67 | sbuild::chroot::ptr |
---|
| 68 | chroot_block_device::clone () const |
---|
| 69 | { |
---|
| 70 | return ptr(new chroot_block_device(*this)); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | sbuild::chroot::ptr |
---|
| 74 | chroot_block_device::clone_session (std::string const& session_id, |
---|
| 75 | std::string const& user, |
---|
| 76 | bool root) const |
---|
| 77 | { |
---|
| 78 | chroot_facet_session_clonable::const_ptr psess |
---|
| 79 | (get_facet<chroot_facet_session_clonable>()); |
---|
| 80 | assert(psess); |
---|
| 81 | |
---|
| 82 | ptr session(new chroot_block_device(*this)); |
---|
| 83 | psess->clone_session_setup(session, session_id, user, root); |
---|
| 84 | |
---|
| 85 | return session; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | sbuild::chroot::ptr |
---|
| 89 | chroot_block_device::clone_source () const |
---|
| 90 | { |
---|
| 91 | ptr clone; |
---|
| 92 | |
---|
| 93 | #ifdef SBUILD_FEATURE_UNION |
---|
| 94 | chroot_facet_union::const_ptr puni(get_facet<chroot_facet_union>()); |
---|
| 95 | assert(puni); |
---|
| 96 | |
---|
| 97 | if (puni->get_union_configured()) |
---|
| 98 | { |
---|
| 99 | clone = ptr(new chroot_block_device(*this)); |
---|
| 100 | puni->clone_source_setup(clone); |
---|
| 101 | } |
---|
| 102 | #endif // SBUILD_FEATURE_UNION |
---|
| 103 | |
---|
| 104 | return clone; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void |
---|
| 108 | chroot_block_device::setup_env (chroot const& chroot, |
---|
| 109 | environment& env) const |
---|
| 110 | { |
---|
| 111 | chroot_block_device_base::setup_env(chroot, env); |
---|
| 112 | } |
---|
| 113 | |
---|
[24314] | 114 | void |
---|
| 115 | chroot_block_device::setup_lock (chroot::setup_type type, |
---|
| 116 | bool lock, |
---|
| 117 | int status) |
---|
| 118 | { |
---|
| 119 | /* Lock is preserved through the entire session. */ |
---|
| 120 | if ((type == SETUP_START && lock == false) || |
---|
| 121 | (type == SETUP_STOP && lock == true)) |
---|
| 122 | return; |
---|
| 123 | |
---|
| 124 | try |
---|
| 125 | { |
---|
| 126 | if (!stat(this->get_device()).is_block()) |
---|
| 127 | { |
---|
| 128 | throw error(get_device(), DEVICE_NOTBLOCK); |
---|
| 129 | } |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | #ifdef SBUILD_FEATURE_UNION |
---|
| 133 | /* We don't lock the device if union is configured. */ |
---|
| 134 | const chroot *base = dynamic_cast<const chroot *>(this); |
---|
| 135 | assert(base); |
---|
| 136 | chroot_facet_union::const_ptr puni |
---|
| 137 | (base->get_facet<chroot_facet_union>()); |
---|
| 138 | assert(puni); |
---|
| 139 | if (!puni->get_union_configured()) |
---|
| 140 | #endif |
---|
| 141 | { |
---|
| 142 | sbuild::device_lock dlock(this->device); |
---|
| 143 | if (lock) |
---|
| 144 | { |
---|
| 145 | try |
---|
| 146 | { |
---|
| 147 | dlock.set_lock(lock::LOCK_EXCLUSIVE, 15); |
---|
| 148 | } |
---|
| 149 | catch (sbuild::lock::error const& e) |
---|
| 150 | { |
---|
| 151 | throw error(get_device(), DEVICE_LOCK, e); |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | else |
---|
| 155 | { |
---|
| 156 | try |
---|
| 157 | { |
---|
| 158 | dlock.unset_lock(); |
---|
| 159 | } |
---|
| 160 | catch (sbuild::lock::error const& e) |
---|
| 161 | { |
---|
| 162 | throw error(get_device(), DEVICE_UNLOCK, e); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | catch (sbuild::stat::error const& e) // Failed to stat |
---|
| 169 | { |
---|
| 170 | // Don't throw if stopping a session and the device stat |
---|
| 171 | // failed. This is because the setup scripts shouldn't fail |
---|
| 172 | // to be run if the block device no longer exists, which |
---|
| 173 | // would prevent the session from being ended. |
---|
| 174 | if (type != SETUP_STOP) |
---|
| 175 | throw; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /* Create or unlink session information. */ |
---|
| 179 | if ((type == SETUP_START && lock == true) || |
---|
| 180 | (type == SETUP_STOP && lock == false && status == 0)) |
---|
| 181 | { |
---|
| 182 | bool start = (type == SETUP_START); |
---|
| 183 | setup_session_info(start); |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
[24167] | 187 | sbuild::chroot::session_flags |
---|
| 188 | chroot_block_device::get_session_flags (chroot const& chroot) const |
---|
| 189 | { |
---|
| 190 | return chroot_block_device_base::get_session_flags(chroot); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | void |
---|
| 194 | chroot_block_device::get_details (chroot const& chroot, |
---|
| 195 | format_detail& detail) const |
---|
| 196 | { |
---|
| 197 | chroot_block_device_base::get_details(chroot, detail); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | void |
---|
| 201 | chroot_block_device::get_keyfile (chroot const& chroot, |
---|
| 202 | keyfile& keyfile) const |
---|
| 203 | { |
---|
| 204 | chroot_block_device_base::get_keyfile(chroot, keyfile); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | void |
---|
| 208 | chroot_block_device::set_keyfile (chroot& chroot, |
---|
| 209 | keyfile const& keyfile, |
---|
| 210 | string_list& used_keys) |
---|
| 211 | { |
---|
| 212 | chroot_block_device_base::set_keyfile(chroot, keyfile, used_keys); |
---|
| 213 | } |
---|