Skip to content

Commit 68e504a

Browse files
authored
Linstor backup snaphots (#8067)
This PR adds an config option for the Linstor primary storage driver, that allows you to automatically backup volume snapshots to the secondary storage. Additionally it will not mangle the need java-linstor dependency into the client.jar, but instead just copy the java-linstor.jar into lib. Config option is called: lin.backup.snapshots and is default false The scope of this change should be limited, as it only touches the Linstor driver and a part of copyAsync was implemented with 2 new Linstor specific commands.
1 parent d3917ef commit 68e504a

File tree

13 files changed

+791
-39
lines changed

13 files changed

+791
-39
lines changed

client/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,12 @@
837837
<overWrite>false</overWrite>
838838
<outputDirectory>${project.build.directory}/lib</outputDirectory>
839839
</artifactItem>
840+
<artifactItem>
841+
<groupId>com.linbit.linstor.api</groupId>
842+
<artifactId>java-linstor</artifactId>
843+
<overWrite>false</overWrite>
844+
<outputDirectory>${project.build.directory}/lib</outputDirectory>
845+
</artifactItem>
840846
<artifactItem>
841847
<groupId>org.bouncycastle</groupId>
842848
<artifactId>bctls-jdk15on</artifactId>
@@ -880,6 +886,7 @@
880886
<exclude>mysql:mysql-connector-java</exclude>
881887
<exclude>org.apache.cloudstack:cloud-plugin-storage-volume-storpool</exclude>
882888
<exclude>org.apache.cloudstack:cloud-plugin-storage-volume-linstor</exclude>
889+
<exclude>com.linbit.linstor.api:java-linstor</exclude>
883890
</excludes>
884891
</artifactSet>
885892
<transformers>

plugins/storage/volume/linstor/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<artifactId>cloud-plugin-hypervisor-kvm</artifactId>
4444
<version>${project.version}</version>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.apache.cloudstack</groupId>
48+
<artifactId>cloud-engine-storage-snapshot</artifactId>
49+
<version>${project.version}</version>
50+
<scope>compile</scope>
51+
</dependency>
4652
</dependencies>
4753
<build>
4854
<plugins>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.api.storage;
18+
19+
import com.cloud.agent.api.to.DataTO;
20+
import org.apache.cloudstack.storage.command.CopyCommand;
21+
22+
public class LinstorBackupSnapshotCommand extends CopyCommand
23+
{
24+
public LinstorBackupSnapshotCommand(DataTO srcData, DataTO destData, int timeout, boolean executeInSequence)
25+
{
26+
super(srcData, destData, timeout, executeInSequence);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.api.storage;
18+
19+
import com.cloud.agent.api.to.DataTO;
20+
import org.apache.cloudstack.storage.command.CopyCommand;
21+
22+
public class LinstorRevertBackupSnapshotCommand extends CopyCommand
23+
{
24+
public LinstorRevertBackupSnapshotCommand(DataTO srcData, DataTO destData, int timeout, boolean executeInSequence)
25+
{
26+
super(srcData, destData, timeout, executeInSequence);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
21+
import com.cloud.agent.api.to.DataStoreTO;
22+
import com.cloud.api.storage.LinstorRevertBackupSnapshotCommand;
23+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
24+
import com.cloud.hypervisor.kvm.storage.KVMStoragePool;
25+
import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
26+
import com.cloud.resource.CommandWrapper;
27+
import com.cloud.resource.ResourceWrapper;
28+
import com.cloud.storage.Storage;
29+
import org.apache.cloudstack.storage.command.CopyCmdAnswer;
30+
import org.apache.cloudstack.storage.to.SnapshotObjectTO;
31+
import org.apache.cloudstack.storage.to.VolumeObjectTO;
32+
import org.apache.cloudstack.utils.qemu.QemuImg;
33+
import org.apache.cloudstack.utils.qemu.QemuImgException;
34+
import org.apache.cloudstack.utils.qemu.QemuImgFile;
35+
import org.apache.log4j.Logger;
36+
import org.libvirt.LibvirtException;
37+
38+
@ResourceWrapper(handles = LinstorRevertBackupSnapshotCommand.class)
39+
public final class LinstorRevertBackupSnapshotCommandWrapper
40+
extends CommandWrapper<LinstorRevertBackupSnapshotCommand, CopyCmdAnswer, LibvirtComputingResource>
41+
{
42+
private static final Logger s_logger = Logger.getLogger(LinstorRevertBackupSnapshotCommandWrapper.class);
43+
44+
private void convertQCow2ToRAW(final String srcPath, final String dstPath, int waitMilliSeconds)
45+
throws LibvirtException, QemuImgException
46+
{
47+
final QemuImgFile srcQemuFile = new QemuImgFile(
48+
srcPath, QemuImg.PhysicalDiskFormat.QCOW2);
49+
final QemuImg qemu = new QemuImg(waitMilliSeconds);
50+
final QemuImgFile dstFile = new QemuImgFile(dstPath, QemuImg.PhysicalDiskFormat.RAW);
51+
qemu.convert(srcQemuFile, dstFile);
52+
}
53+
54+
@Override
55+
public CopyCmdAnswer execute(LinstorRevertBackupSnapshotCommand cmd, LibvirtComputingResource serverResource)
56+
{
57+
s_logger.debug("LinstorRevertBackupSnapshotCommandWrapper: " + cmd.getSrcTO().getPath() + " -> " + cmd.getDestTO().getPath());
58+
final SnapshotObjectTO src = (SnapshotObjectTO) cmd.getSrcTO();
59+
final VolumeObjectTO dst = (VolumeObjectTO) cmd.getDestTO();
60+
KVMStoragePool secondaryPool = null;
61+
final KVMStoragePoolManager storagePoolMgr = serverResource.getStoragePoolMgr();
62+
KVMStoragePool linstorPool = storagePoolMgr.getStoragePool(Storage.StoragePoolType.Linstor, dst.getDataStore().getUuid());
63+
64+
if (linstorPool == null) {
65+
return new CopyCmdAnswer("Unable to get linstor storage pool from destination volume.");
66+
}
67+
68+
try
69+
{
70+
final DataStoreTO srcDataStore = src.getDataStore();
71+
File srcFile = new File(src.getPath());
72+
secondaryPool = storagePoolMgr.getStoragePoolByURI(
73+
srcDataStore.getUrl() + File.separator + srcFile.getParent());
74+
75+
convertQCow2ToRAW(
76+
secondaryPool.getLocalPath() + File.separator + srcFile.getName(),
77+
linstorPool.getPhysicalDisk(dst.getPath()).getPath(),
78+
cmd.getWaitInMillSeconds());
79+
80+
final VolumeObjectTO dstVolume = new VolumeObjectTO();
81+
dstVolume.setPath(dst.getPath());
82+
return new CopyCmdAnswer(dstVolume);
83+
} catch (final Exception e) {
84+
final String error = String.format("Failed to revert snapshot with id [%s] with a pool %s, due to %s",
85+
cmd.getSrcTO().getId(), cmd.getSrcTO().getDataStore().getUuid(), e.getMessage());
86+
s_logger.error(error);
87+
return new CopyCmdAnswer(cmd, e);
88+
} finally {
89+
LinstorBackupSnapshotCommandWrapper.cleanupSecondaryPool(secondaryPool);
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)