How to use Close method of bhyve Package

Best Syzkaller code snippet using bhyve.Close

bhyve.go

Source:bhyve.go Github

copy

Full Screen

...43 sshhost string44 merger *vmimpl.OutputMerger45 vmName string46 bhyve *exec.Cmd47 consolew io.WriteCloser48}49var ipRegex = regexp.MustCompile(`bound to (([0-9]+\.){3}[0-9]+) `)50var tapRegex = regexp.MustCompile(`^tap[0-9]+`)51func ctor(env *vmimpl.Env) (vmimpl.Pool, error) {52 cfg := &Config{53 Count: 1,54 CPU: 1,55 Mem: "512M",56 }57 if err := config.LoadData(env.Config, cfg); err != nil {58 return nil, fmt.Errorf("failed to parse bhyve vm config: %v", err)59 }60 if cfg.Count < 1 || cfg.Count > 128 {61 return nil, fmt.Errorf("invalid config param count: %v, want [1-128]", cfg.Count)62 }63 if env.Debug && cfg.Count > 1 {64 log.Logf(0, "limiting number of VMs from %v to 1 in debug mode", cfg.Count)65 cfg.Count = 166 }67 pool := &Pool{68 cfg: cfg,69 env: env,70 }71 return pool, nil72}73func (pool *Pool) Count() int {74 return pool.cfg.Count75}76func (pool *Pool) Create(workdir string, index int) (vmimpl.Instance, error) {77 inst := &instance{78 cfg: pool.cfg,79 debug: pool.env.Debug,80 os: pool.env.OS,81 sshkey: pool.env.SSHKey,82 sshuser: pool.env.SSHUser,83 vmName: fmt.Sprintf("syzkaller-%v-%v", pool.env.Name, index),84 }85 dataset := inst.cfg.Dataset86 mountpoint, err := osutil.RunCmd(time.Minute, "", "zfs", "get", "-H", "-o", "value", "mountpoint", dataset)87 if err != nil {88 return nil, err89 }90 snapshot := fmt.Sprintf("%v@bhyve-%v", dataset, inst.vmName)91 clone := fmt.Sprintf("%v/bhyve-%v", dataset, inst.vmName)92 prefix := strings.TrimSuffix(string(mountpoint), "\n") + "/"93 image := strings.TrimPrefix(pool.env.Image, prefix)94 if image == pool.env.Image {95 return nil, fmt.Errorf("image file %v not contained in dataset %v", image, prefix)96 }97 inst.image = prefix + fmt.Sprintf("bhyve-%v", inst.vmName) + "/" + image98 // Stop the instance from a previous run in case it's still running.99 osutil.RunCmd(time.Minute, "", "bhyvectl", "--destroy", fmt.Sprintf("--vm=%v", inst.vmName))100 // Destroy a lingering snapshot and clone.101 osutil.RunCmd(time.Minute, "", "zfs", "destroy", "-R", snapshot)102 // Create a snapshot of the data set containing the VM image.103 // bhyve will use a clone of the snapshot, which gets recreated every time the VM104 // is restarted. This is all to work around bhyve's current lack of an105 // image snapshot facility.106 if _, err := osutil.RunCmd(time.Minute, "", "zfs", "snapshot", snapshot); err != nil {107 inst.Close()108 return nil, err109 }110 inst.snapshot = snapshot111 if _, err := osutil.RunCmd(time.Minute, "", "zfs", "clone", snapshot, clone); err != nil {112 inst.Close()113 return nil, err114 }115 tapdev, err := osutil.RunCmd(time.Minute, "", "ifconfig", "tap", "create")116 if err != nil {117 inst.Close()118 return nil, err119 }120 inst.tapdev = tapRegex.FindString(string(tapdev))121 if _, err := osutil.RunCmd(time.Minute, "", "ifconfig", inst.cfg.Bridge, "addm", inst.tapdev); err != nil {122 inst.Close()123 return nil, err124 }125 if err := inst.Boot(); err != nil {126 inst.Close()127 return nil, err128 }129 return inst, nil130}131func (inst *instance) Boot() error {132 loaderArgs := []string{133 "-c", "stdio",134 "-m", inst.cfg.Mem,135 "-d", inst.image,136 "-e", "autoboot_delay=0",137 inst.vmName,138 }139 // Stop the instance from the previous run in case it's still running.140 osutil.RunCmd(time.Minute, "", "bhyvectl", "--destroy", fmt.Sprintf("--vm=%v", inst.vmName))141 _, err := osutil.RunCmd(time.Minute, "", "bhyveload", loaderArgs...)142 if err != nil {143 return err144 }145 bhyveArgs := []string{146 "-H", "-A", "-P",147 "-c", fmt.Sprintf("%d", inst.cfg.CPU),148 "-m", inst.cfg.Mem,149 "-s", "0:0,hostbridge",150 "-s", "1:0,lpc",151 "-s", fmt.Sprintf("2:0,virtio-net,%v", inst.tapdev),152 "-s", fmt.Sprintf("3:0,virtio-blk,%v", inst.image),153 "-l", "com1,stdio",154 inst.vmName,155 }156 outr, outw, err := osutil.LongPipe()157 if err != nil {158 return err159 }160 inr, inw, err := osutil.LongPipe()161 if err != nil {162 outr.Close()163 outw.Close()164 return err165 }166 bhyve := osutil.Command("bhyve", bhyveArgs...)167 bhyve.Stdin = inr168 bhyve.Stdout = outw169 bhyve.Stderr = outw170 if err := bhyve.Start(); err != nil {171 outr.Close()172 outw.Close()173 inr.Close()174 inw.Close()175 return err176 }177 outw.Close()178 outw = nil179 inst.consolew = inw180 inr.Close()181 inst.bhyve = bhyve182 var tee io.Writer183 if inst.debug {184 tee = os.Stdout185 }186 inst.merger = vmimpl.NewOutputMerger(tee)187 inst.merger.Add("console", outr)188 outr = nil189 var bootOutput []byte190 bootOutputStop := make(chan bool)191 ipch := make(chan string, 1)192 go func() {193 gotip := false194 for {195 select {196 case out := <-inst.merger.Output:197 bootOutput = append(bootOutput, out...)198 case <-bootOutputStop:199 close(bootOutputStop)200 return201 }202 if gotip {203 continue204 }205 if ip := parseIP(bootOutput); ip != "" {206 ipch <- ip207 gotip = true208 }209 }210 }()211 select {212 case ip := <-ipch:213 inst.sshhost = ip214 case <-inst.merger.Err:215 bootOutputStop <- true216 <-bootOutputStop217 return vmimpl.BootError{Title: "bhyve exited", Output: bootOutput}218 case <-time.After(10 * time.Minute):219 bootOutputStop <- true220 <-bootOutputStop221 return vmimpl.BootError{Title: "no IP found", Output: bootOutput}222 }223 if err := vmimpl.WaitForSSH(inst.debug, 10*time.Minute, inst.sshhost,224 inst.sshkey, inst.sshuser, inst.os, 22, nil); err != nil {225 bootOutputStop <- true226 <-bootOutputStop227 return vmimpl.MakeBootError(err, bootOutput)228 }229 bootOutputStop <- true230 return nil231}232func (inst *instance) Close() {233 if inst.consolew != nil {234 inst.consolew.Close()235 }236 if inst.bhyve != nil {237 inst.bhyve.Process.Kill()238 inst.bhyve.Wait()239 osutil.RunCmd(time.Minute, "", "bhyvectl", fmt.Sprintf("--vm=%v", inst.vmName), "--destroy")240 inst.bhyve = nil241 }242 if inst.snapshot != "" {243 osutil.RunCmd(time.Minute, "", "zfs", "destroy", "-R", inst.snapshot)244 inst.snapshot = ""245 }246 if inst.tapdev != "" {247 osutil.RunCmd(time.Minute, "", "ifconfig", inst.tapdev, "destroy")248 inst.tapdev = ""249 }250}251func (inst *instance) Forward(port int) (string, error) {252 return fmt.Sprintf("%v:%v", inst.cfg.HostIP, port), nil253}254func (inst *instance) Copy(hostSrc string) (string, error) {255 vmDst := filepath.Join("/root", filepath.Base(hostSrc))256 args := append(vmimpl.SCPArgs(inst.debug, inst.sshkey, 22),257 hostSrc, inst.sshuser+"@"+inst.sshhost+":"+vmDst)258 if inst.debug {259 log.Logf(0, "running command: scp %#v", args)260 }261 _, err := osutil.RunCmd(10*time.Minute, "", "scp", args...)262 if err != nil {263 return "", err264 }265 return vmDst, nil266}267func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command string) (268 <-chan []byte, <-chan error, error) {269 rpipe, wpipe, err := osutil.LongPipe()270 if err != nil {271 return nil, nil, err272 }273 inst.merger.Add("ssh", rpipe)274 args := append(vmimpl.SSHArgs(inst.debug, inst.sshkey, 22),275 inst.sshuser+"@"+inst.sshhost, command)276 if inst.debug {277 log.Logf(0, "running command: ssh %#v", args)278 }279 cmd := osutil.Command("ssh", args...)280 cmd.Stdout = wpipe281 cmd.Stderr = wpipe282 if err := cmd.Start(); err != nil {283 wpipe.Close()284 return nil, nil, err285 }286 wpipe.Close()287 errc := make(chan error, 1)288 signal := func(err error) {289 select {290 case errc <- err:291 default:292 }293 }294 go func() {295 select {296 case <-time.After(timeout):297 signal(vmimpl.ErrTimeout)298 case <-stop:299 signal(vmimpl.ErrTimeout)300 case err := <-inst.merger.Err:...

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 bhyve, err := lochness.NewBhyve()4 if err != nil {5 fmt.Println(err)6 }7 defer bhyve.Close()8}9import (10func main() {11 bhyve, err := lochness.NewBhyve()12 if err != nil {13 fmt.Println(err)14 }15 defer bhyve.Close()16}17import (18func main() {19 bhyve, err := lochness.NewBhyve()20 if err != nil {21 fmt.Println(err)22 }23 defer bhyve.Close()24}25import (26func main() {27 bhyve, err := lochness.NewBhyve()28 if err != nil {29 fmt.Println(err)30 }31 defer bhyve.Close()32}33import (34func main() {35 bhyve, err := lochness.NewBhyve()36 if err != nil {37 fmt.Println(err)38 }39 defer bhyve.Close()40}41import (42func main() {43 bhyve, err := lochness.NewBhyve()44 if err != nil {45 fmt.Println(err)46 }47 defer bhyve.Close()48}49import (50func main() {

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 bhyve := bhyve.NewBhyve()4 bhyve.Close()5}6import (7func main() {8 bhyve := bhyve.NewBhyve()9 fmt.Println(bhyve.List())10}11import (12func main() {13 bhyve := bhyve.NewBhyve()14 bhyve.Create("vm1")15}16import (17func main() {18 bhyve := bhyve.NewBhyve()19 bhyve.Delete("vm1")20}21import (22func main() {23 bhyve := bhyve.NewBhyve()24 bhyve.Start("vm1")25}26import (27func main() {28 bhyve := bhyve.NewBhyve()

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1func main() {2 bhyve := bhyve.NewBhyve()3 bhyve.Close()4}5func main() {6 bhyve := bhyve.NewBhyve()7 bhyve.Close()8 bhyve.Close()9}10func main() {11 bhyve := bhyve.NewBhyve()12 bhyve.Close()13 bhyve.Close()14 bhyve.Close()15}16func main() {17 bhyve := bhyve.NewBhyve()18 bhyve.Close()19 bhyve.Close()20 bhyve.Close()21 bhyve.Close()22}23func main() {24 bhyve := bhyve.NewBhyve()25 bhyve.Close()26 bhyve.Close()27 bhyve.Close()28 bhyve.Close()29 bhyve.Close()30}31func main() {32 bhyve := bhyve.NewBhyve()33 bhyve.Close()34 bhyve.Close()35 bhyve.Close()36 bhyve.Close()37 bhyve.Close()38 bhyve.Close()39}40func main() {41 bhyve := bhyve.NewBhyve()42 bhyve.Close()

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 bhyve := bhyve.New()4 err := bhyve.Close("vm1")5 if err != nil {6 fmt.Println(err)7 }8}9import (10func main() {11 bhyve := bhyve.New()12 err := bhyve.Create("vm1", 1024, 1, "bridge0", "tap0", "boot.iso", "disk1.img")13 if err != nil {14 fmt.Println(err)15 }16}17import (18func main() {19 bhyve := bhyve.New()20 err := bhyve.Destroy("vm1")21 if err != nil {22 fmt.Println(err)23 }24}25import (26func main() {27 bhyve := bhyve.New()28 vms, err := bhyve.List()29 if err != nil {30 fmt.Println(err)31 }32 for _, vm := range vms {33 fmt.Println(vm)34 }35}36import (37func main() {38 bhyve := bhyve.New()39 err := bhyve.Start("vm1")40 if err != nil {41 fmt.Println(err)42 }43}44import (

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 bhyve := bhyve.NewBhyve("vm1")4 bhyve.Close()5 fmt.Println("vm1 closed")6}7import (8func main() {9 bhyve := bhyve.NewBhyve("vm1")10 bhyve.Kill()11 fmt.Println("vm1 killed")12}13import (14func main() {15 bhyve := bhyve.NewBhyve("vm1")16 bhyve.Delete()17 fmt.Println("vm1 deleted")18}19import (20func main() {21 bhyve := bhyve.NewBhyve("vm1")22 bhyve.PowerOff()23 fmt.Println("vm1 powered off")24}25import (26func main() {27 bhyve := bhyve.NewBhyve("vm1")28 bhyve.PowerOn()29 fmt.Println("vm1 powered on")30}31import (32func main() {33 bhyve := bhyve.NewBhyve("vm1")34 bhyve.Reboot()35 fmt.Println("vm1 rebooted")36}37import (38func main() {39 bhyve := bhyve.NewBhyve("vm1")

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1func main() {2 bhyve := bhyve.New()3 bhyve.Close()4}5func main() {6 bhyve := bhyve.New()7 bhyve.Create()8}9func main() {10 bhyve := bhyve.New()11 bhyve.Delete()12}13func main() {14 bhyve := bhyve.New()15 bhyve.Destroy()16}17func main() {18 bhyve := bhyve.New()19 bhyve.Info()20}21func main() {22 bhyve := bhyve.New()23 bhyve.List()24}25func main() {26 bhyve := bhyve.New()27 bhyve.Load()28}29func main() {30 bhyve := bhyve.New()31 bhyve.Pause()32}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful