How to use Close method of vmimpl Package

Best Syzkaller code snippet using vmimpl.Close

vmm.go

Source:vmm.go Github

copy

Full Screen

...40 sshport int41 merger *vmimpl.OutputMerger42 vmName string43 vmm *exec.Cmd44 consolew io.WriteCloser45}46var ipRegex = regexp.MustCompile(`bound to (([0-9]+\.){3}3)`)47func ctor(env *vmimpl.Env) (vmimpl.Pool, error) {48 cfg := &Config{49 Count: 1,50 Mem: 512,51 }52 if !osutil.IsExist(env.Image) {53 return nil, fmt.Errorf("image file '%v' does not exist", env.Image)54 }55 if err := config.LoadData(env.Config, cfg); err != nil {56 return nil, fmt.Errorf("failed to parse vmm vm config: %v", err)57 }58 if cfg.Count < 1 || cfg.Count > 128 {59 return nil, fmt.Errorf("invalid config param count: %v, want [1-128]", cfg.Count)60 }61 if env.Debug && cfg.Count > 1 {62 log.Logf(0, "limiting number of VMs from %v to 1 in debug mode", cfg.Count)63 cfg.Count = 164 }65 if cfg.Mem < 128 || cfg.Mem > 1048576 {66 return nil, fmt.Errorf("invalid config param mem: %v, want [128-1048576]", cfg.Mem)67 }68 if cfg.Kernel == "" {69 return nil, fmt.Errorf("missing config param kernel")70 }71 if !osutil.IsExist(cfg.Kernel) {72 return nil, fmt.Errorf("kernel '%v' does not exist", cfg.Kernel)73 }74 pool := &Pool{75 cfg: cfg,76 env: env,77 }78 return pool, nil79}80func (pool *Pool) Count() int {81 return pool.cfg.Count82}83func (pool *Pool) Create(workdir string, index int) (vmimpl.Instance, error) {84 var tee io.Writer85 if pool.env.Debug {86 tee = os.Stdout87 }88 inst := &instance{89 cfg: pool.cfg,90 image: filepath.Join(workdir, "disk.qcow2"),91 debug: pool.env.Debug,92 os: pool.env.OS,93 sshkey: pool.env.SSHKey,94 sshuser: pool.env.SSHUser,95 sshport: 22,96 vmName: fmt.Sprintf("%v-%v", pool.env.Name, index),97 merger: vmimpl.NewOutputMerger(tee),98 }99 // Stop the instance from the previous run in case it's still running.100 // This is racy even with -w flag, start periodically fails with:101 // vmctl: start vm command failed: Operation already in progress102 // So also sleep for a bit.103 inst.vmctl("stop", inst.vmName, "-f", "-w")104 time.Sleep(3 * time.Second)105 createArgs := []string{106 "create", inst.image,107 "-b", pool.env.Image,108 }109 if _, err := inst.vmctl(createArgs...); err != nil {110 return nil, err111 }112 if err := inst.Boot(); err != nil {113 // Cleans up if Boot fails.114 inst.Close()115 return nil, err116 }117 return inst, nil118}119func (inst *instance) Boot() error {120 outr, outw, err := osutil.LongPipe()121 if err != nil {122 return err123 }124 inr, inw, err := osutil.LongPipe()125 if err != nil {126 outr.Close()127 outw.Close()128 return err129 }130 startArgs := []string{131 "start", inst.vmName,132 "-b", inst.cfg.Kernel,133 "-d", inst.image,134 "-m", fmt.Sprintf("%vM", inst.cfg.Mem),135 "-L", // add a local network interface136 "-c", // connect to the console137 }138 if inst.cfg.Template != "" {139 startArgs = append(startArgs, "-t", inst.cfg.Template)140 }141 if inst.debug {142 log.Logf(0, "running command: vmctl %#v", startArgs)143 }144 cmd := osutil.Command("vmctl", startArgs...)145 cmd.Stdin = inr146 cmd.Stdout = outw147 cmd.Stderr = outw148 if err := cmd.Start(); err != nil {149 outr.Close()150 outw.Close()151 inr.Close()152 inw.Close()153 return err154 }155 inst.vmm = cmd156 inst.consolew = inw157 outw.Close()158 inr.Close()159 inst.merger.Add("console", outr)160 var bootOutput []byte161 bootOutputStop := make(chan bool)162 ipch := make(chan string, 1)163 go func() {164 gotip := false165 for {166 select {167 case out := <-inst.merger.Output:168 bootOutput = append(bootOutput, out...)169 case <-bootOutputStop:170 bootOutputStop <- true171 return172 }173 if gotip {174 continue175 }176 if ip := parseIP(bootOutput); ip != "" {177 ipch <- ip178 gotip = true179 }180 }181 }()182 select {183 case ip := <-ipch:184 inst.sshhost = ip185 case <-inst.merger.Err:186 bootOutputStop <- true187 <-bootOutputStop188 return vmimpl.BootError{Title: "vmm exited", Output: bootOutput}189 case <-time.After(10 * time.Minute):190 bootOutputStop <- true191 <-bootOutputStop192 return vmimpl.BootError{Title: "no IP found", Output: bootOutput}193 }194 if err := vmimpl.WaitForSSH(inst.debug, 20*time.Minute, inst.sshhost,195 inst.sshkey, inst.sshuser, inst.os, inst.sshport, nil); err != nil {196 bootOutputStop <- true197 <-bootOutputStop198 return vmimpl.BootError{Title: err.Error(), Output: bootOutput}199 }200 bootOutputStop <- true201 <-bootOutputStop202 return nil203}204func (inst *instance) Close() {205 inst.vmctl("stop", inst.vmName, "-f")206 if inst.consolew != nil {207 inst.consolew.Close()208 }209 if inst.vmm != nil {210 inst.vmm.Process.Kill()211 inst.vmm.Wait()212 }213 inst.merger.Wait()214}215func (inst *instance) Forward(port int) (string, error) {216 octets := strings.Split(inst.sshhost, ".")217 if len(octets) < 3 {218 return "", fmt.Errorf("too few octets in hostname %v", inst.sshhost)219 }220 addr := fmt.Sprintf("%v.%v.%v.2:%v", octets[0], octets[1], octets[2], port)221 return addr, nil222}223func (inst *instance) Copy(hostSrc string) (string, error) {224 vmDst := filepath.Join("/root", filepath.Base(hostSrc))225 args := append(vmimpl.SCPArgs(inst.debug, inst.sshkey, inst.sshport),226 hostSrc, inst.sshuser+"@"+inst.sshhost+":"+vmDst)227 if inst.debug {228 log.Logf(0, "running command: scp %#v", args)229 }230 _, err := osutil.RunCmd(10*time.Minute, "", "scp", args...)231 if err != nil {232 return "", err233 }234 return vmDst, nil235}236func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command string) (237 <-chan []byte, <-chan error, error) {238 rpipe, wpipe, err := osutil.LongPipe()239 if err != nil {240 return nil, nil, err241 }242 inst.merger.Add("ssh", rpipe)243 args := append(vmimpl.SSHArgs(inst.debug, inst.sshkey, inst.sshport),244 inst.sshuser+"@"+inst.sshhost, command)245 if inst.debug {246 log.Logf(0, "running command: ssh %#v", args)247 }248 cmd := osutil.Command("ssh", args...)249 cmd.Stdout = wpipe250 cmd.Stderr = wpipe251 if err := cmd.Start(); err != nil {252 wpipe.Close()253 return nil, nil, err254 }255 wpipe.Close()256 errc := make(chan error, 1)257 signal := func(err error) {258 select {259 case errc <- err:260 default:261 }262 }263 go func() {264 select {265 case <-time.After(timeout):266 signal(vmimpl.ErrTimeout)267 case <-stop:268 signal(vmimpl.ErrTimeout)269 case err := <-inst.merger.Err:...

Full Screen

Full Screen

isolated.go

Source:isolated.go Github

copy

Full Screen

...80 }81 closeInst := inst82 defer func() {83 if closeInst != nil {84 closeInst.Close()85 }86 }()87 if err := inst.repair(); err != nil {88 return nil, err89 }90 // Create working dir if doesn't exist.91 inst.ssh("mkdir -p '" + inst.cfg.TargetDir + "'")92 // Remove temp files from previous runs.93 inst.ssh("rm -rf '" + filepath.Join(inst.cfg.TargetDir, "*") + "'")94 closeInst = nil95 return inst, nil96}97func (inst *instance) Forward(port int) (string, error) {98 if inst.forwardPort != 0 {99 return "", fmt.Errorf("isolated: Forward port already set")100 }101 if port == 0 {102 return "", fmt.Errorf("isolated: Forward port is zero")103 }104 inst.forwardPort = port105 return fmt.Sprintf("127.0.0.1:%v", port), nil106}107func (inst *instance) ssh(command string) error {108 if inst.debug {109 log.Logf(0, "executing ssh %+v", command)110 }111 rpipe, wpipe, err := osutil.LongPipe()112 if err != nil {113 return err114 }115 // TODO(dvyukov): who is closing rpipe?116 args := append(vmimpl.SSHArgs(inst.debug, inst.sshKey, inst.targetPort),117 inst.sshUser+"@"+inst.targetAddr, command)118 if inst.debug {119 log.Logf(0, "running command: ssh %#v", args)120 }121 cmd := osutil.Command("ssh", args...)122 cmd.Stdout = wpipe123 cmd.Stderr = wpipe124 if err := cmd.Start(); err != nil {125 wpipe.Close()126 return err127 }128 wpipe.Close()129 done := make(chan bool)130 go func() {131 select {132 case <-time.After(time.Second * 30):133 if inst.debug {134 log.Logf(0, "ssh hanged")135 }136 cmd.Process.Kill()137 case <-done:138 }139 }()140 if err := cmd.Wait(); err != nil {141 close(done)142 out, _ := ioutil.ReadAll(rpipe)143 if inst.debug {144 log.Logf(0, "ssh failed: %v\n%s", err, out)145 }146 return fmt.Errorf("ssh %+v failed: %v\n%s", args, err, out)147 }148 close(done)149 if inst.debug {150 log.Logf(0, "ssh returned")151 }152 return nil153}154func (inst *instance) repair() error {155 log.Logf(2, "isolated: trying to ssh")156 if err := inst.waitForSSH(30 * time.Minute); err == nil {157 if inst.cfg.TargetReboot {158 log.Logf(2, "isolated: trying to reboot")159 inst.ssh("reboot") // reboot will return an error, ignore it160 if err := inst.waitForReboot(5 * 60); err != nil {161 log.Logf(2, "isolated: machine did not reboot")162 return err163 }164 log.Logf(2, "isolated: rebooted wait for comeback")165 if err := inst.waitForSSH(30 * time.Minute); err != nil {166 log.Logf(2, "isolated: machine did not comeback")167 return err168 }169 log.Logf(2, "isolated: reboot succeeded")170 } else {171 log.Logf(2, "isolated: ssh succeeded")172 }173 } else {174 log.Logf(2, "isolated: ssh failed")175 return fmt.Errorf("SSH failed")176 }177 return nil178}179func (inst *instance) waitForSSH(timeout time.Duration) error {180 return vmimpl.WaitForSSH(inst.debug, timeout, inst.targetAddr, inst.sshKey, inst.sshUser,181 inst.os, inst.targetPort)182}183func (inst *instance) waitForReboot(timeout int) error {184 var err error185 start := time.Now()186 for {187 if !vmimpl.SleepInterruptible(time.Second) {188 return fmt.Errorf("shutdown in progress")189 }190 // If it fails, then the reboot started191 if err = inst.ssh("pwd"); err != nil {192 return nil193 }194 if time.Since(start).Seconds() > float64(timeout) {195 break196 }197 }198 return fmt.Errorf("isolated: the machine did not reboot on repair")199}200func (inst *instance) Close() {201 close(inst.closed)202}203func (inst *instance) Copy(hostSrc string) (string, error) {204 baseName := filepath.Base(hostSrc)205 vmDst := filepath.Join(inst.cfg.TargetDir, baseName)206 inst.ssh("pkill -9 '" + baseName + "'; rm -f '" + vmDst + "'")207 args := append(vmimpl.SCPArgs(inst.debug, inst.sshKey, inst.targetPort),208 hostSrc, inst.sshUser+"@"+inst.targetAddr+":"+vmDst)209 cmd := osutil.Command("scp", args...)210 if inst.debug {211 log.Logf(0, "running command: scp %#v", args)212 cmd.Stdout = os.Stdout213 cmd.Stderr = os.Stdout214 }215 if err := cmd.Start(); err != nil {216 return "", err217 }218 done := make(chan bool)219 go func() {220 select {221 case <-time.After(3 * time.Minute):222 cmd.Process.Kill()223 case <-done:224 }225 }()226 err := cmd.Wait()227 close(done)228 if err != nil {229 return "", err230 }231 return vmDst, nil232}233func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command string) (234 <-chan []byte, <-chan error, error) {235 args := append(vmimpl.SSHArgs(inst.debug, inst.sshKey, inst.targetPort), inst.sshUser+"@"+inst.targetAddr)236 dmesg, err := vmimpl.OpenRemoteConsole("ssh", args...)237 if err != nil {238 return nil, nil, err239 }240 rpipe, wpipe, err := osutil.LongPipe()241 if err != nil {242 dmesg.Close()243 return nil, nil, err244 }245 args = vmimpl.SSHArgs(inst.debug, inst.sshKey, inst.targetPort)246 // Forward target port as part of the ssh connection (reverse proxy)247 if inst.forwardPort != 0 {248 proxy := fmt.Sprintf("%v:127.0.0.1:%v", inst.forwardPort, inst.forwardPort)249 args = append(args, "-R", proxy)250 }251 args = append(args, inst.sshUser+"@"+inst.targetAddr, "cd "+inst.cfg.TargetDir+" && exec "+command)252 log.Logf(0, "running command: ssh %#v", args)253 if inst.debug {254 log.Logf(0, "running command: ssh %#v", args)255 }256 cmd := osutil.Command("ssh", args...)257 cmd.Stdout = wpipe258 cmd.Stderr = wpipe259 if err := cmd.Start(); err != nil {260 dmesg.Close()261 rpipe.Close()262 wpipe.Close()263 return nil, nil, err264 }265 wpipe.Close()266 var tee io.Writer267 if inst.debug {268 tee = os.Stdout269 }270 merger := vmimpl.NewOutputMerger(tee)271 merger.Add("dmesg", dmesg)272 merger.Add("ssh", rpipe)273 return vmimpl.Multiplex(cmd, merger, dmesg, timeout, stop, inst.closed, inst.debug)274}275func (inst *instance) Diagnose() bool {276 return false277}278func splitTargetPort(addr string) (string, int, error) {279 target := addr...

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx, cancel := context.WithCancel(context.Background())4 defer cancel()5 vcClient, err := govmomi.NewClient(ctx, url, true)6 if err != nil {7 fmt.Println("Error creating vCenter client:", err)8 }

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v := vmimpl.New("Windows")4 v.Close()5}6import (7func main() {8 v := vmimpl.New("Windows")9 v.Start()10}11import (12func main() {13 v := vmimpl.New("Windows")14 v.Stop()15}16import (17func main() {18 v := vmimpl.New("Windows")19 v.Reboot()20}21import (22func main() {23 v := vmimpl.New("Windows")24 v.Shutdown()25}26import (27func main() {28 v := vmimpl.New("Windows")29 v.Suspend()30}31import (32func main() {33 v := vmimpl.New("Windows")34 v.Resume()35}36import (37func main() {38 v := vmimpl.New("Windows")39 v.Pause()40}41import (42func main() {43 v := vmimpl.New("Windows")44 v.Unpause()45}46import (47func main() {48 v := vmimpl.New("Windows")49 v.CreateSnapshot()50}51import (

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1vmimpl v=new vmimpl();2v.close();3vmimpl v=new vmimpl();4v.close();5vmimpl v=new vmimpl();6v.close();7vmimpl v=new vmimpl();8v.close();9vmimpl v=new vmimpl();10v.close();11vmimpl v=new vmimpl();12v.close();13vmimpl v=new vmimpl();14v.close();15vmimpl v=new vmimpl();16v.close();17vmimpl v=new vmimpl();18v.close();19vmimpl v=new vmimpl();20v.close();21vmimpl v=new vmimpl();22v.close();23vmimpl v=new vmimpl();24v.close();25vmimpl v=new vmimpl();26v.close();

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v := vmimpl.NewVM()4 v.Start()5 v.Close()6}7Your name to display (optional):

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful