2013-03-18 01:06:52 +04:00
#
2013-10-28 00:59:46 +04:00
# Copyright 2011, 2013 Red Hat, Inc.
2013-03-18 01:06:52 +04:00
#
2018-04-04 16:35:41 +03:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 22:00:02 +03:00
# See the COPYING file in the top-level directory.
2013-03-18 01:06:52 +04:00
import os
2018-03-20 19:18:35 +03:00
from . device import Device
2018-03-20 19:27:37 +03:00
from . . xmlbuilder import XMLProperty
2013-03-18 01:06:52 +04:00
2013-04-13 22:34:52 +04:00
2018-03-20 19:18:35 +03:00
class DeviceFilesystem ( Device ) :
2018-03-21 17:53:34 +03:00
XML_NAME = " filesystem "
2021-01-25 02:45:07 +03:00
_XML_PROP_ORDER = [ " _type_prop " , " accessmode " , " fmode " , " dmode " ]
2013-03-18 01:06:52 +04:00
TYPE_MOUNT = " mount "
TYPE_TEMPLATE = " template "
TYPE_FILE = " file "
TYPE_BLOCK = " block "
2014-01-21 13:05:28 +04:00
TYPE_RAM = " ram "
2013-03-18 01:06:52 +04:00
MODE_MAPPED = " mapped "
MODE_SQUASH = " squash "
2014-01-21 13:05:29 +04:00
DRIVER_LOOP = " loop "
DRIVER_NBD = " nbd "
2018-09-02 04:58:46 +03:00
_type_prop = XMLProperty ( " ./@type " )
accessmode = XMLProperty ( " ./@accessmode " )
2020-09-10 20:06:41 +03:00
model = XMLProperty ( " ./@model " )
multidevs = XMLProperty ( " ./@multidevs " )
2021-01-25 02:45:07 +03:00
fmode = XMLProperty ( " ./@fmode " )
dmode = XMLProperty ( " ./@dmode " )
readonly = XMLProperty ( " ./readonly " , is_bool = True )
2020-09-10 20:06:41 +03:00
space_hard_limit = XMLProperty ( " ./space_hard_limit " )
space_soft_limit = XMLProperty ( " ./space_soft_limit " )
driver_wrpolicy = XMLProperty ( " ./driver/@wrpolicy " )
driver_type = XMLProperty ( " ./driver/@type " )
driver_format = XMLProperty ( " ./driver/@format " )
driver_queue = XMLProperty ( " ./driver/@queue " )
driver_name = XMLProperty ( " ./driver/@name " )
target_dir = XMLProperty ( " ./target/@dir " )
source_dir = XMLProperty ( " ./source/@dir " )
source_name = XMLProperty ( " ./source/@name " )
source_file = XMLProperty ( " ./source/@file " )
source_dev = XMLProperty ( " ./source/@dev " )
source_usage = XMLProperty ( " ./source/@usage " )
source_units = XMLProperty ( " ./source/@units " )
source_pool = XMLProperty ( " ./source/@pool " )
source_volume = XMLProperty ( " ./source/@volume " )
2023-10-23 22:42:19 +03:00
source_socket = XMLProperty ( " ./source/@socket " )
2020-09-10 20:06:41 +03:00
binary_path = XMLProperty ( " ./binary/@path " )
binary_xattr = XMLProperty ( " ./binary/@xattr " , is_onoff = True )
binary_cache_mode = XMLProperty ( " ./binary/cache/@mode " )
binary_lock_posix = XMLProperty ( " ./binary/lock/@posix " , is_onoff = True )
binary_lock_flock = XMLProperty ( " ./binary/lock/@flock " , is_onoff = True )
2021-07-30 20:15:29 +03:00
binary_sandbox_mode = XMLProperty ( " ./binary/sandbox/@mode " )
2013-07-16 04:53:46 +04:00
2015-05-06 20:54:00 +03:00
def _type_to_source_prop ( self ) :
2018-03-20 19:18:35 +03:00
if self . type == DeviceFilesystem . TYPE_TEMPLATE :
2020-09-10 20:06:41 +03:00
return " source_name "
2018-03-20 19:18:35 +03:00
elif self . type == DeviceFilesystem . TYPE_FILE :
2020-09-10 20:06:41 +03:00
return " source_file "
2018-03-20 19:18:35 +03:00
elif self . type == DeviceFilesystem . TYPE_BLOCK :
2020-09-10 20:06:41 +03:00
return " source_dev "
2018-03-20 19:18:35 +03:00
elif self . type == DeviceFilesystem . TYPE_RAM :
2020-09-10 20:06:41 +03:00
return " source_usage "
2015-05-06 20:54:00 +03:00
else :
2020-09-10 20:06:41 +03:00
return " source_dir "
2015-05-06 20:54:00 +03:00
def _get_source ( self ) :
return getattr ( self , self . _type_to_source_prop ( ) )
def _set_source ( self , val ) :
return setattr ( self , self . _type_to_source_prop ( ) , val )
source = property ( _get_source , _set_source )
2020-09-10 20:06:41 +03:00
def _get_target ( self ) :
return self . target_dir
def _set_target ( self , val ) :
self . target_dir = val
target = property ( _get_target , _set_target )
2017-06-19 11:18:47 +03:00
def _get_type ( self ) :
return getattr ( self , ' _type_prop ' )
def _set_type ( self , val ) :
2018-05-21 22:42:50 +03:00
# Get type/value of the attribute of "source" property
2017-06-19 11:18:47 +03:00
old_source_type = self . _type_to_source_prop ( )
old_source_value = self . source
# Update "type" property
new_type = setattr ( self , ' _type_prop ' , val )
# If the attribute type of 'source' property has changed
# restore the value
if old_source_type != self . _type_to_source_prop ( ) :
self . source = old_source_value
return new_type
type = property ( _get_type , _set_type )
2018-09-04 00:03:02 +03:00
##############
# Validation #
##############
def validate_target ( self , target ) :
# In case of qemu for default fs type (mount) target is not
# actually a directory, it is merely a arbitrary string tag
# that is exported to the guest as a hint for where to mount
if ( ( self . conn . is_qemu ( ) or self . conn . is_test ( ) ) and
( self . type is None or
self . type == self . TYPE_MOUNT ) ) :
return
if not os . path . isabs ( target ) :
raise ValueError ( _ ( " Filesystem target ' %s ' must be an absolute "
" path " ) % target )
def validate ( self ) :
if self . target :
self . validate_target ( self . target )
2022-02-19 17:49:01 +03:00
def default_accessmode ( self ) :
if self . driver_type == " virtiofs " :
# let libvirt fill in default accessmode=passthrough
return None
# libvirt qemu defaults to accessmode=passthrough, but that
# really only works well for qemu running as root, which is
# not the common case. so use mode=mapped
return self . MODE_MAPPED
2018-09-04 00:03:02 +03:00
##################
# Default config #
##################
2015-09-21 03:33:46 +03:00
def set_defaults ( self , guest ) :
ignore = guest
2022-02-19 17:23:47 +03:00
if not ( self . conn . is_qemu ( ) or
self . conn . is_lxc ( ) or
self . conn . is_test ( ) ) :
return
# type=mount is the libvirt default. But hardcode it since other
# bits like validation depend on it
if self . type is None :
self . type = self . TYPE_MOUNT
if self . accessmode is None :
2022-02-19 17:49:01 +03:00
self . accessmode = self . default_accessmode ( )