HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/cc_disk_setup.cpython-38.pyc
U

Ӈglv�@s�UdZddlZddlZddlZddlmZddlmZmZm	Z	ddl
mZddlm
Z
ddlmZddlmZdd	lmZd
diZe�e�Zdeged
dgd�Zeed<ee
eedd�dd�Zdd�Zdd�ZdJdd�ZdKdd�Z dd�Z!dLdd �Z"d!d"�Z#d#d$�Z$dMd&d'�Z%d(d)�Z&d*d+�Z'd,d-�Z(d.d/�Z)d0d1�Z*d2d3�Z+d4d5�Z,d6d7�Z-d8d9�Z.d:d;�Z/d<d=�Z0d>d?�Z1d@dA�Z2dBdC�Z3dDdE�Z4dFdG�Z5dHdI�Z6dS)Nz1Disk Setup: Configure partitions and filesystems.�N)�Path)�performance�subp�util)�Cloud)�Config)�
MetaSchema)�ALL_DISTROS)�PER_INSTANCEZLANG�CZ
cc_disk_setup�
disk_setup�fs_setup)�idZdistrosZ	frequencyZactivate_by_schema_keys�meta)�name�cfg�cloud�args�returnc
s�|�di����fdd�}|�d�}t|t�r�t||�t�dt|��|��D]~\}}t|t�spt�d|�qPz*t	�
d|����t||�W5QRXWqPtk
r�}zt
�td|�W5d	}~XYqPXqP|�d
�}	t|	t��r�t�dt|	��t|	|�|	D]z}t|t��s$t�d|��qz"t	�
d
��t|�W5QRXWn4tk
�rz}zt
�td|�W5d	}~XYnX�qd	S)z[
    See doc/examples/cloud-config-disk-setup.txt for documentation on the
    format.
    �device_aliasescs��|�}��|p|�p|S)N)�getZdevice_name_to_device)Zcandr�rr��@/usr/lib/python3/dist-packages/cloudinit/config/cc_disk_setup.py�alias_to_device(s
zhandle.<locals>.alias_to_devicerzPartitioning disks: %szInvalid disk definition for %szCreating partition on z Failed partitioning operation
%sNr
zsetting up filesystems: %sz"Invalid file system definition: %szCreating new filesystemz%Failed during filesystem operation
%s)r�
isinstance�dict�update_disk_setup_devices�LOG�debug�str�items�warningrZTimed�mkpart�	Exceptionr�logexc�list�update_fs_setup_devices�mkfs)
rrrrrr�disk�
definition�er
rrr�handle!s<



�$

r,cCs�t|�D]t}||�}|dks||kr&q||krBt�d||�||=||||<t||t�rh|||d<||=t�d||�qdS)Nz0Replacing %s in disk_setup for translation of %s�	_orignamez,updated disk_setup device entry '%s' to '%s')r&r�inforrr)r�tformer�orignameZtransformedrrrrMs(��rcCs�|D]�}t|t�s t�d|�q|�d�}|dkr4qt�|�\}}||�}|dk	rv|}t�d|||�||d<||d<|rd|kr�t�d|||�|d|d<||d<qdS)Nz"entry in disk_setup not a dict: %s�devicez%s is mapped to disk=%s part=%sr-�	partitionzKPartition '%s' from dotted device name '%s' overrides 'partition' key in %sZ
_partition)rrrr"rrZexpand_dotted_devnamer)rr/r*r0Zdev�partZtformedrrrr'gs:

��r'ccs@t�|�}|r||d�}dd�|D�D]\}}||fVq(dS)zd
    Returns the key/value pairs of output sent as string
    like:  FOO='BAR' HOME='127.0.0.1'
    NcSsg|]}|�d��qS)�=)�split��.0�xrrr�
<listcomp>�sz"value_splitter.<locals>.<listcomp>)�shlexr5)�values�startZ_values�key�valuerrr�value_splitter�s

