def mount_partition(devname, mountpoint=None): """ Create the mountpoint /mnt/<last part of device specification> and mount a partition on on this mountpoint. Parameters ---------- devname: str The full path of the device. mountpoint: str The mountpoint, will be generated if not provided. Returns ------- str: The mounted partition on Success, None otherwise. """ _logger.debug('__ Mount partition %s.', devname) # # create mountpoint /mnt/<devname> if not specified. if mountpoint is None: mntpoint = migrate_data.loopback_root + '/' + devname.rsplit('/')[-1] _logger.debug('Loopback mountpoint: %s', mntpoint) try: if system_tools.exec_mkdir(mntpoint): _logger.debug('Mountpoint: %s created.', mntpoint) except Exception as e: _logger.critical(' Failed to create mountpoint %s: %s', mntpoint, str(e)) raise OciMigrateException('Failed to create mountpoint %s:' % mntpoint) from e else: mntpoint = mountpoint # # actual mount cmd = ['mount', devname, mntpoint] pause_msg(cmd, pause_flag='_OCI_MOUNT') _, nbcols = terminal_dimension() try: mountpart = ProgressBar(nbcols, 0.2, progress_chars=['mount %s' % devname]) mountpart.start() _logger.debug('command: %s', cmd) cmdret = system_tools.run_call_cmd(cmd) if cmdret == 0: _logger.debug('%s mounted on %s.', devname, mntpoint) return mntpoint raise Exception('Mount %s failed: %d' % (devname, cmdret)) except Exception as e: # # mount failed, need to remove mountpoint. _logger.critical(' Failed to mount %s, missing driver, filesystem corruption...: %s', devname, str(e)) if mountpoint is None: if system_tools.exec_rmdir(mntpoint): _logger.debug('%s removed', mntpoint) else: _logger.critical(' Failed to remove mountpoint %s', mntpoint) finally: if system_tools.is_thread_running(mountpart): mountpart.stop() return None
def unmount_part(devname): """ Unmount a partition from mountpoint from /mnt/<last part of device specification> and remove the mountpoint. Parameters ---------- devname: str The full path of the device. Returns ------- bool True on success, False otherwise. """ mntpoint = migrate_data.loopback_root + '/' + devname.rsplit('/')[-1] cmd = ['umount', mntpoint] _logger.debug('__ Running %s' % cmd) pause_msg(cmd) while True: try: _logger.debug('command: %s' % cmd) cmdret = system_tools.run_call_cmd(cmd) if cmdret == 0: _logger.debug('%s unmounted from %s' % (devname, mntpoint)) # # remove mountpoint if system_tools.exec_rmdir(mntpoint): _logger.debug('%s removed' % mntpoint) return True else: _logger.critical(' Failed to remove mountpoint %s' % mntpoint) raise OciMigrateException( 'Failed to remove mountpoint %s' % mntpoint) else: _logger.critical(' Failed to unmount %s: %d' % (devname, cmdret)) console_msg('Failed to unmount %s, error code %d.\n ' 'Please verify before continuing.' % (devname, cmdret)) retry = read_yn( 'Something prevented to complete %s, please ' 'verify and correct if possible. ' 'Press Y to retry, N to ignore.', waitenter=True) if not retry: break except Exception as e: _logger.critical(' Failed to unmount %s: %s' % (devname, str(e))) return False
def reconfigure_networkmanager(rootdir): """ Replace the networkmanager configuration with Oracle Cloud Infrastructure compatible version. Parameters ---------- rootdir: str Full path of image root dir as loopback mounted. Returns ------- list: list of nic. dict: the network manager system-connections configurations """ _logger.debug('__ The NetworkManager configuration.') netwmg_data = dict() netwmg_nics = list() network_config_dir = rootdir + get_config_data('default_nwconnections') _logger.debug('Network manager dir: %s' % network_config_dir) nw_mgr_cfg = rootdir + get_config_data('default_nwmconfig') _logger.debug('Network manager conf: %s' % nw_mgr_cfg) # # backup try: # # copy if os.path.isfile(nw_mgr_cfg): bck_nw_mgr_cfg = system_tools.exec_rename(nw_mgr_cfg) if bool(bck_nw_mgr_cfg): _logger.debug('Copied %s to %s' % (nw_mgr_cfg, bck_nw_mgr_cfg)) else: _logger.warning( 'Failed to backup network manager configuration.') else: _logger.debug('No %s found.' % nw_mgr_cfg) # if os.path.isdir(network_config_dir): bck_network_config_dir = system_tools.backup_dir( network_config_dir) _logger.debug('Copied %s to %s' % (network_config_dir, bck_network_config_dir)) else: _logger.debug('%s not found.' % network_config_dir) except Exception as e: migrate_tools.error_msg('Failed to backup the networkmanager ' 'configuration: %s' % str(e)) # # if os.path.isdir(network_config_dir): _logger.debug('NetworkManager/%s directory exists.' % network_config_dir) # # contains nwm keyfiles? nwm_files = glob(network_config_dir + '/*') if len(nwm_files) > 0: system_tools.exec_rmdir(network_config_dir) system_tools.exec_mkdir(network_config_dir) _logger.debug('%s emptied.' % network_config_dir) else: _logger.debug('No network manager keyfiles found.') # # update networkmanager configuration # TODO: write config file with configparser nwm_config_data = get_config_data('default_nwm_conf_file') with open(nw_mgr_cfg, 'w') as nwmf: nwmf.write('\n'.join(str(x) for x in nwm_config_data)) migrate_tools.result_msg( msg='Networkmanager configuration updated.', result=False) else: _logger.debug(msg=' No NetworkManager configuration present.') return netwmg_nics, netwmg_data