How to use vmctl method of vmm Package

Best Syzkaller code snippet using vmm.vmctl

vmm.go

Source:vmm.go Github

copy

Full Screen

...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:270 cmd.Process.Kill()271 if cmdErr := cmd.Wait(); cmdErr == nil {272 // If the command exited successfully, we got EOF error from merger.273 // But in this case no error has happened and the EOF is expected.274 err = nil275 }276 signal(err)277 return278 }279 cmd.Process.Kill()280 cmd.Wait()281 }()282 return inst.merger.Output, errc, nil283}284func (inst *instance) Diagnose() ([]byte, bool) {285 return vmimpl.DiagnoseOpenBSD(inst.consolew)286}287// Run the given vmctl(8) command and wait for it to finish.288func (inst *instance) vmctl(args ...string) (string, error) {289 if inst.debug {290 log.Logf(0, "running command: vmctl %#v", args)291 }292 out, err := osutil.RunCmd(time.Minute, "", "vmctl", args...)293 if err != nil {294 if inst.debug {295 log.Logf(0, "vmctl failed: %v", err)296 }297 return "", err298 }299 if inst.debug {300 log.Logf(0, "vmctl output: %v", string(out))301 }302 return string(out), nil303}304func parseIP(output []byte) string {305 matches := ipRegex.FindSubmatch(output)306 if len(matches) < 2 {307 return ""308 }309 return string(matches[1])310}...

Full Screen

Full Screen

driver.go

Source:driver.go Github

copy

Full Screen

...21 GetTapIPAddress(string) (string, error)22 GetVMId(string) string23}24type vmmDriver struct {25 vmctl string26 logfile string27 tty io.WriteCloser28 console int29 ui packer.Ui30}31func (d *vmmDriver) GetVMId(name string) string {32 var stdout bytes.Buffer33 cmd := exec.Command("vmctl", "status", name)34 cmd.Stdout = &stdout35 err := cmd.Run()36 if _, ok := err.(*exec.ExitError); ok {37 err = fmt.Errorf("vmctl status error")38 }39 stdoutString := strings.TrimSpace(stdout.String())40 vmctl := regexp.MustCompile(`(\d+)`)41 resultarr := vmctl.FindAllStringSubmatch(stdoutString, -1)42 if resultarr == nil {43 return "VMAWOL"44 }45 return resultarr[0][1]46}47func (d *vmmDriver) VmctlCmd(args ...string) error {48 var stdout, stderr bytes.Buffer49 var cmd *exec.Cmd50 log.Printf("Executing command: vmctl %s", strings.Join(args, " "))51 cmd = exec.Command(d.vmctl, args...)52 cmd.Stdout = &stdout53 cmd.Stderr = &stderr54 err := cmd.Run()55 stdoutString := strings.TrimSpace(stdout.String())56 stderrString := strings.TrimSpace(stderr.String())57 if _, ok := err.(*exec.ExitError); ok {58 err = fmt.Errorf("vmctl error")59 }60 log.Printf("stdout: %s", stdoutString)61 log.Printf("stderr: %s", stderrString)62 return err63}64// Start the VM and create a pipe to insert commands into the VM. (from packer-builder-vmm)65func (d *vmmDriver) Start(args ...string) error {66 logFile, err := os.OpenFile(d.logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)67 if err != nil {68 return err69 }70 log.Printf("Executing vmctl: vmctl %s", strings.Join(args, " "))71 cmd := exec.Command(d.vmctl, args...)72 cmd.Env = append(os.Environ(),73 "TERM=vt220",74 )75 stdout, err := cmd.StdoutPipe()76 if err != nil {77 return err78 }79 // Create an stdin pipe that is used to issue commands.80 if d.tty, err = cmd.StdinPipe(); err != nil {81 return err82 }83 // Write the console output to the log file.84 go func() {85 defer stdout.Close()86 defer logFile.Close()87 _, _ = io.Copy(logFile, stdout)88 }()89 // Start up the VM.90 if err := cmd.Start(); err != nil {91 return err92 }93 // Give the VM a bit of time to start up.94 time.Sleep(3 * time.Second)95 return nil96}97func (d *vmmDriver) Stop(name string) error {98 cmd := exec.Command(d.vmctl, "stop", name)99 //err := cmd.Run()100 cmd.Run()101 return nil102}103func (d *vmmDriver) GetTapIPAddress(id string) (string, error) {104 var stdout bytes.Buffer105 vmId, _ := strconv.Atoi(id)106 vmName := fmt.Sprintf("vm%d", vmId)107 log.Printf("VM name: %s", vmName)108 // grab all available interfaces from group "tap"109 cmd := exec.Command("ifconfig", "tap")110 cmd.Stdout = &stdout111 err := cmd.Run()112 if _, ok := err.(*exec.ExitError); ok {113 err = fmt.Errorf("ifconfig error")114 }115 // parse interface(s) description and IPv4 addr116 stdoutString := strings.TrimSpace(stdout.String())117 log.Printf("ifconfig: %s", stdoutString)118 // XXX works on OpenBSD 6.6, but ugly119 vmctl := regexp.MustCompile(`description:\s(\w+\d+).*\n.*\n.*\n.*\n.*inet (\d+\.\d+\.\d+\.\d+)`)120 resultarr := vmctl.FindAllStringSubmatch(stdoutString, -1)121 // in case of multiple tap interfaces, loop into the result in order122 // to find the one we started123 for _, line := range resultarr {124 // [1] is the vmName125 // [2] is the IP126 if line[1] == vmName {127 return line[2], err128 }129 }130 err = fmt.Errorf("couldn't parse interface description")131 return "", err132}133//// interface Seq requires the following, not using it so far134// SendKey sends a key press....

Full Screen

Full Screen

vmctl

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client, err := vmctl.New("localhost:8000")4 if err != nil {5 panic(err)6 }7 vms, err := client.ListVms()8 if err != nil {9 panic(err)10 }11 for _, vm := range vms {12 fmt.Println(vm)13 }14}

Full Screen

Full Screen

vmctl

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 vmm := vmm.NewVmm()4 vmctl, err := vmm.Vmctl()5 if err != nil {6 fmt.Println("vmctl error:", err)7 }8 fmt.Println("vmctl:", vmctl)9}

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