fN UdZddlZddlZddlZddlZddlmZddlmZddl m Z m Z m Z ddl mZddlmZddlmZmZdd lmZd Zed Zd gZd ddeeeedgegdZeed<eeZdZej8eZdZej8eZej8dezZ dZ!dZ"dZ#dZ$ejJe&Z'dZ(dZ)dZ*d+dZ+d+dZ,d,dZ-d e.d!e.d"dfd#Z/d-d$Z0d%Z1d&e.d'ed(ed)e2d"df d*Z3y).z-Mounts: Configure mount points and swap filesN) whitespace)dedent)subp type_utilsutil)Cloud)Config) MetaSchema get_meta_doc) PER_INSTANCEa This module can add or remove mountpoints from ``/etc/fstab`` as well as configure swap. The ``mounts`` config key takes a list of fstab entries to add. Each entry is specified as a list of ``[ fs_spec, fs_file, fs_vfstype, fs_mntops, fs-freq, fs_passno ]``. For more information on these options, consult the manual for ``/etc/fstab``. When specifying the ``fs_spec``, if the device name starts with one of ``xvd``, ``sd``, ``hd``, or ``vd``, the leading ``/dev`` may be omitted. Any mounts that do not appear to either an attached block device or network resource will be skipped with a log like "Ignoring nonexistent mount ...". Cloud-init will attempt to add the following mount directives if available and unconfigured in `/etc/fstab`:: mounts: - ["ephemeral0", "/mnt", "auto","defaults,nofail,x-systemd.requires=cloud-init.service", "0", "2"] - ["swap", "none", "swap", "sw", "0", "0"] In order to remove a previously listed mount, an entry can be added to the `mounts` list containing ``fs_spec`` for the device to be removed but no mountpoint (i.e. ``[ swap ]`` or ``[ swap, null ]``). The ``mount_default_fields`` config key allows default values to be specified for the fields in a ``mounts`` entry that are not specified, aside from the ``fs_spec`` and the ``fs_file`` fields. If specified, this must be a list containing 6 values. It defaults to:: mount_default_fields: [none, none, "auto","defaults,nofail,x-systemd.requires=cloud-init.service", "0", "2"] Non-systemd init systems will vary in ``mount_default_fields``. Swap files can be configured by setting the path to the swap file to create with ``filename``, the size of the swap file with ``size`` maximum size of the swap file if using an ``size: auto`` with ``maxsize``. By default no swap file is created. .. note:: If multiple mounts are specified where a subsequent mount's mountpoint is inside of a previously declared mount's mountpoint (i.e. the 1st mount has a mountpoint of ``/abc`` and the 2nd mount has a mountpoint of ``/abc/def``) then this will not work as expected - ``cc_mounts`` first creates the directories for all the mountpoints **before** it starts to perform any mounts and so the sub-mountpoint directory will not be created correctly inside the parent mountpoint. For systems using util-linux's ``mount`` program this issue can be worked around by specifying ``X-mount.mkdir`` as part of a ``fs_mntops`` value for the subsequent mount entry. a< # Mount ephemeral0 with "noexec" flag, /dev/sdc with mount_default_fields, # and /dev/xvdh with custom fs_passno "0" to avoid fsck on the mount. # Also provide an automatically sized swap with a max size of 10485760 # bytes. mounts: - [ /dev/ephemeral0, /mnt, auto, "defaults,noexec" ] - [ sdc, /opt/data ] - [ xvdh, /opt/data, auto, "defaults,nofail", "0", "0" ] mount_default_fields: [None, None, auto, "defaults,nofail", "0", "2"] swap: filename: /my/swapfile size: auto maxsize: 10485760 all cc_mountsMountsz%Configure mount points and swap filesz # Create a 2 GB swap file at /swapfile using human-readable values swap: filename: /swapfile size: 2G maxsize: 2G )idnametitle descriptiondistrosexamples frequencyactivate_by_schema_keysmetaz&^([x]{0,1}[shv]d[a-z][0-9]*|sr[0-9]+)$z^.+:.*z[%s]+z /etc/fstabzcomment=cloudconfigi@cl|dvrydD]*}|j|s|jddk(s*yy)N)amirootswapT) ephemeralebs:F) startswithfind)renumnames >4 5- -. r'ctjj|jdd}tjj d|}|Otjj |tjj|jdd}tjj |S)N/r!z /sys/block/)r/r0realpathsplitjoinr1)r2partition_path device_namesys_paths r%_is_block_devicer@s''"";/55c:2>Kww||M;7H!77<< bgg&&~6<>( ##r'c:tjd||}|dk(rd}tjdt|r|Stj|\}}|}|r.|j ||}||k7rtjd||t |r9||}|sy|jdsd|}tjd||ntj|rd|}d}| t|d }nt||}|yt||r||S|Sy) Nz+Attempting to determine the real name of %sr ephemeral0z2Adjusted mount option from ephemeral to ephemeral0zMapped device alias %s to %sr9z/dev/zMapped metadata name %s to %s) LOGdebugr+rexpand_dotted_devnamegetr&r"DEVICE_NAME_REr*r7r@) startname transformeraliasesdevnamer2r3origr=s r%sanitize_devnamerNs-II;YGG+ FG#$($>$>w$G!K! Dkk+{; ;  II4dK H;'!+. %%c*'24K 14E    *'24KN6{AF6 )   ! ^4  %! ! r'c |tjd}|dz}d||d}|||}nk|retj|}|j|j z}||d<|t t|dz |}n||dzkDrt|dz}n||}||d<|dtzkr|}nB|d tzkr dtz}n,ttj|tz tz}t ||}||d <i} |jD]*\} } t| trd | tz z| | <&| | | <,tj!d | d | d | d| d| d|S)Ntotalna)availmax_inmemrSg?maxsizez%s MBzCsuggest %s swap for %s memory with '%s' disk given max=%s [max=%s]'rUrT)r read_meminfor/statvfsf_frsizef_bfreeminintGBroundmathsqrtitems isinstanceMBrDrE) memsizemaxsizefsyssugg_maxinfor[rSminsizerYpinfokvs r%suggested_swapsizerps##%g.{HWW =D | **T"  7??2W ?#eai.(3G  &%#+&G DKR 27 b& 'B,/025 w DDL E 1 a !b&)E!HE!H  IIM f  e  g h e  Kr'fnamerYreturncdfd}tjj|}tj|tj |ddk(r1t j ddd|gt j dd |gd k(r"tjd kr |||d n |||d tjj|rtj|d t j d|gy#t j$r"tjd|||d YwxYw#t j$rtj|wxYw)zSize is in MiB.z6Failed to create swapfile '%s' of size %sMB via %s: %sc<tjd|||dk(r ddd|z|g}n|dk(r ddd|zdd |zg} tjd y#tj$r4}tj ||||t j |d}~wwxYw) Nz3Creating swapfile in '%s' on fstype '%s' using '%s' fallocatez-lz%sMddz if=/dev/zerozof=%szbs=1Mzcount=%sT)capture)rDrErProcessExecutionErrorrkrdel_file)rqrYmethodcmdeerrmsgfstypes r% create_swapz$create_swapfile..create_swap!s A      [ edlE:C t^%T! C  IIc4 ())  HHVUD&! 4 MM%  sAB'/BBrCbtrfstruncatez-s0chattrz+Cxfs)rVrvruz4fallocate swap creation failed, will attempt with ddimkswapN)r/r0dirnamer ensure_dirget_mount_inforkernel_versionrxrDrkr1chmodry)rqrYrswap_dirr}r~s @@r%create_swapfilers4FF4wwu%HOOH   *1 -F  :tS%01 8T5)* 4..07:E4& + t[ 1  ww~~e 5%  8U#$)) + HHK L tT * +  % % e s5 D 5E 2EE*E/ctjj|}t|j dk(r; t j d}t j|t|||}tt|dz }|stjdyt jtjdt||g |S#t$rtjdYywxYw) z fname: full path string of filename to setup size: the size to create. set to "auto" for recommended maxsize: the maximum size autorPz)Not creating swap: failed to read meminfoN)rirhrgrz'Not creating swap: suggested size was 0zSetting up swap file)msgfuncargs)r/r0rr.lowerrrZIOErrorrDrErrpr_log_timer)rqrYrhrrgmibsizes r%setup_swapfilerVs wwu%H 4yF" '')'2G !!7G #den%&G  ;<MM " W   L+  IIA B  sCC65C6ct|tstjdy|j dd}|j dd}|j dd}|r|stj dyt jj|rt jjd stj d ||S tjd jD]0}|j|d zstj d ||cStj d | t|tr|dk7rtj|}t|trtj|}t!|||S#t$rtjd||cYSwxYw#t$r }tjd|Yd}~yd}~wwxYw)zahandle the swap config, calling setup_swap if necessary. return None or (filename, size) z%input for swap config was not a dict.Nfilenamez /swap.imgrYrrhzno need to setup swapz /proc/swapsz:swap file %s exists, but no /proc/swaps exists, being safe zswap file %s already in usez+swap file %s exists, but not in /proc/swapsz.swap file %s exists. Error reading /proc/swapsr)rqrYrhzfailed to setup swap: %s)redictrDwarningrGrEr/r0r1rload_text_file splitlinesr" Exceptionr. human2bytesr)swapcfgrqrYrhliner|s r%handle_swapcfgrxs gt $ ;< KK K 0E ;;vq !Dkk)T*G U )* ww~~eww~~m, IIL L ++M:EEG !??53;/II;UC L ! IICU K3 dC TV^##D)D gs #&&w/GEgFF  KK@% L   3 .22 3s7:F F#F;AF?!F<;F<? G(G##G(rcfgcloudrc <d}|jj}|rd}ddd|ddg}|jd|}ddd|d ddggd g}g}d |vr|d }tj d |g} i} g} t j jtrutjtjD]K} t| vr| j|  tj| } | |  d <| j| M|jdi}t#t%|D]}t'||t(s1tj+d|dzt-j.||Ht1||d }t3||j4|}||k7rtj d|||tj d||| vrtj7d|| ||||d <t#t%||D]"}||| t1||||||<$ t#t%|D]}t#t%|D]?}t%|||kr||j||,|||5|||||<A||dbt#|D]}||d ||d k(sd||d<|D]}|d }t3||j4|}||k7rtj d|||tj d|U|| vrtj d|| |t||d <d}|D]}|d |d k(sd}n|rtj d||j|g}|D]2}|dtj d|d "|j|4t9|jdi}|r|j|dddddgt%|d k(rtj dyg}d}d}g}|D]e}|d dt|d <|d dk(rd}|dj;d!r|j|d|jd"j=|gtj>jADcgc]\}}d#|vs |d#} }}|D]"}! tjB|!|r|!| vs!d}$|D"cgc]}"tjGd%|"}#}"| D"cgc]}"tjGd%|"}$}"|$D%cgc] }%|%|#vsd&|%zc}%|#D&cgc] }&|&|$vsd'|&zc}&z}'| jI|d(d)j=| z}(tjJt|(g})|r|)jd*d+gt%|'d k(rtj d,ntj d-|'d}|r(|)jd.d+g|r|)jd/d0gd1}*|)D]B}+d2d%j=|+z}* tMjL|+tj |*d3Dy#t $rYwxYwcc}}w#t $rtjDtd$|!YwxYwcc}"wcc}"wcc}%wcc}&w#tLjN$r4tj+|*d4tjDt|*d4YwxYw)5Nzdefaults,nobootwaitz=defaults,nofail,x-systemd.requires=cloud-init.service,_netdevrr2mount_default_fieldsrBz/mnt)rnonerswrrmountszmounts configuration is %srdevice_aliasesz,Mount option %s not a list, got a %s insteadrC)rKzchanged %s => %sz#Ignoring nonexistent named mount %sz&Device %s already defined in fstab: %szchanged default device %s => %sz+Ignoring nonexistent default named mount %sFTz-Not including %s, already previously includedz$Skipping nonexistent device named %srrrz No modifications to fstab needed,rQr9 mountpointz Failed to make '%s' config-mountrz- z+ z%s  swaponz-azNo changes to /etc/fstab made.zChanges to fstab: %smount systemctlz daemon-reloadz#Activating swap and mounts with: %szActivate mounts: %s:PASSFAIL)(distro uses_systemdrGrDrEr/r0r1 FSTAB_PATHrrr MNT_COMMENTappendWSr;rrangelenrelistrrobj_namer.rNdevice_name_to_devicerkrr"r<rrdrlogexcsubextend write_filerrx),rrrr def_mnt_optsrdefvalsdefmntscfgmnt fstab_lines fstab_devs fstab_removedrtoksristart sanitizedjdefmnt cfgmnt_hascfgmactlistxswapretcc_linesneedswapneed_mount_alldirsentryrnro mount_pointsdnsaddssdropsdropaddsopscontents activate_cmdsfmtr{s, r%handlers\(L<<,,.L K T6<c:Ggg,g6G vvwqz3<0G F3XII*F3KJM ww~~j!'' 3>>@ %Dd"$$T* xx~#'JtAw    t $ %WW-r2N 3v; $1&)T* KK>Q##F1I.  F1IaL!$ 5..    II(% ;   II;U C  * $ HH89%   q ! s6!9~& 1Aay|#"6!9Q<0q !  1A$1L3v; (s7|$ *A6!9~"q   ,1%&qzq !  * !9Q< 1X (!9Q<6!9Q</#'F1IaL (($q $ 5..    II7 J   IICU K  * $ II89%  q   DAw&)#!    IIEu M  f=DG  Q4< IIrsy4 ,,!#<+3j  $ '   4%   !'j, t ?./"**01RZZ:&'  #   g!  $.b6r737c7d7tD-`Q*Q*6Q*%Q*tQ*Q*r'