def inject_data(image, key=None, net=None, metadata=None,
                partition=None, nbd=False, tune2fs=True):
    """Injects a ssh key and optionally net data into a disk image.

    it will mount the image as a fully partitioned disk and attempt to inject
    into the specified partition number.

    If partition is not specified it mounts the image as a single partition.

    """
    device = _link_device(image, nbd)
    try:
        if not partition is None:
            # create partition
            out, err = utils.execute('kpartx', '-a', device, run_as_root=True)
            if err:
                raise exception.Error(_('Failed to load partition: %s') % err)
            mapped_device = '/dev/mapper/%sp%s' % (device.split('/')[-1],
                                                   partition)
        else:
            mapped_device = device

        try:
            # We can only loopback mount raw images. If the device isn't there,
            # it's normally because it's a .vmdk or a .vdi etc
            if not os.path.exists(mapped_device):
                raise exception.Error('Mapped device was not found (we can'
                                      ' only inject raw disk images): %s' %
                                      mapped_device)

            if tune2fs:
                # Configure ext2fs so that it doesn't auto-check every N boots
                out, err = utils.execute('tune2fs', '-c', 0, '-i', 0,
                                         mapped_device, run_as_root=True)
            tmpdir = tempfile.mkdtemp()
            try:
                # mount loopback to dir
                out, err = utils.execute('mount', mapped_device, tmpdir,
                                         run_as_root=True)
                if err:
                    raise exception.Error(_('Failed to mount filesystem: %s')
                                          % err)

                try:
                    inject_data_into_fs(tmpdir, key, net, metadata,
                                        utils.execute)
                finally:
                    # unmount device
                    utils.execute('umount', mapped_device, run_as_root=True)
            finally:
                # remove temporary directory
                utils.execute('rmdir', tmpdir)
        finally:
            if not partition is None:
                # remove partitions
                utils.execute('kpartx', '-d', device, run_as_root=True)
    finally:
        _unlink_device(device, nbd)
dataset/ETHPy150Open nii-cloud/dodai-compute/nova/virt/disk.py/inject_data/original