11package qemu
22
33import (
4- "bufio"
54 "bytes"
65 "errors"
76 "fmt"
@@ -53,18 +52,21 @@ func (c *MachineConfig) Exec(cmd string, root bool) (string, error) {
5352 if c .VMNet {
5453 if ip == "localhost" || ip == "" {
5554 log .Println ("getting instance IP address from DHCP leases" )
56-
5755 for {
58- ip = c .GetIPAddressByMac ()
56+ dhcpLeasesContent , err := os .ReadFile ("/var/db/dhcpd_leases" )
57+ if err != nil {
58+ return "" , err
59+ }
60+ lip , _ := c .GetIPAddressByMac (dhcpLeasesContent )
5961 //ip = c.GetIPAddressFromMachine()
60- if ip != "" {
62+ if lip != "" {
63+ c .MachineIP = lip
6164 break
6265 }
6366 fmt .Print ("." )
6467 time .Sleep (4 * time .Second )
6568 }
6669
67- c .MachineIP = ip
6870 config , err := yaml .Marshal (& c )
6971
7072 if err != nil {
@@ -83,7 +85,7 @@ func (c *MachineConfig) Exec(cmd string, root bool) (string, error) {
8385
8486 }
8587
86- host := ip + ":" + c .SSHPort
88+ host := c . MachineIP + ":" + c .SSHPort
8789 user := c .SSHUser
8890 pwd := c .SSHPassword
8991
@@ -130,7 +132,6 @@ func (c *MachineConfig) Exec(cmd string, root bool) (string, error) {
130132
131133 for i := 0 ; i < 11 ; i ++ {
132134 conn , err = ssh .Dial ("tcp" , host , conf )
133-
134135 if err == nil {
135136 break
136137 }
@@ -140,8 +141,8 @@ func (c *MachineConfig) Exec(cmd string, root bool) (string, error) {
140141
141142 fmt .Print ("." )
142143 time .Sleep (4 * time .Second )
143-
144144 }
145+
145146 defer conn .Close ()
146147
147148 session , err := conn .NewSession ()
@@ -174,9 +175,7 @@ func (c *MachineConfig) Exec(cmd string, root bool) (string, error) {
174175 if i == 4 {
175176 return "" , err
176177 }
177-
178178 }
179-
180179 }
181180
182181 output := stdoutBuf .String ()
@@ -260,6 +259,7 @@ func (c *MachineConfig) Status() (string, int) {
260259 status = "Paused"
261260 }
262261 }
262+
263263 return status , pid
264264}
265265
@@ -581,43 +581,16 @@ func (c *MachineConfig) Start() error {
581581
582582// AssignIP obtains machine IP address from bootpd.plist. Only applicale to machines created on
583583// VMNet
584- func (c * MachineConfig ) GetIPAddressByMac () string {
585- ip := ""
586- mac := ""
587-
588- file , err := os .Open ("/var/db/dhcpd_leases" )
589-
590- if err != nil {
591- log .Fatalf ("Error opening file: %v" , err )
592- }
584+ func (c * MachineConfig ) GetIPAddressByMac (dhcpLeasesContent []byte ) (string , error ) {
585+ result := utils .ParseDhcpLeasesFile (string (dhcpLeasesContent ))
586+ dhcpData := utils .ConvertStringArrayToDhcpDataArray (result )
587+ dhcpConfig := utils .MatchHwAddress (dhcpData , c .MACAddress )
593588
594- scanner := bufio .NewScanner (file )
595- for scanner .Scan () {
596-
597- // Process each line in the dhcpd_leases file
598- line := scanner .Text ()
599-
600- // Check if the line contains an IP address
601- if strings .Contains (line , "ip_address" ) {
602- ip = strings .Split (strings .Fields (line )[0 ], "ip_address=" )[1 ]
603- }
604-
605- // Check if the line contains a MAC address
606- if strings .Contains (line , "hw_address" ) {
607- mac = strings .Split (strings .Fields (line )[0 ], "hw_address=1," )[1 ]
608- }
609-
610- if mac == c .MACAddress {
611- return ip
612- }
613-
614- }
615-
616- // Check for any errors during scanning
617- if err := scanner .Err (); err != nil {
618- fmt .Println ("error scanning /var/db/dhcpd_lease:" , err )
589+ if dhcpConfig != nil {
590+ return dhcpConfig .IpAddress , nil
591+ } else {
592+ return "" , errors .New ("no machine dhcp configuration found" )
619593 }
620- return ""
621594}
622595
623596// Launch macpine downloads a fresh image and creates a VM directory
@@ -697,6 +670,9 @@ func (c *MachineConfig) Launch() error {
697670 return errors .New ("unable to launch a new machine. " + err .Error ())
698671 }
699672
673+ log .Println ("waiting for machine..." )
674+ time .Sleep (10 * time .Second )
675+
700676 // Make sure DNS is set up correctly
701677 _ , err = c .Exec ("echo 'nameserver 8.8.8.8' > /etc/resolv.conf" , true )
702678 if err != nil {
0 commit comments