Mini Shell

Direktori : /lib/python3.6/site-packages/blivet/devices/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/blivet/devices/__pycache__/btrfs.cpython-36.opt-1.pyc

3

�Mf�c�@sJddlZddlZddlZddlZejdd�ddlmZddlm	Z	ddlm
Z
ddlmZdd	l
m
Z
dd
lmZddlmZddlmZdd
lmZmZddlmZddlmZddlZejd�ZddlmZddlmZddl
m Z ddl!m"Z"ddl#m$Z$Gdd�de�Z%Gdd�de%ee �Z&Gdd�de%�Z'Gdd�de'�Z(dS)�N�BlockDevz2.0)r�)�btrfs)�raid)�errors)�flags)�log_method_call)�udev)�util)�
get_format�DeviceFormat)�Size)�mounts_cacheZblivet�)�
StorageDevice)�ContainerDevice)�
RaidDevice)�availability)�contextmanagercs�eZdZdZdZdgZejgZ�fdd�Z	dd�Z
d$d	d
�Z�fdd�Z�fd
d�Z
dd�Zdd�Zedd��Zedd��Zedd��Zed%dd��Zedd��Zedd��Ze�fd d!��Zd"d#�Z�ZS)&�BTRFSDevicez5 Base class for BTRFS volume and sub-volume devices. rzbtrfs-progscsX|s|drd|jf}|jd�dkr4tjd��|jdd�|_tt|�j||�dS)z? Passing None or no name means auto-generate one like btrfs.%d rzbtrfs.%d�parentsNz)BTRFSDevice must have at least one parent�size)	�id�getr�BTRFSValueError�popZreq_size�superr�__init__)�self�args�kwargs)�	__class__��/usr/lib/python3.6/btrfs.pyr>s
zBTRFSDevice.__init__cCsDt||j|jd�|jdj�|jdj|_tjd|j|j�dS)z" Update this device's sysfs path. )�statusrz%s sysfs_path set to %sN)r�namer$r�update_sysfs_path�
sysfs_path�log�debug)rr"r"r#r&IszBTRFSDevice.update_sysfs_pathNcCsdS)Nr")r�newsizer"r"r#�update_sizePszBTRFSDevice.update_sizecs$tt|�j�d|j_|j|j_dS)NT)rr�_post_create�format�exists�path�device)r)r!r"r#r,SszBTRFSDevice._post_createcstt|�j�|jdd�dS)z? Preparation and precondition checking for device destruction. T)�origN)rr�_pre_destroy�
setup_parents)r)r!r"r#r2XszBTRFSDevice._pre_destroycCstdd�|jD�td��S)Ncss|]}|jVqdS)N)r)�.0�dr"r"r#�	<genexpr>^sz(BTRFSDevice._get_size.<locals>.<genexpr>r)�sumrr
)rr"r"r#�	_get_size]szBTRFSDevice._get_sizecCstd��dS)Nz(cannot directly set size of btrfs volume)�RuntimeError)rr*r"r"r#�	_set_size`szBTRFSDevice._set_sizecCs|jS)N)r)rr"r"r#�current_sizecszBTRFSDevice.current_sizecCs|jotdd�|jD��S)Ncss|]}|jVqdS)N)r$)r4r5r"r"r#r6isz%BTRFSDevice.status.<locals>.<genexpr>)r.�allr)rr"r"r#r$gszBTRFSDevice.statuscCs
d|jS)Nzbtrfs-tmp.%s)r)rr"r"r#�_temp_dir_prefixkszBTRFSDevice._temp_dir_prefixFccs�|jstjd��|r|j}n|j}|jr2|jVn�tj|j	d�}yt
j|j||j
|jd�Wn2tjk
r�}ztjd|��WYdd}~XnXz
|VWdt
j|d�tj|�XdS)Nzformat doesn't exist)�prefix)r0�
mountpointZfstypeZoptionszbtrfs temp mount failed: %s)r?)r.r�FSError�original_formatr-r$�system_mountpoint�tempfileZmkdtempr=r
Zmountr0�type�	mountoptsr(r)Zumount�os�rmdir)rr1�fmtZtmpdir�er"r"r#�_do_temp_mountos$