r?Fc
cs�dddd|g}|r|�d�d}zt�|�\}}Wn4tk
rf}ztd||f�|�W5d}~XYnXdd	�|����D�}|D]6}ddddd
�}t|�D]\}	}
|
||	��<q�|Vq�dS)a�
    Enumerate the elements of a child device.

    Parameters:
        device: the kernel device name
        nodeps <BOOL>: don't enumerate children devices

    Return a dict describing the disk:
        type: the entry type, i.e disk or part
        fstype: the filesystem type, if it exists
        label: file system label, if it exists
        name: the device name, i.e. sda
    Zlsblkz--pairsz--outputzNAME,TYPE,FSTYPE,LABELz--nodepsN�"Failed during disk check for %s
%scSs g|]}t|���dkr|�qS)r)�lenr5r6rrrr9�sz"enumerate_disk.<locals>.<listcomp>)r�type�fstype�label)�appendrr$�RuntimeError�strip�
splitlinesr?�lower)r1�nodepsZ	lsblk_cmdr.�_errr+�partsr3�dr=r>rrr�enumerate_disk�s6�

���rNcCs.t|dd�D]}d|kr|d��SqdS)z@
    Return the device type of the device by calling lsblk.
    T)rJrBN)rNrI)r1rMrrr�device_type�srOcCsXd}zt|�}Wn"tk
r2t�d|�YdSX|rD|dkrDdS|sT|dkrTdSdS)z0
    Check if the device is a valid device.
    �zQuery against device %s failedFr3Tr))rOr$rr")rr2Zd_typerrr�is_device_valid�srQc

Cs�d\}}}}ddd|g}ztj|ddgd�\}}Wn4tk
rf}ztd||f�|�W5d	}~XYnX|r�t|���d
kr�t|d
d�D]<\}}	|��dkr�|	}q�|��d
kr�|	}q�|��dkr�|	}q�|||fS)z�
    Check if the device has a filesystem on it

    Output of blkid is generally something like:
    /dev/sda: LABEL="Backup500G" UUID="..." TYPE="ext4"

    Return values are device, label, type, uuid
    )NNNNZblkidz-cz	/dev/nullr�)Zrcsr@N�)r<rDrB�uuid)rr$rFrArHr?rI)
r1�outrD�fs_typerTZ	blkid_cmdrKr+r=r>rrr�check_fs�s(	
��rWcCst|�\}}}|S)z7
    Returns true if the device has a file system.
    )rW)r1�_rVrrr�
is_filesystem	srYTcCs�|dkrd}|sddg}d}t|�D]�}|d|krP|dkrPd|ddfS|d|kr�|rl|d	|ksp|s�d|dd
fS|d|kr$|ddks�|dr�d
}|ddkr�q$|ds$d|ddfSq$|s�|dfSt�d�d
S)a
    Find a device that is either matches the spec, or the first

    The return is value is (<device>, <bool>) where the device is the
    device to use and the bool is whether the device matches the
    fs_type and label.

    Note: This works with GPT partition tables!
    NrPr)r3FrC�/dev/%srrDTrBz5Failed to find device during available device search.)NF)rNrr")r1rVrDZ
valid_targets�label_match�
replace_fsZraw_device_usedrMrrr�find_device_nodes6�
��
r]cCs2ttt|���dkrdSt|�\}}}|r.dSdS)z�
    Check if the device is currently used. Returns true if the device
    has either a file system or a partition entry
    is no filesystem found on the disk.
    rSTF)rAr&rNrW)r1rX�check_fstyperrr�is_disk_usedIs	r_c
Csrz,t�dd|g�\}}t�dd|g�\}}Wn4tk
r`}ztd||f�|�W5d}~XYnXt|�t|�S)N�blockdevz--getsize64z--getsszFailed to get %s size
%s)rr$rF�int)r1Z
size_in_bytesrXZsector_sizer+rrr�get_hdd_size]s$rbc
Cs�t|�dd|g}ztj|d|d�\}}Wn4tk
r`}ztd||f�|�W5d}~XYnXg}|��D]�}|��}t|�dkr�qn||dkrn|d��d	kr�qnd}	tt	d
t|��dd�D](}
||
�
�r�||
d
kr�||
}	q�q�|�|	�qn|S)z�
    Returns true if the partition layout matches the one on the disk

    Layout should be a list of values. At this time, this only
    verifies that the number of partitions and their labels is correct.
    �sfdiskz-l�%s
