o
    fw                  
   @   s  U d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ d
diZeeZdegeddgdZeed< dedede
deddf
ddZdd Zdd ZdNddZdOdd Zd!d" Z dOd#d$Z!d%d& Z"d'd( Z#				)	dPd*d+Z$d,d- Z%d.d/ Z&d0d1 Z'd2d3 Z(d4d5 Z)d6d7 Z*d8d9 Z+d:d; Z,d<d= Z-d>d? Z.d@dA Z/dBdC Z0dDdE Z1dFdG Z2dHdI Z3dJdK Z4dLdM Z5dS )Qz1Disk Setup: Configure partitions and filesystems.    N)Path)subputil)Cloud)Config)
MetaSchema)ALL_DISTROS)PER_INSTANCELANGCcc_disk_setup
disk_setupfs_setup)iddistros	frequencyactivate_by_schema_keysmetanamecfgcloudargsreturnc                    s  | di  fdd}| d}t|trht|| tdt| | D ]?\}}t|ts8td| q(ztd t	j
tjd| t||fd	 W q( tyg } zt	td
|  W Y d}~q(d}~ww | d}	t|	trtdt|	 t|	| |	D ]C}t|tstd| qztd | d}
t	j
tjd|
 t|fd	 W q ty } zt	td|  W Y d}~qd}~ww dS dS )z[
    See doc/examples/cloud-config-disk-setup.txt for documentation on the
    format.
    device_aliasesc                    s    | } |p
| p|S N)getdevice_name_to_device)candr   r   r    @/usr/lib/python3/dist-packages/cloudinit/config/cc_disk_setup.pyalias_to_device(   s   
zhandle.<locals>.alias_to_devicer   zPartitioning disks: %szInvalid disk definition for %sz!Creating new partition table/diskzCreating partition on %s)logfuncmsgfuncr   z Failed partitioning operation
%sNr   zsetting up filesystems: %sz"Invalid file system definition: %szCreating new filesystem.devicezCreating fs for %sz%Failed during filesystem operation
%s)r   
isinstancedictupdate_disk_setup_devicesLOGdebugstritemswarningr   log_timemkpart	Exceptionlogexclistupdate_fs_setup_devicesmkfs)r   r   r   r   r!   r   disk
definitioner   r%   r   r   r    handle!   sZ   












r8   c                 C   s   t | D ]:}||}|d u s||krq|| v r!td|| | |= | | | |< t| | tr4|| | d< | |= td|| qd S )Nz0Replacing %s in disk_setup for translation of %s	_orignamez,updated disk_setup device entry '%s' to '%s')r2   r)   infor&   r'   r*   )r   tformerorignametransformedr   r   r    r(   V   s*   r(   c                 C   s   | D ]Q}t |tstd| q|d}|d u rqt|\}}||}|d ur;|}td||| ||d< ||d< |rSd|v rOtd||| |d |d< ||d< qd S )Nz"entry in disk_setup not a dict: %sr%   z%s is mapped to disk=%s part=%sr9   	partitionzKPartition '%s' from dotted device name '%s' overrides 'partition' key in %s
_partition)r&   r'   r)   r-   r   r   expand_dotted_devnamer*   )r   r;   r6   r<   devparttformedr   r   r    r3   p   s8   

r3   c                 c   sB    t | }|r||d }dd |D D ]	\}}||fV  qdS )zd
    Returns the key/value pairs of output sent as string
    like:  FOO='BAR' HOME='127.0.0.1'
    Nc                 S   s   g | ]}| d qS )=)split.0xr   r   r    
<listcomp>   s    z"value_splitter.<locals>.<listcomp>)shlexrE   )valuesstart_valueskeyvaluer   r   r    value_splitter   s   
rP   Fc              
   c   s    dddd| g}|r| d d}z	t|\}}W n ty/ } z	td| |f |d}~ww dd	 |  D }|D ]}ddddd
}t|D ]
\}	}
|
||	 < qJ|V  q=dS )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
    lsblkz--pairsz--outputzNAME,TYPE,FSTYPE,LABELz--nodepsN"Failed during disk check for %s
%sc                 S   s    g | ]}t | d kr|qS )r   )lenrE   rF   r   r   r    rI      s     z"enumerate_disk.<locals>.<listcomp>)r   typefstypelabel)appendr   r0   RuntimeErrorstrip
splitlinesrP   lower)r%   nodeps	lsblk_cmdr:   _errr7   partsrB   drN   rO   r   r   r    enumerate_disk   s>   

ra   c                 C   s.   t | ddD ]}d|v r|d    S qdS )z@
    Return the device type of the device by calling lsblk.
    T)r\   rT   N)ra   r[   )r%   r`   r   r   r    device_type   s
   rb   c                 C   sV   d}zt | }W n ty   td|  Y dS w |r!|dkr!dS |s)|dkr)dS dS )z0
    Check if the device is a valid device.
     zQuery against device %s failedFrB   Tr5   )rb   r0   r)   r-   )r   r>   d_typer   r   r    is_device_valid   s   re   c           
   
   C   s   d\}}}}ddd| g}zt j |ddgd\}}W n ty. } z	td| |f |d	}~ww |r^t| d
kr^t|d
dD ]\}}	| dkrL|	}q?| dkrU|	}q?| dkr]|	}q?|||fS )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
    )NNNNblkidz-cz	/dev/nullr      )rcsrR   N   )rL   rV   rT   uuid)r   r0   rX   rS   rZ   rP   r[   )
r%   outrV   fs_typerj   	blkid_cmdr^   r7   rN   rO   r   r   r    check_fs   s.   	

rn   c                 C   s   t | \}}}|S )z7
    Returns true if the device has a file system.
    )rn   )r%   _rl   r   r   r    is_filesystem  s   rp   Tc                 C   s   |du rd}|sddg}d}t | D ]W}|d |kr(|du r(d|d  df  S |d |krB|r6|d	 |ks8|sBd|d  d
f  S |d |v ri|d dksR|d rTd
}|d dkr[q|d sid|d  df  S q|sp| dfS t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!
    Nrc   r5   rB   FrU   /dev/%sr   rV   TrT   z5Failed to find device during available device search.)NF)ra   r)   r-   )r%   rl   rV   valid_targetslabel_match
replace_fsraw_device_usedr`   r   r   r    find_device_node  s2   
rv   c                 C   s2   t tt| dkrdS t| \}}}|rdS dS )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.
    ri   TF)rS   r2   ra   rn   )r%   ro   check_fstyper   r   r    is_disk_usedR  s   	rx   c              
   C   sh   zt  dd| g\}}t  dd| g\}}W n ty+ } z	td| |f |d }~ww t|t| S )Nblockdevz--getsize64z--getsszFailed to get %s size
%s)r   r0   rX   int)r%   size_in_bytesro   sector_sizer7   r   r   r    get_hdd_sizef  s   r}   c              
   C   s   t |  dd| g}ztj|d| d\}}W n ty+ } z	td| |f |d}~ww g }| D ]C}| }t|dkr?q2| |d v ru|d  d	v rNq2d}	tt	d
t|ddD ]}
||
 
 ro||
 dkro||
 }	 nq[||	 q2|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   )extendedemptyri   T)reverse/)read_parttblr   r0   rX   rZ   rE   rS   r[   sortedrangeisdigitrW   )r%   layoutprt_cmdrk   r^   r7   found_layoutline_line
type_labelrH   r   r   r    check_partition_mbr_layoutp  s:   


r   c              
   C   s   dd| g}zt j |td\}}W n ty% } z	td| |f |d }~ww t| }|D ]}| dr9 nq.dd |D }g }	|D ]}
t|
dkrX|
	d	rX|
d
d }
|	
|
 qE|	S )Nsgdiskz-p)
update_envr   Numberc                 S   s   g | ]
}|   d  qS )   )rY   rE   )rG   r   r   r   r    rI     s    z.check_partition_gpt_layout.<locals>.<listcomp>   00r   rg   )r   
LANG_C_ENVr0   rX   iterrZ   rY   
startswithrS   endswithrW   )r%   r   r   rk   r^   r7   	out_linesr   codescleanedcoder   r   r    check_partition_gpt_layout  s.   