zBTRFSDevice._do_temp_mountcCs|jr|jdjSdS)Nr)rr/)rr"r"r#r/�szBTRFSDevice.pathcCsdS)z% Is this device directly accessible? Tr")rr"r"r#�direct�szBTRFSDevice.directcs&|jjrd|jj}ntt|�j}|S)NzUUID=%s)r-�vol_uuidrr�
fstab_spec)r�spec)r!r"r#rM�szBTRFSDevice.fstab_speccCs
tj|�S)N)rZis_btrfs_name_valid)rr%r"r"r#�
is_name_valid�szBTRFSDevice.is_name_valid)N)F)�__name__�
__module__�__qualname__�__doc__�_typeZ	_packagesrZBLOCKDEV_BTRFS_PLUGINZ_external_dependenciesrr&r+r,r2r8r:�propertyr;r$r=rrJr/rKrMrO�
__classcell__r"r")r!r#r7s(
rcsReZdZdZejZedd��Zedd��Z	�fdd�Z
edd��Zd	d
�Zedd��Z
e
jd
d��Z
edd��Zejdd��Ze�fdd��Zdd�Z�fdd�Zdd�Z�fdd�Zdd�Zdd�Zdd �Zd<d"d#�Zd=d$d%�Zd&d'�Zd(d)�Zd*d+�Zed,d-��Z�fd.d/�Zd0d1�Z�fd2d3�Z d4d5�Z!d6d7�Z"d8d9�Z#�fd:d;�Z$�Z%S)>�BTRFSVolumeDevicezbtrfs volumecCsdS)Nrr")�sr"r"r#�<lambda>�szBTRFSVolumeDevice.<lambda>cCsdS)NrLr")rXr"r"r#rY�sc	s|jdd�}|jdd�}|jdd�}tt|�j||�d|_|_y||_||_WnBtj	k
r�}z$x|j
D]}|j|�qnW|�WYdd}~XnXg|_|j
|_|j
r�|jjr�t|j
djdd�}td|j||j|j|jd|j|d	�|_tj|j�|_d|_dS)
a�
            :param str name: the volume name
            :keyword bool exists: does this device exist?
            :keyword :class:`~.size.Size` size: the device's size
            :keyword :class:`~.ParentList` parents: a list of parent devices
            :keyword fmt: this device's formatting
            :type fmt: :class:`~.formats.DeviceFormat`
            :keyword str uuid: UUID of top-level filesystem/volume
            :keyword str sysfs_path: sysfs device path
            :keyword data_level: RAID level for data
            :type data_level: any valid raid level descriptor
            :keyword metadata_level: RAID level for metadata
            :type metadata_level: any valid raid level descriptor
        �
data_levelN�metadata_level�create_optionsr�labelrzsubvolid=%d)r.r]rLr0�
subvolspecrEr\)rrrWr�_data_level�_metadata_levelrZr[rrrZremove_child�
subvolumesrZsize_policyr-rD�getattrrr.�uuidr/�vol_id�copy�deepcopyrA�_default_subvolume_id)	rrr rZr[r\rI�devr])r!r"r#r�s4

zBTRFSVolumeDevice.__init__cCs
t|j�S)N)�listr)rr"r"r#�members�szBTRFSVolumeDevice.memberscCsld}|rV|rtjntj}y|j||�}Wn,tk
rT}ztj|��WYdd}~XnX|rb||_n||_dS)a� Sets a valid level for this device and level type.

            :param object value: value for this RAID level
            :param bool data: True if for data, False if for metadata

            :returns: a valid level for value, if any, else None
            :rtype: :class:`~.devicelibs.raid.RAIDLevel` or NoneType

            :raises :class:`~.errors.BTRFSValueError`: if value represents
            an invalid level.
        N)	rZraid_levelsZmetadata_levelsZ
