You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by GitBox <gi...@apache.org> on 2022/03/28 18:26:59 UTC

[GitHub] [cloudstack] nvazquez commented on a change in pull request #3724: Storage-based Snapshots for KVM VMs

nvazquez commented on a change in pull request #3724:
URL: https://github.com/apache/cloudstack/pull/3724#discussion_r836715012



##########
File path: plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuCommand.java
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cloudstack.utils.qemu;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+public class QemuCommand {
+    //Qemu agent commands
+    public static final String AGENT_FREEZE = "guest-fsfreeze-freeze";
+    public static final String AGENT_THAW = "guest-fsfreeze-thaw";
+    public static final String AGENT_FREEZE_STATUS = "guest-fsfreeze-status";
+    //Qemu monitor commands
+    public static final String QEMU_QUERY_BLOCK_JOBS = "query-block-jobs";
+    public static final String QEMU_BLOCK = "query-block";
+    public static final String QEMU_DRIVE_BACKUP = "drive-backup";
+
+    public static final String QEMU_CMD = "execute";
+
+    public static Map<String, Object> executeQemuCommand(String command, Map<String, String> args ){

Review comment:
       Can this be renamed? It is a but misleading since nothing is executed at this time

##########
File path: plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtFreezeThawVMCommandWrapper.java
##########
@@ -0,0 +1,104 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.hypervisor.kvm.resource.wrapper;
+
+import org.apache.cloudstack.utils.qemu.QemuCommand;
+import org.apache.log4j.Logger;
+import org.libvirt.Connect;
+import org.libvirt.Domain;
+import org.libvirt.DomainInfo.DomainState;
+import org.libvirt.LibvirtException;
+
+import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.FreezeThawVMAnswer;
+import com.cloud.agent.api.FreezeThawVMCommand;
+import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
+import com.cloud.resource.CommandWrapper;
+import com.cloud.resource.ResourceWrapper;
+import com.google.gson.Gson;
+import com.google.gson.JsonParser;
+
+@ResourceWrapper(handles = FreezeThawVMCommand.class)
+public class LibvirtFreezeThawVMCommandWrapper extends CommandWrapper<FreezeThawVMCommand, Answer, LibvirtComputingResource> {
+
+    private static final Logger s_logger = Logger.getLogger(LibvirtFreezeThawVMCommandWrapper.class);
+
+    @Override
+    public Answer execute(FreezeThawVMCommand command, LibvirtComputingResource serverResource) {
+        String vmName = command.getVmName();
+        Domain domain = null;
+
+        try {
+            final LibvirtUtilitiesHelper libvirtUtilitiesHelper = serverResource.getLibvirtUtilitiesHelper();
+            Connect connect = libvirtUtilitiesHelper.getConnection();
+            domain = serverResource.getDomain(connect, vmName);
+            if (domain == null) {
+                return new FreezeThawVMAnswer(command, false, String.format("Failed to %s due to %s was not found",
+                        command.getOption(), vmName));
+            }
+            DomainState domainState = domain.getInfo().state ;
+            if (domainState != DomainState.VIR_DOMAIN_RUNNING) {
+                return new FreezeThawVMAnswer(command, false,
+                        String.format("%s of VM failed due to vm %s is in %s state", command.getOption(),
+                                vmName, domainState));
+            }
+
+            String result = getResultOfQemuCommand(command.getOption(), domain);
+            s_logger.debug(String.format("Result of %s command is %s", command.getOption(), result));
+            if (result == null || (result.startsWith("error"))) {
+                return new FreezeThawVMAnswer(command, false, String.format("Failed to %s vm %s due to result status is: %s",
+                        command.getOption(), vmName, result));
+            }
+            String status = getResultOfQemuCommand(FreezeThawVMCommand.STATUS, domain);
+            s_logger.debug(String.format("Status of %s command is %s", command.getOption(), status));
+            if (status != null && new JsonParser().parse(status).isJsonObject()) {
+                String statusResult = new JsonParser().parse(status).getAsJsonObject().get("return").getAsString();
+                if (statusResult.equals(command.getOption())) {
+                    return new FreezeThawVMAnswer(command, true, String.format("%s of VM - %s is successful", command.getOption(), vmName));
+                }
+            }
+            return new FreezeThawVMAnswer(command, false, String.format("Failed to %s vm %s due to result status is: %s",
+                    command.getOption(), vmName, status));
+        } catch (LibvirtException libvirtException) {
+            return new FreezeThawVMAnswer(command, false,  String.format("Failed to %s VM - %s due to %s",
+                    command.getOption(), vmName, libvirtException.getMessage()));
+        }finally {
+            if (domain != null) {
+                try {
+                    domain.free();
+                } catch (LibvirtException e) {
+                    s_logger.trace("Ingore error ", e);
+                }
+            }
+        }
+    }
+
+    private String getResultOfQemuCommand(String cmd, Domain domain) throws LibvirtException {
+        String result = null;
+        if (cmd.equals(FreezeThawVMCommand.FREEZE)) {
+            result = domain.qemuAgentCommand(new Gson().toJson(QemuCommand.executeQemuCommand(QemuCommand.AGENT_FREEZE, null)).toString(), 10, 0);

Review comment:
       I think readability could be increased on these 3 calls by making the `QemuCommand.executeQemuCommand` (or renamed) method return a string instead of a map.
   
   Also, these 3 calls could be reduced to only 1, by defining a collection that returns the agent "guest-fsfreeze-*" command based on the `cmd`

##########
File path: plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuCommand.java
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cloudstack.utils.qemu;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+public class QemuCommand {
+    //Qemu agent commands
+    public static final String AGENT_FREEZE = "guest-fsfreeze-freeze";
+    public static final String AGENT_THAW = "guest-fsfreeze-thaw";
+    public static final String AGENT_FREEZE_STATUS = "guest-fsfreeze-status";
+    //Qemu monitor commands
+    public static final String QEMU_QUERY_BLOCK_JOBS = "query-block-jobs";

Review comment:
       These 3 are not used, could be removed?

##########
File path: server/src/main/java/com/cloud/storage/snapshot/SnapshotManager.java
##########
@@ -59,6 +59,8 @@
     public static final ConfigKey<Boolean> BackupSnapshotAfterTakingSnapshot = new ConfigKey<Boolean>(Boolean.class, "snapshot.backup.to.secondary",  "Snapshots", "true",
             "Indicates whether to always backup primary storage snapshot to secondary storage. Keeping snapshots only on Primary storage is applicable for KVM + Ceph only.", false, ConfigKey.Scope.Global, null);
 
+    public static final ConfigKey<Boolean> VMsnapshotKVM = new ConfigKey<>(Boolean.class, "kvm.vmstoragesnapshot.enabled", "Snapshots", "false", "For live snapshot of virtual machine instance on KVM hypervisor without memory. Requieres qemu version 1.6+ (on NFS or Local file system) and qemu-guest-agent installed on guest VM", true, ConfigKey.Scope.Global, null);

Review comment:
       Could it be renamed to something like 'VMStorageSnapshotKVM'? Could the scope be reduced to zone?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org