o
    w7e                  
   @   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mZ ddlmZ dd	lmZ ed
ZedZedZedZedZedZedZddiZeeZdZ ddde egeedgddgdZ!ee"d< ee!Z de#dede
de$d df
d!d"Z%d#d$ Z&d%d& Z'dYd'd(Z(dZd*d+Z)d,d- Z*dZd.d/Z+d0d1 Z,d2d3 Z-				4	d[d5d6Z.d7d8 Z/d9d: Z0d;d< Z1d=d> Z2d?d@ Z3dAdB Z4dCdD Z5dEdF Z6dGdH Z7dIdJ Z8dKdL Z9dMdN Z:dOdP Z;dQdR Z<dSdT Z=dUdV Z>dWdX Z?dS )\z1Disk Setup: Configure partitions and filesystems.    N)dedent)subputil)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROS)PER_INSTANCEsfdisksgdisklsblkblkidblockdev	partprobewipefsLANGCai  This module is able to configure simple partition tables and filesystems.

.. note::
    for more detail about configuration options for disk setup, see the disk
    setup example

.. note::
    if a swap partition is being created via ``disk_setup`` then a ``fs_entry``
    entry is also needed in order for mkswap to be run, otherwise when swap
    activation is later attempted it will fail.

For convenience, aliases can be specified for disks using the
``device_aliases`` config key, which takes a dictionary of alias: path
mappings. There are automatic aliases for ``swap`` and ``ephemeral<X>``, where
``swap`` will always refer to the active swap partition and ``ephemeral<X>``
will refer to the block device of the ephemeral image.

Disk partitioning is done using the ``disk_setup`` directive. This config
directive accepts a dictionary where each key is either a path to a block
device or an alias specified in ``device_aliases``, and each value is the
configuration options for the device. File system configuration is done using
the ``fs_setup`` directive. This config directive accepts a list of
filesystem configs.
cc_disk_setupz
Disk Setupz$Configure partitions and filesystemsan              device_aliases:
              my_alias: /dev/sdb
              swap_disk: /dev/sdc
            disk_setup:
              my_alias:
                table_type: gpt
                layout: [50, 50]
                overwrite: true
              swap_disk:
                table_type: gpt
                layout: [[100, 82]]
                overwrite: true
              /dev/sdd:
                table_type: mbr
                layout: true
                overwrite: true
            fs_setup:
            - label: fs1
              filesystem: ext4
              device: my_alias.1
              cmd: mkfs -t %(filesystem)s -L %(label)s %(device)s
            - label: fs2
              device: my_alias.2
              filesystem: ext4
            - label: swap
              device: swap_disk.1
              filesystem: swap
            - label: fs3
              device: /dev/sdd1
              filesystem: ext4
            mounts:
            - ["my_alias.1", "/mnt1"]
            - ["my_alias.2", "/mnt2"]
            - ["swap_disk.1", "none", "swap", "sw", "0", "0"]
            - ["/dev/sdd1", "/mnt3"]
            
disk_setupfs_setup)idnametitledescriptiondistros	frequencyexamplesactivate_by_schema_keysmetar   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_devicey   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   r0   r*   r)   r+   handler   sZ   












rC   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')r=   r4   infor1   r2   r5   )r   tformerorignametransformedr*   r*   r+   r3      s*   r3   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: %sr0   z%s is mapped to disk=%s part=%srD   	partitionzKPartition '%s' from dotted device name '%s' overrides 'partition' key in %s
_partition)r1   r2   r4   r8   r&   r   expand_dotted_devnamer5   )r   rF   rA   rG   devparttformedr*   r*   r+   r>      s8   

r>   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>)shlexrP   )valuesstart_valueskeyvaluer*   r*   r+   value_splitter   s   
r[   Fc              
   c   s    t 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
    z--pairsz--outputzNAME,TYPE,FSTYPE,LABELz--nodepsN"Failed during disk check for %s
%sc                 S   s    g | ]}t | d kr|qS )r   )lenrP   rQ   r*   r*   r+   rT     s     z"enumerate_disk.<locals>.<listcomp>)r   typefstypelabel)		LSBLK_CMDappendr   r;   RuntimeErrorstrip
splitlinesr[   lower)r0   nodeps	lsblk_cmdrE   _errrB   partsrM   drY   rZ   r*   r*   r+   enumerate_disk   s>   

rl   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)rg   r^   N)rl   rf   )r0   rk   r*   r*   r+   device_type&  s
   rm   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 failedFrM   Tr@   )rm   r;   r4   r8   )r   rI   d_typer*   r*   r+   is_device_valid1  s   rp   c           
   
   C   s   d\}}}}t 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
    )NNNNz-cz	/dev/nullr      )rcsr\   N   )rW   r`   r^   uuid)	BLKID_CMDr   r;   rc   r]   re   r[   rf   )
r0   outr`   fs_typert   	blkid_cmdri   rB   rY   rZ   r*   r*   r+   check_fsC  s.   	

ry   c                 C   s   t | \}}}|S )z7
    Returns true if the device has a file system.
    )ry   )r0   _rw   r*   r*   r+   is_filesystemc  s   r{   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!
    Nrn   r@   rM   Fr_   /dev/%sr   r`   Tr^   z5Failed to find device during available device search.)NF)rl   r4   r8   )r0   rw   r`   valid_targetslabel_match
replace_fsraw_device_usedrk   r*   r*   r+   find_device_nodek  s2   
r   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.
    rs   TF)r]   r=   rl   ry   )r0   rz   check_fstyper*   r*   r+   is_disk_used  s   	r   c              
   C   sh   zt  td| g\}}t  td| g\}}W n ty+ } z	td| |f |d }~ww t|t| S )Nz--getsize64z--getsszFailed to get %s size
%s)r   
BLKDEV_CMDr;   rc   int)r0   size_in_bytesrz   sector_sizerB   r*   r*   r+   get_hdd_size  s   r   c              
   C   s   t |  t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.
    z-l%s
data(Error running partition command on %s
%sNr   )extendedemptyrs   T)reverse/)read_parttbl
SFDISK_CMDr   r;   rc   re   rP   r]   rf   sortedrangeisdigitrb   )r0   layoutprt_cmdrv   ri   rB   found_layoutline_line
type_labelrS   r*   r*   r+   check_partition_mbr_layout  s:   


r   c              
   C   s   t 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 )Nz-p)
update_envr   Numberc                 S   s   g | ]
}|   d  qS )   )rd   rP   )rR   r   r*   r*   r+   rT     s    z.check_partition_gpt_layout.<locals>.<listcomp>   00r   rq   )
SGDISK_CMDr   
LANG_C_ENVr;   rc   iterre   rd   
startswithr]   endswithrb   )r0   r   r   rv   ri   rB   	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: %srs   TFc                 S   s*   g | ]}t |ttfrt|d  ndqS )rs   N)r1   tupler=   r6   rQ   r*   r*   r+   rT   1  s    z*check_partition_layout.<locals>.<listcomp>zLayout types=%s. Found types=%sN)
r   r   rc   r4   r5   r1   boolr]   zipr6   )
table_typer0   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   rs   rq   %Partition was incorrectly defined: %sd   z,,%sz,%s,%s
z-Calculated partition definition is too big
%s)	r1   r=   r   r]   rc   r   floatrb   join)
sizer   last_part_numpart_definitionpart_numrM   	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   rq   r   r   z+{}r   )	r1   r   r=   r]   rc   r   r   rb   format)r   r   partition_specsrI   r   partition_typer   r*   r*   r+   get_partition_gpt_layoutx  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   )r0   null	start_lenend_lenfpr*   r*   r+   purge_disk_ptable  s   
r   c                 C   s   t | D ]6}|d dvr:t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 parition table entries
    r^   )r@   cryptz--allr|   r   zPurging filesystem on /dev/%szFailed FS purge of /dev/%sN)rl   
WIPEFS_CMDr4   rE   r   r;   rc   r   )r0   rk   
wipefs_cmdrB   r*   r*   r+   
purge_disk  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   rc   )r   r   r   r*   r*   r+   get_partition_layout  s
   	

r   c              
   C   st   t dur	t | g}ntd| g}t  zt| W n ty3 } 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.
    Nz
--rereadptz%Failed reading the partition table %s)PARTPROBE_CMDr   r   udevadm_settler   r;   r<   r4   )r0   	probe_cmdrB   r*   r*   r+   r     s   

r   c              
   C   sX   t 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
    z--forcer   r   z Failed to partition device %s
%sN)r   r   r;   rc   r   )r0   r   r   rB   r*   r*   r+   exec_mkpart_mbr  s   

r   c              
   C   s   zBt  td| g t|D ]3\}\}\}}|d7 }t  tdd|||| g |d ur@t|dd}t  tdd||| g qW n tyP   td	|   w t	|  d S )
Nz-Zrs   z-nz{}:{}:{}r   0z-tz{}:{}zFailed to partition device %s)
r   r   	enumerater   r6   ljustr;   r4   r8   r   )r0   r   indexr   rW   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   rc   r0   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-exisiting 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   realpathr4   r5   r&   r1   r   rp   rc   r   r6   rf   r   r   r   r{   r   r   r   r   )r0   rA   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.rn   )rf   r4   r8   )fsflagsr*   r*   r+   lookup_force_flagX  s   r   c              
   C   sr  |  d}|  d}t|  dd}|  d}|  dg }|  dg }|  dd	}|  d
d	}t| tj|}td| |rE| r| rVd||f }td|| td| t	|\}	}
}td||	|
 |	|kr|
|krtd| |std| dS t
d| nmtd| nf|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|rt| dkrtd| ntd dS td ||| |s
t
d!| dS |s|std"j|d#d	}|r<| d |||d$ }d}|r2t
d%| |r;t
d&| nMtd'| }|sMtd(| }|sYt
d)|| dS |g}|rf|d*|g |spt|d+kr|t|}|r||| |r|| || td,|| td-t| z
tj||d. W dS  ty } z	td/||f |d}~ww )0a  
    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.
    r`   r0   rI   any
filesystemcmd
extra_optsr   Fr   z#Checking %s against default devicesz%s%sz%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 systemNzDestroying filesystem on %sz"Device %s is cleared for formating)autor   z,Identifying device to create %s filesytem onT)rw   r`   r~   r   z(Automatic device for %s identified as %sz+Found filesystem match, skipping formating.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.)r`   )r`   r   r0   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-Lr@   zCreating file system %s on %sz     Using cmd: %s)shellzFailed to exec of '%s':
%s)r&   r6   r   r   r   r   r4   r5   r   ry   r8   rf   r   rc   r   r   whichextendrm   r   rb   r;   )fs_cfgr`   r0   rI   rw   fs_cmdfs_opts
fs_replacer   check_labelr   rz   odevicer~   reuser   mkfs_cmd
force_flagrB   r*   r*   r+   r?   n  s   








r?   r%   )F)NNNTN)@__doc__loggingr   rU   textwrapr   	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   r   cloudinit.distrosr	   cloudinit.settingsr
   r   r   r   ra   ru   r   r   r   r   	getLogger__name__r4   MODULE_DESCRIPTIONr   __annotations__r6   r=   rC   r3   r>   r[   rl   rm   rp   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r:   r   r?   r*   r*   r*   r+   <module>   s   







)35
&
2
 

8
').9G