��data�(Error running partition command on %s
%sNr���)Zextended�emptyrST)�reverse�/)�read_parttblrr$rFrHr5rArI�sorted�range�isdigitrE)r1�layout�prt_cmdrUrKr+�found_layout�line�_lineZ
type_labelr8rrr�check_partition_mbr_layoutgs2

��ruc
Cs�dd|g}ztj|td�\}}Wn4tk
rT}ztd||f�|�W5d}~XYnXt|���}|D]}|���d�rfq~qfdd�|D�}g}	|D]0}
t|
�dkr�|
�	d	�r�|
d
d�}
|	�
|
�q�|	S)N�sgdiskz-p)Z
update_envrg�NumbercSsg|]}|����d�qS)�)rGr5)r7rsrrrr9�sz.check_partition_gpt_layout.<locals>.<listcomp>�Z00rrR)r�
LANG_C_ENVr$rF�iterrHrG�
startswithrA�endswithrE)r1rprqrUrKr+Z	out_linesrsZcodesZcleaned�coderrr�check_partition_gpt_layout�s(

��rcCs�d|krt||�}nd|kr(t||�}ntd��t�d||||�t|t�rd|r`t|�dkr`dSdSt|�t|�kr�dd	�|D�}t�d
||�t||�D]&\}}|dk	r�t	|�t	|�kr�dSq�dSdS)z�
    See if the partition lay out matches.

    This is future a future proofing function. In order
    to add support for other disk layout schemes, add a
    function called check_partition_%s_layout
    �gpt�mbr�Unable to determine table typez6called check_partition_%s_layout(%s, %s), returned: %srSTFcSs*g|]"}t|ttf�r"t|d�nd�qS)rSN)r�tupler&r r6rrrr9�sz*check_partition_layout.<locals>.<listcomp>zLayout types=%s. Found types=%sN)
rrurFrrr�boolrA�zipr )�
table_typer1rprrZlayout_typesZitypeZftyperrr�check_partition_layout�s<�
��r�c
Cst|t�st|t�rdSt|�dkr.t|t�s8t|t�s@td��t|�}|dkrXtd��g}d}|D]�}d}|}|d7}t|t�r�t|�dkr�td	|��|\}}tt|�t|�d
�}||kr�|�d|�qd|�d||f�qdd
�|�}	t|�dk�r
td|	��|	S)a@
    Calculate the layout of the partition table. Partition sizes
    are defined as percentage values or a tuple of percentage and
    partition type.

    For example:
        [ 33, [66: 82] ]

    Defines the first partition to be a size of 1/3 the disk,
    while the remaining 2/3's will be of type Linux Swap.
    z,,83rzPartition layout is invalidryz$Only simply partitioning is allowed.�SrSrR�%Partition was incorrectly defined: %s�dz,,%sz,%s,%s�
z-Calculated partition definition is too big
%s)	rr&r�rArFra�floatrE�join)
�sizerpZ
last_part_num�part_definitionZpart_numr3Z	part_type�percent�	part_sizeZsfdisk_definitionrrr�get_partition_mbr_layout�sD
�
�
��r�cCs�t|t�rdddgfgSg}|D]h}t|t�rPt|�dkrFtd|��|\}}n|}d}tt|�t|�d�}|�|dd�|�gf�q d|ddd<|S)NrrRr�r�z+{}rh)	rr�r&rArFrar�rE�format)r�rpZpartition_specsr2r��partition_typer�rrr�get_partition_gpt_layouts 

�
r�c	Csbd}d}d}t|d��:}|�||�|�|tj�|�||�|��W5QRXt|�dS)N�izrb+)�open�write�seek�os�SEEK_END�flushrl)r1ZnullZ	start_lenZend_len�fprrr�purge_disk_ptable7sr�cCs�t|�D]v}|ddkrddd|dg}zt�d|d�t�|�Wqtk
r|}ztd|d�|�W5d	}~XYqXqt|�d	S)
z(
    Remove partition table entries
    rB)r)ZcryptZwipefsz--allrZrzPurging filesystem on /dev/%szFailed FS purge of /dev/%sN)rNrr.rr$rFr�)r1rMZ
wipefs_cmdr+rrr�
purge_diskFs
��r�cCs0d|krt||�Sd|kr$t||�Std��dS)z�
    Call the appropriate function for creating the table
    definition. Returns the table definition

    This is a future proofing function. To add support for
    other layouts, simply add a "get_partition_%s_layout"
    function.
    r�r�r�N)r�r�rF)r�r�rprrr�get_partition_layoutZs
	

r�c
Csxd}t�|�r||g}n
dd|g}t��zt�|�Wn2tk
rj}zt�td|�W5d}~XYnXt��dS)zq
    `Partprobe` is preferred over `blkdev` since it is more reliably
    able to probe the partition table.
    �	partprober`z
--rereadptz%Failed reading the partition table %sN)r�whichr�udevadm_settler$r%r)r1r�Z	probe_cmdr+rrrrljs


"rlc
Csbdd|g}ztj|d|d�Wn4tk
rT}ztd||f�|�W5d}~XYnXt|�dS)zV
    Break out of mbr partition to allow for future partition
    types, i.e. gpt
    rcz--forcerdrez Failed to partition device %s
%sN)rr$rFrl)r1rprqr+rrr�exec_mkpart_mbr}s

��r�c
Cs�z�t�dd|g�t|�D]f\}\}\}}|d7}t�ddd�|||�|g�|dk	rt|��dd�}t�ddd	�||�|g�qWn"tk
r�t�d
|��YnXt|�dS)Nrvz-ZrSz-nz{}:{}:{}ry�0z-tz{}:{}zFailed to partition device %s)	r�	enumerater�r �ljustr$rr"rl)r1rp�indexr�r<�endZpinputrrr�exec_mkpart_gpt�s(���
r�cCs8tj�|�s,t��tj�|�s,td|��t��dS)z?Assert that device exists and settle so it is fully recognized.zBDevice %s did not exist and was not created with a udevadm settle.N)r��path�existsrr�rF�r1rrr�assert_and_settle_device�s��r�cCs�t|�tj�|�}t�d|�|�dd�}|�dd�}|�dd�}t�d�t|t�r\|r`|snt�d�d	St�d
|�t	|�s�t
dj|d���t|t�r�|�
�d
kr�t�d�t|�d	St�d�t|||�r�t�d�dSt�d�|�st|��st|��rt�d|�d	St�d|�t|�}t�d�t|||�}t�d|�t�d|�d|k�rnt||�nd|k�r�t||�nt
d��t�d|�d	S)a�
    Creates the partition table.

    Parameters:
        definition: dictionary describing how to create the partition.

            The following are supported values in the dict:
                overwrite: Should the partition table be created regardless
                            of any pre-existing data?
                layout: the layout of the partition table
                table_type: Which partition table to use, defaults to MBR
                device: the device to work on.
    z!Checking values for %s definition�	overwriteFrpr�r�z Checking against default devicesz)Device is not to be partitioned, skippingNz'Checking if device %s is a valid devicez%Device {device} is not a disk device!r��removez,Instructed to remove partition table entriesz!Checking if device layout matchesz"Device partitioning layout matchesTz'Checking if device is safe to partitionz-Skipping partitioning on configured device %szChecking for device size of %szCalculating partition layoutz   Layout is: %szCreating partition table on %sr�r�zPartition table created for %s)r�r�r��realpathrrrrr�rQrFr�r rIr�r�r_rYrbr�r�r�)r1r*r�rpr�Zdevice_sizer�rrrr#�sN


�






