2 * Copyright (C) 2014-2015 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 (* Implement 'mclu boot'. *)
21 module C = Libvirt.Connect
22 module D = Libvirt.Domain
23 module MS = Mclu_status
29 let memory = ref 0L (* 0 = choose for me *)
31 try memory := bytes_of_human_size s
33 eprintf "mclu: don't understand --memory parameter '%s'
34 Try something like --memory 1G\n" s;
36 let size = ref 0L (* 0 = default *)
38 try size := bytes_of_human_size s
40 eprintf "mclu: don't understand --size parameter '%s'
41 Try something like --size 20G\n" s;
43 let timezone = ref "" (* "" = no timezone set *)
44 let vcpus = ref 0 (* 0 = choose for me *)
46 let open_console = ref false
47 let open_viewer = ref false
49 let get_arg_speclist () = Arg.align [
50 "--console", Arg.Set open_console, " Open the serial console";
51 "--cpus", Arg.Set_int vcpus, "n Number of virtual CPUs";
52 "--memory", Arg.String set_memory, "nnG Amount of RAM to give guest";
53 "--ram", Arg.String set_memory, "nnG Amount of RAM to give guest";
54 "--size", Arg.String set_size, "nnG Size of disk to give guest";
55 "--timezone", Arg.Set_string timezone, "TZ Set timezone of guest";
56 "--vcpus", Arg.Set_int vcpus, "n Number of virtual CPUs";
57 "--viewer", Arg.Set open_viewer, " Open the graphical console";
60 let boot ~verbose template name =
61 let templates = Template.templates () in
63 (* Does the template exist? *)
64 let template_filename =
65 try List.assoc template templates
67 eprintf "mclu: template %s not found
68 Try `mclu list --templates' to list all known templates.\n" template;
71 (* Probe the template for various features. *)
72 let template_info = Template.probe ~verbose template_filename in
74 (* Decide how much RAM we will give the guest. This affects our
75 * choice of node, so do it early.
77 let memory = !memory in
80 (* User requested, just check it's above the minimum. *)
81 match template_info.Template.minimum_memory with
83 | Some min when min > memory ->
84 eprintf "mclu: minimum memory for this template is %s\n"
89 (* User didn't request any memory setting, use the recommended. *)
90 match template_info.Template.recommended_memory with
91 | Some memory -> memory
92 | None -> 4L *^ 1024L *^ 1024L *^ 1024L (* 4 GB *)
95 (* Check what's running. *)
96 let summary = MS.node_guest_summary ~verbose () in
98 (* Did the user request a specific host? If not, choose one. *)
100 match name_parse name with
101 | Some hostname, name -> hostname, name
103 (* Choose the first host with enough free memory. *)
104 let nodes = List.filter (
105 fun { MS.free_memory = free_memory } -> free_memory >= memory
109 eprintf "mclu: no node with enough free memory found
110 Try: `mclu status' and `mclu on <node>'\n";
114 node.MS.node_status.MS.node.Mclu_conf.hostname in
117 (* Check there isn't a guest with this name running anywhere
118 * in the cluster already.
121 fun ({ MS.active_guests = guests } as node) ->
123 fun { Mclu_list.dom_name = n } ->
126 node.MS.node_status.MS.node.Mclu_conf.hostname
128 eprintf "mclu: there is already a guest called '%s' (running on %s)\n"
135 (* Convert hostname to a specific node, and check it is up. *)
139 node.MS.node_status.MS.node.Mclu_conf.hostname = hostname
142 eprintf "mclu: no node is called '%s'\n" hostname;
144 if not node.MS.node_status.MS.node_on then (
145 eprintf "mclu: node '%s' is switched off
146 Try: `mclu on %s'\n" hostname hostname;
150 (* Where we upload the template and image on remote. *)
151 let format, extension = "qcow2", "qcow2" in
152 let remote_filename = sprintf "/tmp/mclu%s.sh" (string_random8 ()) in
153 let remote_image = sprintf "/var/tmp/%s.%s" name extension in
155 (* Get ready to generate the guest XML. *)
156 let vcpus = !vcpus in
158 if vcpus > 0 then vcpus
159 else min 4 node.MS.node_status.MS.node_info.C.cpus in
161 sprintf "52:54:00:%02x:%02x:%02x"
162 (Random.int 256) (Random.int 256) (Random.int 256) in
164 (* Generate the guest XML. XXX Quoting. *)
168 <memory unit='KiB'>%Ld</memory>
169 <currentMemory unit='KiB'>%Ld</currentMemory>
180 <cpu mode='host-model' fallback='allow' />
182 <timer name='rtc' tickpolicy='catchup'/>
183 <timer name='pit' tickpolicy='delay'/>
184 <timer name='hpet' present='no'/>
186 <on_poweroff>destroy</on_poweroff>
187 <on_reboot>restart</on_reboot>
188 <on_crash>restart</on_crash>
190 " name (memory /^ 1024L) (memory /^ 1024L) vcpus in
192 let xml = xml ^ sprintf "\
193 <disk type='file' device='disk'>
194 <driver name='qemu' type='%s' cache='none' io='native'/>
196 " format remote_image in
198 match template_info.Template.disk_bus with
200 " <target dev='sda' bus='ide'/>\n"
201 | Some "virtio-scsi" | None ->
202 " <target dev='sda' bus='scsi'/>\n"
204 eprintf "mclu: unknown disk-bus: %s\n" bus;
211 if template_info.Template.disk_bus = Some "virtio-scsi" then
212 " <controller type='scsi' index='0' model='virtio-scsi'/>\n"
216 (* XXX Don't hard-code bridge name here. *)
218 match template_info with
219 | { Template.network_model = None } -> "virtio"
220 | { Template.network_model = Some d } -> d in
221 let xml = xml ^ sprintf "\
222 <interface type='bridge'>
224 <source bridge='br0'/>
227 " mac_addr network_model in
234 <target type='serial' port='0'/>
236 <input type='tablet' bus='usb'/>
237 <input type='mouse' bus='ps2'/>
238 <input type='keyboard' bus='ps2'/>
239 <graphics type='vnc' autoport='yes'/>
241 <model type='cirrus' vram='9216' heads='1'/>
246 (* Copy the template to remote and build the guest. *)
248 sprintf "scp %s root@%s:%s"
249 (quote template_filename) (quote hostname) remote_filename in
250 if verbose then printf "%s\n%!" cmd;
251 if Sys.command cmd <> 0 then (
252 eprintf "mclu: scp template to remote failed\n";
256 (* XXX Don't hard-code network_bridge here. *)
257 sprintf "ssh root@%s \
258 LIBGUESTFS_BACKEND_SETTINGS=network_bridge=br0 \
267 (quote template_info.Template.base_image) (* base_image *)
270 (quote remote_image) (* output *)
273 | size -> sprintf "size=%Ldb" size) (* size *)
274 (match !timezone with
276 | tz -> quote "timezone=" ^ tz) (* timezone *)
278 if verbose then printf "%s\n%!" cmd;
279 if Sys.command cmd <> 0 then (
280 eprintf "mclu: remote build failed\n";
284 (* Start the guest. *)
288 let name = node.MS.node_status.MS.node.Mclu_conf.libvirt_uri in
289 C.connect ~name () in
290 let dom = D.create_xml conn xml [] in
291 printf "mclu: %s:%s started\n" hostname (D.get_name dom);
293 with Libvirt.Virterror msg ->
294 eprintf "mclu: %s: %s\n" hostname (Libvirt.Virterror.to_string msg);
297 (* Graphical console? *)
299 Mclu_viewer.viewer ~verbose ~host:hostname (D.get_name dom);
301 (* Serial console? (Interactive, so run it last) *)
302 if !open_console then
303 Mclu_console.console ~verbose ~host:hostname (D.get_name dom)
305 let run ~verbose = function
306 | [ template; name ] ->
307 boot ~verbose template name
309 eprintf "Usage: mclu boot <template> <[host:]name>\n";