コード例 #1
0
ファイル: imagebuilder.py プロジェクト: zhuizhuhaomeng/photon
def createImage(options):
    (validImage, config) = verifyImageTypeAndConfig(options.config_file,
                                                    options.img_name)
    if not validImage:
        raise Exception("Image type/config not supported")

    if 'ova' in config['artifacttype'] and shutil.which("ovftool") is None:
        raise Exception("ovftool is not available")

    install_config = config['installer']

    image_type = config['image_type']
    image_name = config.get('image_name', 'photon-' + image_type)
    workingDir = os.path.abspath(options.stage_path + "/" + image_type)
    if os.path.exists(workingDir) and os.path.isdir(workingDir):
        shutil.rmtree(workingDir)
    os.mkdir(workingDir)
    script_dir = os.path.dirname(os.path.realpath(__file__))

    grub_script = replaceScript(script_dir, image_type, "mk-setup-grub.sh",
                                options.installer_path)
    install_config['setup_grub_script'] = grub_script

    # Set absolute path for 'packagelist_file'
    if 'packagelist_file' in install_config:
        plf = install_config['packagelist_file']
        if not plf.startswith('/'):
            plf = os.path.join(options.generated_data_path, plf)
        install_config['packagelist_file'] = plf

    os.chdir(workingDir)
    image_file = workingDir + "/" + image_name + ".raw"

    # Create disk image
    Utils.runshellcommand(
        "dd if=/dev/zero of={} bs=1024 seek={} count=0".format(
            image_file, config['size'] * 1024))
    Utils.runshellcommand("chmod 755 {}".format(image_file))

    if 'log_level' not in install_config:
        install_config['log_level'] = options.log_level

    install_config['search_path'] = [
        os.path.abspath(os.path.join(script_dir, image_type)),
        os.path.abspath(script_dir),
    ]

    # Associating loopdevice to raw disk and save the name as a target's 'disk'
    install_config['disk'] = (Utils.runshellcommand(
        "losetup --show -f {}".format(image_file))).rstrip('\n')

    # No return value, it throws exception on error.
    runInstaller(options, install_config, workingDir)

    # Detaching loop device from vmdk
    Utils.runshellcommand("losetup -d {}".format(install_config['disk']))

    os.chdir(script_dir)
    imagegenerator.createOutputArtifact(image_file, config, options.src_root,
                                        options.src_root + '/tools/bin/')
コード例 #2
0
def createImage(options):
    (validImage, config) = verifyImageTypeAndConfig(options.config_file,
                                                    options.img_name)
    if not validImage:
        raise Exception("Image type/config not supported")

    if 'ova' in config['artifacttype'] and shutil.which("ovftool") is None:
        raise Exception("ovftool is not available")

    install_config = config['installer']

    image_type = config['image_type']
    image_name = config.get('image_name', 'photon-' + image_type)
    workingDir = os.path.abspath(options.stage_path + "/" + image_type)
    if os.path.exists(workingDir) and os.path.isdir(workingDir):
        shutil.rmtree(workingDir)
    os.mkdir(workingDir)
    script_dir = os.path.dirname(os.path.realpath(__file__))

    grub_script = replaceScript(script_dir, image_type, "mk-setup-grub.sh",
                                options.installer_path)
    install_config['setup_grub_script'] = grub_script

    # Set absolute path for 'packagelist_file'
    if 'packagelist_file' in install_config:
        plf = install_config['packagelist_file']
        if not plf.startswith('/'):
            plf = os.path.join(options.generated_data_path, plf)
        install_config['packagelist_file'] = plf

    os.chdir(workingDir)

    if 'log_level' not in install_config:
        install_config['log_level'] = options.log_level

    install_config['search_path'] = [
        os.path.abspath(os.path.join(script_dir, image_type)),
        os.path.abspath(script_dir),
    ]

    if 'size' in config and 'disks' in config:
        raise Exception(
            "Both 'size' and 'disks' key should not be defined together.Please use 'disks' for defining multidisks only."
        )
    elif 'size' in config:
        # 'BOOTDISK' key name doesn't matter. It is just a name given for better understanding
        config['disks'] = {"BOOTDISK": config['size']}
    elif 'disks' not in config:
        raise Exception("Disk size not defined!!")

    image_file = []
    loop_device = {}
    # Create disk image
    for ndisk, k in enumerate(config['disks']):
        image_file.append(workingDir + "/" + image_name + "-" + str(ndisk) +
                          ".raw")
        Utils.runshellcommand(
            "dd if=/dev/zero of={} bs=1024 seek={} count=0".format(
                image_file[ndisk], config['disks'].get(k) * 1024))
        Utils.runshellcommand("chmod 755 {}".format(image_file[ndisk]))
        # Associating loopdevice to raw disk and save the name as a target's 'disk'
        loop_device[k] = (Utils.runshellcommand("losetup --show -f {}".format(
            image_file[ndisk]))).rstrip('\n')

    # Assigning first loop device as BOOTDISK
    install_config['disk'] = loop_device[next(iter(loop_device))]

    # Mapping the given disks to the partition table disk
    # Assigning the appropriate loop device to the partition 'disk'
    if 'partitions' in install_config:
        for partition in install_config['partitions']:
            if len(loop_device) == 1:
                partition['disk'] = install_config['disk']
            elif 'disk' in partition:
                if partition['disk'] in loop_device.keys():
                    partition['disk'] = loop_device[partition['disk']]
                else:
                    cleanup(loop_device.values(), image_file)
                    raise Exception(
                        "disk name:{} defined in partition table not found in list of 'disks'!!"
                        .format(partition['disk']))
            else:
                cleanup(loop_device.values(), image_file)
                raise Exception(
                    "disk name must be defined in partition table for multidisks!!"
                )

    # No return value, it throws exception on error.
    runInstaller(options, install_config, workingDir)

    # Detaching loop device from vmdk
    for loop_dev in loop_device.values():
        Utils.runshellcommand("losetup -d {}".format(loop_dev))

    os.chdir(script_dir)
    imagegenerator.createOutputArtifact(image_file, config, options.src_root,
                                        options.src_root + '/tools/bin/')