Skip to content

Commit 106a0d2

Browse files
authored
Merge pull request #2731 from zli/CA-209401
CA-209401: add static-vdis detach feature for HA
2 parents c062888 + 5667bef commit 106a0d2

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

ocaml/xapi/static_vdis.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ let permanent_vdi_detach_by_uuid ~__context ~uuid =
5555
end;
5656
ignore(Helpers.call_script !Xapi_globs.static_vdis [ "del"; uuid ])
5757

58+
let detach_only vdi =
59+
if vdi.currently_attached then begin
60+
info "vdi_detach_by_uuid: vdi-uuid = %s" vdi.uuid;
61+
ignore (Helpers.call_script !Xapi_globs.static_vdis ["detach"; vdi.uuid])
62+
end
63+
5864
(** Added for CA-48539. Deactivates a vdi. You should probably follow
5965
this call with one of the previous vdi_detach functions. *)
6066
let permanent_vdi_deactivate_by_uuid ~__context ~uuid =
@@ -86,6 +92,6 @@ let gc () =
8692
let reattempt_on_boot_attach () =
8793
let script = "attach-static-vdis" in
8894
try
89-
ignore(Helpers.call_script "/sbin/service" [ script; "restart" ])
95+
ignore(Helpers.call_script "/sbin/service" [ script; "start" ])
9096
with e ->
91-
warn "Attempt to reattach static VDIs via '%s restart' failed: %s" script (ExnHelper.string_of_exn e)
97+
warn "Attempt to reattach static VDIs via '%s start' failed: %s" script (ExnHelper.string_of_exn e)

ocaml/xapi/xapi_ha.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ let before_clean_shutdown_or_reboot ~__context ~host =
15541554
then raise (Api_errors.Server_error(Api_errors.ha_lost_statefile, []));
15551555

15561556
(* From this point we will fence ourselves if any unexpected error occurs *)
1557-
try
1557+
begin try
15581558
begin
15591559
try ha_disarm_fencing __context host
15601560
with Xha_error Xha_errno.Mtc_exit_daemon_is_not_present ->
@@ -1584,4 +1584,6 @@ let before_clean_shutdown_or_reboot ~__context ~host =
15841584
Thread.delay 300.;
15851585
info "Still waiting to reboot after %.2f seconds" (Unix.gettimeofday () -. start)
15861586
done
1587+
end;
1588+
List.iter Static_vdis.detach_only (Static_vdis.list())
15871589
end

scripts/init.d-attach-static-vdis

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#! /bin/bash
22
#
3-
# attach-static-vdis Attaches any statically-configured VDIs to dom0
3+
# attach-static-vdis Attaches/detaches any statically-configured VDIs to dom0
44
#
55
# chkconfig: 2345 20 78
6-
# description: attaches any statically-configured VDIs to dom0
6+
# description: attaches/detaches any statically-configured VDIs to dom0
77

88
STATE_DIR=/etc/xensource/static-vdis
99

@@ -32,23 +32,48 @@ clear_stale_state(){
3232
}
3333

3434
attach_all(){
35+
RC=0
3536
ALL=$(ls -1 ${STATE_DIR})
3637

3738
for i in ${ALL}; do
3839
UUID=$(cat ${STATE_DIR}/${i}/vdi-uuid)
3940
logger "Attempting to attach VDI: ${UUID}"
4041
OUTPUT=$(/opt/xensource/bin/static-vdis attach ${UUID} 2>&1)
4142
if [ $? -ne 0 ]; then
43+
RC=1
4244
logger "Attempt to attach VDI: ${UUID} failed -- skipping (Error was: ${OUTPUT})"
4345
fi
4446
done
47+
return $RC
48+
}
49+
50+
detach_all(){
51+
ALL=$(ls -1 ${STATE_DIR})
52+
53+
for i in ${ALL}; do
54+
UUID=$(cat ${STATE_DIR}/${i}/vdi-uuid)
55+
logger "Attempting to detach VDI: ${UUID}"
56+
OUTPUT=$(/opt/xensource/bin/static-vdis detach ${UUID} 2>&1)
57+
if [ $? -ne 0 ]; then
58+
logger "Attempt to detach VDI: ${UUID} failed -- skipping (Error was: ${OUTPUT})"
59+
fi
60+
done
4561
}
4662

4763
start() {
4864
echo -n $"Attempting to attach all statically-configured VDIs"
4965
clear_stale_state
5066
attach_all
67+
RC=$?
5168
echo
69+
return $RC
70+
}
71+
72+
73+
stop() {
74+
echo -n $"Attempting to detach all statically-configured VDIs"
75+
detach_all
76+
echo
5277
return 0
5378
}
5479

@@ -57,6 +82,7 @@ start)
5782
start
5883
;;
5984
stop)
85+
stop
6086
;;
6187
restart)
6288
start

scripts/static-vdis

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ def call_backend_attach(driver, config):
197197
path = xmlrpc[0][0]
198198
return path
199199

200+
def call_backend_detach(driver, config):
201+
params = xmlrpclib.loads(config)[0][0]
202+
params['command'] = 'vdi_detach_from_config'
203+
config = xmlrpclib.dumps(tuple([params]), params['command'])
204+
xml = doexec([ driver, config ])
205+
if xml[0] <> 0:
206+
raise Exception("SM_BACKEND_FAILURE(%d, %s, %s)" % xml)
207+
xmlrpc = xmlrpclib.loads(xml[1])
208+
try:
209+
res = xmlrpc[0][0]['params']
210+
except:
211+
res = xmlrpc[0][0]
212+
return res
213+
200214
def attach(vdi_uuid):
201215
found = False
202216
for existing in list():
@@ -230,13 +244,40 @@ def attach(vdi_uuid):
230244
return d + "/disk"
231245
if not found:
232246
raise Exception("Disk configuration not found")
247+
248+
def detach(vdi_uuid):
249+
found = False
250+
for existing in list():
251+
if existing['vdi-uuid'] == vdi_uuid:
252+
if not (existing.has_key('disk')):
253+
return
254+
found = True
255+
d = main_dir + "/" + existing['id']
256+
if not (os.path.exists(d + "/sr-uri")):
257+
# SMAPIv1
258+
config = read_whole_file(d + "/config")
259+
driver = read_whole_file(d + "/driver")
260+
call_backend_detach(driver, config)
261+
else:
262+
volume_plugin = read_whole_file(d + "/volume-plugin")
263+
vol_key = read_whole_file(d + "/volume-key")
264+
vol_uri = read_whole_file(d + "/volume-uri")
265+
scheme = urlparse.urlparse(vol_uri).scheme
266+
call_datapath_plugin(scheme, "Datapath.deactivate", [ vol_uri, "0" ])
267+
call_datapath_plugin(scheme, "Datapath.detach", [ vol_uri, "0" ])
268+
os.unlink(d + "/disk")
269+
return
270+
if not found:
271+
raise Exception("Disk configuration not found")
272+
233273

234274
def usage():
235275
print "Usage:"
236276
print " %s list -- print a list of VDIs which will be attached on host boot" % sys.argv[0]
237277
print " %s add <uuid> <reason> -- make the VDI <uuid> available on host boot" % sys.argv[0]
238278
print " %s del <uuid> -- cease making the VDI <uuid> available on host boot" % sys.argv[0]
239279
print " %s attach <uuid> -- attach the VDI immediately" % sys.argv[0]
280+
print " %s detach <uuid> -- detach the VDI immediately" % sys.argv[0]
240281
sys.exit(1)
241282

242283
if __name__ == "__main__":
@@ -260,6 +301,8 @@ if __name__ == "__main__":
260301
elif sys.argv[1] == "attach" and len(sys.argv) == 3:
261302
path = attach(sys.argv[2])
262303
print path
304+
elif sys.argv[1] == "detach" and len(sys.argv) == 3:
305+
detach(sys.argv[2])
263306
else:
264307
usage()
265308

0 commit comments

Comments
 (0)