@@ -1228,21 +1228,46 @@ protected Answer execute(CreateCommand cmd) {
12281228 protected ManageSnapshotAnswer execute (final ManageSnapshotCommand cmd ) {
12291229 String snapshotName = cmd .getSnapshotName ();
12301230 String VolPath = cmd .getVolumePath ();
1231+ String snapshotPath = cmd .getSnapshotPath ();
1232+ String vmName = cmd .getVmName ();
12311233 try {
1232- StorageVol vol = getVolume (VolPath );
1233- if (vol == null ) {
1234- return new ManageSnapshotAnswer (cmd , false , null );
1234+ DomainInfo .DomainState state = null ;
1235+ Domain vm = null ;
1236+ if (vmName != null ) {
1237+ try {
1238+ vm = getDomain (cmd .getVmName ());
1239+ state = vm .getInfo ().state ;
1240+ } catch (LibvirtException e ) {
1241+
1242+ }
12351243 }
1236- Domain vm = getDomain (cmd .getVmName ());
1237- String vmUuid = vm .getUUIDString ();
1238- Object [] args = new Object [] {snapshotName , vmUuid };
1239- String snapshot = SnapshotXML .format (args );
1240- s_logger .debug (snapshot );
1241- if (cmd .getCommandSwitch ().equalsIgnoreCase (ManageSnapshotCommand .CREATE_SNAPSHOT )) {
1242- vm .snapshotCreateXML (snapshot );
1244+
1245+ if (state == DomainInfo .DomainState .VIR_DOMAIN_RUNNING ) {
1246+ String vmUuid = vm .getUUIDString ();
1247+ Object [] args = new Object [] {snapshotName , vmUuid };
1248+ String snapshot = SnapshotXML .format (args );
1249+ s_logger .debug (snapshot );
1250+ if (cmd .getCommandSwitch ().equalsIgnoreCase (ManageSnapshotCommand .CREATE_SNAPSHOT )) {
1251+ vm .snapshotCreateXML (snapshot );
1252+ } else {
1253+ DomainSnapshot snap = vm .snapshotLookupByName (snapshotName );
1254+ snap .delete (0 );
1255+ }
12431256 } else {
1244- DomainSnapshot snap = vm .snapshotLookupByName (snapshotName );
1245- snap .delete (0 );
1257+ /*VM is not running, create a snapshot by ourself*/
1258+ final Script command = new Script (_manageSnapshotPath , _timeout , s_logger );
1259+ if (cmd .getCommandSwitch ().equalsIgnoreCase (ManageSnapshotCommand .CREATE_SNAPSHOT )) {
1260+ command .add ("-c" , VolPath );
1261+ } else {
1262+ command .add ("-d" , snapshotPath );
1263+ }
1264+
1265+ command .add ("-n" , snapshotName );
1266+ String result = command .execute ();
1267+ if (result != null ) {
1268+ s_logger .debug ("Failed to manage snapshot: " + result );
1269+ return new ManageSnapshotAnswer (cmd , false , "Failed to manage snapshot: " + result );
1270+ }
12461271 }
12471272 } catch (LibvirtException e ) {
12481273 s_logger .debug ("Failed to manage snapshot: " + e .toString ());
@@ -1259,28 +1284,52 @@ protected BackupSnapshotAnswer execute(final BackupSnapshotCommand cmd) {
12591284 String snapshotName = cmd .getSnapshotName ();
12601285 String snapshotPath = cmd .getSnapshotUuid ();
12611286 String snapshotDestPath = null ;
1287+ String vmName = cmd .getVmName ();
12621288
12631289 try {
12641290 StoragePool secondaryStoragePool = getNfsSPbyURI (_conn , new URI (secondaryStoragePoolURL ));
12651291 String ssPmountPath = _mountPoint + File .separator + secondaryStoragePool .getUUIDString ();
12661292 snapshotDestPath = ssPmountPath + File .separator + dcId + File .separator + "snapshots" + File .separator + accountId + File .separator + volumeId ;
1267- final Script command = new Script (_manageSnapshotPath , _timeout , s_logger );
1293+ Script command = new Script (_manageSnapshotPath , _timeout , s_logger );
12681294 command .add ("-b" , snapshotPath );
12691295 command .add ("-n" , snapshotName );
12701296 command .add ("-p" , snapshotDestPath );
1297+ command .add ("-t" , snapshotName );
12711298 String result = command .execute ();
12721299 if (result != null ) {
12731300 s_logger .debug ("Failed to backup snaptshot: " + result );
12741301 return new BackupSnapshotAnswer (cmd , false , result , null );
12751302 }
12761303 /*Delete the snapshot on primary*/
1277- Domain vm = getDomain (cmd .getVmName ());
1278- String vmUuid = vm .getUUIDString ();
1279- Object [] args = new Object [] {snapshotName , vmUuid };
1280- String snapshot = SnapshotXML .format (args );
1281- s_logger .debug (snapshot );
1282- DomainSnapshot snap = vm .snapshotLookupByName (snapshotName );
1283- snap .delete (0 );
1304+
1305+ DomainInfo .DomainState state = null ;
1306+ Domain vm = null ;
1307+ if (vmName != null ) {
1308+ try {
1309+ vm = getDomain (cmd .getVmName ());
1310+ state = vm .getInfo ().state ;
1311+ } catch (LibvirtException e ) {
1312+
1313+ }
1314+ }
1315+
1316+ if (state == DomainInfo .DomainState .VIR_DOMAIN_RUNNING ) {
1317+ String vmUuid = vm .getUUIDString ();
1318+ Object [] args = new Object [] {snapshotName , vmUuid };
1319+ String snapshot = SnapshotXML .format (args );
1320+ s_logger .debug (snapshot );
1321+ DomainSnapshot snap = vm .snapshotLookupByName (snapshotName );
1322+ snap .delete (0 );
1323+ } else {
1324+ command = new Script (_manageSnapshotPath , _timeout , s_logger );
1325+ command .add ("-d" , snapshotPath );
1326+ command .add ("-n" , snapshotName );
1327+ result = command .execute ();
1328+ if (result != null ) {
1329+ s_logger .debug ("Failed to backup snapshot: " + result );
1330+ return new BackupSnapshotAnswer (cmd , false , "Failed to backup snapshot: " + result , null );
1331+ }
1332+ }
12841333 } catch (LibvirtException e ) {
12851334 return new BackupSnapshotAnswer (cmd , false , e .toString (), null );
12861335 } catch (URISyntaxException e ) {
@@ -1356,7 +1405,11 @@ protected CreatePrivateTemplateAnswer execute(final CreatePrivateTemplateFromSna
13561405 try {
13571406 secondaryPool = getNfsSPbyURI (_conn , new URI (cmd .getSecondaryStoragePoolURL ()));
13581407 /*TODO: assuming all the storage pools mounted under _mountPoint, the mount point should be got from pool.dumpxml*/
1359- String templatePath = _mountPoint + File .separator + secondaryPool .getUUIDString () + File .separator + templateInstallFolder ;
1408+ String templatePath = _mountPoint + File .separator + secondaryPool .getUUIDString () + File .separator + templateInstallFolder ;
1409+ File f = new File (templatePath );
1410+ if (!f .exists ()) {
1411+ f .mkdir ();
1412+ }
13601413 String tmplPath = templateInstallFolder + File .separator + tmplFileName ;
13611414 Script command = new Script (_createTmplPath , _timeout , s_logger );
13621415 command .add ("-t" , templatePath );
@@ -1403,38 +1456,58 @@ protected GetStorageStatsAnswer execute(final GetStorageStatsCommand cmd) {
14031456 }
14041457 protected CreatePrivateTemplateAnswer execute (CreatePrivateTemplateCommand cmd ) {
14051458 String secondaryStorageURL = cmd .getSecondaryStorageURL ();
1406- String snapshotUUID = cmd .getSnapshotPath ();
14071459
14081460 StoragePool secondaryStorage = null ;
1409- StoragePool privateTemplStorage = null ;
1410- StorageVol privateTemplateVol = null ;
1411- StorageVol snapshotVol = null ;
14121461 try {
14131462 String templateFolder = cmd .getAccountId () + File .separator + cmd .getTemplateId () + File .separator ;
14141463 String templateInstallFolder = "/template/tmpl/" + templateFolder ;
1415-
1464+
14161465 secondaryStorage = getNfsSPbyURI (_conn , new URI (secondaryStorageURL ));
14171466 /*TODO: assuming all the storage pools mounted under _mountPoint, the mount point should be got from pool.dumpxml*/
1418- String mountPath = _mountPoint + File .separator + secondaryStorage .getUUIDString () + templateInstallFolder ;
1419- File mpfile = new File (mountPath );
1467+ String tmpltPath = _mountPoint + File .separator + secondaryStorage .getUUIDString () + templateInstallFolder ;
1468+ File mpfile = new File (tmpltPath );
14201469 if (!mpfile .exists ()) {
14211470 mpfile .mkdir ();
14221471 }
1472+
1473+ Script command = new Script (_createTmplPath , _timeout , s_logger );
1474+ command .add ("-f" , cmd .getSnapshotPath ());
1475+ command .add ("-c" , cmd .getSnapshotName ());
1476+ command .add ("-t" , tmpltPath );
1477+ command .add ("-n" , cmd .getUniqueName () + ".qcow2" );
1478+ command .add ("-s" );
1479+ String result = command .execute ();
14231480
1424- // Create a SR for the secondary storage installation folder
1425- privateTemplStorage = getNfsSPbyURI (_conn , new URI (secondaryStorageURL + templateInstallFolder ));
1426- snapshotVol = getVolume (snapshotUUID );
1427-
1428- LibvirtStorageVolumeDef vol = new LibvirtStorageVolumeDef (UUID .randomUUID ().toString (), snapshotVol .getInfo ().capacity , volFormat .QCOW2 , null , null );
1429- s_logger .debug (vol .toString ());
1430- privateTemplateVol = copyVolume (privateTemplStorage , vol , snapshotVol );
1481+ if (result != null ) {
1482+ s_logger .debug ("failed to create template: " + result );
1483+ return new CreatePrivateTemplateAnswer (cmd ,
1484+ false ,
1485+ result ,
1486+ null ,
1487+ 0 ,
1488+ null ,
1489+ null );
1490+ }
1491+
1492+ Map <String , Object > params = new HashMap <String , Object >();
1493+ params .put (StorageLayer .InstanceConfigKey , _storage );
1494+ Processor qcow2Processor = new QCOW2Processor ();
1495+
1496+ qcow2Processor .configure ("QCOW2 Processor" , params );
1497+
1498+ FormatInfo info = qcow2Processor .process (tmpltPath , null , cmd .getUniqueName ());
1499+
1500+ TemplateLocation loc = new TemplateLocation (_storage , tmpltPath );
1501+ loc .create (1 , true , cmd .getUniqueName ());
1502+ loc .addFormat (info );
1503+ loc .save ();
14311504
14321505 return new CreatePrivateTemplateAnswer (cmd ,
14331506 true ,
14341507 null ,
1435- templateInstallFolder + privateTemplateVol . getName () ,
1436- privateTemplateVol . getInfo (). capacity / 1024 * 1024 , /*in Mega unit*/
1437- privateTemplateVol . getName (),
1508+ templateInstallFolder + cmd . getUniqueName () + ".qcow2" ,
1509+ info . virtualSize ,
1510+ cmd . getUniqueName (),
14381511 ImageFormat .QCOW2 );
14391512 } catch (URISyntaxException e ) {
14401513 return new CreatePrivateTemplateAnswer (cmd ,
@@ -1453,7 +1526,31 @@ protected CreatePrivateTemplateAnswer execute(CreatePrivateTemplateCommand cmd)
14531526 0 ,
14541527 null ,
14551528 null );
1456- }
1529+ } catch (InternalErrorException e ) {
1530+ return new CreatePrivateTemplateAnswer (cmd ,
1531+ false ,
1532+ e .toString (),
1533+ null ,
1534+ 0 ,
1535+ null ,
1536+ null );
1537+ } catch (IOException e ) {
1538+ return new CreatePrivateTemplateAnswer (cmd ,
1539+ false ,
1540+ e .toString (),
1541+ null ,
1542+ 0 ,
1543+ null ,
1544+ null );
1545+ } catch (ConfigurationException e ) {
1546+ return new CreatePrivateTemplateAnswer (cmd ,
1547+ false ,
1548+ e .toString (),
1549+ null ,
1550+ 0 ,
1551+ null ,
1552+ null );
1553+ }
14571554 }
14581555
14591556 private StoragePool getNfsSPbyURI (Connect conn , URI uri ) throws LibvirtException {
@@ -3165,7 +3262,7 @@ private List<diskDef> createSystemVMDisk(List<VolumeVO> vols) throws InternalErr
31653262 String datadiskPath = tmplVol .getKey ();
31663263
31673264 diskDef hda = new diskDef ();
3168- hda .defFileBasedDisk (rootkPath , "vda " , diskDef .diskBus .IDE , diskDef .diskFmtType .QCOW2 );
3265+ hda .defFileBasedDisk (rootkPath , "hda " , diskDef .diskBus .IDE , diskDef .diskFmtType .QCOW2 );
31693266 disks .add (hda );
31703267
31713268 diskDef hdb = new diskDef ();
0 commit comments