܍~cL UdZddlZddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z m Z ddlmZddlmZdd lmZmZdd lmZd Ze d Zd gZdddeeee dgegdZeed<eeZdZejeZdZejeZ ejdezZ!dZ"dZ#dZ$dZ%ej&e'Z(dZ)dZ*dZ+d-dZ,d-dZ-d.d Z.d!e/d"e/d#dfd$Z0d/d%Z1d&Z2d'e/d(ed)ed*ed+e3d#df d,Z4dS)0z-Mounts: Configure mount points and swap filesN)Logger) 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 options to be specified for the values in a ``mounts`` entry that are not specified, aside from the ``fs_spec`` and the ``fs_file``. 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. 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@c~|dvrdSdD]3}||r|ddkrdS4dS)N)amirootswapT) ephemeralebs:F) startswithfind)renumnames t|rdSdS)NTF)NETWORK_NAME_REmatch)rs r&is_network_devicer,s"T""t 5r(ct|d|d|g}|D]+}||}tj|r|cS,dS)Npz-part)strospathexists) device_pathpartition_numberpotential_suffixessuffixpotential_partition_devices r&_get_nth_partition_for_devicer8s{ !!##%%' %../:{FF%C" 7>>4 5 5 .- - - - . 4r(ctj|dd}tjd|}|Vtj|tj|dd}tj|S)N/r"z /sys/block/)r0r1realpathsplitjoinr2)r3partition_path device_namesys_paths r&_is_block_devicerAs'"";//55c::2>Kw||M;77H!7<< bg&&~66<>( # ##r(c|d||}|dkrd}|dt|r|Stj|\}}|}|r3|||}||kr|d||t |rA||}|sdS|dsd|}|d||nt|rd|}d}|t|d }nt||}|dSt||r||S|SdS) Nz+Attempting to determine the real name of %sr ephemeral0z2Adjusted mount option from ephemeral to ephemeral0zMapped device alias %s to %sr:z/dev/zMapped metadata name %s to %s) debugr,rexpand_dotted_devnamegetr'r#DEVICE_NAME_REr+r8rA) startname transformerlogaliasesdevnamer3r4origr>s r&sanitize_devnamerOsII;YGGGG+ FGGG##$($>w$G$G!K! DIkk+{;; ;   II4dK H H H;'' 5!k+..  4%%c** 5 5'2{4K 14EEEE    * * 5 5'2{4KN6{AFF6 )    !4 ^44  %! ! 4r(c ,|tjd}|dz}d||d}|||}nm|rgtj|}|j|jz}||d<|!t t|dz |}n ||dzkrt|dz}n||}||d<|dtzkr|}nJ|d tzkr dtz}n1ttj |tz tz}t ||}||d <i} | D]0\} } t| trd | tz z| | <+| | | <1td | d | d | d| d| d|S)Ntotalna)availmax_inmemrTg?maxsizez%s MBzCsuggest %s swap for %s memory with '%s' disk given max=%s [max=%s]'rVrU)r read_meminfor0statvfsf_frsizef_bfreeminintGBroundmathsqrtitems isinstanceMBLOGrE) memsizemaxsizefsyssugg_maxinfor\rTminsizerZpinfokvs r&suggested_swapsizerrs#%%g.{HWW = =D | *T"" 7?2W ?#eai..(33GG  & &%#+&&G DKR 27  b& 'B,//0025 w DDL E 1 a   !b&)E!HHE!HHIIM f  e  g h e  Kr(fnamerZreturncdfd}tj|}tj|tj|ddkrtjdksdkr|||dnK |||dn<#tj$r*t d |||dYnwxYwtj |rtj |d  tjd |gd S#tj$rtj |wxYw) zSize is in MiB.z6Failed to create swapfile '%s' of size %sMB via %s: %scJtd|||dkr ddd|z|g}n|dkr ddd|zdd |zg} tj|d dS#tj$r8}t||||t j|d}~wwxYw) Nz3Creating swapfile in '%s' on fstype '%s' using '%s' fallocatez-lz%sMddz if=/dev/zerozof=%szbs=1Mzcount=%sT)capture)rhrErProcessExecutionErrorrmrdel_file)rsrZmethodcmdeerrmsgfstypes r& create_swapz$create_swapfile..create_swaps A        [ edlE:CC t^^%T! C  Ic4 ( ( ( ( ( ()    HHVUD&! 4 4 4 M%  sAB"*3BB"rDxfs)rWbtrfsrxrwz4fallocate swap creation failed, will attempt with ddimkswapN)r0r1dirnamer ensure_dirget_mount_infokernel_versionrrzrhrmr2chmodr{)rsrZrswap_dirrrs @@r&create_swapfilersFF4wu%%HOH   * *1 -F %D/11G;; 7   E4&&&& + Kt[ 1 1 1 1) + + + HHK L L L KtT * * * * * + w~~e! 5%    8U#$$$$$  % e s B6CC D!!%Ec2tj|}t|dkrl t jd}n+#t$rt dYdSwxYwt j |t|||}tt|dz }|st ddSt j tj dt||g |S) z fname: full path string of filename to setup size: the size to create. set to "auto" for recommended maxsize: the maximum size autorQz)Not creating swap: failed to read meminfoN)rkrjrirz'Not creating swap: suggested size was 0zSetting up swap file)msgfuncargs)r0r1rr/lowerrr[IOErrorrhrErrrr`log_timer)rsrZrjrrimibsizes r&setup_swapfilerHs wu%%H 4yyF"" '))'2GG    IIA B B B FF  !!!!7G   #den%%&&G  ;<<<M " W   LsA $BBcpt|tstddS|dd}|dd}|dd}|r|stddSt j|rt jd std ||S tj d  D]9}| |d zrtd ||cS:td |n-#t$r td||cYSwxYw t|tr|dkrtj|}t|trtj|}t!|||S#t$r%}td|Yd}~nd}~wwxYwdS)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.imgrZrrjzno 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)rsrZrjzfailed to setup swap: %s)rfdictrhwarningrGrEr0r1r2r load_file splitlinesr# Exceptionr/ human2bytesr)swapcfgrsrZrjliner~s r&handle_swapcfgrjsM gt $ $ ;<<<t KK K 0 0E ;;vq ! !Dkk)T**G U )*** w~~ew~~m,,  IIL   L }55@@BB ! !??53;//!II;UCCC LLL! IICU K K K K    KK@%   LLL   3 dC  *TV^^#D))D gs # # 0&w//GEgFFFF 333 .222222223 4s20AE,E,,'FFA)H H3H..H3rcfgcloudrKrc ^%&d}|j}|rd}ddd|ddg}|d|}ddd|d ddggd g}g} d |vr|d } td | g} i} g} t jtrtj t D]m} t| vr| | ! t| }n#t $rYnwxYw| | |d <| | n|di}t#t%| D]C}t'| |t(s3|d|dzt-j| |Qt1| |d }t3||j||}||kr|d|d|||d||| vr|d|| ||| |d <t#t%| |D]5}| ||t1| ||| ||<6Et#t%| D]}t#t%|D]\}t%| ||kr"| | ||=| ||||| ||<]| |d;t#|D]+}| |d | |d kr d| |d<,|D]}|d }t3||j||}||kr|d|d|||d|\|| vr|d|| |~||d <d}| D]}|d |d krd}n|r|d|| |g}| D]<}|d|d|d '| |=t9|di}|r| |dddddgt%|d kr|ddSg}d}d}g}|D]} | d d t| d <| d!dkrd}| dd"r| | d| d#| d$tj D}|D]F} tj!|n&#t $rtj"|d%|YnwxYw|s||vrd}Gd&|D%d'| D&%fd(&D&fd)%Dz} | #|d*d+| z}!tj$t|!g}"|r|" d,d-gt%| d kr|d.n|d/| d}|r0|" d0d-g|r|" d1d2gd3}#|"D]}$d4d5|$z}# tKj%|$||#d6F#tJj&$r/||#d7tj"||#d7YwxYwdS)8Nzdefaults,nobootwaitz=defaults,nofail,x-systemd.requires=cloud-init.service,_netdevr02mount_default_fieldsrCz/mnt)rnonerswrrmountszmounts configuration is %srdevice_aliasesz,Mount option %s not a list, got a %s insteadrD)rLzchanged z => z#Ignoring nonexistent named mount %sz&Device %s already defined in fstab: %szchanged default device z+Ignoring nonexistent default named mount %sFTz-Not including %s, already previously includedz$Skipping nonexistent device named %srrrz No modifications to fstab needed,rRr: c,g|]\}}d|v |dS) mountpoint).0rprqs r& zhandle..=s1 Aq\Q=N=N,=N=N=Nr(z Failed to make '%s' config-mountcDg|]}td|SrWSsubrns r&rzhandle..Ks$ . . .RVVC^^ . . .r(cDg|]}td|Srrrs r&rzhandle..Ls$ 4 4 4bffS!nn 4 4 4r(c"g|] }|vd|z S)z- r)rdropsaddss r&rzhandle..Ns' @ @ @Dd%.?.?D4K.?.?.?r(c"g|] }|vd|z S)z+ r)raddsdropss r&rzhandle..Ns1DDDs&'8'8s '8'8'8r(z%s  swaponz-azNo changes to /etc/fstab made.zChanges to fstab: %smount systemctlz daemon-reloadz#Activating swap and mounts with: %szActivate mounts: %s:rPASSFAIL)'distro uses_systemdrGrhrEr0r1r2 FSTAB_PATHrrr MNT_COMMENTappendrr<rrangelenrflistrrobj_namer/rOdevice_name_to_devicermrr#r=rrerlogexcextend write_filerrz)'rrrrKr def_mnt_optsrdefvalsdefmntscfgmnt fstab_lines fstab_devs fstab_removedrtoksristart sanitizedjdefmnt cfgmnt_hascfgmactlistxswapretcc_linesneedswapneed_mount_alldirs mount_pointsdsopscontents activate_cmdsfmtr}rrs' @@r&handlers )L<,,..L K T6<c:Ggg,g66G vvwqz3<000G F3XII*F333KJM w~~j!! %N:..99;; % %Dd""$$T*** xx~~    "&JtAw    t $ $ $ $WW-r22N 3v;;  $1$1&)T**  KK>Q#F1I..    F1IaL!!$ 5.^       IIIEEE99= > > >   II;U C C C  * $ $ HH89%     q ! s6!9~~&& 1 1Aay|#"6!9Q<00q !  1 3v;;  ((s7||$$ * *A6!9~~""q   ,,,,1%&qzq ! !9Q< 1XX ( (!9Q<6!9Q<//#'F1IaLq $ 5.^       III555))L M M M   IICU K K K  * $ $ II89%    q    DAw&)##! $   IIEu M M M  fG  Q4< IIrs433 ,,,,,,,,,,!!!!!!######<<<<<<<<++++++&P &  $ '   4%    !'j, ,t  ?.//"*011RZ:&''  #   g!!      $$$$....b6666r535c5d5555pD---`S* S*S*#(S*/5S*=AS* S*S*S*S*S*S*r(