|
| 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 org.apache.cloudstack.utils.rbd; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Files; |
| 23 | + |
| 24 | +import org.apache.commons.lang3.StringUtils; |
| 25 | +import org.apache.logging.log4j.LogManager; |
| 26 | +import org.apache.logging.log4j.Logger; |
| 27 | + |
| 28 | +import com.cloud.utils.script.Script; |
| 29 | + |
| 30 | +/** |
| 31 | + * Looks after the guests which the RBD method of nasbackup.sh freezes or pauses while it takes its |
| 32 | + * snapshots. The script records that in a state file per VM. If the script is killed, or the agent which |
| 33 | + * ran it restarts, nothing else undoes it: SIGKILL runs no trap. This releases the guests the state |
| 34 | + * files say are still held. |
| 35 | + */ |
| 36 | +public class RbdBackupGuestManager { |
| 37 | + protected static final Logger LOGGER = LogManager.getLogger(RbdBackupGuestManager.class); |
| 38 | + |
| 39 | + // Keep in step with RBD_STATE_DIR of nasbackup.sh. |
| 40 | + public static final String STATE_DIRECTORY = "/run/csbackup"; |
| 41 | + public static final String STATE_FILE_SUFFIX = ".guest"; |
| 42 | + |
| 43 | + private final File stateDirectory; |
| 44 | + |
| 45 | + public RbdBackupGuestManager() { |
| 46 | + this(new File(STATE_DIRECTORY)); |
| 47 | + } |
| 48 | + |
| 49 | + public RbdBackupGuestManager(File stateDirectory) { |
| 50 | + this.stateDirectory = stateDirectory; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * The file nasbackup.sh keeps while its RBD method holds the guest frozen or paused. |
| 55 | + */ |
| 56 | + public File getStateFile(String vmName) { |
| 57 | + return new File(stateDirectory, vmName + STATE_FILE_SUFFIX); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Releases the guest of every state file left in the directory, which the agent does when it |
| 62 | + * starts: the scripts of the agent which went down are gone with it. |
| 63 | + */ |
| 64 | + public void releaseAllGuests() { |
| 65 | + File[] stateFiles = stateDirectory.listFiles((dir, name) -> name.endsWith(STATE_FILE_SUFFIX)); |
| 66 | + if (stateFiles == null) { |
| 67 | + return; |
| 68 | + } |
| 69 | + for (File stateFile : stateFiles) { |
| 70 | + String name = stateFile.getName(); |
| 71 | + String vmName = name.substring(0, name.length() - STATE_FILE_SUFFIX.length()); |
| 72 | + if (!vmName.isEmpty()) { |
| 73 | + releaseGuest(vmName); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Releases the guest, but only if the state file says the script held it, so a VM which an |
| 80 | + * operator paused is left alone. The state file is removed once the guest is released. |
| 81 | + */ |
| 82 | + public void releaseGuest(String vmName) { |
| 83 | + File stateFile = getStateFile(vmName); |
| 84 | + if (!stateFile.isFile()) { |
| 85 | + return; |
| 86 | + } |
| 87 | + String state; |
| 88 | + try { |
| 89 | + state = new String(Files.readAllBytes(stateFile.toPath()), StandardCharsets.UTF_8); |
| 90 | + } catch (IOException e) { |
| 91 | + LOGGER.warn("Unable to read the state file [{}] of the backup of VM {}", stateFile, vmName, e); |
| 92 | + return; |
| 93 | + } |
| 94 | + LOGGER.warn("The RBD backup of VM {} ended while holding the guest ({}), releasing it", vmName, state.trim().replace('\n', ' ')); |
| 95 | + String virsh = Script.getExecutableAbsolutePath("virsh"); |
| 96 | + boolean released = true; |
| 97 | + if (state.contains("suspended")) { |
| 98 | + String domState = Script.executeCommand(virsh, "-c", "qemu:///system", "domstate", vmName); |
| 99 | + if (domState != null && StringUtils.equals(domState.trim(), "paused") |
| 100 | + && Script.executeCommandForExitValue(virsh, "-c", "qemu:///system", "resume", vmName) != 0) { |
| 101 | + LOGGER.warn("Failed to resume VM {} after its backup", vmName); |
| 102 | + released = false; |
| 103 | + } |
| 104 | + } |
| 105 | + if (state.contains("frozen")) { |
| 106 | + // Thawing a guest which is not frozen only makes the agent complain. |
| 107 | + if (Script.executeCommandForExitValue(virsh, "-c", "qemu:///system", "qemu-agent-command", vmName, |
| 108 | + "{\"execute\":\"guest-fsfreeze-thaw\"}") != 0) { |
| 109 | + LOGGER.warn("Failed to thaw the filesystems of VM {} after its backup", vmName); |
| 110 | + } |
| 111 | + } |
| 112 | + if (released && !stateFile.delete()) { |
| 113 | + LOGGER.warn("Failed to delete the state file [{}]", stateFile); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments