|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.hypervisor.kvm.resource.wrapper; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | + |
| 22 | +import com.cloud.agent.api.to.DataStoreTO; |
| 23 | +import com.cloud.agent.api.to.NfsTO; |
| 24 | +import com.cloud.api.storage.LinstorBackupSnapshotCommand; |
| 25 | +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; |
| 26 | +import com.cloud.hypervisor.kvm.storage.KVMStoragePool; |
| 27 | +import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; |
| 28 | +import com.cloud.resource.CommandWrapper; |
| 29 | +import com.cloud.resource.ResourceWrapper; |
| 30 | +import com.cloud.storage.Storage; |
| 31 | +import com.cloud.utils.script.Script; |
| 32 | +import org.apache.cloudstack.storage.command.CopyCmdAnswer; |
| 33 | +import org.apache.cloudstack.storage.to.SnapshotObjectTO; |
| 34 | +import org.apache.cloudstack.utils.qemu.QemuImg; |
| 35 | +import org.apache.cloudstack.utils.qemu.QemuImgException; |
| 36 | +import org.apache.cloudstack.utils.qemu.QemuImgFile; |
| 37 | +import org.apache.commons.io.FileUtils; |
| 38 | +import org.apache.log4j.Logger; |
| 39 | +import org.joda.time.Duration; |
| 40 | +import org.libvirt.LibvirtException; |
| 41 | + |
| 42 | +@ResourceWrapper(handles = LinstorBackupSnapshotCommand.class) |
| 43 | +public final class LinstorBackupSnapshotCommandWrapper |
| 44 | + extends CommandWrapper<LinstorBackupSnapshotCommand, CopyCmdAnswer, LibvirtComputingResource> |
| 45 | +{ |
| 46 | + private static final Logger s_logger = Logger.getLogger(LinstorBackupSnapshotCommandWrapper.class); |
| 47 | + |
| 48 | + private String zfsSnapdev(boolean hide, String zfsUrl) { |
| 49 | + Script script = new Script("/usr/bin/zfs", Duration.millis(5000)); |
| 50 | + script.add("set"); |
| 51 | + script.add("snapdev=" + (hide ? "hidden" : "visible")); |
| 52 | + script.add(zfsUrl.substring(6)); // cutting zfs:// |
| 53 | + return script.execute(); |
| 54 | + } |
| 55 | + |
| 56 | + private String qemuShrink(String path, long sizeByte, long timeout) { |
| 57 | + Script qemuImg = new Script("qemu-img", Duration.millis(timeout)); |
| 58 | + qemuImg.add("resize"); |
| 59 | + qemuImg.add("--shrink"); |
| 60 | + qemuImg.add(path); |
| 61 | + qemuImg.add("" + sizeByte); |
| 62 | + return qemuImg.execute(); |
| 63 | + } |
| 64 | + |
| 65 | + static void cleanupSecondaryPool(final KVMStoragePool secondaryPool) { |
| 66 | + if (secondaryPool != null) { |
| 67 | + try { |
| 68 | + secondaryPool.delete(); |
| 69 | + } catch (final Exception e) { |
| 70 | + s_logger.debug("Failed to delete secondary storage", e); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private String convertImageToQCow2( |
| 76 | + final String srcPath, |
| 77 | + final SnapshotObjectTO dst, |
| 78 | + final KVMStoragePool secondaryPool, |
| 79 | + int waitMilliSeconds |
| 80 | + ) |
| 81 | + throws LibvirtException, QemuImgException, IOException |
| 82 | + { |
| 83 | + final String dstDir = secondaryPool.getLocalPath() + File.separator + dst.getPath(); |
| 84 | + FileUtils.forceMkdir(new File(dstDir)); |
| 85 | + |
| 86 | + final String dstPath = dstDir + File.separator + dst.getName(); |
| 87 | + final QemuImgFile srcFile = new QemuImgFile(srcPath, QemuImg.PhysicalDiskFormat.RAW); |
| 88 | + final QemuImgFile dstFile = new QemuImgFile(dstPath, QemuImg.PhysicalDiskFormat.QCOW2); |
| 89 | + |
| 90 | + // NOTE: the qemu img will also contain the drbd metadata at the end |
| 91 | + final QemuImg qemu = new QemuImg(waitMilliSeconds); |
| 92 | + qemu.convert(srcFile, dstFile); |
| 93 | + s_logger.info("Backup snapshot " + srcFile + " to " + dstPath); |
| 94 | + return dstPath; |
| 95 | + } |
| 96 | + |
| 97 | + private SnapshotObjectTO setCorrectSnapshotSize(final SnapshotObjectTO dst, final String dstPath) { |
| 98 | + final File snapFile = new File(dstPath); |
| 99 | + final long size = snapFile.exists() ? snapFile.length() : 0; |
| 100 | + |
| 101 | + final SnapshotObjectTO snapshot = new SnapshotObjectTO(); |
| 102 | + snapshot.setPath(dst.getPath() + File.separator + dst.getName()); |
| 103 | + snapshot.setPhysicalSize(size); |
| 104 | + return snapshot; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public CopyCmdAnswer execute(LinstorBackupSnapshotCommand cmd, LibvirtComputingResource serverResource) |
| 109 | + { |
| 110 | + s_logger.debug("LinstorBackupSnapshotCommandWrapper: " + cmd.getSrcTO().getPath() + " -> " + cmd.getDestTO().getPath()); |
| 111 | + final SnapshotObjectTO src = (SnapshotObjectTO) cmd.getSrcTO(); |
| 112 | + final SnapshotObjectTO dst = (SnapshotObjectTO) cmd.getDestTO(); |
| 113 | + KVMStoragePool secondaryPool = null; |
| 114 | + final KVMStoragePoolManager storagePoolMgr = serverResource.getStoragePoolMgr(); |
| 115 | + KVMStoragePool linstorPool = storagePoolMgr.getStoragePool(Storage.StoragePoolType.Linstor, src.getDataStore().getUuid()); |
| 116 | + boolean zfsHidden = false; |
| 117 | + String srcPath = src.getPath(); |
| 118 | + |
| 119 | + if (linstorPool == null) { |
| 120 | + return new CopyCmdAnswer("Unable to get linstor storage pool from destination volume."); |
| 121 | + } |
| 122 | + |
| 123 | + final DataStoreTO dstDataStore = dst.getDataStore(); |
| 124 | + if (!(dstDataStore instanceof NfsTO)) { |
| 125 | + return new CopyCmdAnswer("Backup Linstor snapshot: Only NFS secondary supported at present!"); |
| 126 | + } |
| 127 | + |
| 128 | + try |
| 129 | + { |
| 130 | + // provide the linstor snapshot block device |
| 131 | + // on lvm thin this should already be there in /dev/mapper/vg-snapshotname |
| 132 | + // on zfs we need to unhide the snapshot block device |
| 133 | + s_logger.info("Src: " + srcPath + " | " + src.getName()); |
| 134 | + if (srcPath.startsWith("zfs://")) { |
| 135 | + zfsHidden = true; |
| 136 | + if (zfsSnapdev(false, srcPath) != null) { |
| 137 | + return new CopyCmdAnswer("Unable to unhide zfs snapshot device."); |
| 138 | + } |
| 139 | + srcPath = "/dev/" + srcPath.substring(6); |
| 140 | + } |
| 141 | + |
| 142 | + secondaryPool = storagePoolMgr.getStoragePoolByURI(dstDataStore.getUrl()); |
| 143 | + |
| 144 | + String dstPath = convertImageToQCow2(srcPath, dst, secondaryPool, cmd.getWaitInMillSeconds()); |
| 145 | + |
| 146 | + // resize to real volume size, cutting of drbd metadata |
| 147 | + String result = qemuShrink(dstPath, src.getVolume().getSize(), cmd.getWaitInMillSeconds()); |
| 148 | + if (result != null) { |
| 149 | + return new CopyCmdAnswer("qemu-img shrink failed: " + result); |
| 150 | + } |
| 151 | + s_logger.info("Backup shrunk " + dstPath + " to actual size " + src.getVolume().getSize()); |
| 152 | + |
| 153 | + SnapshotObjectTO snapshot = setCorrectSnapshotSize(dst, dstPath); |
| 154 | + return new CopyCmdAnswer(snapshot); |
| 155 | + } catch (final Exception e) { |
| 156 | + final String error = String.format("Failed to backup snapshot with id [%s] with a pool %s, due to %s", |
| 157 | + cmd.getSrcTO().getId(), cmd.getSrcTO().getDataStore().getUuid(), e.getMessage()); |
| 158 | + s_logger.error(error); |
| 159 | + return new CopyCmdAnswer(cmd, e); |
| 160 | + } finally { |
| 161 | + cleanupSecondaryPool(secondaryPool); |
| 162 | + if (zfsHidden) { |
| 163 | + zfsSnapdev(true, src.getPath()); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments