From e7749afdfe4d492bf61a1e4d849f35e89eaafe18 Mon Sep 17 00:00:00 2001 From: Kelven Yang Date: Wed, 29 Dec 2010 21:52:06 -0800 Subject: [PATCH] Bug 7772: Add hypervisor type to snapshots table, fix issues with createVolumeFromSnapshot if snapshot was taken under data volume --- api/src/com/cloud/storage/Snapshot.java | 5 ++++- core/src/com/cloud/storage/SnapshotVO.java | 18 +++++++++++++++--- .../com/cloud/storage/StorageManagerImpl.java | 6 +++--- .../storage/snapshot/SnapshotManagerImpl.java | 3 ++- server/src/com/cloud/vm/UserVmManagerImpl.java | 5 ++++- setup/db/create-schema.sql | 7 ++++--- 6 files changed, 32 insertions(+), 12 deletions(-) diff --git a/api/src/com/cloud/storage/Snapshot.java b/api/src/com/cloud/storage/Snapshot.java index 284c091b192..7a0cd896842 100644 --- a/api/src/com/cloud/storage/Snapshot.java +++ b/api/src/com/cloud/storage/Snapshot.java @@ -19,6 +19,8 @@ package com.cloud.storage; import java.util.Date; + +import com.cloud.hypervisor.Hypervisor.HypervisorType; public interface Snapshot { public enum Type { @@ -59,5 +61,6 @@ public interface Snapshot { String getName(); Date getCreated(); short getSnapshotType(); - Status getStatus(); + Status getStatus(); + HypervisorType getHypervisorType(); } diff --git a/core/src/com/cloud/storage/SnapshotVO.java b/core/src/com/cloud/storage/SnapshotVO.java index 4b4c3b4dbac..f499db87c99 100644 --- a/core/src/com/cloud/storage/SnapshotVO.java +++ b/core/src/com/cloud/storage/SnapshotVO.java @@ -31,6 +31,7 @@ import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.TableGenerator; +import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.utils.db.GenericDao; import com.google.gson.annotations.Expose; @@ -79,10 +80,14 @@ public class SnapshotVO implements Snapshot { @Column(name="prev_snap_id") long prevSnapshotId; + + @Column(name="hypervisor_type") + @Enumerated(value=EnumType.STRING) + HypervisorType hypervisorType; public SnapshotVO() { } - public SnapshotVO(long id, long accountId, long volumeId, String path, String name, short snapshotType, String typeDescription) { + public SnapshotVO(long id, long accountId, long volumeId, String path, String name, short snapshotType, String typeDescription, HypervisorType hypervisorType) { this.id = id; this.accountId = accountId; this.volumeId = volumeId; @@ -91,7 +96,8 @@ public class SnapshotVO implements Snapshot { this.snapshotType = snapshotType; this.typeDescription = typeDescription; this.status = Status.Creating; - this.prevSnapshotId = 0; + this.prevSnapshotId = 0; + this.hypervisorType = hypervisorType; } @Override @@ -126,7 +132,13 @@ public class SnapshotVO implements Snapshot { @Override public short getSnapshotType() { return snapshotType; - } + } + + @Override + public HypervisorType getHypervisorType() { + return hypervisorType; + } + public void setSnapshotType(short snapshotType) { this.snapshotType = snapshotType; } diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index f7642152f44..533db871e9a 100755 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -546,7 +546,7 @@ public class StorageManagerImpl implements StorageManager, StorageService, Manag } @DB - protected Pair createVolumeFromSnapshot(VolumeVO volume, SnapshotVO snapshot, VMTemplateVO template, long virtualsize) { + protected Pair createVolumeFromSnapshot(VolumeVO volume, SnapshotVO snapshot, long virtualsize) { VolumeVO createdVolume = null; Long volumeId = volume.getId(); @@ -565,7 +565,7 @@ public class StorageManagerImpl implements StorageManager, StorageService, Manag DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId()); DataCenterVO dc = _dcDao.findById(volume.getDataCenterId()); - DiskProfile dskCh = new DiskProfile(volume, diskOffering, template.getHypervisorType()); + DiskProfile dskCh = new DiskProfile(volume, diskOffering, snapshot.getHypervisorType()); int retry = 0; // Determine what pod to store the volume in @@ -658,7 +658,7 @@ public class StorageManagerImpl implements StorageManager, StorageService, Manag template = _templateDao.findById(originalVolume.getTemplateId()); } - Pair volumeDetails = createVolumeFromSnapshot(volume, snapshot, template, originalVolume.getSize()); + Pair volumeDetails = createVolumeFromSnapshot(volume, snapshot, originalVolume.getSize()); createdVolume = volumeDetails.first(); Transaction txn = Transaction.currentTxn(); diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java index 5132c20851b..ded81534115 100755 --- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java +++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java @@ -231,8 +231,9 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma // Create the Snapshot object and save it so we can return it to the // user Type snapshotType = SnapshotVO.getSnapshotType(policyId); + HypervisorType hypervisorType = this._volsDao.getHypervisorType(volumeId); SnapshotVO snapshotVO = new SnapshotVO(snapshotId, volume.getAccountId(), volume.getId(), null, snapshotName, - (short) snapshotType.ordinal(), snapshotType.name()); + (short) snapshotType.ordinal(), snapshotType.name(), hypervisorType); snapshotVO = _snapshotDao.persist(snapshotVO); id = snapshotVO.getId(); assert id != null; diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 632d9f4985c..ee2da1dab70 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -1844,7 +1844,10 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager // Specify RAW format makes it unusable for snapshots. privateTemplate.setFormat(ImageFormat.RAW); } - + + if(snapshot != null) + privateTemplate.setHypervisorType(snapshot.getHypervisorType()); + _templateDao.update(templateId, privateTemplate); // add template zone ref for this template diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index da234a6d30f..6bf31db3609 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -228,7 +228,7 @@ CREATE TABLE `cloud`.`cluster` ( `guid` varchar(255) UNIQUE DEFAULT NULL COMMENT 'guid for the cluster', `pod_id` bigint unsigned NOT NULL COMMENT 'pod id', `data_center_id` bigint unsigned NOT NULL COMMENT 'data center id', - `hypervisor_type` varchar(255), + `hypervisor_type` varchar(32), `cluster_type` varchar(64) DEFAULT 'CloudManaged', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; @@ -361,6 +361,7 @@ CREATE TABLE `cloud`.`snapshots` ( `removed` datetime COMMENT 'Date removed. not null if removed', `backup_snap_id` varchar(255) COMMENT 'Back up uuid of the snapshot', `prev_snap_id` bigint unsigned COMMENT 'Id of the most recent snapshot', + `hypervisor_type` varchar(32) NOT NULL COMMENT 'hypervisor that the snapshot was taken under', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; @@ -693,7 +694,7 @@ CREATE TABLE `cloud`.`vm_template` ( `prepopulate` int(1) unsigned NOT NULL default 0 COMMENT 'prepopulate this template to primary storage', `cross_zones` int(1) unsigned NOT NULL default 0 COMMENT 'Make this template available in all zones', `extractable` int(1) unsigned NOT NULL default 1 COMMENT 'Is this template extractable', - `hypervisor_type` varchar(255) COMMENT 'hypervisor that the template is belonged to', + `hypervisor_type` varchar(32) COMMENT 'hypervisor that the template is belonged to', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; @@ -1144,7 +1145,7 @@ CREATE TABLE `cloud`.`guest_os` ( CREATE TABLE `cloud`.`guest_os_hypervisor` ( `id` bigint unsigned NOT NULL auto_increment, - `hypervisor_type` varchar(255) NOT NULL, + `hypervisor_type` varchar(32) NOT NULL, `guest_os_name` varchar(255) NOT NULL, `guest_os_id` bigint unsigned NOT NULL, PRIMARY KEY (`id`)