@@ -16,6 +16,10 @@ open Network_interface
16
16
17
17
let name = " networkd_db"
18
18
19
+ type error = Skip | Msg of string
20
+
21
+ let ( let * ) = Result. bind
22
+
19
23
let _ =
20
24
let bridge = ref " " in
21
25
let iface = ref " " in
@@ -31,58 +35,61 @@ let _ =
31
35
(Printf. sprintf " Usage: %s [-bridge <bridge> | -iface <interface>]" name) ;
32
36
try
33
37
let config = Network_config. read_config () in
34
- ( if ! bridge <> " " then
35
- match List. assoc_opt ! bridge config.bridge_config with
36
- | Some bridge_config -> (
37
- let ifaces =
38
- List. concat_map
39
- (fun (_ , port ) -> port.interfaces)
40
- bridge_config.ports
41
- in
42
- let macs =
43
- let to_mac ~order name =
44
- match List. find_opt (fun dev -> dev.name = name) order with
45
- | Some dev ->
46
- Either. Left (Macaddr. to_string dev.mac)
47
- | None ->
48
- Either. Right name
49
- in
50
- match (config.interface_order, ifaces) with
51
- | Some order , _ :: _ ->
52
- let oks, errs = List. partition_map (to_mac ~order ) ifaces in
53
- if errs = [] then
54
- Ok oks
55
- else
56
- Error
57
- (Printf. sprintf " Could not find MAC address(es) for %s"
58
- (String. concat " , " errs)
59
- )
60
- | _ , [] ->
61
- (* No ifaces, no hwaddrs. *)
62
- Ok []
63
- | None , _ :: _ ->
64
- (* Fallback to use the bridge MAC address when the interface_order
65
- is not available. This can work only because the host installer
66
- requires only one network interface to setup its own networking so far. *)
67
- Ok (Option. to_list bridge_config.bridge_mac)
68
- in
69
- match macs with
70
- | Error msg ->
71
- rc := 1 ;
72
- Printf. fprintf stderr " %s\n " msg
73
- | Ok macs -> (
74
- Printf. printf " interfaces=%s\n " (String. concat " ," ifaces) ;
75
- Printf. printf " hwaddrs=%s\n " (String. concat " ," macs) ;
76
- match bridge_config.vlan with
77
- | None ->
78
- ()
79
- | Some (parent , id ) ->
80
- Printf. printf " vlan=%d\n parent=%s\n " id parent
81
- )
82
- )
38
+ let r =
39
+ let * bridge = if ! bridge = " " then Error Skip else Ok ! bridge in
40
+ let * bridge_config =
41
+ match List. assoc_opt bridge config.bridge_config with
42
+ | Some bridge_config ->
43
+ Ok bridge_config
83
44
| None ->
84
- rc := 1 ;
85
- Printf. fprintf stderr " Could not find bridge %s\n " ! bridge
45
+ Error (Msg (Printf. sprintf " Could not find bridge %s\n " bridge))
46
+ in
47
+ let ifaces =
48
+ List. concat_map (fun (_ , port ) -> port.interfaces) bridge_config.ports
49
+ in
50
+ let * macs =
51
+ let to_mac ~order name =
52
+ match List. find_opt (fun dev -> dev.name = name) order with
53
+ | Some dev ->
54
+ Either. Left (Macaddr. to_string dev.mac)
55
+ | None ->
56
+ Either. Right name
57
+ in
58
+ match (config.interface_order, ifaces) with
59
+ | Some order , _ :: _ ->
60
+ let oks, errs = List. partition_map (to_mac ~order ) ifaces in
61
+ if errs = [] then
62
+ Ok oks
63
+ else
64
+ Error
65
+ (Msg
66
+ (Printf. sprintf " Could not find MAC address(es) for %s"
67
+ (String. concat " , " errs)
68
+ )
69
+ )
70
+ | _ , [] ->
71
+ (* No ifaces, no hwaddrs. *)
72
+ Ok []
73
+ | None , _ :: _ ->
74
+ (* Fallback to use the bridge MAC address when the interface_order
75
+ is not available. This can work only because the host installer
76
+ requires only one network interface to setup its own networking so far. *)
77
+ Ok (Option. to_list bridge_config.bridge_mac)
78
+ in
79
+ Printf. printf " interfaces=%s\n " (String. concat " ," ifaces) ;
80
+ Printf. printf " hwaddrs=%s\n " (String. concat " ," macs) ;
81
+ match bridge_config.vlan with
82
+ | None ->
83
+ Ok ()
84
+ | Some (parent , id ) ->
85
+ Ok (Printf. printf " vlan=%d\n parent=%s\n " id parent)
86
+ in
87
+ ( match r with
88
+ | Ok () | Error Skip ->
89
+ ()
90
+ | Error (Msg msg ) ->
91
+ rc := 1 ;
92
+ Printf. fprintf stderr " %s" msg
86
93
) ;
87
94
if ! iface <> " " then
88
95
if List. mem_assoc ! iface config.interface_config then
0 commit comments