r#cCsDdddddd�}d|��kr d}|��|kr4||St�d|�dS)z9
    A force flag might be -F or -F, this look it up
    z-Fz-f)�extZbtrfsZxfsZreiserfsZswapr�zForce flag for %s is unknown.rP)rIrr")Zfs�flagsrrr�lookup_force_flags�r�c
Cs�|�d�}|�d�}t|�dd��}|�d�}|�dg�}|�dg�}|�dd	�}|�d
d	�}t|�tj�|�}t�d|�|r�|���rZ|��r�|d��r�|�d
�}d||f}t	|��
�s�t�d�dSt�d||�t�d|�t|�\}	}
}t�d||	|
�|	|k�rL|
|k�rLt�d|�|�s>t�d|�dSt�d|�nt�d|�n�|�rt|��
�dk�r|}t�d|�d}
|�
�dk�r�d	}
t||||
|d�\}}t�d||�|�r�t�d�dS|�s�|�r�|�r�t�d|�|�s:t�d|�dSn4|�rt|��
�d k�r,t�d!|�nt�d"�dSt�d#|||�|�s`t�d$|�dS|�s||�s|td%j|d&���d	}|�r�|d|||d'�}d}|�r�t�d(|�|�r^t�d)|�n�t�d*|�}|�s�t�d+|�}|�s�t�d,||�dS|g}|�r|�d-|g�|�s,t|�d.k�rDt|�}|�rD|�|�|�rT|�|�|�|�t�d/||�ztj||d0�Wn6tk
�r�}ztd1||f�|�W5d}~XYnXdS)2a�
    Create a file system on the device.

        label: defines the label to use on the device
        fs_cfg: defines how the filesystem is to look
            The following values are required generally:
                device: which device or cloud defined default_device
                filesystem: which file system type
                overwrite: indiscriminately create the file system
                partition: when device does not define a partition,
                            setting this to a number will mean
                            device + partition. When set to 'auto', the
                            first free device or the first device which
                            matches both label and type will be used.

                            'any' means the first filesystem that matches
                            on the device.

            When 'cmd' is provided then no other parameter is required.
    rDr1r2�any�
filesystem�cmdZ
extra_optsr\Fr�z#Checking %s against default devicesrh�pz%s%sz/Path %s does not exist or is not a block deviceNz%Manual request of partition %s for %szChecking device %sz0Device '%s' has check_label='%s' check_fstype=%sz Existing file system found at %sz"Device %s has required file systemzDestroying filesystem on %sz#Device %s is cleared for formatting)�autor�z-Identifying device to create %s filesystem onT)rVrDr[r\z(Automatic device for %s identified as %sz,Found filesystem match, skipping formatting.z*Replacing file system on %s as instructed.zENo device available that matches request. Skipping fs creation for %sZnonez.Using the raw device to place filesystem %s onz(Error in device identification handling.z;File system type '%s' with label '%s' will be created on %szDevice is not known: %szENo way to create filesystem '{label}'. fs_type or fs_cmd must be set.)rD)rDr�r1z8fs_setup:overwrite ignored because cmd was specified: %sz9fs_setup:extra_opts ignored because cmd was specified: %szmkfs.%szmk%sz.Cannot create fstype '%s'.  No mkfs.%s commandz-Lr)zCreating file system %s on %s)�shellzFailed to exec of '%s':
%s)rr r�r�r�r�rrrorZis_block_devicer"rWrIr]rFr�rr��extendrOr�rEr$)Zfs_cfgrDr1r2rVZfs_cmdZfs_optsZ
fs_replacer�Zcheck_labelr^rXZodevicer[Zreuser�Zmkfs_cmdZ
force_flagr+rrrr(s�




���

�
�������


r()N)F)F)NNNTN)7�__doc__Zloggingr�r:�pathlibrZ	cloudinitrrrZcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemarZcloudinit.distrosr	Zcloudinit.settingsr
rzZ	getLogger�__name__rr�__annotations__r r&r,rr'r?rNrOrQrWrYr]r_rbrurr�r�r�r�r�r�rlr�r�r�r#r�r(rrrr�<module>sb
�,&

2
 
�
8
().9G