r   c                 C   s   d| kr
t ||}nd| krt||}ntdtd| ||| t|tr2|r0t|dkr0dS dS t|t|krcdd	 |D }td
|| t||D ]\}}|dur`t	|t	|kr` dS qMdS dS )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: %sri   TFc                 S   s*   g | ]}t |ttfrt|d  ndqS )ri   N)r&   tupler2   r+   rF   r   r   r    rI     s    z*check_partition_layout.<locals>.<listcomp>zLayout types=%s. Found types=%sN)
r   r   rX   r)   r*   r&   boolrS   zipr+   )
table_typer%   r   r   layout_typesitypeftyper   r   r    check_partition_layout  s:   
r   c           
      C   s  t |tst |trdS t|dkrt |tst |ts tdt|}|dkr,tdg }d}|D ]@}d}|}|d7 }t |trQt|dkrMtd	| |\}}tt| t|d
  }||kri|d|  q2|d||f  q2d|}	t|dkrtd|	 |	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,,83r   zPartition layout is invalidr   z$Only simply partitioning is allowed.S   ri   rg   %Partition was incorrectly defined: %sd   z,,%sz,%s,%s
z-Calculated partition definition is too big
%s)	r&   r2   r   rS   rX   rz   floatrW   join)
sizer   last_part_numpart_definitionpart_numrB   	part_typepercent	part_sizesfdisk_definitionr   r   r    get_partition_mbr_layout  sB   

