def CreateArchImage(args, aur_packages):
  logging.info('Creating Arch Image')
  logging.info('========================')
  image_path = os.path.join(os.getcwd(), IMAGE_FILE)
  CreateBlankImage(image_path, size_gb=int(args.size_gb), fs_type=args.fs_type)
  mount_path = utils.CreateTempDirectory(base_dir='/')
  image_mapping = utils.ImageMapper(image_path, mount_path)
  try:
    image_mapping.InstallLoopback()
    image_mapping.Map()
    primary_mapping = image_mapping.GetFirstMapping()
    image_mapping_path = primary_mapping['path']
    FormatImage(image_mapping_path)
    try:
      image_mapping.Mount()
      utils.CreateDirectory('/run/shm')
      utils.CreateDirectory(os.path.join(mount_path, 'run', 'shm'))
      InstallArchLinux(mount_path)
      disk_uuid = SetupFileSystem(mount_path, image_mapping_path, args.fs_type)
      ConfigureArchInstall(
          args, mount_path, primary_mapping['parent'], disk_uuid, aur_packages)
      utils.DeleteDirectory(os.path.join(mount_path, 'run', 'shm'))
      PurgeDisk(mount_path)
    finally:
      image_mapping.Unmount()
    ShrinkDisk(image_mapping_path)
  finally:
    image_mapping.Unmap()
  utils.Run(['parted', image_path, 'set', '1', 'boot', 'on'])
  utils.Sync()
  logging.info('========================')
  return image_path
Beispiel #2
0
def main():
    args = utils.DecodeArgs(sys.argv[1])
    utils.SetupLogging(quiet=args['quiet'], verbose=args['verbose'])
    logging.info('Setup Bootstrapper Environment')
    utils.SetupArchLocale()
    InstallPackagesForStagingEnvironment()
    image_path = os.path.join(os.getcwd(), IMAGE_FILE)
    CreateImage(image_path, size_gb=int(args['size_gb']))
    mount_path = utils.CreateTempDirectory(base_dir='/')
    image_mapping = utils.ImageMapper(image_path, mount_path)
    try:
        image_mapping.Map()
        primary_mapping = image_mapping.GetFirstMapping()
        image_mapping_path = primary_mapping['path']
        FormatImage(image_mapping_path)
        try:
            image_mapping.Mount()
            utils.CreateDirectory('/run/shm')
            utils.CreateDirectory(os.path.join(mount_path, 'run', 'shm'))
            InstallArchLinux(mount_path)
            disk_uuid = SetupFileSystem(mount_path, image_mapping_path)
            ConfigureArchInstall(args, mount_path, primary_mapping['parent'],
                                 disk_uuid)
            utils.DeleteDirectory(os.path.join(mount_path, 'run', 'shm'))
            PurgeDisk(mount_path)
        finally:
            image_mapping.Unmount()
        ShrinkDisk(image_mapping_path)
    finally:
        image_mapping.Unmap()
    utils.Run(['parted', image_path, 'set', '1', 'boot', 'on'])
    utils.Sync()
Beispiel #3
0
def InstallGoogleCloudSdk():
    # TODO: There's a google-cloud-sdk in AUR which should be used
    # but it's not optimal for cloud use. The image is too large.
    utils.LogStep('Install Google Cloud SDK')
    usr_share_google = '/usr/share/google'
    archive = os.path.join(usr_share_google, 'google-cloud-sdk.zip')
    unzip_dir = os.path.join(usr_share_google, 'google-cloud-sdk')
    utils.CreateDirectory(usr_share_google)
    utils.DownloadFile(
        'https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.zip',
        archive)
    utils.Run(['unzip', archive, '-d', usr_share_google])
    utils.AppendFile('/etc/bash.bashrc',
                     'export CLOUDSDK_PYTHON=/usr/bin/python2')
    utils.Run([
        os.path.join(unzip_dir, 'install.sh'), '--usage-reporting', 'false',
        '--bash-completion', 'true', '--disable-installation-options',
        '--rc-path', '/etc/bash.bashrc', '--path-update', 'true'
    ],
              cwd=unzip_dir,
              env={'CLOUDSDK_PYTHON': '/usr/bin/python2'})
    utils.Symlink(os.path.join(unzip_dir, 'bin/gcloud'), '/usr/bin/gcloud')
    utils.Symlink(os.path.join(unzip_dir, 'bin/gcutil'), '/usr/bin/gcutil')
    utils.Symlink(os.path.join(unzip_dir, 'bin/gsutil'), '/usr/bin/gsutil')
    utils.SecureDeleteFile(archive)
def ConfigureArchInstall(args, mount_path, parent_path, disk_uuid, aur_packages):
  relative_builder_path = utils.CopyBuilder(mount_path)
  packages_dir = utils.CreateTempDirectory(mount_path)
  utils.Run(['git', 'clone', COMPUTE_IMAGE_PACKAGES_GIT_URL, packages_dir])
  utils.CreateDirectory(os.path.join(mount_path, ''))
  aur_packages_dir = os.path.join(packages_dir, 'aur')
  for aur_package in aur_packages:
    utils.CopyFiles(aur_package, aur_packages_dir + '/')
  packages_dir = os.path.relpath(packages_dir, mount_path)
  params = {
    'packages_dir': '/%s' % packages_dir,
    'device': parent_path,
    'disk_uuid': disk_uuid,
    'accounts': args.accounts,
    'debugmode': args.debug,
    'quiet': args.quiet,
    'verbose': args.verbose,
    'packages': args.packages,
    'size_gb': args.size_gb
  }
  config_arch_py = os.path.join(
      '/', relative_builder_path, 'arch-image.py')
  utils.RunChroot(mount_path,
                  '%s "%s"' % (config_arch_py, utils.EncodeArgs(params)),
                  use_custom_path=False)
  utils.DeleteDirectory(os.path.join(mount_path, relative_builder_path))
