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
# Cole Robinson <crobinso@redhat.com>
#
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 "
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_PASSTHROUGH = " passthrough "
MODE_MAPPED = " mapped "
MODE_SQUASH = " squash "
2018-09-02 04:58:46 +03:00
MODES = [ MODE_PASSTHROUGH , MODE_MAPPED , MODE_SQUASH ]
2013-03-18 01:06:52 +04:00
WRPOLICY_IMM = " immediate "
2018-09-02 04:58:46 +03:00
WRPOLICIES = [ WRPOLICY_IMM ]
2013-03-18 01:06:52 +04:00
DRIVER_PATH = " path "
DRIVER_HANDLE = " handle "
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 " )
wrpolicy = XMLProperty ( " ./driver/@wrpolicy " )
driver = XMLProperty ( " ./driver/@type " )
2014-01-21 13:05:29 +04:00
format = XMLProperty ( " ./driver/@format " )
2013-07-16 04:53:46 +04:00
2013-09-19 21:27:30 +04:00
readonly = XMLProperty ( " ./readonly " , is_bool = True )
2013-07-16 04:53:46 +04:00
2014-01-21 13:05:28 +04:00
units = XMLProperty ( " ./source/@units " )
2013-07-16 04:53:46 +04:00
def _validate_set_target ( self , val ) :
2013-03-18 01:06:52 +04:00
# 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
2015-09-21 03:33:46 +03:00
if ( ( self . conn . is_qemu ( ) or self . conn . is_test ( ) ) and
2014-04-22 00:51:23 +04:00
( self . type is None or
2013-03-18 01:06:52 +04:00
self . type == self . TYPE_MOUNT ) ) :
pass
elif not os . path . isabs ( val ) :
raise ValueError ( _ ( " Filesystem target ' %s ' must be an absolute "
" path " ) % val )
2013-07-16 04:53:46 +04:00
return val
2013-09-19 21:27:30 +04:00
target = XMLProperty ( " ./target/@dir " ,
2013-07-16 04:53:46 +04:00
set_converter = _validate_set_target )
2013-07-24 16:46:55 +04:00
2015-05-06 20:54:00 +03:00
_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 " )
def _type_to_source_prop ( self ) :
2018-03-20 19:18:35 +03:00
if self . type == DeviceFilesystem . TYPE_TEMPLATE :
2015-05-06 20:54:00 +03:00
return " _source_name "
2018-03-20 19:18:35 +03:00
elif self . type == DeviceFilesystem . TYPE_FILE :
2015-05-06 20:54:00 +03:00
return " _source_file "
2018-03-20 19:18:35 +03:00
elif self . type == DeviceFilesystem . TYPE_BLOCK :
2015-05-06 20:54:00 +03:00
return " _source_dev "
2018-03-20 19:18:35 +03:00
elif self . type == DeviceFilesystem . TYPE_RAM :
2015-05-06 20:54:00 +03:00
return " _source_usage "
else :
return " _source_dir "
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 )
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 )
2015-09-21 03:33:46 +03:00
def set_defaults ( self , guest ) :
ignore = guest
2017-06-19 11:18:48 +03:00
if self . conn . is_qemu ( ) or self . conn . is_lxc ( ) or self . conn . is_test ( ) :
# type=mount is the libvirt default. But hardcode it
2015-09-21 03:33:46 +03:00
# here since we need it for the accessmode check
2018-09-02 04:58:46 +03:00
if self . type is None :
2015-09-21 03:33:46 +03:00
self . type = self . TYPE_MOUNT
# 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
2018-09-02 04:58:46 +03:00
if self . accessmode is None :
2015-09-21 03:33:46 +03:00
self . accessmode = self . MODE_MAPPED