
Y]*                 @   s   d  d l  Z  d  d l Z d  d l Z d  d l Z d d   Z d d d  Z d d d	  Z d
 d   Z d d   Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z e d k r e  j d e  j  e e d d   d S)    Nc             C   s  t  j d  |  rO |  j rO t  j d  | j d d d  | j d d d  t j d d  } | j d d	 |  } t  j d
 |  | t j d <| j d d	 |  t j d d  } t j	 | d  | j d d |  } t  j d |  | j d d d  r1t  j d  | j d d d  d t j d <| j d d d  rst  j d  | j d d d  | t j d <| j d d d  rt  j d  d t j d <d S)z setup the environment based on the config and options
    It will use
    config("Aufs","Enabled") - to show if its enabled
    and
    config("Aufs","RWDir") - for the writable overlay dir
    z aufsOptionsAndEnvironmentSetup()z&enabling full overlay from commandlineZAufsZEnabledTrueZEnableFullOverlayprefixzupgrade-rw-ZRWDirzusing '%s' as aufs_rw_dirZRELEASE_UPGRADE_AUFS_RWDIRzupgrade-chroot-i  Z	ChrootDirzusing '%s' as aufs chroot dirFz(enabling aufs full overlay (from config)1Z%RELEASE_UPGRADE_USE_AUFS_FULL_OVERLAYZEnableChrootOverlayzenabling aufs chroot overlayZRELEASE_UPGRADE_USE_AUFS_CHROOTZEnableChrootRsyncz,enable aufs chroot rsync back to real systemZ!RELEASE_UPGRADE_RSYNC_AUFS_CHROOTN)
loggingdebugZuseAufssettempfileZmkdtempZgetWithDefaultosenvironchmod)optionsconfigZtmprwaufs_rw_dirZ	tmpchrootaufs_chroot_dir r   =/usr/lib/python3/dist-packages/DistUpgrade/DistUpgradeAufs.pyaufsOptionsAndEnvironmentSetup   s2    r   Fc             C   s   t  j j |  s t  j |  | r. d } n d } d | |  | g } t j d |  t j |  } | d k r t j d |  | f  d Sd S)	z4 helper that bind mounts a given dir to a new place z--rbindz--bindmountzcmd: %sr   z&Failed to bind mount from '%s' to '%s'FT)	r	   pathexistsmakedirsr   r   
subprocesscallerror)Zfrom_dirZto_dirrbindZbindcmdresr   r   r   
_bindMountC   s    	r   /c             C   s  t  j j | |   s' t  j | |   t  j j | |   sN t  j | |   d } | r d d d d d | |  |  f d | |  g } n- d d d d d	 | |  |  f d | |  g } t j |  } | d
 k r t j d |   d St j d | | f  d S)zf 
    helper that takes a target dir and mounts a rw dir over it, e.g.
    /var , /tmp/upgrade-rw
    Fr   z-tZ	overlayfsz-ozupperdir=%s,lowerdir=%snoneaufszbr:%s:%s=ror   z(Failed to mount rw aufs overlay for '%s'zcmd '%s' return '%s' T)	r	   r   r   r   r   r   r   r   r   )targetrw_dir
chroot_dirZuse_overlayfsr   r   r   r   r   _aufsOverlayMountV   s,    r$   c       	      C   sq   t  d  ^ } xT | D]L } | j   \ } } } } } } | d k r | d k r | |  k r d Sq WWd QRXd S)z< test if the given dir is already mounted with aufs overlay z/proc/mountsr   r    TNF)opensplit)	dirflinedevice
mountpointfstyper   abr   r   r   is_aufs_mountv   s    $r/   c             C   sX   t  j d |  | f  x: | D]2 } | j d  s= | d 7} |  j |  r d Sq Wd S)zD helper: check if the given mountpoint is a submount of a systemdir zis_submount: %s %sr   TF)r   r   endswith
startswith)r+   
systemdirsdr   r   r   is_submount   s    
r4   c          	   C   s'   |  j  d  r d S|  d k r# d Sd S)NZfuseFrootfstmpfsprocfusectrlr    devptsbinfmt_miscsysfsT)r5   r6   zprocr8   zaufsr9   r:   r;   )r1   )Zfsr   r   r   
is_real_fs   s    	r<   c          	   C   s   d d l  m } x | D]{ } t j j |  s2 q d d d d d d |  | f d	 | g } t j d
 |  t j |  } t j d | | f  q Wd S)zW
    helper that rsyncs the changes in the aufs chroot back to the
    real system
       )SYSTEM_DIRSZrsyncz-aHAXz--delz-vz
--progressz/%s/%s/z/%s/zrunning: '%s'zrsync back result for %s: %iT)	DistUpgradeMainr>   r	   r   r   r   r   r   r   )r   r>   r3   r   Zretr   r   r   doAufsChrootRsync   s    r@   c             C   s1   t  |  |  s d St j |  t j d  d S)z: helper that sets the chroot up and does chroot() into it Fr   T)setupAufsChrootr	   chrootchdir)r   r   r   r   r   doAufsChroot   s
    rD   c             C   s}  t  d   } | j   } Wd QRXd d l m } | } x t j d  D] } t j j d |  } t j j |  rH | | k r t	 j
 d |  t | |  |  s d SqH t	 j
 d |  t | | | d	 d
 sH d SqH Wx | j d  D] } | j   } | sq | j   \ } }	 }
 } } } |
 d k r t |
  r t |	 |  r t	 j
 d |	  t |	 | |	  s d Sq Wd
 S)zB setup aufs chroot that is based on / but with a writable overlay z/proc/mountsNr=   )r>   r   zbind mounting %sFzoverlay mounting %sr   T
r    zfound %s that needs bind mount)r%   readr?   r>   r	   listdirr   joinisdirr   r   r$   r   r&   stripr<   r4   )r"   r#   r(   Zmountsr>   r2   r3   r)   r*   r+   r,   r   r-   r.   r   r   r   rA      s4    	rA   c             C   s  t  j d  t j j d  s0 t  j d  d Sd d l m } | } t   } | j d  t	 d   } x | D] } | j
   \ } } } }	 }
 } t |  r t | |  r t  j d |  d S| d	 k rr t |  rr t | |  rr t  j d
 |  | j |  qr WWd QRXx) | D]! } t | |  d |  sd SqWx- | D]% } t |  sIt | |   sId SqIWx) | D]! } t |  d | |  syd SqyWd S)z$ setup aufs overlay over the rootfs 	setupAufsz/proc/mountsz(no /proc/mounts, can not do aufs overlayFr=   )r>   z/var/cache/apt/archivesz#mountpoint %s submount of systemdirr    zfound %s that needs bind mountNz/needs_bind_mount/T)r   r   r	   r   r   r?   r>   r   addr%   r&   r<   r4   Zwarningr   r/   r$   )r"   r>   r2   Zneeds_bind_mountr(   r)   r*   r+   r,   r   r-   r.   r3   r   r   r   rK      s>    		rK   __main__levelz/tmp/upgrade-chroot-rwz/tmp/upgrade-chroot)r   r	   r   r   r   r   r$   r/   r4   r<   r@   rD   rA   rK   __name__ZbasicConfigDEBUGprintr   r   r   r   <module>   s"   ' 
		+A	