
    d                         d dl Z d dlmc mZ d dlmc mc mZ d dlmc mc mZ d dl	mc m
Z
 d dlmZ d dlmZ  G d de      Zy)    N)ResourceDiskError)ResourceDiskHandlerc                   >     e Zd ZdZ fdZed        Zd Zd Z xZ	S )FreeBSDResourceDiskHandlera  
    This class handles resource disk mounting for FreeBSD.

    The resource disk locates at following slot:
    scbus2 on blkvsc1 bus 0:
    <Msft Virtual Disk 1.0>            at scbus2 target 1 lun 0 (da1,pass2)

    There are 2 variations based on partition table type:
    1. MBR: The resource disk partition is /dev/da1s1
    2. GPT: The resource disk partition is /dev/da1p2, /dev/da1p1 is for reserved usage.
    c                 *    t         t        |           y )N)superr   __init__)self	__class__s    M/usr/lib/python3/dist-packages/azurelinuxagent/daemon/resourcedisk/freebsd.pyr	   z#FreeBSDResourceDiskHandler.__init__)   s    ($8:    c                     i }| j                  d      D ]9  }|j                  d      dk7  r|dd  }|j                  d      dk7  s2|dd  |<   ; |S )N
zGeom name:    zscheme:    )splitfind)datadicline	geom_names       r   parse_gpart_listz+FreeBSDResourceDiskHandler.parse_gpart_list,   sb    JJt$ 	*Dyy'2- I	:&",!%abI		*
 
r   c                 @   | j                   }|dk7  rt        dj                  |            t        j                  d      \  }}|rt        dj                  |            | j                  |      }| j                  j                  d      }|||vrt        j                  d      \  }}|r4t        j                  d      \  }}|rt        dj                  |            |j                  d      D ]&  }|j                  d	      }|d
kD  s|d | }	|	|v s$|	} n |st        d      t        j                  d|       ||   }
|
dk(  r|dz   }n%|
dk(  r|dz   }nt        dj                  |            t        j                  dj                  |            \  }}|s|j                  |      dk(  rt        d      d|z   }t        j                  d|       t        j                  d      d   }| j                  j                  ||      }|rt        j                  d|       |S t        j                  |d       dj                  |||      }t        j                  |d      }|rt        j                  dj                  ||             t        j                  dj                  |            \  }}|rt        d j                  ||            t        j                  |d      \  }}|rt        d!j                  ||            t        j                  d"|||       |S )#Nufsz7Unsupported filesystem type:{0}, only ufs is supported.z
gpart listz)Unable to detect resource disk device:{0}   zcamcontrol periphlist 2:1:0zcamcontrol periphlist 3:1:0r   :r   z&Unable to detect resource disk device.zResource disk device {0} found.MBRs1GPTp2z$Unsupported partition table type:{0}zgpart show -p {0}r   z"Resource disk partition not found.z/dev/z"Resource disk partition {0} found.mountz$Resource disk {0} is already mountedi  )modezmount -t {0} {1} {2}Fchk_errz(Creating {0} filesystem on partition {1}znewfs -U {0}z;Failed to create new filesystem on partition {0}, error:{1}z(Failed to mount partition {0}, error {1}z=Resource disk partition {0} is mounted at {1} with fstype {2})fsr   format	shellutilrun_get_outputr   osutildevice_for_ide_portr   r   loggerinfoget_mount_pointfileutilmkdirrun)r
   mount_pointr&   erroutputdisksdevicer   indexr   partition_table_typeprovider_name	partition
mount_listexisting	mount_cmds                   r   mount_resource_diskz.FreeBSDResourceDiskHandler.mount_resource_disk6   s3   WW;#IPPQSTV V  ..|<V#;BB6JL L%%f-003>V50#22-/KC'6613V+CJJ6RT T T* 		#19 $VeI E)!* #$LMM5v>  %V}5("TMM!U*"TMM#6==fEG G  ..&&v.0V&++m,2#$HIIm+	8)D --g6q9
;;..z9EKK>	JO{/*11"iM	mmIu5KK:AA	#$ $22%%i02KC'QYY!6+, , $229eLKC'>EE!6+, , 	K		
 r   c                    |dz  }|dz  }t         j                  j                  |d      }t        j                  d      d   }| j                  |||      ry t         j                  j                  |      rrt         j                  j                  |      |k7  rPt        j                  d       t        j                  dj                  |      d       t        j                  |       t         j                  j                  |      s*t        j                  d	       | j                  ||dz         t        j                  d
j                  |            d   j                         }t        j                  dj                  |             t        j                          rt        j                  d       t        j                  d       t        j                  d       t        j                  dj                  |             t        j                  dj                  |             t        j                  dj                  |            rt#        dj                  |            t        j                  dj                  |||             y t        j                  dj                  |            rt#        dj                  |            t        j                  dj                  |||             y )Ni   swapfilez
swapctl -lr   zRemove old swap filezswapoff {0}Fr$   zCreate swap filezmdconfig -a -t vnode -f {0}zchmod 0600 /dev/{0}zkldload aesnizkldload cryptodevzkldload geom_eliz*geli onetime -e AES-XTS -l 256 -d /dev/{0}zchmod 0600 /dev/{0}.elizswapon /dev/{0}.eliz/dev/{0}.eliz+Enabled {0}KB of swap at /dev/{1}.eli ({2})zswapon /dev/{0}z/dev/{0}z'Enabled {0}KB of swap at /dev/{1} ({2}))ospathjoinr(   r)   check_existing_swap_fileisfilegetsizer,   r-   r1   r'   removemkfilerstripconf'get_resourcedisk_enable_swap_encryptionr   )r
   r2   size_mbsize_kbsizer@   swaplistmddevices           r   create_swap_spacez,FreeBSDResourceDiskHandler.create_swap_space   s"   D.~77<<Z8++L9!<((8TB77>>(#(AT(IKK./MM-..x8%HIIhww~~h'KK*+KK'D.1++)00:<<=??Evx 	+228<=779MM/*MM-.MM,-MM<CCHMOMM3::8DE}}299(CD'(=(=h(GHHKK=DDWhX`ac }}.55h?@'
(9(9((CDDKK9@@(T\]_r   )
__name__
__module____qualname____doc__r	   staticmethodr   r>   rQ   __classcell__)r   s   @r   r   r      s.    
;  Zx%_r   r   )rA   azurelinuxagent.common.loggercommonr,   %azurelinuxagent.common.utils.fileutilutilsr/   &azurelinuxagent.common.utils.shellutilr(   azurelinuxagent.common.confrJ    azurelinuxagent.common.exceptionr   +azurelinuxagent.daemon.resourcedisk.defaultr   r    r   r   <module>ra      s9   & 
 . . 8 8 : : * * > K[_!4 [_r   