Bootstrap azure images directly to VHD

This commit is contained in:
Anders Ingemann 2015-10-14 22:39:42 +02:00
parent d5ef584b42
commit f572703ecc
5 changed files with 27 additions and 31 deletions

View file

@ -22,10 +22,12 @@ def load_volume(data, bootloader):
from bootstrapvz.common.fs.loopbackvolume import LoopbackVolume from bootstrapvz.common.fs.loopbackvolume import LoopbackVolume
from bootstrapvz.providers.ec2.ebsvolume import EBSVolume from bootstrapvz.providers.ec2.ebsvolume import EBSVolume
from bootstrapvz.common.fs.virtualdiskimage import VirtualDiskImage from bootstrapvz.common.fs.virtualdiskimage import VirtualDiskImage
from bootstrapvz.common.fs.virtualharddisk import VirtualHardDisk
from bootstrapvz.common.fs.virtualmachinedisk import VirtualMachineDisk from bootstrapvz.common.fs.virtualmachinedisk import VirtualMachineDisk
volume_backing = {'raw': LoopbackVolume, volume_backing = {'raw': LoopbackVolume,
's3': LoopbackVolume, 's3': LoopbackVolume,
'vdi': VirtualDiskImage, 'vdi': VirtualDiskImage,
'vhd': VirtualHardDisk,
'vmdk': VirtualMachineDisk, 'vmdk': VirtualMachineDisk,
'ebs': EBSVolume 'ebs': EBSVolume
}.get(data['backing']) }.get(data['backing'])

View file

@ -0,0 +1,23 @@
from qemuvolume import QEMUVolume
from ..tools import log_check_call
class VirtualHardDisk(QEMUVolume):
extension = 'vhd'
qemu_format = 'vpc'
ovf_uri = 'http://go.microsoft.com/fwlink/?LinkId=137171'
# Azure requires the image size to be a multiple of 1 MiB.
# VHDs are dynamic by default, so we add the option
# to make the image size fixed (subformat=fixed)
def _before_create(self, e):
self.image_path = e.image_path
vol_size = str(self.size.bytes.get_qty_in('MiB')) + 'M'
log_check_call(['qemu-img', 'create', '-o', 'subformat=fixed', '-f', self.qemu_format, self.image_path, vol_size])
def get_uuid(self):
if not hasattr(self, 'uuid'):
import uuid
self.uuid = uuid.uuid4()
return self.uuid

View file

@ -1,7 +1,6 @@
from bootstrapvz.common import task_groups from bootstrapvz.common import task_groups
import tasks.packages import tasks.packages
import tasks.boot import tasks.boot
import tasks.image
from bootstrapvz.common.tasks import loopback from bootstrapvz.common.tasks import loopback
from bootstrapvz.common.tasks import initd from bootstrapvz.common.tasks import initd
from bootstrapvz.common.tasks import ssh from bootstrapvz.common.tasks import ssh
@ -20,6 +19,7 @@ def resolve_tasks(taskset, manifest):
tasks.packages.DefaultPackages, tasks.packages.DefaultPackages,
loopback.AddRequiredCommands, loopback.AddRequiredCommands,
loopback.Create, loopback.Create,
loopback.MoveImage,
initd.InstallInitScripts, initd.InstallInitScripts,
ssh.AddOpenSSHPackage, ssh.AddOpenSSHPackage,
ssh.ShredHostkeys, ssh.ShredHostkeys,
@ -27,7 +27,6 @@ def resolve_tasks(taskset, manifest):
tasks.packages.Waagent, tasks.packages.Waagent,
tasks.boot.ConfigureGrub, tasks.boot.ConfigureGrub,
tasks.boot.PatchUdev, tasks.boot.PatchUdev,
tasks.image.ConvertToVhd,
]) ])

View file

@ -26,7 +26,7 @@ properties:
properties: properties:
backing: backing:
type: string type: string
enum: [raw] enum: [vhd]
partitions: partitions:
type: object type: object
properties: properties:

View file

@ -1,28 +0,0 @@
from bootstrapvz.base import Task
from bootstrapvz.common import phases
class ConvertToVhd(Task):
description = 'Convert raw image to vhd disk'
phase = phases.image_registration
@classmethod
def run(cls, info):
image_name = info.manifest.image['name'].format(**info.manifest_vars)
filename = image_name + '.vhd'
import os.path
destination = os.path.join(info.manifest.bootstrapper['workspace'], filename)
file_size = os.path.getsize(info.volume.image_path)
rounded_vol_size = str(((file_size / (1024 * 1024) + 1) * (1024 * 1024)))
from bootstrapvz.common.tools import log_check_call
log_check_call(['qemu-img', 'resize', info.volume.image_path, rounded_vol_size])
log_check_call(['qemu-img', 'convert',
'-o', 'subformat=fixed',
'-O', 'vpc',
info.volume.image_path, destination])
os.remove(info.volume.image_path)
import logging
log = logging.getLogger(__name__)
log.info('The volume image has been moved to ' + destination)