r   c                 C   s   t |trd ddgfgS g }|D ]4}t |tr(t|dkr#td| |\}}n|}d }tt| t|d  }||dd|gf qd|d d d< |S )Nr   rg   r   r   z+{}r   )	r&   r   r2   rS   rX   rz   r   rW   format)r   r   partition_specsr>   r   partition_typer   r   r   r    get_partition_gpt_layout(  s    


r   c                 C   sv   d}d}d}t | d"}|||  || tj |||  |  W d    n1 s0w   Y  t|  d S )N    i   zrb+)openwriteseekosSEEK_ENDflushr   )r%   null	start_lenend_lenfpr   r   r    purge_disk_ptable@  s   
r   c                 C   s   t | D ]6}|d dvr:ddd|d  g}ztd|d  t| W q ty9 } z	td|d  |d	}~ww qt|  d	S )
z(
    Remove partition table entries
    rT   )r5   cryptwipefsz--allrq   r   zPurging filesystem on /dev/%szFailed FS purge of /dev/%sN)ra   r)   r:   r   r0   rX   r   )r%   r`   
wipefs_cmdr7   r   r   r    
purge_diskO  s    

r   c                 C   s,   d| kr	t ||S d| krt||S td)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   )r   r   rX   )r   r   r   r   r   r    get_partition_layoutc  s
   	

r   c              
   C   sz   d}t |r|| g}ndd| g}t  zt  | W n ty6 } zttd|  W Y d}~nd}~ww t  dS )zq
    `Partprobe` is preferred over `blkdev` since it is more reliably
    able to probe the partition table.
    	partprobery   z
--rereadptz%Failed reading the partition table %sN)r   whichr   udevadm_settler0   r1   r)   )r%   r   	probe_cmdr7   r   r   r    r   s  s   


r   c              
   C   sX   dd| g}zt j |d| d W n ty% } z	td| |f |d}~ww t|  dS )zV
    Break out of mbr partition to allow for future partition
    types, i.e. gpt
    r~   z--forcer   r   z Failed to partition device %s
%sN)r   r0   rX   r   )r%   r   r   r7   r   r   r    exec_mkpart_mbr  s   

r   c              
   C   s   zBt  dd| g t|D ]3\}\}\}}|d7 }t  ddd|||| g |d ur@t|dd}t  ddd	||| g qW n tyP   td
|   w t|  d S )Nr   z-Zri   z-nz{}:{}:{}r   0z-tz{}:{}zFailed to partition device %s)	r   	enumerater   r+   ljustr0   r)   r-   r   )r%   r   indexr   rL   endpinputr   r   r    exec_mkpart_gpt  s.   r   c                 C   s8   t j| st  t j| std|  t  dS )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existsr   r   rX   r%   r   r   r    assert_and_settle_device  s   r   c                 C   s  t |  tj| } td|  |dd}|dd}|dd}td t|tr.|r0|s7td d	S td
|  t	| sIt
dj| dt|tr_| dkr_td t|  d	S td t|| |rqtd dS td |st| st| rtd|  d	S td|  t| }td t|||}td| td|  d|krt| | nd|krt| | nt
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	overwriteFr   r   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 %szChecking for device size of %szCalculating partition layoutz   Layout is: %szCreating partition table on %sr   r   zPartition table created for %s)r   r   r   realpathr)   r*   r   r&   r   re   rX   r   r+   r[   r   r   rx   rp   r}   r   r   r   )r%   r6   r   r   r   device_sizer   r   r   r    r/     sN   







r/   c                 C   sD   dddddd}d|   v rd} |   |v r||  S td|  dS )z9
    A force flag might be -F or -F, this look it up
    z-Fz-f)extbtrfsxfsreiserfsswapr   zForce flag for %s is unknown.rc   )r[   r)   r-   )fsflagsr   r   r    lookup_force_flag	  s   r   c              
   C   s  |  d}|  d}t|  dd}|  d}|  dg }|  dg }|  dd	}|  d
d	}t| tj|}td| |rE| r| rn|d  rT| d}d||f }t	|
 sgtd dS td|| td| t|\}	}
}td||	|
 |	|kr|
|krtd| |std| dS td| notd| nh|rt| dv r|}td| d}| dkrd	}t|||||d\}}td|| |rtd dS |s|r|rtd| |std|  dS n|rt| d kr
td!| ntd" dS td#||| |s$td$| dS |s2|s2td%j|d&d	}|rV| d |||d' }d}|rLtd(| |rUtd)| nMtd*| }|sgtd+| }|sstd,|| dS |g}|r|d-|g |st|d.krt|}|r|| |r|| || td/|| td0t| z
tj||d1 W dS  ty } z	td2||f |d}~ww )3a  
    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.
    rV   r%   r>   any
filesystemcmd
extra_optsrt   Fr   z#Checking %s against default devicesr   pz%s%sz/Path %s does not exist or is not a block deviceNz%Manual request of partition %s for %szChecking device %sz0Device '%s' has check_label='%s' check_fstype=%sz Existing file system found at %sz"Device %s has required file systemzDestroying filesystem on %sz#Device %s is cleared for formatting)autor   z-Identifying device to create %s filesystem onT)rl   rV   rs   rt   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 %snonez.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 %szDevice is not known: %szENo way to create filesystem '{label}'. fs_type or fs_cmd must be set.)rV   )rV   r   r%   z8fs_setup:overwrite ignored because cmd was specified: %sz9fs_setup:extra_opts ignored because cmd was specified: %szmkfs.%szmk%sz.Cannot create fstype '%s'.  No mkfs.%s commandz-Lr5   zCreating file system %s on %sz     Using cmd: %s)shellzFailed to exec of '%s':
%s)r   r+   r   r   r   r   r)   r*   r   r   is_block_devicer-   rn   r[   rv   rX   r   r   r   extendrb   r   rW   r0   )fs_cfgrV   r%   r>   rl   fs_cmdfs_opts
fs_replacer   check_labelrw   ro   odevicers   reuser   mkfs_cmd
force_flagr7   r   r   r    r4     s  










r4   r   )F)NNNTN)6__doc__loggingr   rJ   pathlibr   	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   cloudinit.distrosr   cloudinit.settingsr	   r   	getLogger__name__r)   r   __annotations__r+   r2   r8   r(   r3   rP   ra   rb   re   rn   rp   rv   rx   r}   r   r   r   r   r   r   r   r   r   r   r   r   r/   r   r4   r   r   r   r    <module>   sd   
5
&
2
 

8
().9G