11(*
2- * Copyright (C) 2006-2009 Citrix Systems Inc.
2+ * Copyright (C) 2006-2010 Citrix Systems Inc.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU Lesser General Public License as published
@@ -20,10 +20,33 @@ open Threadext
2020module D = Debug. Debugger (struct let name= " xapi" end )
2121open D
2222
23+ let inventory_filename = Util_globs_inventory. inventory_filename
24+
25+ (* Keys which must exist: *)
26+ let _installation_uuid = " INSTALLATION_UUID"
27+ let _control_domain_uuid = " CONTROL_DOMAIN_UUID"
28+ let _management_interface = " MANAGEMENT_INTERFACE"
29+
30+ (* Optional keys: *)
31+ let _current_interfaces = " CURRENT_INTERFACES"
32+ let _oem_manufacturer = " OEM_MANUFACTURER"
33+ let _oem_model = " OEM_MODEL"
34+ let _oem_build_number = " OEM_BUILD_NUMBER"
35+ let _machine_serial_number = " MACHINE_SERIAL_NUMBER"
36+ let _machine_serial_name = " MACHINE_SERIAL_NAME"
37+
2338let loaded_inventory = ref false
2439let inventory = Hashtbl. create 10
2540let inventory_m = Mutex. create ()
2641
42+ (* Compute the minimum necessary inventory file contents *)
43+ let minimum_default_entries () =
44+ let host_uuid = Uuid. string_of_uuid (Uuid. make_uuid () ) in
45+ let dom0_uuid = Uuid. string_of_uuid (Uuid. make_uuid () ) in
46+ [ _installation_uuid, host_uuid;
47+ _control_domain_uuid, dom0_uuid;
48+ _management_interface, " " ]
49+
2750(* trim any quotes off the ends *)
2851let strip_quotes v =
2952 if String. length v > = 2
@@ -39,15 +62,25 @@ let parse_inventory_entry line =
3962 Some (k, strip_quotes ++ String. strip String. isspace $ v)
4063 | _ -> None
4164
65+ let string_of_table h =
66+ let lines = List. fold_left (fun acc (k , v ) ->
67+ Printf. sprintf " %s='%s'\n " k v :: acc) [] h in
68+ String. concat " " lines
69+
4270let read_inventory_contents () =
71+ if not (Sys. file_exists inventory_filename) then begin
72+ warn " %s does not exist: generating a minimal one" inventory_filename;
73+ Unixext. write_string_to_file inventory_filename (
74+ string_of_table (minimum_default_entries () ))
75+ end ;
4376 (* Perhaps we should blank the old inventory before we read the new one?
4477 What is the desired behaviour? *)
4578 Unixext. file_lines_iter (fun line ->
4679 match parse_inventory_entry line with
4780 | Some (k , v ) -> Hashtbl. add inventory k v
4881 | None -> warn
4982 " Failed to parse line from xensource-inventory file: %s" line)
50- Util_globs_inventory. inventory_filename;
83+ inventory_filename;
5184 loaded_inventory := true
5285
5386let read_inventory () = Mutex. execute inventory_m read_inventory_contents
@@ -57,18 +90,19 @@ let reread_inventory () = Mutex.execute inventory_m (fun () ->
5790
5891exception Missing_inventory_key of string
5992
60- let lookup key =
61- if not (! loaded_inventory) then read_inventory() ;
62- if not (Hashtbl. mem inventory key)
63- then raise (Missing_inventory_key key);
64- Hashtbl. find inventory key
93+ let lookup ?default key =
94+ (if not (! loaded_inventory) then read_inventory() );
95+ if (Hashtbl. mem inventory key)
96+ then
97+ Hashtbl. find inventory key
98+ else
99+ match default with
100+ | None -> raise (Missing_inventory_key key)
101+ | Some v -> v
65102
66103let flush_to_disk_locked () =
67- let lines = Hashtbl. fold
68- (fun k v acc -> Printf. sprintf " %s='%s'\n " k v :: acc)
69- inventory [] in
70- Unixext. write_string_to_file Util_globs_inventory. inventory_filename
71- $ String. concat " " lines
104+ let h = Hashtbl. fold (fun k v acc -> (k, v) :: acc) inventory [] in
105+ Unixext. write_string_to_file inventory_filename (string_of_table h)
72106
73107let update key value = Mutex. execute inventory_m (fun () ->
74108 Hashtbl. clear inventory;
@@ -81,25 +115,3 @@ let remove key = Mutex.execute inventory_m (fun () ->
81115 read_inventory_contents () ;
82116 Hashtbl. remove inventory key;
83117 flush_to_disk_locked () )
84-
85- let _product_brand = " PRODUCT_BRAND"
86- let _product_name = " PRODUCT_NAME"
87- let _product_version = " PRODUCT_VERSION='0.5.1'"
88- let _build_number = " BUILD_NUMBER"
89- let _kernel_version = " KERNEL_VERSION"
90- let _xen_version = " XEN_VERSION"
91- let _installation_date = " INSTALLATION_DATE"
92- let _default_sr = " DEFAULT_SR"
93- let _primary_disk = " PRIMARY_DISK"
94- let _backup_partition = " BACKUP_PARTITION"
95- let _installation_uuid = " INSTALLATION_UUID"
96- let _default_sr_physdevs = " DEFAULT_SR_PHYSDEVS"
97- let _control_domain_uuid = " CONTROL_DOMAIN_UUID"
98- let _management_interface = " MANAGEMENT_INTERFACE"
99- let _current_interfaces = " CURRENT_INTERFACES"
100- let _dom0_mem = " DOM0_MEM"
101- let _oem_manufacturer = " OEM_MANUFACTURER"
102- let _oem_model = " OEM_MODEL"
103- let _oem_build_number = " OEM_BUILD_NUMBER"
104- let _machine_serial_number = " MACHINE_SERIAL_NUMBER"
105- let _machine_serial_name = " MACHINE_SERIAL_NAME"
0 commit comments