/usr/share/virt-manager/virtManager/createvol.py is in virt-manager 1:1.5.1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | #
# Copyright (C) 2008, 2013, 2014 Red Hat, Inc.
# Copyright (C) 2008 Cole Robinson <crobinso@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
#
import logging
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Gdk
from virtinst import StorageVolume
from . import uiutil
from .baseclass import vmmGObjectUI
from .asyncjob import vmmAsyncJob
class vmmCreateVolume(vmmGObjectUI):
__gsignals__ = {
"vol-created": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
}
def __init__(self, conn, parent_pool):
vmmGObjectUI.__init__(self, "createvol.ui", "vmm-create-vol")
self.conn = conn
self.parent_pool = parent_pool
self.name_hint = None
self.vol = None
self.storage_browser = None
self.builder.connect_signals({
"on_vmm_create_vol_delete_event": self.close,
"on_vol_cancel_clicked": self.close,
"on_vol_create_clicked": self.finish,
"on_vol_name_changed": self.vol_name_changed,
"on_vol_format_changed": self.vol_format_changed,
"on_backing_store_changed": self._show_alloc,
"on_vol_allocation_value_changed": self.vol_allocation_changed,
"on_vol_capacity_value_changed": self.vol_capacity_changed,
"on_backing_browse_clicked": self.browse_backing,
})
self.bind_escape_key_close()
self._init_state()
self.reset_state()
def show(self, parent):
try:
parent_xml = self.parent_pool.xmlobj.get_xml_config()
except Exception:
logging.debug("Error getting parent_pool xml", exc_info=True)
parent_xml = None
logging.debug("Showing new volume wizard for parent_pool=\n%s",
parent_xml)
self.reset_state()
self.topwin.set_transient_for(parent)
self.topwin.present()
def close(self, ignore1=None, ignore2=None):
logging.debug("Closing new volume wizard")
self.topwin.hide()
if self.storage_browser:
self.storage_browser.close()
self.set_modal(False)
return 1
def _cleanup(self):
self.conn = None
self.parent_pool = None
if self.storage_browser:
self.storage_browser.cleanup()
self.storage_browser = None
def set_name_hint(self, hint):
self.name_hint = hint
def set_modal(self, modal):
self.topwin.set_modal(bool(modal))
def set_parent_pool(self, conn, pool):
self.conn = conn
self.parent_pool = pool
def default_vol_name(self):
if not self.name_hint:
return ""
suffix = self.default_suffix()
ret = ""
try:
ret = StorageVolume.find_free_name(
self.parent_pool.get_backend(), self.name_hint, suffix=suffix)
except Exception:
logging.exception("Error finding a default vol name")
return ret
def default_suffix(self):
if self.vol.file_type != self.vol.TYPE_FILE:
return ""
return StorageVolume.get_file_extension_for_format(
self.get_config_format())
def _init_state(self):
blue = Gdk.color_parse("#0072A8")
self.widget("header").modify_bg(Gtk.StateType.NORMAL, blue)
format_list = self.widget("vol-format")
format_model = Gtk.ListStore(str, str)
format_list.set_model(format_model)
uiutil.init_combo_text_column(format_list, 1)
def _make_stub_vol(self):
self.vol = StorageVolume(self.conn.get_backend())
self.vol.pool = self.parent_pool.get_backend()
def _can_only_sparse(self):
if self.get_config_format() == "qcow2":
return True
if (self.widget("backing-store").is_visible() and
self.widget("backing-store").get_text()):
return True
return False
def _can_alloc(self):
if self._can_only_sparse():
return False
if self.parent_pool.get_type() == "logical":
# Sparse LVM volumes don't auto grow, so alloc=0 is useless
return False
return True
def _show_alloc(self, *args, **kwargs):
ignore = args
ignore = kwargs
uiutil.set_grid_row_visible(
self.widget("vol-allocation"), self._can_alloc())
def _can_backing(self):
if self.parent_pool.get_type() == "logical":
return True
if self.get_config_format() == "qcow2":
return True
return False
def _show_backing(self):
uiutil.set_grid_row_visible(
self.widget("backing-expander"), self._can_backing())
def reset_state(self):
self._make_stub_vol()
self.widget("vol-name").set_text(self.default_vol_name() or "")
self.widget("vol-name").grab_focus()
self.vol_name_changed(self.widget("vol-name"))
self.populate_vol_format()
hasformat = bool(len(self.vol.list_formats()))
uiutil.set_grid_row_visible(self.widget("vol-format"), hasformat)
if hasformat:
# Select the default storage format
self.widget("vol-format").set_active(0)
default = self.conn.get_default_storage_format()
for row in self.widget("vol-format").get_model():
if row[0] == default:
self.widget("vol-format").set_active_iter(row.iter)
break
default_alloc = 0
default_cap = 20
self.widget("backing-store").set_text("")
alloc = default_alloc
if not self._can_alloc():
alloc = default_cap
self._show_alloc()
self._show_backing()
self.widget("backing-expander").set_expanded(False)
self.widget("vol-allocation").set_range(0,
int(self.parent_pool.get_available() / 1024 / 1024 / 1024))
self.widget("vol-allocation").set_value(alloc)
self.widget("vol-capacity").set_range(0.1, 1000000)
self.widget("vol-capacity").set_value(default_cap)
self.widget("vol-parent-name").set_markup(
"<b>" + self.parent_pool.get_name() + "'s</b>")
self.widget("vol-parent-space").set_text(
self.parent_pool.get_pretty_available())
def get_config_format(self):
return uiutil.get_list_selection(self.widget("vol-format"))
def populate_vol_format(self):
stable_whitelist = ["raw", "qcow2", "qed"]
model = self.widget("vol-format").get_model()
model.clear()
formats = self.vol.list_formats()
if self.vol.list_create_formats() is not None:
formats = self.vol.list_create_formats()
if (self.vol.file_type == self.vol.TYPE_FILE and
self.conn.stable_defaults()):
newfmts = []
for f in stable_whitelist:
if f in formats:
newfmts.append(f)
formats = newfmts
for f in formats:
model.append([f, f])
def vol_name_changed(self, src):
text = src.get_text()
suffix = self.default_suffix()
if "." in text:
suffix = ""
self.widget("vol-name-suffix").set_text(suffix)
self.widget("vol-create").set_sensitive(bool(text))
def vol_allocation_changed(self, src):
cap_widget = self.widget("vol-capacity")
alloc = src.get_value()
cap = cap_widget.get_value()
if alloc > cap:
cap_widget.set_value(alloc)
def vol_capacity_changed(self, src):
alloc_widget = self.widget("vol-allocation")
cap = src.get_value()
alloc = self.widget("vol-allocation").get_value()
if cap < alloc:
alloc_widget.set_value(cap)
def vol_format_changed(self, src):
ignore = src
self._show_alloc()
self._show_backing()
self.widget("vol-name").emit("changed")
def browse_backing(self, src):
ignore = src
self._browse_file()
def _signal_vol_created(self, pool, volname):
self.emit("vol-created", pool.get_connkey(), volname)
def _finish_cb(self, error, details):
self.reset_finish_cursor()
if error:
error = _("Error creating vol: %s") % error
self.show_err(error,
details=details)
else:
self.parent_pool.connect("refreshed", self._signal_vol_created,
self.vol.name)
self.idle_add(self.parent_pool.refresh)
self.close()
def finish(self, src_ignore):
try:
if not self.validate():
return
except Exception as e:
self.show_err(_("Uncaught error validating input: %s") % str(e))
return
self.set_finish_cursor()
progWin = vmmAsyncJob(self._async_vol_create, [],
self._finish_cb, [],
_("Creating storage volume..."),
_("Creating the storage volume may take a "
"while..."),
self.topwin)
progWin.run()
def _async_vol_create(self, asyncjob):
conn = self.conn.get_backend()
# Lookup different pool obj
newpool = conn.storagePoolLookupByName(self.parent_pool.get_name())
self.vol.pool = newpool
meter = asyncjob.get_meter()
logging.debug("Starting backround vol creation.")
self.vol.install(meter=meter)
logging.debug("vol creation complete.")
def validate(self):
name = self.widget("vol-name").get_text()
suffix = self.widget("vol-name-suffix").get_text()
volname = name + suffix
fmt = self.get_config_format()
alloc = self.widget("vol-allocation").get_value()
cap = self.widget("vol-capacity").get_value()
backing = self.widget("backing-store").get_text()
if not self.widget("vol-allocation").get_visible():
alloc = cap
if self._can_only_sparse():
alloc = 0
try:
self._make_stub_vol()
self.vol.name = volname
self.vol.capacity = (cap * 1024 * 1024 * 1024)
self.vol.allocation = (alloc * 1024 * 1024 * 1024)
if backing:
self.vol.backing_store = backing
if fmt:
self.vol.format = fmt
self.vol.validate()
except ValueError as e:
return self.val_err(_("Volume Parameter Error"), e)
return True
def show_err(self, info, details=None):
self.err.show_err(info, details, modal=self.topwin.get_modal())
def val_err(self, info, details):
return self.err.val_err(info, details, modal=self.topwin.get_modal())
def _browse_file(self):
if self.storage_browser and self.storage_browser.conn != self.conn:
self.storage_browser.cleanup()
self.storage_browser = None
if self.storage_browser is None:
def cb(src, text):
ignore = src
self.widget("backing-store").set_text(text)
from .storagebrowse import vmmStorageBrowser
self.storage_browser = vmmStorageBrowser(self.conn)
self.storage_browser.set_finish_cb(cb)
self.storage_browser.topwin.set_modal(self.topwin.get_modal())
self.storage_browser.set_browse_reason(
self.config.CONFIG_DIR_IMAGE)
self.storage_browser.show(self.topwin)
|