/* -------------------------------------------------------------------------- */ /* Copyright 2002-2019, OpenNebula Project, OpenNebula Systems */ /* */ /* Licensed under the Apache License, Version 2.0 (the "License"); you may */ /* not use this file except in compliance with the License. You may obtain */ /* a copy of the License at */ /* */ /* http://www.apache.org/licenses/LICENSE-2.0 */ /* */ /* Unless required by applicable law or agreed to in writing, software */ /* distributed under the License is distributed on an "AS IS" BASIS, */ /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ /* See the License for the specific language governing permissions and */ /* limitations under the License. */ /* -------------------------------------------------------------------------- */ #include "VirtualMachineDisk.h" #include "NebulaUtil.h" #include "Nebula.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ bool VirtualMachineDisk::is_volatile() const { std::string type = vector_value("TYPE"); one_util::toupper(type); return ( type == "SWAP" || type == "FS"); } /* -------------------------------------------------------------------------- */ string VirtualMachineDisk::get_tm_target() const { bool clone; std::string target; if (vector_value("CLONE", clone) != 0) { return ""; } if (clone) { target = vector_value("CLONE_TARGET"); } else { target = vector_value("LN_TARGET"); } return one_util::toupper(target); } /* -------------------------------------------------------------------------- */ int VirtualMachineDisk::get_uid(int _uid) { istringstream is; string uid_s ; string uname; int uid; if (!(uid_s = vector_value("IMAGE_UID")).empty()) { is.str(uid_s); is >> uid; if( is.fail() ) { return -1; } } else if (!(uname = vector_value("IMAGE_UNAME")).empty()) { User * user; Nebula& nd = Nebula::instance(); UserPool * upool = nd.get_upool(); user = upool->get_ro(uname); if ( user == 0 ) { return -1; } uid = user->get_oid(); user->unlock(); } else { uid = _uid; } return uid; } /* -------------------------------------------------------------------------- */ int VirtualMachineDisk::get_image_id(int &id, int uid) { int iid; string iname; if ( vector_value("IMAGE_ID", iid) == 0 ) { id = iid; return 0; } else if ( vector_value("IMAGE", iname) == 0 ) { ImagePool * ipool = Nebula::instance().get_ipool(); int uiid = get_uid(uid); if ( uiid == -1) { return -1; } Image * image = ipool->get_ro(iname, uiid); if ( image != 0 ) { id = image->get_oid(); image->unlock(); } return 0; } return -1; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ string VirtualMachineDisk::get_tm_mad_system() { std::string tm_mad_system; if (vector_value("TM_MAD_SYSTEM", tm_mad_system) != 0) { return ""; } return tm_mad_system; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ void VirtualMachineDisk::extended_info(int uid) { ImagePool * ipool = Nebula::instance().get_ipool(); ipool->disk_attribute(this, get_id(), uid); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ void VirtualMachineDisk::authorize(int uid, AuthRequest* ar, bool check_lock) { string source; Image * img = 0; int iid; PoolObjectAuth perm; ImagePool * ipool = Nebula::instance().get_ipool(); if ( vector_value("IMAGE", source) == 0 ) { int uiid = get_uid(uid); if ( uiid == -1) { return; } img = ipool->get_ro(source , uiid); if ( img != 0 ) { replace("IMAGE_ID", img->get_oid()); } } else if ( vector_value("IMAGE_ID", iid) == 0 ) { img = ipool->get_ro(iid); } if (img == 0) { return; } img->get_permissions(perm); img->unlock(); //cloning disks can be used with lock, lcm will track image state updates. if (is_cloning() || !check_lock) { ar->add_auth(AuthRequest::USE_NO_LCK, perm); } else { ar->add_auth(AuthRequest::USE, perm); } } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ int VirtualMachineDisk::create_snapshot(const string& name, string& error) { long long size_mb, snap_size; int snap_id; bool ro; if (is_volatile()) { error = "Cannot make snapshots on volatile disks"; return -1; } if ( vector_value("SIZE", size_mb) != 0 ) { error = "Wrong size in disk"; return -1; } if ((vector_value("READONLY", ro) == 0) && ro == true ) { error = "Cannot make snapshots on readonly disks"; return -1; } if ( snapshots == 0 ) { snapshots = new Snapshots(get_id(), allow_orphans()); snap_id = snapshots->create_snapshot(name, size_mb); snap_size = size_mb; if (snap_id == -1) { snapshots = 0; delete snapshots; } } else { snap_id = snapshots->create_snapshot(name, size_mb); snap_size = snapshots->get_total_size(); } if (snap_id != -1) { replace("DISK_SNAPSHOT_ACTIVE", "YES"); replace("DISK_SNAPSHOT_ID", snap_id); replace("DISK_SNAPSHOT_TOTAL_SIZE", snap_size); } return snap_id; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ int VirtualMachineDisk::revert_snapshot(int snap_id, bool revert) { if ( snapshots == 0 ) { return -1; } return snapshots->active_snapshot(snap_id, revert); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ // +--------+-------------------------------------+ // |LN/CLONE| PERSISTENT | NO PERSISTENT | // | |---------+---------+-----------------+ // | TARGET | created | quota | created | quota | // +--------+---------+---------+-----------------+ // | SYSTEM | system | VM + DS | system | VM | // | SELF | image | DS | image | DS | // | NONE | image | DS | image | DS | // +----------------------------------------------+ /* -------------------------------------------------------------------------- */ void VirtualMachineDisk::delete_snapshot(int snap_id, Template **ds_quotas, Template **vm_quotas, bool& img_owner, bool& vm_owner) { vm_owner = false; img_owner = false; if ( snapshots == 0 ) { return; } long long ssize = snapshots->get_snapshot_size(snap_id); snapshots->delete_snapshot(snap_id); long long snap_size = snapshots->get_total_size(); replace("DISK_SNAPSHOT_TOTAL_SIZE", snap_size); if (snapshots->size() == 0) { delete snapshots; snapshots = 0; } string tm_target = get_tm_target(); vm_owner = tm_target == "SELF"; img_owner = is_persistent() || tm_target == "NONE"; if ( img_owner || vm_owner ) { *ds_quotas = new Template(); (*ds_quotas)->add("DATASTORE", vector_value("DATASTORE_ID")); (*ds_quotas)->add("SIZE", ssize); (*ds_quotas)->add("IMAGES",0 ); } if (tm_target == "SYSTEM") { *vm_quotas = new Template(); VectorAttribute * delta_disk = new VectorAttribute("DISK"); delta_disk->replace("TYPE", "FS"); delta_disk->replace("SIZE", ssize); (*vm_quotas)->add("VMS", 0); (*vm_quotas)->set(delta_disk); } } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ long long VirtualMachineDisk::system_ds_size() { long long disk_sz, snapshot_sz = 0; if ( vector_value("SIZE", disk_sz) != 0 ) { return 0; } //Volatile disks don't have snapshots if (vector_value("DISK_SNAPSHOT_TOTAL_SIZE", snapshot_sz) == 0) { disk_sz += snapshot_sz; } if ( is_volatile() || get_tm_target() == "SYSTEM" ) { return disk_sz; } return 0; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ long long VirtualMachineDisk::image_ds_size() { long long disk_sz, snapshot_sz = 0; string tm_target = get_tm_target(); if ( get_tm_target() != "SELF" ) { return 0; } if ( vector_value("SIZE", disk_sz) != 0 ) { return 0; } if ( vector_value("DISK_SNAPSHOT_TOTAL_SIZE", snapshot_sz) == 0 ) { disk_sz += snapshot_sz; } return disk_sz; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ // Owner to update ds usage quotas // // +--------+-------------------------------------+ // |LN/CLONE| PERSISTENT | NO PERSISTENT | // | |---------+---------+-----------------+ // | TARGET | created | quota | created | quota | // +--------+---------+---------+-----------------+ // | SYSTEM | system | IMG | system | - | // | SELF | image | IMG+VM | image | VM | // | NONE | image | IMG | image | IMG | // +----------------------------------------------+ /* -------------------------------------------------------------------------- */ void VirtualMachineDisk::resize_quotas(long long new_size, Template& ds_deltas, Template& vm_deltas, bool& do_img_owner, bool& do_vm_owner) { long long current_size, delta_size; do_vm_owner = false; do_img_owner= false; if ( vector_value("SIZE", current_size) != 0 ) { return; } delta_size = new_size - current_size; //Quotas uses del operation to substract counters, delta needs to be > 0 if ( delta_size < 0 ) { delta_size = - delta_size; } string tm = get_tm_target(); do_vm_owner = !is_volatile() && tm == "SELF"; do_img_owner = !is_volatile() && (is_persistent() || tm == "NONE"); bool is_system = tm == "SYSTEM"; string ds_id = vector_value("DATASTORE_ID"); if ( do_vm_owner || do_img_owner ) { ds_deltas.add("DATASTORE", ds_id); ds_deltas.add("SIZE", delta_size); ds_deltas.add("IMAGES", 0); } if ( is_volatile() || is_system ) { VectorAttribute * delta_disk = new VectorAttribute("DISK"); delta_disk->replace("TYPE", "FS"); delta_disk->replace("SIZE", delta_size); vm_deltas.add("VMS", 0); vm_deltas.set(delta_disk); } } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ void VirtualMachineDisk::datastore_sizes(int& ds_id, long long& image_sz, long long& system_sz) { long long tmp_size, snapshot_size; image_sz = 0; system_sz = 0; ds_id = -1; if ( vector_value("SIZE", tmp_size) != 0 ) { return; } if ( vector_value("DISK_SNAPSHOT_TOTAL_SIZE", snapshot_size) == 0 ) { tmp_size += snapshot_size; } if ( is_volatile() ) { system_sz = tmp_size; return; } else { string target = get_tm_target(); if ( target == "SYSTEM" ) { system_sz = tmp_size; } else if ( target == "SELF" ) { vector_value("DATASTORE_ID", ds_id); image_sz = tmp_size; }// else if ( target == "NONE" ) } } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ void VirtualMachineDisk::clear_resize(bool restore) { string size, size_prev; if ( restore && vector_value("SIZE_PREV", size_prev) == 0 ) { replace("SIZE", size_prev); } remove("SIZE_PREV"); clear_resize(); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ void VirtualMachineDisk::set_types(const string& ds_name) { string type = vector_value("TYPE"); switch(Image::str_to_disk_type(type)) { case Image::RBD_CDROM: case Image::GLUSTER_CDROM: case Image::SHEEPDOG_CDROM: case Image::CD_ROM: if (ds_name != "FILE" && ds_name != "ISCSI" && ds_name != "NONE") { replace("TYPE", ds_name+"_CDROM"); } else { replace("TYPE", "CDROM"); } break; default: replace("TYPE", ds_name); break; } replace("DISK_TYPE", ds_name); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ #define XML_DISK_ATTR(Y,X) ( Y << "<" << X << ">" << \ one_util::escape_xml(vector_value(X)) << "") void VirtualMachineDisk::to_xml_short(std::ostringstream& oss) const { oss << "" ; XML_DISK_ATTR(oss, "DISK_ID"); XML_DISK_ATTR(oss, "DATASTORE"); XML_DISK_ATTR(oss, "DATASTORE_ID"); XML_DISK_ATTR(oss, "IMAGE"); XML_DISK_ATTR(oss, "IMAGE_ID"); XML_DISK_ATTR(oss, "SIZE"); XML_DISK_ATTR(oss, "TYPE"); XML_DISK_ATTR(oss, "CLONE"); XML_DISK_ATTR(oss, "CLONE_TARGET"); XML_DISK_ATTR(oss, "LN_TARGET"); XML_DISK_ATTR(oss, "DISK_SNAPSHOT_TOTAL_SIZE"); oss << ""; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* VIRTUALMACHINEDISKS */ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ const char * VirtualMachineDisks::DISK_NAME = "DISK"; const char * VirtualMachineDisks::DISK_ID_NAME = "DISK_ID"; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ long long VirtualMachineDisks::system_ds_size() { long long size = 0; for ( disk_iterator disk = begin() ; disk != end() ; ++disk ) { size += (*disk)->system_ds_size(); } return size; } long long VirtualMachineDisks::system_ds_size(Template * ds_tmpl) { VirtualMachineDisks disks(ds_tmpl, false); return disks.system_ds_size(); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ void VirtualMachineDisks::image_ds_quotas(Template * tmpl, vector