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>
#
# 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
2013-10-28 00:59:47 +04:00
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
2013-03-18 01:06:52 +04:00
#
# 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 os
2014-09-12 23:59:22 +04:00
from . device import VirtualDevice
from . xmlbuilder import XMLProperty
2013-03-18 01:06:52 +04:00
2013-04-13 22:34:52 +04:00
2013-04-11 03:48:07 +04:00
class VirtualFilesystem ( VirtualDevice ) :
2013-07-16 17:14:37 +04:00
virtual_device_type = VirtualDevice . VIRTUAL_DEV_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
TYPE_DEFAULT = " default "
2015-09-21 03:15:34 +03:00
TYPES = [ TYPE_MOUNT , TYPE_TEMPLATE , TYPE_FILE , TYPE_BLOCK , TYPE_RAM ,
TYPE_DEFAULT ]
2013-03-18 01:06:52 +04:00
MODE_PASSTHROUGH = " passthrough "
MODE_MAPPED = " mapped "
MODE_SQUASH = " squash "
MODE_DEFAULT = " default "
2013-07-16 04:53:46 +04:00
MODES = [ MODE_PASSTHROUGH , MODE_MAPPED , MODE_SQUASH , MODE_DEFAULT ]
2013-03-18 01:06:52 +04:00
WRPOLICY_IMM = " immediate "
WRPOLICY_DEFAULT = " default "
WRPOLICIES = [ WRPOLICY_IMM , WRPOLICY_DEFAULT ]
DRIVER_PATH = " path "
DRIVER_HANDLE = " handle "
2014-01-21 13:05:29 +04:00
DRIVER_LOOP = " loop "
DRIVER_NBD = " nbd "
2013-03-18 01:06:52 +04:00
DRIVER_DEFAULT = " default "
2015-09-21 03:15:34 +03:00
DRIVERS = [ DRIVER_PATH , DRIVER_HANDLE , DRIVER_LOOP , DRIVER_NBD ,
DRIVER_DEFAULT ]
2014-01-21 13:05:29 +04:00
2013-07-16 04:53:46 +04:00
2013-09-19 21:27:30 +04:00
type = XMLProperty ( " ./@type " ,
2013-07-16 04:53:46 +04:00
default_cb = lambda s : None ,
default_name = TYPE_DEFAULT )
2015-09-21 03:15:34 +03:00
accessmode = XMLProperty ( " ./@accessmode " ,
2013-07-16 04:53:46 +04:00
default_cb = lambda s : None ,
default_name = MODE_DEFAULT )
2013-09-19 21:27:30 +04:00
wrpolicy = XMLProperty ( " ./driver/@wrpolicy " ,
2013-07-16 04:53:46 +04:00
default_cb = lambda s : None ,
default_name = WRPOLICY_DEFAULT )
2013-09-19 21:27:30 +04:00
driver = XMLProperty ( " ./driver/@type " ,
2013-07-16 04:53:46 +04:00
default_cb = lambda s : None ,
default_name = DRIVER_DEFAULT )
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
2013-07-06 04:36:28 +04:00
if ( self . conn . is_qemu ( ) and
2014-04-22 00:51:23 +04:00
( self . type is None or
self . type == self . TYPE_DEFAULT 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 ) :
if self . type == VirtualFilesystem . TYPE_TEMPLATE :
return " _source_name "
elif self . type == VirtualFilesystem . TYPE_FILE :
return " _source_file "
elif self . type == VirtualFilesystem . TYPE_BLOCK :
return " _source_dev "
elif self . type == VirtualFilesystem . TYPE_RAM :
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 )
2013-07-24 16:46:55 +04:00
VirtualFilesystem . register_type ( )