def SetupNetwork():
  utils.LogStep('Setup Networking')
  utils.SecureDeleteFile('/etc/hostname')
  utils.WriteFile('/etc/hosts', ETC_HOSTS)
  utils.WriteFile('/etc/sysctl.d/70-disable-ipv6.conf',
                  ETC_SYSCTL_D_70_DISABLE_IPV6_CONF)
  utils.EnableService('dhcpcd.service')
  utils.EnableService('systemd-networkd.service')
  utils.EnableService('systemd-networkd-wait-online.service')
  # Set Google Compute specific MTU
  # https://cloud.google.com/compute/docs/troubleshooting#packetfragmentation
  utils.WriteFile('/etc/systemd/system/[email protected]', ETC_SYSTEM_D_SET_MTU)
  utils.CreateDirectory('/etc/conf.d/')
  utils.WriteFile('/etc/conf.d/setmtu', ETC_CONF_D_SET_MTU)
  utils.EnableService('*****@*****.**')
Beispiel #6
0
def InstallBootloader(device, uuid, debugmode):
    utils.LogStep('Install Syslinux bootloader')
    '''
  utils.Run(['syslinux-install_update', '-i', '-a', '-m'])
  '''
    utils.Run(['blkid', '-s', 'PTTYPE', '-o', 'value', device])
    utils.CreateDirectory('/boot/syslinux')
    utils.CopyFiles('/usr/lib/syslinux/bios/*.c32', '/boot/syslinux/')
    utils.Run(['extlinux', '--install', '/boot/syslinux'])
    utils.Replace('/boot/syslinux/syslinux.cfg', 'sda3', 'sda1')
    utils.Run(['fdisk', '-l', device])
    utils.Run([
        'dd', 'bs=440', 'count=1', 'conv=notrunc',
        'if=/usr/lib/syslinux/bios/mbr.bin',
        'of=%s' % device
    ])

    boot_params = [
        'console=ttyS0,38400',
        'CONFIG_KVM_GUEST=y',
        'CONFIG_KVM_CLOCK=y',
        'CONFIG_VIRTIO_PCI=y',
        'CONFIG_SCSI_VIRTIO=y',
        'CONFIG_VIRTIO_NET=y',
        'CONFIG_STRICT_DEVMEM=y',
        'CONFIG_DEVKMEM=n',
        'CONFIG_DEFAULT_MMAP_MIN_ADDR=65536',
        'CONFIG_DEBUG_RODATA=y',
        'CONFIG_DEBUG_SET_MODULE_RONX=y',
        'CONFIG_CC_STACKPROTECTOR=y',
        'CONFIG_COMPAT_VDSO=n',
        'CONFIG_COMPAT_BRK=n',
        'CONFIG_X86_PAE=y',
        'CONFIG_SYN_COOKIES=y',
        'CONFIG_SECURITY_YAMA=y',
        'CONFIG_SECURITY_YAMA_STACKED=y',
    ]
    if debugmode:
        boot_params += [
            'systemd.log_level=debug',
            'systemd.log_target=console',
            'systemd.journald.forward_to_syslog=yes',
            'systemd.journald.forward_to_kmsg=yes',
            'systemd.journald.forward_to_console=yes',
        ]
    boot_params = ' '.join(boot_params)
    boot_spec = '    APPEND root=UUID=%s rw append %s' % (uuid, boot_params)
    utils.ReplaceLine('/boot/syslinux/syslinux.cfg', 'APPEND root=', boot_spec)
def PrepareBootstrap(workspace_dir, mirror_server, use_pacman_keys):
    utils.LogStep('Setting up Bootstrap Environment')
    arch_root = os.path.join(workspace_dir, os.listdir(workspace_dir)[0])
    mirrorlist = 'Server = {MIRROR_SERVER}'.format(MIRROR_SERVER=mirror_server)
    utils.AppendFile(os.path.join(arch_root, 'etc/pacman.d/mirrorlist'),
                     mirrorlist)
    utils.CreateDirectory(os.path.join(arch_root, 'run/shm'))
    if use_pacman_keys:
        utils.RunChroot(arch_root, 'pacman-key --init')
        utils.RunChroot(arch_root, 'pacman-key --populate archlinux')
    else:
        utils.ReplaceLine(os.path.join(arch_root, 'etc/pacman.conf'),
                          'SigLevel', 'SigLevel = Never')
    # Install the most basic utilities for the bootstrapper.
    utils.RunChroot(arch_root, 'pacman --noconfirm -Sy python3 sed')

    return arch_root
Beispiel #8
0
def ConfigureArchInstall(args, mount_path, parent_path, disk_uuid):
    relative_builder_path = utils.CopyBuilder(mount_path)
    utils.LogStep('Download compute-image-packages')
    packages_dir = utils.CreateTempDirectory(mount_path)
    utils.Run(['git', 'clone', COMPUTE_IMAGE_PACKAGES_GIT_URL, packages_dir])
    utils.CreateDirectory(os.path.join(mount_path, ''))
    packages_dir = os.path.relpath(packages_dir, mount_path)
    params = {
        'packages_dir': '/%s' % packages_dir,
        'device': parent_path,
        'disk_uuid': disk_uuid,
        'accounts': args['accounts'],
        'debugmode': args['debugmode'],
    }
    params.update(args)
    config_arch_py = os.path.join('/', relative_builder_path, 'arch-image.py')
    utils.RunChroot(mount_path,
                    '%s "%s"' % (config_arch_py, utils.EncodeArgs(params)),
                    use_custom_path=False)
    utils.DeleteDirectory(os.path.join(mount_path, relative_builder_path))