3 \me* @sdUdZddlZddlZddlZddlZddlmZddlmZm Z ddl m Z ddl m Z ddlmZmZddlmZdd lmZd Zd d d edegddgegdZeeeZejeZddZddZddZddZddZddZddZ d d!Z!d"efd#efd$efd%efd&efd'efd(e fgZ"d%e!iZ#d)d*Z$d+d,Z%e&e e e'dd-d.d/Z(d0d1Z)dS)2z:Resizefs: cloud-config module which resizes the filesystemN)dedent)subputil)Cloud)Config) MetaSchema get_meta_doc) ALL_DISTROS) PER_ALWAYSZnoblockZ cc_resizefsZResizefszResize filesystema Resize a filesystem to use all avaliable space on partition. This module is useful along with ``cc_growpart`` and will ensure that if the root partition has been resized the root filesystem will be resized along with it. By default, ``cc_resizefs`` will resize the root partition and will block the boot process while the resize command is running. Optionally, the resize operation can be performed in the background while cloud-init continues running modules. This can be enabled by setting ``resize_rootfs`` to ``noblock``. This module can be disabled altogether by setting ``resize_rootfs`` to ``false``.z@resize_rootfs: false # disable root filesystem resize operationzAresize_rootfs: noblock # runs resize operation in the background)idnametitle descriptionZdistrosZexamplesZ frequencyZactivate_by_schema_keyscCstj| r0tjjd|r0ddddd|g}ndddd|g}tjjd}tjjtjddgdjd d j }||kr|j d}|j |d d t |S) Nz %s/.snapshotsbtrfsZ filesystemresizemaxz5.10z --versionrvz --enqueue) rZmount_is_read_writeospathisdirZVersionZfrom_strrsplitstripindexinserttuple) mount_pointdevpthcmdZbtrfs_with_queueZsystem_btrfs_veridxr!!/usr/lib/python3.6/cc_resizefs.py _resize_btrfs8s   " r#cCsd|fS)NZ resize2fsr!)rrr!r!r" _resize_extYsr$cCsd|fS)NZ xfs_growfsr!)rrr!r!r" _resize_xfs]sr%cCs dd|fS)Ngrowfsz-yr!)rrr!r!r" _resize_ufsasr'cCsddd||fS)NzpoolZonlinez-er!)rrr!r!r" _resize_zfsesr)cCs dd|fS)Nhammer2r&r!)rrr!r!r"_resize_hammer2isr+cCs ddd|fS)zSingle device resizebcachefsZdevicerr!)rrr!r!r"_resize_bcachefsmsr-cCsdd}d}ytjdd|gWnBtjk r^}z$|jj|rJ||jkrJdS|WYdd}~XnXdS)Nzgrowfs: requested sizez.is not larger than the current filesystem sizer&z-NTF)rProcessExecutionErrorstderr startswith)rrZ skip_startZ skip_container!r!r"_can_skip_resize_ufsrsr2rZextZxfsZufszfsr*r,cCs6|j}x(tjD]\}}|j|r|||SqWdS)NF)lowerRESIZE_FS_PRECHECK_CMDSitemsr0)fs_type resize_whatr fstype_lcifuncr!r!r"can_skip_resizes  r<cCstj}|dkrTtjj| rT| rTtjtj}|dkrHtjddStj d||dkrntj d||dS|j drtj d|S|j d  rtjj| rd |j d }tj d |||}ytj |}Wnft k r4}zH|o|jtjkrtj d ||n"|jtjkr"tjd ||n|dSd}~XnXt j|j r~t j|j r~|rltj d||ntjd||dS|S)aReturn updated devpath if the devpath is a writable block device. @param devpath: Requested path to the root device we want to resize. @param info: String representing information about the requested device. @param log: Logger to which logs will be added upon error. @returns devpath or updated devpath per kernel commandline if the device path is a writable block device, returns None otherwise. z /dev/rootNz!Unable to find device '/dev/root'z.Converted /dev/root to '%s' per kernel cmdlineZ overlayrootz)Not attempting to resize devpath '%s': %szgpt/z#We have a gpt label - just go aheadz/dev//z:'%s' doesn't appear to be a valid device path. Trying '%s'z9Device '%s' did not exist in container. cannot resize: %sz,Device '%s' did not exist. cannot resize: %sz>device '%s' not a block device in container. cannot resize: %sz1device '%s' not a block device. cannot resize: %s)rZ is_containerrrexistsZrootdev_from_cmdlineZ get_cmdlineLOGwarningdebugr0lstripstatOSErrorerrnoENOENTS_ISBLKst_modeS_ISCHR)ZdevpathinfoZ containerZ fulldevpathZstatretexcr!r!r"maybe_get_writable_device_paths^       rL)r cfgcloudargsreturncCst|dkr|d}ntj|dd}tj|tgdsDtjd|dSd}tj|t}|shtjd|dS|\}}} |dkr|j dd} tj | }|sdS| }d || |f} tjd | t || }|sdSd} t |||rtjd ||dS|j } x$tD]\}}| j|r|} PqW| s2tjd ||dS| ||}tjd ||dj||tkrxtjtjtjdt|fdntjtjdt|fdd}|tkrd}tjd|||dS)NrZ resize_rootfsT)Zaddonsz+Skipping module named %s, resizing disabledr=z)Could not determine filesystem type of %sr3zdev=%s mnt_point=%s path=%szresize_info: %sz%Skip resize filesystem type %s for %sz.Not resizing unknown filesystem type %s for %szResizing %s (%s) using %s zbackgrounded Resizing)Zlogfuncmsgr;rOZResizingZResizedzResizing (via forking)z$%s root filesystem (type=%s, val=%s))lenrZget_cfg_option_strZtranslate_boolNOBLOCKr?rAZget_mount_infor@rZget_device_info_from_zpoolrLr<r4RESIZE_FS_PREFIXES_CMDSr0joinZfork_cbZlog_time do_resize)r rMrNrOZ resize_rootr8resultrr7rr(rJZresizerr9ZpfixZroot_cmd resize_cmdactionr!r!r"handlesv                 r[c Cs:ytj|Wn&tjk r4tjtd|YnXdS)Nz$Failed to resize filesystem (cmd=%s))rr.rZlogexcr?)rYr!r!r"rWFs rW)*__doc__rEZloggingrrCtextwraprZ cloudinitrrZcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemarrZcloudinit.distrosr Zcloudinit.settingsr rTmetaZ getLogger__name__r?r#r$r%r'r)r+r-r2rUr5r<rLstrlistr[rWr!r!r!r" sX        ! PY