|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +package org.apache.cloudstack.storage; |
| 19 | + |
| 20 | +import java.io.BufferedReader; |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.net.URI; |
| 24 | +import java.net.URISyntaxException; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Paths; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Random; |
| 30 | +import java.util.concurrent.ConcurrentHashMap; |
| 31 | +import java.util.concurrent.ConcurrentMap; |
| 32 | + |
| 33 | +import javax.annotation.PreDestroy; |
| 34 | + |
| 35 | +import com.cloud.storage.StorageLayer; |
| 36 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 37 | +import com.cloud.utils.script.OutputInterpreter; |
| 38 | +import com.cloud.utils.script.Script; |
| 39 | +import org.apache.cloudstack.framework.config.ConfigKey; |
| 40 | +import org.apache.cloudstack.utils.identity.ManagementServerNode; |
| 41 | +import org.apache.log4j.Logger; |
| 42 | +import org.springframework.stereotype.Component; |
| 43 | + |
| 44 | +@Component |
| 45 | +public class NfsMountManagerImpl implements NfsMountManager { |
| 46 | + private static final Logger s_logger = Logger.getLogger(NfsMountManager.class); |
| 47 | + |
| 48 | + private StorageLayer storage; |
| 49 | + private int timeout; |
| 50 | + private final Random rand = new Random(System.currentTimeMillis()); |
| 51 | + private final ConcurrentMap<String, String> storageMounts = new ConcurrentHashMap<>(); |
| 52 | + |
| 53 | + public static final ConfigKey<String> MOUNT_PARENT = new ConfigKey<>("Advanced", String.class, |
| 54 | + "mount.parent", "/var/cloudstack/mnt", |
| 55 | + "The mount point on the Management Server for Secondary Storage.", |
| 56 | + true, ConfigKey.Scope.Global); |
| 57 | + |
| 58 | + public NfsMountManagerImpl(StorageLayer storage, int timeout) { |
| 59 | + this.storage = storage; |
| 60 | + this.timeout = timeout; |
| 61 | + } |
| 62 | + |
| 63 | + public String getMountPoint(String storageUrl, Integer nfsVersion) { |
| 64 | + String mountPoint = storageMounts.get(storageUrl); |
| 65 | + if (mountPoint != null) { |
| 66 | + return mountPoint; |
| 67 | + } |
| 68 | + |
| 69 | + URI uri; |
| 70 | + try { |
| 71 | + uri = new URI(storageUrl); |
| 72 | + } catch (URISyntaxException e) { |
| 73 | + s_logger.error("Invalid storage URL format ", e); |
| 74 | + throw new CloudRuntimeException("Unable to create mount point due to invalid storage URL format " + storageUrl); |
| 75 | + } |
| 76 | + |
| 77 | + mountPoint = mount(uri.getHost() + ":" + uri.getPath(), MOUNT_PARENT.value(), nfsVersion); |
| 78 | + if (mountPoint == null) { |
| 79 | + s_logger.error("Unable to create mount point for " + storageUrl); |
| 80 | + throw new CloudRuntimeException("Unable to create mount point for " + storageUrl); |
| 81 | + } |
| 82 | + |
| 83 | + storageMounts.putIfAbsent(storageUrl, mountPoint); |
| 84 | + return mountPoint; |
| 85 | + } |
| 86 | + |
| 87 | + private String mount(String path, String parent, Integer nfsVersion) { |
| 88 | + String mountPoint = setupMountPoint(parent); |
| 89 | + if (mountPoint == null) { |
| 90 | + s_logger.warn("Unable to create a mount point"); |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + Script command = new Script(true, "mount", timeout, s_logger); |
| 95 | + command.add("-t", "nfs"); |
| 96 | + if (nfsVersion != null){ |
| 97 | + command.add("-o", "vers=" + nfsVersion); |
| 98 | + } |
| 99 | + // command.add("-o", "soft,timeo=133,retrans=2147483647,tcp,acdirmax=0,acdirmin=0"); |
| 100 | + if ("Mac OS X".equalsIgnoreCase(System.getProperty("os.name"))) { |
| 101 | + command.add("-o", "resvport"); |
| 102 | + } |
| 103 | + command.add(path); |
| 104 | + command.add(mountPoint); |
| 105 | + String result = command.execute(); |
| 106 | + if (result != null) { |
| 107 | + s_logger.warn("Unable to mount " + path + " due to " + result); |
| 108 | + deleteMountPath(mountPoint); |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + // Change permissions for the mountpoint |
| 113 | + Script script = new Script(true, "chmod", timeout, s_logger); |
| 114 | + script.add("1777", mountPoint); |
| 115 | + result = script.execute(); |
| 116 | + if (result != null) { |
| 117 | + s_logger.warn("Unable to set permissions for " + mountPoint + " due to " + result); |
| 118 | + } |
| 119 | + return mountPoint; |
| 120 | + } |
| 121 | + |
| 122 | + private String setupMountPoint(String parent) { |
| 123 | + String mountPoint = null; |
| 124 | + for (int i = 0; i < 10; i++) { |
| 125 | + String mntPt = parent + File.separator + String.valueOf(ManagementServerNode.getManagementServerId()) + "." + Integer.toHexString(rand.nextInt(Integer.MAX_VALUE)); |
| 126 | + File file = new File(mntPt); |
| 127 | + if (!file.exists()) { |
| 128 | + if (storage.mkdir(mntPt)) { |
| 129 | + mountPoint = mntPt; |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + s_logger.error("Unable to create mount: " + mntPt); |
| 134 | + } |
| 135 | + |
| 136 | + return mountPoint; |
| 137 | + } |
| 138 | + |
| 139 | + private void umount(String localRootPath) { |
| 140 | + if (!mountExists(localRootPath)) { |
| 141 | + return; |
| 142 | + } |
| 143 | + Script command = new Script(true, "umount", timeout, s_logger); |
| 144 | + command.add(localRootPath); |
| 145 | + String result = command.execute(); |
| 146 | + if (result != null) { |
| 147 | + // Fedora Core 12 errors out with any -o option executed from java |
| 148 | + String errMsg = "Unable to umount " + localRootPath + " due to " + result; |
| 149 | + s_logger.error(errMsg); |
| 150 | + throw new CloudRuntimeException(errMsg); |
| 151 | + } |
| 152 | + deleteMountPath(localRootPath); |
| 153 | + s_logger.debug("Successfully umounted " + localRootPath); |
| 154 | + } |
| 155 | + |
| 156 | + private void deleteMountPath(String localRootPath) { |
| 157 | + try { |
| 158 | + Files.deleteIfExists(Paths.get(localRootPath)); |
| 159 | + } catch (IOException e) { |
| 160 | + s_logger.warn(String.format("unable to delete mount directory %s:%s.%n", localRootPath, e.getMessage())); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private boolean mountExists(String localRootPath) { |
| 165 | + Script script = new Script(true, "mount", timeout, s_logger); |
| 166 | + ZfsPathParser parser = new ZfsPathParser(localRootPath); |
| 167 | + script.execute(parser); |
| 168 | + return parser.getPaths().stream().filter(s -> s.contains(localRootPath)).findAny().map(s -> true).orElse(false); |
| 169 | + } |
| 170 | + |
| 171 | + public static class ZfsPathParser extends OutputInterpreter { |
| 172 | + String _parent; |
| 173 | + List<String> paths = new ArrayList<>(); |
| 174 | + |
| 175 | + public ZfsPathParser(String parent) { |
| 176 | + _parent = parent; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public String interpret(BufferedReader reader) throws IOException { |
| 181 | + String line; |
| 182 | + while ((line = reader.readLine()) != null) { |
| 183 | + paths.add(line); |
| 184 | + } |
| 185 | + return null; |
| 186 | + } |
| 187 | + |
| 188 | + public List<String> getPaths() { |
| 189 | + return paths; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public boolean drain() { |
| 194 | + return true; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @PreDestroy |
| 199 | + public void destroy() { |
| 200 | + s_logger.info("Clean up mounted NFS mount points used in current session."); |
| 201 | + storageMounts.values().stream().forEach(this::umount); |
| 202 | + } |
| 203 | +} |
0 commit comments