_get_level�
ValueErrorrrr_r`)r�value�data�level�levelsrIr"r"r#�
_set_level�szBTRFSVolumeDevice._set_levelcCs|jS)z� Return the RAID level for data.

            :returns: raid level
            :rtype: an object that represents a raid level
        )r_)rr"r"r#rZ�szBTRFSVolumeDevice.data_levelcCs|j|d�dS)z� Set the RAID level for data.

            :param object value: new raid level
            :returns: None
            :raises :class:`~.errors.BTRFSValueError`: if value represents
            an invalid level.
        TN)rp)rrlr"r"r#rZs	cCs|jS)z� Return the RAID level for metadata.

            :returns: raid level
            :rtype: an object that represents a raid level
        )r`)rr"r"r#r[sz BTRFSVolumeDevice.metadata_levelcCs|j|d�dS)z� Set the RAID level for metadata.

            :param object value: new raid level
            :returns: None
            :raises :class:`~.errors.BTRFSValueError`: if value represents
            an invalid level.
        FN)rp)rrlr"r"r#r[s	cstt|�jp|jS)N)rrW�format_immutabler.)r)r!r"r#rq"sz"BTRFSVolumeDevice.format_immutablecCs
||_dS)N)�_name)rrlr"r"r#�	_set_name&szBTRFSVolumeDevice._set_namecsPtt|�j|�d|j|_t|jdd�}|r4||_|jsL|jdjj	|j_	dS)z Set the Device's format. zbtrfs.%dr]Nr)
rrW�_set_formatrr%rbr-r.rrE)rrHr])r!r"r#rt)szBTRFSVolumeDevice._set_formatcCs0|jp
tj}|jdd�|jD�td�dd�d�S)NcSsg|]
}|j�qSr")r)r4r5r"r"r#�
<listcomp>8sz/BTRFSVolumeDevice._get_size.<locals>.<listcomp>rcSsdS)Nrr")�xr"r"r#rY:sz-BTRFSVolumeDevice._get_size.<locals>.<lambda>)Z
chunk_sizeZsuperblock_size_func)rZr�SingleZget_sizerr
)rrZr"r"r#r85szBTRFSVolumeDevice._get_sizecsRdd�|j|jfD�}x&|D]}|j||�}|rtj|��qWtt|�j|�dS)Ncss|]}|r|VqdS)Nr")r4�lr"r"r#r6=sz3BTRFSVolumeDevice._remove_parent.<locals>.<genexpr>)rZr[Z_validate_parent_removalr�DeviceErrorrrW�_remove_parent)r�parentrorxZ	error_msg)r!r"r#rz<s
z BTRFSVolumeDevice._remove_parentcCs6|jdd�|jD�kr&tjd|j��|jj|�dS)NcSsg|]
}|j�qSr")r%)r4�vr"r"r#ruEsz4BTRFSVolumeDevice._add_subvolume.<locals>.<listcomp>zsubvolume %s already exists)r%rarr�append)r�volr"r"r#�_add_subvolumeDsz BTRFSVolumeDevice._add_subvolumecCsH|dd�|jD�kr"tjd|��dd�|jD�}|jj|j|��dS)NcSsg|]
}|j�qSr")r%)r4r|r"r"r#ruKsz7BTRFSVolumeDevice._remove_subvolume.<locals>.<listcomp>z'cannot remove non-existent subvolume %scSsg|]
}|j�qSr")r%)r4r|r"r"r#ruNs)rarrr�index)rr%�namesr"r"r#�_remove_subvolumeJsz#BTRFSVolumeDevice._remove_subvolumecs�|jjr|jjS|jjr |jjSdd�|jD��t�fdd�tjD�d�}|r~ytj|d|dd�d	Stk
r|dSXdS)
a Get any of the mountpoints for this btrfs volume.
            This includes mountpoints of subvolumes. The idea is
            to get a mountpoint usable for calling btrfs functions
            like btrfs.list_subvolumes where any mountpoint works.
        cSsg|]
}|j�qSr")r%)r4�pr"r"r#ru^sz?BTRFSVolumeDevice._get_any_btrfs_mountpoint.<locals>.<listcomp>c3s"|]\}}|�kr||fVqdS)Nr")r4rh�subvol)rr"r#r6_sz>BTRFSVolumeDevice._get_any_btrfs_mountpoint.<locals>.<genexpr>Nrr)Zdevspecr^���)	r-rBrAr�nextrZmountpointsZget_mountpoints�
IndexError)rZ
mount_specr")rr#�_get_any_btrfs_mountpointQs
z+BTRFSVolumeDevice._get_any_btrfs_mountpointFcCs\g}ytjj||d�}Wn6tjtjfk
rN}ztjd|�WYdd}~Xn
X|j�|S)N)�snapshots_onlyzfailed to list subvolumes: %s)�blockdevr�list_subvolumes�
BtrfsError�BlockDevNotImplementedErrorr(r)�_get_default_subvolume_id)rr?r��subvolsrIr"r"r#�_list_subvolumeshsz"BTRFSVolumeDevice._list_subvolumescCs|g}tjr|jdd�|j�}|r0|j||d�Stjrxy*|jdd��}|j||d�}WdQRXWntjk
rvYnX|S)NT)r1)r?r�)rZauto_dev_updates�setupr�r�rJrr@)rr�r�r?r"r"r#r�tsz!BTRFSVolumeDevice.list_subvolumescCs
t��dS)N)�NotImplementedError)rr%r"r"r#�remove_subvolume�sz"BTRFSVolumeDevice.remove_subvolumecCsjd}|j��N}ytjj|�}Wn6tjtjfk
rT}ztjd|�WYdd}~XnXWdQRX||_dS)Nz&failed to get default subvolume id: %s)	rJr�rZget_default_subvolume_idr�r�r(r)rg)rZsubvolidr?rIr"r"r#r��s
(z+BTRFSVolumeDevice._get_default_subvolume_idcCsl|j��Z}ytjj||�Wn:tjtjfk
rV}ztjd||��WYdd}~XnX||_WdQRXdS)zz Set a new default subvolume by id.

            This writes the change to the filesystem, which must be mounted.
        z/failed to set new default subvolume id (%s): %sN)	rJr�rZset_default_subvolumer�r�r(�errorrg)rrdr?rIr"r"r#�_set_default_subvolume_id�s
z+BTRFSVolumeDevice._set_default_subvolume_idcCsHd}|jdkrdS|j|jkr"|Sx |jD]}|j|jkr*|}Pq*W|S)N)rgrdra)r�defaultZsvr"r"r#�default_subvolume�s
z#BTRFSVolumeDevice.default_subvolumecs6tdd�|jD��r$tjdtj��tt|�j�dS)Ncss|]}|jtjkVqdS)N)rr�MIN_MEMBER_SIZE)r4r�r"r"r#r6�sz0BTRFSVolumeDevice._pre_create.<locals>.<genexpr>z4All BTRFS member devices must have size at least %s.)	�anyrrZDeviceCreateErrorrr�rrW�_pre_create)r)r!r"r#r��szBTRFSVolumeDevice._pre_createcCsft||j|jd�|jr$t|j�}nd}|jr:t|j�}nd}tjjdd�|j	D�|j
j||d�dS)N)r$cSsg|]
}|j�qSr")r/)r4r5r"r"r#ru�sz-BTRFSVolumeDevice._create.<locals>.<listcomp>)r]rZ�md_level)rr%r$rZ�strr[r�rZ
create_volumerr-r])rrZr�r"r"r#�_create�szBTRFSVolumeDevice._createcs�tt|�j�tj|j�}|s*tjd�ntj|�|j	_
|j	j
s�ytjj
|jdj�}Wn6tjtjfk
r�}ztjd|�WYdd}~XnX|j|j	_
d|j	_d|j_dS)Nz4failed to get updated udev info for new btrfs volumerz5failed to get filesystem info for new btrfs volume %sT)rrWr,r	Z
get_devicer'r(r�Zdevice_get_uuidr-rLr�rZfilesystem_inforr/r�r�rcr.rA)r�infoZbd_inforI)r!r"r#r,�s
zBTRFSVolumeDevice._post_createcCsDt||j|jd�x,|jD]"}|jdd�t|jdd�j�qWdS)N)r$T)r1)r0r.)rr%r$rr�rr/Zdestroy)rr0r"r"r#�_destroy�szBTRFSVolumeDevice._destroyc
Cs:t||j|jd�|j��}tjj||j�WdQRXdS)N)r$)rr%r$rJr�rZ
remove_devicer/)r�memberr?r"r"r#�_remove�s
zBTRFSVolumeDevice._removec
Cs(|j��}tjj||j�WdQRXdS)N)rJr�rZ
add_devicer/)rr�r?r"r"r#�_add�s
zBTRFSVolumeDevice._addcsVtt|�j|�|jr|jjnd|_|jr2|jjnd|_dd�|jD�|_	|j
|_dS)NcSsg|]}d|j�qS)zbtrfs.%d)r)r4r�r"r"r#ru�sz5BTRFSVolumeDevice.populate_ksdata.<locals>.<listcomp>)rrW�populate_ksdatarZr%Z	dataLevelr[Z
metaDataLevelr�devicesr.�preexist)rrm)r!r"r#r��s
z!BTRFSVolumeDevice.populate_ksdata)F)F)&rPrQrRrTrZMAIN_VOLUME_IDrdrUZ_format_class_nameZ_format_uuid_attrrrjrprZ�setterr[rqrsrtr8rzrr�r�r�r�r�r�r�r�r�r�r,r�r�r�r�rVr"r")r!r#rW�s@6		


rWcs�eZdZdZdZdZ�fdd�Z�fdd�Zedd	��Z	ed
d��Z
dd
d�Zdd�Z�fdd�Z
dd�Zd�fdd�	Zd�fdd�	Z�fdd�Z�ZS)�BTRFSSubVolumeDevicez" A btrfs subvolume pseudo-device. zbtrfs subvolumeTcsn|jdd�|_tt|�j||�t|j�dkr>tjd|j	��t
|jdt�s^tjd|j	��|jj
|�dS)a�
            :param str name: the subvolume name
            :keyword bool exists: does this device exist?
            :keyword :class:`~.size.Size` size: the device's size
            :keyword :class:`~.ParentList` parents: a list of parent devices
            :keyword fmt: this device's formatting
            :type fmt: :class:`~.formats.DeviceFormat`
            :keyword str sysfs_path: sysfs device path
        rdNrz %s must have exactly one parent.rz'%s unique parent must be a BTRFSDevice.)rrdrr�r�lenrrrrD�
isinstancer�volumer)rrr )r!r"r#rs
zBTRFSSubVolumeDevice.__init__cs�tt|�j|�|jrdSd|j}d}|jjjrpx<|jjjjd�D](}|j	d�s^|d|7}|j	d�rDd}qDWt
jr�|r�|d	t
j7}||j_dS)
z Set the Device's format. Nz	subvol=%sF�,r�z,%s�compressTz,compress=%s)rr�rtr.r%r�r-rE�split�
startswithrZbtrfs_compression)rrHZoptsZhas_compress�opt)r!r"r#rts



z BTRFSSubVolumeDevice._set_formatcCsR|jd}d}xt|t�s |}P|jd}qWt|t�sNtjd|j|jf��|S)z�Return the first ancestor that is not a BTRFSSubVolumeDevice.

           Note: Assumes that each ancestor in traversal has only one parent.

           Raises a DeviceError if the ancestor found is not a
           BTRFSVolumeDevice.
        rNz;%s %s's first non subvolume ancestor must be a btrfs volume)rr�r�rWrryrDr%)rr{r~r"r"r#r�.s	


