1# 2# Copyright 2011, 2013 Red Hat, Inc. 3# 4# This work is licensed under the GNU GPLv2 or later. 5# See the COPYING file in the top-level directory. 6 7import os 8 9from .device import Device 10from ..xmlbuilder import XMLProperty 11 12 13class DeviceFilesystem(Device): 14 XML_NAME = "filesystem" 15 16 TYPE_MOUNT = "mount" 17 TYPE_TEMPLATE = "template" 18 TYPE_FILE = "file" 19 TYPE_BLOCK = "block" 20 TYPE_RAM = "ram" 21 22 MODE_MAPPED = "mapped" 23 MODE_SQUASH = "squash" 24 25 DRIVER_LOOP = "loop" 26 DRIVER_NBD = "nbd" 27 28 _type_prop = XMLProperty("./@type") 29 accessmode = XMLProperty("./@accessmode") 30 model = XMLProperty("./@model") 31 readonly = XMLProperty("./readonly", is_bool=True) 32 multidevs = XMLProperty("./@multidevs") 33 space_hard_limit = XMLProperty("./space_hard_limit") 34 space_soft_limit = XMLProperty("./space_soft_limit") 35 36 driver_wrpolicy = XMLProperty("./driver/@wrpolicy") 37 driver_type = XMLProperty("./driver/@type") 38 driver_format = XMLProperty("./driver/@format") 39 driver_queue = XMLProperty("./driver/@queue") 40 driver_name = XMLProperty("./driver/@name") 41 42 target_dir = XMLProperty("./target/@dir") 43 44 source_dir = XMLProperty("./source/@dir") 45 source_name = XMLProperty("./source/@name") 46 source_file = XMLProperty("./source/@file") 47 source_dev = XMLProperty("./source/@dev") 48 source_usage = XMLProperty("./source/@usage") 49 source_units = XMLProperty("./source/@units") 50 source_pool = XMLProperty("./source/@pool") 51 source_volume = XMLProperty("./source/@volume") 52 53 binary_path = XMLProperty("./binary/@path") 54 binary_xattr = XMLProperty("./binary/@xattr", is_onoff=True) 55 binary_cache_mode = XMLProperty("./binary/cache/@mode") 56 binary_lock_posix = XMLProperty("./binary/lock/@posix", is_onoff=True) 57 binary_lock_flock = XMLProperty("./binary/lock/@flock", is_onoff=True) 58 59 def _type_to_source_prop(self): 60 if self.type == DeviceFilesystem.TYPE_TEMPLATE: 61 return "source_name" 62 elif self.type == DeviceFilesystem.TYPE_FILE: 63 return "source_file" 64 elif self.type == DeviceFilesystem.TYPE_BLOCK: 65 return "source_dev" 66 elif self.type == DeviceFilesystem.TYPE_RAM: 67 return "source_usage" 68 else: 69 return "source_dir" 70 71 def _get_source(self): 72 return getattr(self, self._type_to_source_prop()) 73 def _set_source(self, val): 74 return setattr(self, self._type_to_source_prop(), val) 75 source = property(_get_source, _set_source) 76 77 def _get_target(self): 78 return self.target_dir 79 def _set_target(self, val): 80 self.target_dir = val 81 target = property(_get_target, _set_target) 82 83 def _get_type(self): 84 return getattr(self, '_type_prop') 85 def _set_type(self, val): 86 # Get type/value of the attribute of "source" property 87 old_source_type = self._type_to_source_prop() 88 old_source_value = self.source 89 90 # Update "type" property 91 new_type = setattr(self, '_type_prop', val) 92 93 # If the attribute type of 'source' property has changed 94 # restore the value 95 if old_source_type != self._type_to_source_prop(): 96 self.source = old_source_value 97 98 return new_type 99 100 type = property(_get_type, _set_type) 101 102 103 ############## 104 # Validation # 105 ############## 106 107 def validate_target(self, target): 108 # In case of qemu for default fs type (mount) target is not 109 # actually a directory, it is merely a arbitrary string tag 110 # that is exported to the guest as a hint for where to mount 111 if ((self.conn.is_qemu() or self.conn.is_test()) and 112 (self.type is None or 113 self.type == self.TYPE_MOUNT)): 114 return 115 116 if not os.path.isabs(target): 117 raise ValueError(_("Filesystem target '%s' must be an absolute " 118 "path") % target) 119 120 def validate(self): 121 if self.target: 122 self.validate_target(self.target) 123 124 125 ################## 126 # Default config # 127 ################## 128 129 def set_defaults(self, guest): 130 ignore = guest 131 132 if self.conn.is_qemu() or self.conn.is_lxc() or self.conn.is_test(): 133 # type=mount is the libvirt default. But hardcode it 134 # here since we need it for the accessmode check 135 if self.type is None: 136 self.type = self.TYPE_MOUNT 137 138 # libvirt qemu defaults to accessmode=passthrough, but that 139 # really only works well for qemu running as root, which is 140 # not the common case. so use mode=mapped 141 if self.accessmode is None: 142 self.accessmode = self.MODE_MAPPED 143