def mount_system(root_path, fstab):
    log = logging.getLogger(Defaults.get_migration_log_name())
    log.info('Mount system in {0}'.format(root_path))
    system_mount = Fstab()
    explicit_mount_points = {
        'devtmpfs': os.sep.join([root_path, 'dev']),
        'proc': os.sep.join([root_path, 'proc']),
        'sysfs': os.sep.join([root_path, 'sys'])
    }
    try:
        for fstab_entry in fstab.get_devices():
            mountpoint = ''.join([root_path, fstab_entry.mountpoint])
            if mountpoint not in explicit_mount_points.values():
                if fstab_entry.eligible_for_mount:
                    log.info('Mounting {0}'.format(mountpoint))
                    Command.run([
                        'mount', '-o', fstab_entry.options, fstab_entry.device,
                        mountpoint
                    ])
                system_mount.add_entry(fstab_entry.device, mountpoint,
                                       fstab_entry.fstype,
                                       fstab_entry.eligible_for_mount)

        log.info('Mounting kernel file systems inside {0}'.format(root_path))
        for mount_type, mount_point in explicit_mount_points.items():
            Command.run(['mount', '-t', mount_type, mount_type, mount_point])
            system_mount.add_entry(mount_type, mount_point)
    except Exception as issue:
        log.error('Mounting system for upgrade failed with {0}'.format(issue))
        raise DistMigrationSystemMountException(
            'Mounting system for upgrade failed with {0}'.format(issue))
    system_mount.export(Defaults.get_system_mount_info_file())
Beispiel #2
0
def mount_system(root_path, fstab):
    log = logging.getLogger(Defaults.get_migration_log_name())
    log.info('Mount system in {0}'.format(root_path))
    system_mount = Fstab()
    try:
        for fstab_entry in fstab.get_devices():
            mountpoint = ''.join([root_path, fstab_entry.mountpoint])
            log.info('Mounting {0}'.format(mountpoint))
            Command.run([
                'mount', '-o', fstab_entry.options, fstab_entry.device,
                mountpoint
            ])
            system_mount.add_entry(fstab_entry.device, mountpoint,
                                   fstab_entry.fstype)

        log.info('Mounting kernel file systems inside {0}'.format(root_path))
        dev_mount_point = os.sep.join([root_path, 'dev'])
        Command.run(['mount', '-t', 'devtmpfs', 'devtmpfs', dev_mount_point])
        system_mount.add_entry('devtmpfs', dev_mount_point)
        proc_mount_point = os.sep.join([root_path, 'proc'])
        Command.run(['mount', '-t', 'proc', 'proc', proc_mount_point])
        system_mount.add_entry('/proc', proc_mount_point)
        sys_mount_point = os.sep.join([root_path, 'sys'])
        Command.run(['mount', '-t', 'sysfs', 'sysfs', sys_mount_point])
        system_mount.add_entry('sysfs', sys_mount_point)
    except Exception as issue:
        log.error('Mounting system for upgrade failed with {0}'.format(issue))
        raise DistMigrationSystemMountException(
            'Mounting system for upgrade failed with {0}'.format(issue))
    system_mount.export(Defaults.get_system_mount_info_file())
Beispiel #3
0
 def test_export(self):
     fstab = Fstab()
     fstab.add_entry('/dev/sda', '/foo')
     with patch('builtins.open', create=True) as mock_open:
         mock_open.return_value = MagicMock(spec=io.IOBase)
         fstab.export('filename')
         file_handle = mock_open.return_value.__enter__.return_value
         mock_open.assert_called_once_with('filename', 'w')
         assert file_handle.write.call_args_list == [
             call('/dev/sda /foo none defaults 0 0\n')
         ]
def main():
    """
    DistMigration activate host network setup

    Setup and activate the network as it is setup on the host
    to become migrated. This includes the import of the resolver
    and network configuration from the migration host
    """
    Logger.setup()
    log = logging.getLogger(Defaults.get_migration_log_name())
    root_path = Defaults.get_system_root_path()

    resolv_conf = os.sep.join([root_path, 'etc', 'resolv.conf'])
    if not os.path.exists(resolv_conf):
        raise DistMigrationNameResolverException(
            'Could not find {0} on migration host'.format(resolv_conf))
    if has_host_resolv_setup(resolv_conf):
        log.info('Copying {}'.format(resolv_conf))
        shutil.copy(resolv_conf, '/etc/resolv.conf')
    else:
        log.info('Empty {}, continuing without copying it'.format(resolv_conf))

    sysconfig_network_providers = os.sep.join(
        [root_path, 'etc', 'sysconfig', 'network', 'providers'])
    sysconfig_network_setup = os.sep.join(
        [root_path, 'etc', 'sysconfig', 'network', '*'])
    try:
        log.info('Running setup host network service')
        system_mount = Fstab()
        system_mount.read(Defaults.get_system_mount_info_file())
        Command.run([
            'mount', '--bind', sysconfig_network_providers,
            '/etc/sysconfig/network/providers'
        ])
        system_mount.add_entry(sysconfig_network_providers,
                               '/etc/sysconfig/network/providers')
        for network_setup in glob.glob(sysconfig_network_setup):
            if os.path.isfile(network_setup):
                shutil.copy(network_setup, '/etc/sysconfig/network')
        Command.run(['systemctl', 'reload', 'network'])
        system_mount.export(Defaults.get_system_mount_info_file())
    except Exception as issue:
        log.error(
            'Preparation of migration host network failed with {0}'.format(
                issue))
        raise DistMigrationHostNetworkException(
            'Preparation of migration host network failed with {0}'.format(
                issue))
Beispiel #5
0
def mount_system(root_path, fstab):
    log.info('Mount system in {0}'.format(root_path))
    mount_list = []
    system_mount = Fstab()
    for fstab_entry in fstab.get_devices():
        try:
            mountpoint = ''.join([root_path, fstab_entry.mountpoint])
            log.info('Mounting {0}'.format(mountpoint))
            Command.run([
                'mount', '-o', fstab_entry.options, fstab_entry.device,
                mountpoint
            ])
            system_mount.add_entry(fstab_entry.device, mountpoint,
                                   fstab_entry.fstype)
            mount_list.append(mountpoint)
        except Exception as issue:
            log.error(
                'Mounting system for upgrade failed with {0}'.format(issue))
            for mountpoint in reversed(mount_list):
                Command.run(['umount', mountpoint])
            raise DistMigrationSystemMountException(
                'Mounting system for upgrade failed with {0}'.format(issue))
    system_mount.export(Defaults.get_system_mount_info_file())
def main():
    """
    DistMigration prepare for migration

    Prepare the migration live system to allow zypper migration to
    upgrade the system across major distribution versions. The zypper
    migration process contacts the service that provides the configured
    repositories on the system being migrated. The service must be one
    of SUSE's repository services, SCC, RMT, or SMT. This requiers
    information from the target system. This service makes the necessary
    information available inside the live system that performs the migration.
    """
    Logger.setup()
    log = logging.getLogger(Defaults.get_migration_log_name())
    root_path = Defaults.get_system_root_path()
    suse_connect_setup = os.sep.join([root_path, 'etc', 'SUSEConnect'])
    suse_cloud_regionsrv_setup = os.sep.join(
        [root_path, 'etc', 'regionserverclnt.cfg'])
    hosts_setup = os.sep.join([root_path, 'etc', 'hosts'])
    trust_anchors = os.sep.join(
        [root_path, 'usr', 'share', 'pki', 'trust', 'anchors'])
    if os.path.exists(suse_connect_setup):
        shutil.copy(suse_connect_setup, '/etc/SUSEConnect')
    if os.path.exists(suse_cloud_regionsrv_setup):
        migration_suse_cloud_regionsrv_setup = '/etc/regionserverclnt.cfg'
        shutil.copy(suse_cloud_regionsrv_setup,
                    migration_suse_cloud_regionsrv_setup)
        update_regionsrv_setup(root_path, migration_suse_cloud_regionsrv_setup)
    if os.path.exists(hosts_setup):
        shutil.copy(hosts_setup, '/etc/hosts')
    if os.path.exists(trust_anchors):
        certificates = os.listdir(trust_anchors)
        if certificates:
            for cert in certificates:
                log.info('Importing certificate: {0}'.format(cert))
                shutil.copy(os.sep.join([trust_anchors, cert]),
                            '/usr/share/pki/trust/anchors/')
            log.info('Update certificate pool')
            Command.run(['update-ca-certificates'])

    zypp_metadata = os.sep.join([root_path, 'etc', 'zypp'])
    zypp_plugins_services = os.sep.join(
        [root_path, 'usr', 'lib', 'zypp', 'plugins', 'services'])
    cloud_register_metadata = os.sep.join(
        [root_path, 'var', 'lib', 'cloudregister'])
    zypper_log_file = os.sep.join([root_path, 'var', 'log', 'zypper.log'])
    if os.path.exists(zypper_log_file):
        try:
            zypper_host_log_file = zypper_log_file.replace(root_path, '')
            if not os.path.exists(zypper_host_log_file):
                with open(zypper_host_log_file, 'w'):
                    # we bind mount the system zypper log file
                    # but the mount target does not exist.
                    # Create it as empty file prior bind mounting
                    pass
                Command.run(
                    ['mount', '--bind', zypper_log_file, zypper_host_log_file])
        except Exception as issue:
            log.warning(
                'Bind mounting zypper log file failed with: {0}'.format(issue))
    try:
        # log network info as network-online.target is done at this point
        log_network_details()
        log.info('Running prepare service')
        system_mount = Fstab()
        system_mount.read(Defaults.get_system_mount_info_file())
        log.info('Bind mounting /etc/zypp')
        Command.run(['mount', '--bind', zypp_metadata, '/etc/zypp'])
        system_mount.add_entry(zypp_metadata, '/etc/zypp')
        log.info('Bind mounting /usr/lib/zypp/plugins')
        Command.run([
            'mount', '--bind', zypp_plugins_services,
            '/usr/lib/zypp/plugins/services'
        ])
        system_mount.add_entry(zypp_plugins_services,
                               '/usr/lib/zypp/plugins/services')
        if os.path.exists(cloud_register_metadata):
            log.info('Bind mounting /var/lib/cloudregister')
            Path.create('/var/lib/cloudregister')
            Command.run([
                'mount', '--bind', cloud_register_metadata,
                '/var/lib/cloudregister'
            ])
            update_smt_cache = '/usr/sbin/updatesmtcache'
            if os.path.isfile(update_smt_cache):
                log.info('Updating SMT cache')
                Command.run([update_smt_cache])
        system_mount.export(Defaults.get_system_mount_info_file())
        # Check if system is registered
        migration_config = MigrationConfig()
        if migration_config.is_zypper_migration_plugin_requested():
            if not SUSEConnect.is_registered():
                message = 'System not registered. Aborting migration.'
                log.error(message)
                raise DistMigrationSystemNotRegisteredException(message)
    except Exception as issue:
        log.error(
            'Preparation of zypper metadata failed with {0}'.format(issue))
        # Not unmounting any of the bind mounts above; the reboot
        # service should take care of that anyway
        raise DistMigrationZypperMetaDataException(
            'Preparation of zypper metadata failed with {0}'.format(issue))
def main():
    """
    DistMigration prepare for migration

    Prepare the migration live system to allow zypper migration to
    upgrade the system across major distribution versions. The zypper
    migration process contacts the service that provides the configured
    repositories on the system being migrated. The service must be one
    of SUSE's repository services, SCC, RMT, or SMT. This requiers
    information from the target system. This service makes the necessary
    information available inside the live system that performs the migration.
    """
    root_path = Defaults.get_system_root_path()
    suse_connect_setup = os.sep.join(
        [root_path, 'etc', 'SUSEConnect']
    )
    suse_cloud_regionsrv_setup = os.sep.join(
        [root_path, 'etc', 'regionserverclnt.cfg']
    )
    hosts_setup = os.sep.join(
        [root_path, 'etc', 'hosts']
    )
    trust_anchors = os.sep.join(
        [root_path, 'usr', 'share', 'pki', 'trust', 'anchors']
    )
    if os.path.exists(suse_connect_setup):
        shutil.copy(
            suse_connect_setup, '/etc/SUSEConnect'
        )
    if os.path.exists(suse_cloud_regionsrv_setup):
        shutil.copy(
            suse_cloud_regionsrv_setup, '/etc/regionserverclnt.cfg'
        )
    if os.path.exists(hosts_setup):
        shutil.copy(
            hosts_setup, '/etc/hosts'
        )
    if os.path.exists(trust_anchors):
        certificates = os.listdir(trust_anchors)
        if certificates:
            for cert in certificates:
                log.info(
                    'Importing certificate: {0}'.format(cert)
                )
                shutil.copy(
                    os.sep.join([trust_anchors, cert]),
                    '/usr/share/pki/trust/anchors/'
                )
            log.info('Update certificate pool')
            Command.run(
                ['update-ca-certificates']
            )

    zypp_metadata = os.sep.join(
        [root_path, 'etc', 'zypp']
    )
    zypp_plugins = os.sep.join(
        [root_path, 'usr', 'lib', 'zypp', 'plugins']
    )
    cloud_register_metadata = os.sep.join(
        [root_path, 'var', 'lib', 'cloudregister']
    )
    dev_mount_point = os.sep.join(
        [root_path, 'dev']
    )
    proc_mount_point = os.sep.join(
        [root_path, 'proc']
    )
    sys_mount_point = os.sep.join(
        [root_path, 'sys']
    )
    try:
        # log network info as network-online.target is done at this point
        log_network_details()
        log.info('Running prepare service')
        system_mount = Fstab()
        system_mount.read(
            Defaults.get_system_mount_info_file()
        )
        log.info('Bind mounting /etc/zypp')
        Command.run(
            ['mount', '--bind', zypp_metadata, '/etc/zypp']
        )
        system_mount.add_entry(
            zypp_metadata, '/etc/zypp'
        )
        log.info('Bind mounting /usr/lib/zypp/plugins')
        Command.run(
            ['mount', '--bind', zypp_plugins, '/usr/lib/zypp/plugins']
        )
        system_mount.add_entry(
            zypp_plugins, '/usr/lib/zypp/plugins'
        )
        if os.path.exists(cloud_register_metadata):
            log.info('Bind mounting /var/lib/cloudregister')
            Path.create('/var/lib/cloudregister')
            Command.run(
                [
                    'mount', '--bind', cloud_register_metadata,
                    '/var/lib/cloudregister'
                ]
            )
        log.info('Mounting kernel file systems inside {0}'.format(root_path))
        Command.run(
            ['mount', '-t', 'devtmpfs', 'devtmpfs', dev_mount_point]
        )
        system_mount.add_entry(
            'devtmpfs', dev_mount_point
        )
        Command.run(
            ['mount', '-t', 'proc', 'proc', proc_mount_point]
        )
        system_mount.add_entry(
            '/proc', proc_mount_point
        )
        Command.run(
            ['mount', '-t', 'sysfs', 'sysfs', sys_mount_point]
        )
        system_mount.add_entry(
            'sysfs', sys_mount_point
        )
        system_mount.export(
            Defaults.get_system_mount_info_file()
        )
    except Exception as issue:
        log.error(
            'Preparation of zypper metadata failed with {0}'.format(
                issue
            )
        )
        log.info('Unmounting kernel file systems, if any')
        for entry in reversed(system_mount.get_devices()):
            Command.run(
                ['umount', entry.mountpoint], raise_on_error=False
            )
        raise DistMigrationZypperMetaDataException(
            'Preparation of zypper metadata failed with {0}'.format(
                issue
            )
        )