root / lib / hypervisor / hv_chroot.py @ d3ce528b
History | View | Annotate | Download (9.2 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008, 2009 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Chroot manager hypervisor
|
23 |
|
24 |
"""
|
25 |
|
26 |
import os |
27 |
import os.path |
28 |
import time |
29 |
import logging |
30 |
|
31 |
from ganeti import constants |
32 |
from ganeti import errors # pylint: disable-msg=W0611 |
33 |
from ganeti import utils |
34 |
from ganeti.hypervisor import hv_base |
35 |
from ganeti.errors import HypervisorError |
36 |
|
37 |
|
38 |
class ChrootManager(hv_base.BaseHypervisor): |
39 |
"""Chroot manager.
|
40 |
|
41 |
This not-really hypervisor allows ganeti to manage chroots. It has
|
42 |
special behaviour and requirements on the OS definition and the node
|
43 |
environemnt:
|
44 |
- the start and stop of the chroot environment are done via a
|
45 |
script called ganeti-chroot located in the root directory of the
|
46 |
first drive, which should be created by the OS definition
|
47 |
- this script must accept the start and stop argument and, on
|
48 |
shutdown, it should cleanly shutdown the daemons/processes
|
49 |
using the chroot
|
50 |
- the daemons run in chroot should only bind to the instance IP
|
51 |
(to which the OS create script has access via the instance name)
|
52 |
- since some daemons in the node could be listening on the wildcard
|
53 |
address, some ports might be unavailable
|
54 |
- the instance listing will show no memory usage
|
55 |
- on shutdown, the chroot manager will try to find all mountpoints
|
56 |
under the root dir of the instance and unmount them
|
57 |
- instance alive check is based on whether any process is using the chroot
|
58 |
|
59 |
"""
|
60 |
_ROOT_DIR = constants.RUN_GANETI_DIR + "/chroot-hypervisor"
|
61 |
|
62 |
PARAMETERS = { |
63 |
constants.HV_INIT_SCRIPT: (True, utils.IsNormAbsPath,
|
64 |
"must be an absolute normalized path",
|
65 |
None, None), |
66 |
} |
67 |
|
68 |
def __init__(self): |
69 |
hv_base.BaseHypervisor.__init__(self)
|
70 |
utils.EnsureDirs([(self._ROOT_DIR, constants.RUN_DIRS_MODE)])
|
71 |
|
72 |
@staticmethod
|
73 |
def _IsDirLive(path): |
74 |
"""Check if a directory looks like a live chroot.
|
75 |
|
76 |
"""
|
77 |
if not os.path.ismount(path): |
78 |
return False |
79 |
result = utils.RunCmd(["fuser", "-m", path]) |
80 |
return not result.failed |
81 |
|
82 |
@staticmethod
|
83 |
def _GetMountSubdirs(path): |
84 |
"""Return the list of mountpoints under a given path.
|
85 |
|
86 |
This function is Linux-specific.
|
87 |
|
88 |
"""
|
89 |
#TODO(iustin): investigate and document non-linux options
|
90 |
#(e.g. via mount output)
|
91 |
data = [] |
92 |
fh = open("/proc/mounts", "r") |
93 |
try:
|
94 |
for line in fh: |
95 |
_, mountpoint, _ = line.split(" ", 2) |
96 |
if (mountpoint.startswith(path) and |
97 |
mountpoint != path): |
98 |
data.append(mountpoint) |
99 |
finally:
|
100 |
fh.close() |
101 |
data.sort(key=lambda x: x.count("/"), reverse=True) |
102 |
return data
|
103 |
|
104 |
@classmethod
|
105 |
def _InstanceDir(cls, instance_name): |
106 |
"""Return the root directory for an instance.
|
107 |
|
108 |
"""
|
109 |
return utils.PathJoin(cls._ROOT_DIR, instance_name)
|
110 |
|
111 |
def ListInstances(self): |
112 |
"""Get the list of running instances.
|
113 |
|
114 |
"""
|
115 |
return [name for name in os.listdir(self._ROOT_DIR) |
116 |
if self._IsDirLive(utils.PathJoin(self._ROOT_DIR, name))] |
117 |
|
118 |
def GetInstanceInfo(self, instance_name): |
119 |
"""Get instance properties.
|
120 |
|
121 |
@type instance_name: string
|
122 |
@param instance_name: the instance name
|
123 |
|
124 |
@return: (name, id, memory, vcpus, stat, times)
|
125 |
|
126 |
"""
|
127 |
dir_name = self._InstanceDir(instance_name)
|
128 |
if not self._IsDirLive(dir_name): |
129 |
raise HypervisorError("Instance %s is not running" % instance_name) |
130 |
return (instance_name, 0, 0, 0, 0, 0) |
131 |
|
132 |
def GetAllInstancesInfo(self): |
133 |
"""Get properties of all instances.
|
134 |
|
135 |
@return: [(name, id, memory, vcpus, stat, times),...]
|
136 |
|
137 |
"""
|
138 |
data = [] |
139 |
for file_name in os.listdir(self._ROOT_DIR): |
140 |
path = utils.PathJoin(self._ROOT_DIR, file_name)
|
141 |
if self._IsDirLive(path): |
142 |
data.append((file_name, 0, 0, 0, 0, 0)) |
143 |
return data
|
144 |
|
145 |
def StartInstance(self, instance, block_devices): |
146 |
"""Start an instance.
|
147 |
|
148 |
For the chroot manager, we try to mount the block device and
|
149 |
execute '/ganeti-chroot start'.
|
150 |
|
151 |
"""
|
152 |
root_dir = self._InstanceDir(instance.name)
|
153 |
if not os.path.exists(root_dir): |
154 |
try:
|
155 |
os.mkdir(root_dir) |
156 |
except IOError, err: |
157 |
raise HypervisorError("Failed to start instance %s: %s" % |
158 |
(instance.name, err)) |
159 |
if not os.path.isdir(root_dir): |
160 |
raise HypervisorError("Needed path %s is not a directory" % root_dir) |
161 |
|
162 |
if not os.path.ismount(root_dir): |
163 |
if not block_devices: |
164 |
raise HypervisorError("The chroot manager needs at least one disk") |
165 |
|
166 |
sda_dev_path = block_devices[0][1] |
167 |
result = utils.RunCmd(["mount", sda_dev_path, root_dir])
|
168 |
if result.failed:
|
169 |
raise HypervisorError("Can't mount the chroot dir: %s" % result.output) |
170 |
init_script = instance.hvparams[constants.HV_INIT_SCRIPT] |
171 |
result = utils.RunCmd(["chroot", root_dir, init_script, "start"]) |
172 |
if result.failed:
|
173 |
raise HypervisorError("Can't run the chroot start script: %s" % |
174 |
result.output) |
175 |
|
176 |
def StopInstance(self, instance, force=False, retry=False, name=None): |
177 |
"""Stop an instance.
|
178 |
|
179 |
This method has complicated cleanup tests, as we must:
|
180 |
- try to kill all leftover processes
|
181 |
- try to unmount any additional sub-mountpoints
|
182 |
- finally unmount the instance dir
|
183 |
|
184 |
"""
|
185 |
if name is None: |
186 |
name = instance.name |
187 |
|
188 |
root_dir = self._InstanceDir(name)
|
189 |
if not os.path.exists(root_dir) or not self._IsDirLive(root_dir): |
190 |
return
|
191 |
|
192 |
# Run the chroot stop script only once
|
193 |
if not retry and not force: |
194 |
result = utils.RunCmd(["chroot", root_dir, "/ganeti-chroot", "stop"]) |
195 |
if result.failed:
|
196 |
raise HypervisorError("Can't run the chroot stop script: %s" % |
197 |
result.output) |
198 |
|
199 |
if not force: |
200 |
utils.RunCmd(["fuser", "-k", "-TERM", "-m", root_dir]) |
201 |
else:
|
202 |
utils.RunCmd(["fuser", "-k", "-KILL", "-m", root_dir]) |
203 |
# 2 seconds at most should be enough for KILL to take action
|
204 |
time.sleep(2)
|
205 |
|
206 |
if self._IsDirLive(root_dir): |
207 |
if force:
|
208 |
raise HypervisorError("Can't stop the processes using the chroot") |
209 |
return
|
210 |
|
211 |
def CleanupInstance(self, instance_name): |
212 |
"""Cleanup after a stopped instance
|
213 |
|
214 |
"""
|
215 |
root_dir = self._InstanceDir(instance_name)
|
216 |
|
217 |
if not os.path.exists(root_dir): |
218 |
return
|
219 |
|
220 |
if self._IsDirLive(root_dir): |
221 |
raise HypervisorError("Processes are still using the chroot") |
222 |
|
223 |
for mpath in self._GetMountSubdirs(root_dir): |
224 |
utils.RunCmd(["umount", mpath])
|
225 |
|
226 |
result = utils.RunCmd(["umount", root_dir])
|
227 |
if result.failed:
|
228 |
msg = ("Processes still alive in the chroot: %s" %
|
229 |
utils.RunCmd("fuser -vm %s" % root_dir).output)
|
230 |
logging.error(msg) |
231 |
raise HypervisorError("Can't umount the chroot dir: %s (%s)" % |
232 |
(result.output, msg)) |
233 |
|
234 |
def RebootInstance(self, instance): |
235 |
"""Reboot an instance.
|
236 |
|
237 |
This is not (yet) implemented for the chroot manager.
|
238 |
|
239 |
"""
|
240 |
raise HypervisorError("The chroot manager doesn't implement the" |
241 |
" reboot functionality")
|
242 |
|
243 |
def GetNodeInfo(self): |
244 |
"""Return information about the node.
|
245 |
|
246 |
This is just a wrapper over the base GetLinuxNodeInfo method.
|
247 |
|
248 |
@return: a dict with the following keys (values in MiB):
|
249 |
- memory_total: the total memory size on the node
|
250 |
- memory_free: the available memory on the node for instances
|
251 |
- memory_dom0: the memory used by the node itself, if available
|
252 |
|
253 |
"""
|
254 |
return self.GetLinuxNodeInfo() |
255 |
|
256 |
@classmethod
|
257 |
def GetShellCommandForConsole(cls, instance, hvparams, beparams): |
258 |
"""Return a command for connecting to the console of an instance.
|
259 |
|
260 |
"""
|
261 |
root_dir = cls._InstanceDir(instance.name) |
262 |
if not os.path.ismount(root_dir): |
263 |
raise HypervisorError("Instance %s is not running" % instance.name) |
264 |
|
265 |
return "chroot %s" % root_dir |
266 |
|
267 |
def Verify(self): |
268 |
"""Verify the hypervisor.
|
269 |
|
270 |
For the chroot manager, it just checks the existence of the base dir.
|
271 |
|
272 |
"""
|
273 |
if not os.path.exists(self._ROOT_DIR): |
274 |
return "The required directory '%s' does not exist." % self._ROOT_DIR |
275 |
|
276 |
@classmethod
|
277 |
def PowercycleNode(cls): |
278 |
"""Chroot powercycle, just a wrapper over Linux powercycle.
|
279 |
|
280 |
"""
|
281 |
cls.LinuxPowercycle() |
282 |
|
283 |
def MigrateInstance(self, instance, target, live): |
284 |
"""Migrate an instance.
|
285 |
|
286 |
@type instance: L{objects.Instance}
|
287 |
@param instance: the instance to be migrated
|
288 |
@type target: string
|
289 |
@param target: hostname (usually ip) of the target node
|
290 |
@type live: boolean
|
291 |
@param live: whether to do a live or non-live migration
|
292 |
|
293 |
"""
|
294 |
raise HypervisorError("Migration not supported by the chroot hypervisor") |