def _update_grub(g: guestfs.GuestFS): """Update and rebuild grub to ensure the image is bootable on GCP. See https://cloud.google.com/compute/docs/import/import-existing-image """ g.write('/etc/default/grub', configs.update_grub_conf( g.cat('/etc/default/grub'), GRUB_CMDLINE_LINUX_DEFAULT='console=ttyS0,38400n8', GRUB_CMDLINE_LINUX='', )) g.command(['grub2-mkconfig', '-o', '/boot/grub2/grub.cfg'])
def _install_product(g: guestfs.GuestFS, release: _SuseRelease): """Executes SuseConnect -p for each product on `release`. Raises: ValueError: If there was a failure adding the subscription. """ if release.cloud_product: try: g.command(['SUSEConnect', '--debug', '-p', release.cloud_product]) except Exception as e: raise _disambiguate_suseconnect_product_error( g, release.cloud_product, e)
def DistroSpecific(g: guestfs.GuestFS): el_release = utils.GetMetadataAttribute('el_release') install_gce = utils.GetMetadataAttribute('install_gce_packages') rhel_license = utils.GetMetadataAttribute('use_rhel_gce_license') # This must be performed prior to making network calls from the guest. # Otherwise, if /etc/resolv.conf is present, and has an immutable attribute, # guestfs will fail with: # # rename: /sysroot/etc/resolv.conf to # /sysroot/etc/i9r7obu6: Operation not permitted utils.common.ClearEtcResolv(g) # Some imported images haven't contained `/etc/yum.repos.d`. if not g.exists('/etc/yum.repos.d'): g.mkdir('/etc/yum.repos.d') if rhel_license == 'true': if 'Red Hat' in g.cat('/etc/redhat-release'): g.command(['yum', 'remove', '-y', '*rhui*']) logging.info('Adding in GCE RHUI package.') g.write('/etc/yum.repos.d/google-cloud.repo', repo_compute % el_release) yum_install(g, 'google-rhui-client-rhel' + el_release) if install_gce == 'true': logging.info('Installing GCE packages.') g.write('/etc/yum.repos.d/google-cloud.repo', repo_compute % el_release) if el_release == '6': if 'CentOS' in g.cat('/etc/redhat-release'): logging.info('Installing CentOS SCL.') g.command(['rm', '-f', '/etc/yum.repos.d/CentOS-SCL.repo']) yum_install(g, 'centos-release-scl') # Install Google Cloud SDK from the upstream tar and create links for the # python27 SCL environment. logging.info('Installing python27 from SCL.') yum_install(g, 'python27') logging.info('Installing Google Cloud SDK from tar.') sdk_base_url = 'https://dl.google.com/dl/cloudsdk/channels/rapid' sdk_base_tar = '%s/google-cloud-sdk.tar.gz' % sdk_base_url tar = utils.HttpGet(sdk_base_tar) g.write('/tmp/google-cloud-sdk.tar.gz', tar) g.command( ['tar', 'xzf', '/tmp/google-cloud-sdk.tar.gz', '-C', '/tmp']) sdk_version = g.cat('/tmp/google-cloud-sdk/VERSION').strip() logging.info('Getting Cloud SDK Version %s', sdk_version) sdk_version_tar = 'google-cloud-sdk-%s-linux-x86_64.tar.gz' % sdk_version sdk_version_tar_url = '%s/downloads/%s' % (sdk_base_url, sdk_version_tar) logging.info('Getting versioned Cloud SDK tar file from %s', sdk_version_tar_url) tar = utils.HttpGet(sdk_version_tar_url) sdk_version_tar_file = os.path.join('/tmp', sdk_version_tar) g.write(sdk_version_tar_file, tar) g.mkdir_p('/usr/local/share/google') g.command([ 'tar', 'xzf', sdk_version_tar_file, '-C', '/usr/local/share/google', '--no-same-owner' ]) logging.info('Creating CloudSDK SCL symlinks.') sdk_bin_path = '/usr/local/share/google/google-cloud-sdk/bin' g.ln_s(os.path.join(sdk_bin_path, 'git-credential-gcloud.sh'), os.path.join('/usr/bin', 'git-credential-gcloud.sh')) for binary in ['bq', 'gcloud', 'gsutil']: binary_path = os.path.join(sdk_bin_path, binary) new_bin_path = os.path.join('/usr/bin', binary) bin_str = '#!/bin/bash\nsource /opt/rh/python27/enable\n%s $@' % \ binary_path g.write(new_bin_path, bin_str) g.chmod(0o755, new_bin_path) else: g.write_append('/etc/yum.repos.d/google-cloud.repo', repo_sdk % el_release) yum_install(g, 'google-cloud-sdk') yum_install(g, 'google-compute-engine', 'google-osconfig-agent') logging.info('Updating initramfs') for kver in g.ls('/lib/modules'): # Although each directory in /lib/modules typically corresponds to a # kernel version [1], that may not always be true. # kernel-abi-whitelists, for example, creates extra directories in # /lib/modules. # # Skip building initramfs if the directory doesn't look like a # kernel version. Emulates the version matching from depmod [2]. # # 1. https://tldp.org/LDP/Linux-Filesystem-Hierarchy/html/lib.html # 2. https://kernel.googlesource.com/pub/scm/linux/kernel/git/mmarek/kmod # /+/tip/tools/depmod.c#2537 if not re.match(r'^\d+.\d+', kver): logging.debug( '/lib/modules/{} doesn\'t look like a kernel directory. ' 'Skipping creation of initramfs for it'.format(kver)) continue if not g.exists(os.path.join('/lib/modules', kver, 'modules.dep')): try: g.command(['depmod', kver]) except RuntimeError as e: logging.info( 'Failed to write initramfs for {kver}. If image fails to ' 'boot, verify that depmod /lib/modules/{kver} runs on ' 'the original machine'.format(kver=kver)) logging.debug('depmod error: {}'.format(e)) continue if el_release == '6': # Version 6 doesn't have option --kver g.command(['dracut', '-v', '-f', kver]) else: g.command(['dracut', '--stdlog=1', '-f', '--kver', kver]) logging.info('Update grub configuration') if el_release == '6': # Version 6 doesn't have grub2, file grub.conf needs to be updated by hand g.write('/tmp/grub_gce_generated', grub_cfg) g.sh(r'grep -P "^[\t ]*initrd|^[\t ]*root|^[\t ]*kernel|^[\t ]*title" ' r'/boot/grub/grub.conf >> /tmp/grub_gce_generated;' r'sed -i "s/console=ttyS0[^ ]*//g" /tmp/grub_gce_generated;' r'sed -i "/^[\t ]*kernel/s/$/ console=ttyS0,38400n8/" ' r'/tmp/grub_gce_generated;' r'mv /tmp/grub_gce_generated /boot/grub/grub.conf') else: g.write('/etc/default/grub', grub2_cfg) g.command(['grub2-mkconfig', '-o', '/boot/grub2/grub.cfg']) # Reset network for DHCP. logging.info('Resetting network to DHCP for eth0.') # Remove NetworkManager-config-server if it's present. The package configures # NetworkManager to *not* use DHCP. # https://access.redhat.com/solutions/894763 g.command(['yum', 'remove', '-y', 'NetworkManager-config-server']) g.write('/etc/sysconfig/network-scripts/ifcfg-eth0', ifcfg_eth0)
def _install_virtio_drivers(g: guestfs.GuestFS): """Rebuilds initramfs to ensure that virtio drivers are present.""" logging.info('Installing virtio drivers.') for kernel in g.ls('/lib/modules'): g.command(['dracut', '-v', '-f', '--kver', kernel])
def _refresh_zypper(g: guestfs.GuestFS): try: g.command(['zypper', '--debug', 'refresh']) except Exception as e: raise ValueError('Failed to call zypper refresh', e)
def _disambiguate_suseconnect_product_error( g: guestfs.GuestFS, product: str, error: Exception) -> Exception: """Creates a user-debuggable error after failing to add a product using SUSEConnect. Args: g: Mounted GuestFS instance. product: The product that failed to be added. error: The error returned from `SUSEConnect -p`. """ statuses = [] try: statuses = json.loads(g.command(['SUSEConnect', '--status'])) except Exception as e: return ValueError( 'Unable to communicate with SCC. Ensure the import ' 'is running in a network that allows internet access.', e) # `SUSEConnect --status` returns a list of status objects, # where the triple of (identifier, version, arch) uniquely # identifies a product in SCC. Below are two examples. # # Example 1: SLES for SAP 12.2, No subscription # [ # { # "identifier":"SLES_SAP", # "version":"12.2", # "arch":"x86_64", # "status":"Not Registered" # } # ] # # Example 2: SLES 15.1, Active # [ # { # "status":"Registered", # "version":"15.1", # "arch":"x86_64", # "identifier":"SLES", # "subscription_status":"ACTIVE" # }, # { # "status":"Registered", # "version":"15.1", # "arch":"x86_64", # "identifier":"sle-module-basesystem" # } # ] for status in statuses: if status.get('identifier') not in ('SLES', 'SLES_SAP'): continue if status.get('subscription_status') == 'ACTIVE': return ValueError( 'Unable to add product "%s" using SUSEConnect. Please ensure that ' 'your subscription includes access to this product.' % product, error) return ValueError( 'Unable to find an active SLES subscription. ' 'SCC returned: %s' % statuses)
def DistroSpecific(g: guestfs.GuestFS): el_release = utils.GetMetadataAttribute('el_release') install_gce = utils.GetMetadataAttribute('install_gce_packages') rhel_license = utils.GetMetadataAttribute('use_rhel_gce_license') # This must be performed prior to making network calls from the guest. # Otherwise, if /etc/resolv.conf is present, and has an immutable attribute, # guestfs will fail with: # # rename: /sysroot/etc/resolv.conf to # /sysroot/etc/i9r7obu6: Operation not permitted utils.common.ClearEtcResolv(g) # Some imported images haven't contained `/etc/yum.repos.d`. if not g.exists('/etc/yum.repos.d'): g.mkdir('/etc/yum.repos.d') if rhel_license == 'true': if 'Red Hat' in g.cat('/etc/redhat-release'): g.command(['yum', 'remove', '-y', '*rhui*']) logging.info('Adding in GCE RHUI package.') g.write('/etc/yum.repos.d/google-cloud.repo', repo_compute % el_release) yum_install(g, 'google-rhui-client-rhel' + el_release) if install_gce == 'true': logging.info('Installing GCE packages.') g.write('/etc/yum.repos.d/google-cloud.repo', repo_compute % el_release) if el_release == '6': if 'CentOS' in g.cat('/etc/redhat-release'): logging.info('Installing CentOS SCL.') g.command(['rm', '-f', '/etc/yum.repos.d/CentOS-SCL.repo']) yum_install(g, 'centos-release-scl') # Install Google Cloud SDK from the upstream tar and create links for the # python27 SCL environment. logging.info('Installing python27 from SCL.') yum_install(g, 'python27') logging.info('Installing Google Cloud SDK from tar.') sdk_base_url = 'https://dl.google.com/dl/cloudsdk/channels/rapid' sdk_base_tar = '%s/google-cloud-sdk.tar.gz' % sdk_base_url tar = utils.HttpGet(sdk_base_tar) g.write('/tmp/google-cloud-sdk.tar.gz', tar) g.command(['tar', 'xzf', '/tmp/google-cloud-sdk.tar.gz', '-C', '/tmp']) sdk_version = g.cat('/tmp/google-cloud-sdk/VERSION').strip() logging.info('Getting Cloud SDK Version %s', sdk_version) sdk_version_tar = 'google-cloud-sdk-%s-linux-x86_64.tar.gz' % sdk_version sdk_version_tar_url = '%s/downloads/%s' % (sdk_base_url, sdk_version_tar) logging.info('Getting versioned Cloud SDK tar file from %s', sdk_version_tar_url) tar = utils.HttpGet(sdk_version_tar_url) sdk_version_tar_file = os.path.join('/tmp', sdk_version_tar) g.write(sdk_version_tar_file, tar) g.mkdir_p('/usr/local/share/google') g.command(['tar', 'xzf', sdk_version_tar_file, '-C', '/usr/local/share/google', '--no-same-owner']) logging.info('Creating CloudSDK SCL symlinks.') sdk_bin_path = '/usr/local/share/google/google-cloud-sdk/bin' g.ln_s(os.path.join(sdk_bin_path, 'git-credential-gcloud.sh'), os.path.join('/usr/bin', 'git-credential-gcloud.sh')) for binary in ['bq', 'gcloud', 'gsutil']: binary_path = os.path.join(sdk_bin_path, binary) new_bin_path = os.path.join('/usr/bin', binary) bin_str = '#!/bin/bash\nsource /opt/rh/python27/enable\n%s $@' % \ binary_path g.write(new_bin_path, bin_str) g.chmod(0o755, new_bin_path) else: g.write_append( '/etc/yum.repos.d/google-cloud.repo', repo_sdk % el_release) yum_install(g, 'google-cloud-sdk') yum_install(g, 'google-compute-engine', 'google-osconfig-agent') logging.info('Updating initramfs') for kver in g.ls('/lib/modules'): if not g.exists(os.path.join('/lib/modules', kver, 'modules.dep')): g.command(['depmod', kver]) if el_release == '6': # Version 6 doesn't have option --kver g.command(['dracut', '-v', '-f', kver]) else: g.command(['dracut', '--stdlog=1', '-f', '--kver', kver]) logging.info('Update grub configuration') if el_release == '6': # Version 6 doesn't have grub2, file grub.conf needs to be updated by hand g.write('/tmp/grub_gce_generated', grub_cfg) g.sh( r'grep -P "^[\t ]*initrd|^[\t ]*root|^[\t ]*kernel|^[\t ]*title" ' r'/boot/grub/grub.conf >> /tmp/grub_gce_generated;' r'sed -i "s/console=ttyS0[^ ]*//g" /tmp/grub_gce_generated;' r'sed -i "/^[\t ]*kernel/s/$/ console=ttyS0,38400n8/" ' r'/tmp/grub_gce_generated;' r'mv /tmp/grub_gce_generated /boot/grub/grub.conf') else: g.write('/etc/default/grub', grub2_cfg) g.command(['grub2-mkconfig', '-o', '/boot/grub2/grub.cfg']) # Reset network for DHCP. logging.info('Resetting network to DHCP for eth0.') # Remove NetworkManager-config-server if it's present. The package configures # NetworkManager to *not* use DHCP. # https://access.redhat.com/solutions/894763 g.command(['yum', 'remove', '-y', 'NetworkManager-config-server']) g.write('/etc/sysconfig/network-scripts/ifcfg-eth0', ifcfg_eth0)