zBTRFSSubVolumeDevice.volumecCs|jS)N)r�)rr"r"r#�	containerDszBTRFSSubVolumeDevice.containerFcCs"t||j|d�|jj|d�dS)z) Run setup method of all parent devices. )r%r1)r1N)rr%r�r�)rr1r"r"r#r3Hsz"BTRFSSubVolumeDevice.setup_parentsc
Cs<t||j|jd�|jj��}tjj||j�WdQRXdS)N)r$)rr%r$r�rJr�rZcreate_subvolume)rr?r"r"r#r�MszBTRFSSubVolumeDevice._createcs tt|�j�|jjj|j_dS)N)rr�r,r�r-rL)r)r!r"r#r,Ssz!BTRFSSubVolumeDevice._post_createc
CsZt||j|jd�|jj��4}|jj|jkr<|jj|jj�tj	j
||j�WdQRXdS)N)r$)rr%r$r�rJrgrdr�r�rZdelete_subvolume)rr?r"r"r#r�Ws
zBTRFSSubVolumeDevice._destroycs(|r|jj|j�tt|�j|d�dS)N)�	modparent)r�r�r%rr��remove_hook)rr�)r!r"r#r�asz BTRFSSubVolumeDevice.remove_hookcs6tt|�j|d�|rdS||jjkr2|jj|�dS)N)�new)rr��add_hookr�rar)rr�)r!r"r#r�gs
zBTRFSSubVolumeDevice.add_hookcsHtt|�j|�d|_|j|_|j|_|jdjj	}|rDd|g|_
dS)NTrzLABEL=%s)rr�r�r�r%r.r�rr-r]r�)rrmr])r!r"r#r�osz$BTRFSSubVolumeDevice.populate_ksdata)F)T)T)rPrQrRrSrTZ_format_immutablerrtrUr�r�r3r�r,r�r�r�r�rVr"r")r!r#r��s

r�cs8eZdZdZdZ�fdd�Zdd�Z�fdd�Z�ZS)	�BTRFSSnapShotDevicea A btrfs snapshot pseudo-device.

        BTRFS snapshots are a specialized type of subvolume that contains a
        source attribute which identifies which subvolume the snapshot was taken
        from. They do not have to be removed when removing the source subvolume.
    zbtrfs snapshotcs�|jdd�}|jd�r(|r(tjd��|rBt|t�rBtjd��|rX|jrXtjd��||_|jdd�|_t	t
|�j||�|r�t|d	|�|j
kr�|j
j|j�g|_tjd
��dS)a{
            :param str name: the subvolume name
            :keyword bool exists: does this device exist?
            :keyword :class:`~.size.Size` size: the device's size
            :keyword :class:`~.ParentList` parents: a list of parent devices
            :keyword fmt: this device's formatting
            :type fmt: :class:`~.formats.DeviceFormat`
            :keyword str sysfs_path: sysfs device path
            :keyword :class:`~.BTRFSDevice` source: the snapshot source
            :keyword bool read_only: create a read-only snapshot

            Snapshot source can be either a subvolume or a top-level volume.

        �sourceNr.z/non-existent btrfs snapshots must have a sourcez/btrfs snapshot source must be a btrfs subvolumez(btrfs snapshot source must already exist�	read_onlyFr�z4btrfs snapshot and source must be in the same volume)rrrrr�rr.r�r�rr�rrbr�r�r%r)rrr r�)r!r"r#r�s


zBTRFSSnapShotDevice.__init__cCspt||j|jd�|jj��J}t|jt�r0|}nd||jjf}d||jf}tj	j
|||jd�WdQRXdS)N)r$z%s/%s)Zro)rr%r$r�rJr�r�rWr�rZcreate_snapshotr�)rr?�source_pathZ	dest_pathr"r"r#r��szBTRFSSnapShotDevice._createcs||jkptt|�j|�S)N)r�rr��
depends_on)rZdep)r!r"r#r��s
zBTRFSSnapShotDevice.depends_on)	rPrQrRrSrTrr�r�rVr"r")r!r#r�~s
&
r�))rFrerCZgiZrequire_versionZ
gi.repositoryrr�Z
devicelibsrr�rrZstorage_logrr	r
Zformatsrrrr
ZmountsrZloggingZ	getLoggerr(Zstoragerr�rrZtasksr�
contextlibrrrWr�r�r"r"r"r#�<module>s:
j\