How to use Close method of qemu Package

Best Syzkaller code snippet using qemu.Close

qemu.go

Source:qemu.go Github

copy

Full Screen

...44 workdir string45 sshkey string46 sshuser string47 port int48 rpipe io.ReadCloser49 wpipe io.WriteCloser50 qemu *exec.Cmd51 waiterC chan error52 merger *vmimpl.OutputMerger53}54type archConfig struct {55 Qemu string56 QemuArgs string57}58var archConfigs = map[string]archConfig{59 "linux/amd64": {60 Qemu: "qemu-system-x86_64",61 QemuArgs: "-enable-kvm -usb -usbdevice mouse -usbdevice tablet -soundhw all",62 },63 "linux/386": {64 Qemu: "qemu-system-i386",65 },66 "linux/arm64": {67 Qemu: "qemu-system-aarch64",68 QemuArgs: "-machine virt -cpu cortex-a57",69 },70 "linux/arm": {71 Qemu: "qemu-system-arm",72 },73 "linux/ppc64le": {74 Qemu: "qemu-system-ppc64",75 },76 "fuchsia/amd64": {77 Qemu: "qemu-system-x86_64",78 QemuArgs: "-enable-kvm",79 },80}81func ctor(env *vmimpl.Env) (vmimpl.Pool, error) {82 archConfig := archConfigs[env.OS+"/"+env.Arch]83 cfg := &Config{84 Count: 1,85 Qemu: archConfig.Qemu,86 Qemu_Args: archConfig.QemuArgs,87 }88 if err := config.LoadData(env.Config, cfg); err != nil {89 return nil, fmt.Errorf("failed to parse qemu vm config: %v", err)90 }91 if cfg.Count < 1 || cfg.Count > 1000 {92 return nil, fmt.Errorf("invalid config param count: %v, want [1, 1000]", cfg.Count)93 }94 if env.Debug {95 cfg.Count = 196 }97 if _, err := exec.LookPath(cfg.Qemu); err != nil {98 return nil, err99 }100 if env.Image == "9p" {101 if env.OS != "linux" {102 return nil, fmt.Errorf("9p image is supported for linux only")103 }104 if cfg.Kernel == "" {105 return nil, fmt.Errorf("9p image requires kernel")106 }107 } else {108 if !osutil.IsExist(env.Image) {109 return nil, fmt.Errorf("image file '%v' does not exist", env.Image)110 }111 if !osutil.IsExist(env.SshKey) {112 return nil, fmt.Errorf("ssh key '%v' does not exist", env.SshKey)113 }114 }115 if cfg.Cpu <= 0 || cfg.Cpu > 1024 {116 return nil, fmt.Errorf("bad qemu cpu: %v, want [1-1024]", cfg.Cpu)117 }118 if cfg.Mem < 128 || cfg.Mem > 1048576 {119 return nil, fmt.Errorf("bad qemu mem: %v, want [128-1048576]", cfg.Mem)120 }121 cfg.Kernel = osutil.Abs(cfg.Kernel)122 cfg.Initrd = osutil.Abs(cfg.Initrd)123 pool := &Pool{124 cfg: cfg,125 env: env,126 }127 return pool, nil128}129func (pool *Pool) Count() int {130 return pool.cfg.Count131}132func (pool *Pool) Create(workdir string, index int) (vmimpl.Instance, error) {133 sshkey := pool.env.SshKey134 sshuser := pool.env.SshUser135 if pool.env.Image == "9p" {136 sshkey = filepath.Join(workdir, "key")137 sshuser = "root"138 keygen := exec.Command("ssh-keygen", "-t", "rsa", "-b", "2048", "-N", "", "-C", "", "-f", sshkey)139 if out, err := keygen.CombinedOutput(); err != nil {140 return nil, fmt.Errorf("failed to execute ssh-keygen: %v\n%s", err, out)141 }142 initFile := filepath.Join(workdir, "init.sh")143 if err := osutil.WriteExecFile(initFile, []byte(strings.Replace(initScript, "{{KEY}}", sshkey, -1))); err != nil {144 return nil, fmt.Errorf("failed to create init file: %v", err)145 }146 }147 for i := 0; ; i++ {148 inst, err := pool.ctor(workdir, sshkey, sshuser, index)149 if err == nil {150 return inst, nil151 }152 if i < 1000 && strings.Contains(err.Error(), "could not set up host forwarding rule") {153 continue154 }155 return nil, err156 }157}158func (pool *Pool) ctor(workdir, sshkey, sshuser string, index int) (vmimpl.Instance, error) {159 inst := &instance{160 cfg: pool.cfg,161 image: pool.env.Image,162 debug: pool.env.Debug,163 workdir: workdir,164 sshkey: sshkey,165 sshuser: sshuser,166 }167 closeInst := inst168 defer func() {169 if closeInst != nil {170 closeInst.Close()171 }172 }()173 var err error174 inst.rpipe, inst.wpipe, err = osutil.LongPipe()175 if err != nil {176 return nil, err177 }178 if err := inst.Boot(); err != nil {179 return nil, err180 }181 closeInst = nil182 return inst, nil183}184func (inst *instance) Close() {185 if inst.qemu != nil {186 inst.qemu.Process.Kill()187 err := <-inst.waiterC188 inst.waiterC <- err // repost it for waiting goroutines189 }190 if inst.merger != nil {191 inst.merger.Wait()192 }193 if inst.rpipe != nil {194 inst.rpipe.Close()195 }196 if inst.wpipe != nil {197 inst.wpipe.Close()198 }199}200func (inst *instance) Boot() error {201 for {202 // Find an unused TCP port.203 inst.port = rand.Intn(64<<10-1<<10) + 1<<10204 ln, err := net.Listen("tcp", fmt.Sprintf("localhost:%v", inst.port))205 if err == nil {206 ln.Close()207 break208 }209 }210 // TODO: ignores inst.cfg.Cpu211 args := []string{212 "-m", strconv.Itoa(inst.cfg.Mem),213 "-net", "nic",214 "-net", fmt.Sprintf("user,host=%v,hostfwd=tcp::%v-:22", hostAddr, inst.port),215 "-display", "none",216 "-serial", "stdio",217 "-no-reboot",218 "-numa", "node,nodeid=0,cpus=0-1", "-numa", "node,nodeid=1,cpus=2-3",219 "-smp", "sockets=2,cores=2,threads=1",220 }221 args = append(args, strings.Split(inst.cfg.Qemu_Args, " ")...)222 if inst.image == "9p" {223 args = append(args,224 "-fsdev", "local,id=fsdev0,path=/,security_model=none,readonly",225 "-device", "virtio-9p-pci,fsdev=fsdev0,mount_tag=/dev/root",226 )227 } else {228 args = append(args,229 "-hda", inst.image,230 "-snapshot",231 )232 }233 if inst.cfg.Initrd != "" {234 args = append(args,235 "-initrd", inst.cfg.Initrd,236 )237 }238 if inst.cfg.Kernel != "" {239 cmdline := []string{240 "console=ttyS0",241 "vsyscall=native",242 "rodata=n",243 "oops=panic",244 "nmi_watchdog=panic",245 "panic_on_warn=1",246 "panic=86400",247 "ftrace_dump_on_oops=orig_cpu",248 "earlyprintk=serial",249 "net.ifnames=0",250 "biosdevname=0",251 "kvm-intel.nested=1",252 "kvm-intel.unrestricted_guest=1",253 "kvm-intel.vmm_exclusive=1",254 "kvm-intel.fasteoi=1",255 "kvm-intel.ept=1",256 "kvm-intel.flexpriority=1",257 "kvm-intel.vpid=1",258 "kvm-intel.emulate_invalid_guest_state=1",259 "kvm-intel.eptad=1",260 "kvm-intel.enable_shadow_vmcs=1",261 "kvm-intel.pml=1",262 "kvm-intel.enable_apicv=1",263 }264 if inst.image == "9p" {265 cmdline = append(cmdline,266 "root=/dev/root",267 "rootfstype=9p",268 "rootflags=trans=virtio,version=9p2000.L,cache=loose",269 "init="+filepath.Join(inst.workdir, "init.sh"),270 )271 } else {272 cmdline = append(cmdline,273 "root=/dev/sda",274 )275 }276 cmdline = append(cmdline, inst.cfg.Cmdline)277 args = append(args,278 "-kernel", inst.cfg.Kernel,279 "-append", strings.Join(cmdline, " "),280 )281 }282 if inst.debug {283 Logf(0, "running command: %v %#v", inst.cfg.Qemu, args)284 }285 qemu := exec.Command(inst.cfg.Qemu, args...)286 qemu.Stdout = inst.wpipe287 qemu.Stderr = inst.wpipe288 if err := qemu.Start(); err != nil {289 return fmt.Errorf("failed to start %v %+v: %v", inst.cfg.Qemu, args, err)290 }291 inst.wpipe.Close()292 inst.wpipe = nil293 inst.qemu = qemu294 // Qemu has started.295 // Start output merger.296 var tee io.Writer297 if inst.debug {298 tee = os.Stdout299 }300 inst.merger = vmimpl.NewOutputMerger(tee)301 inst.merger.Add("qemu", inst.rpipe)302 inst.rpipe = nil303 var bootOutput []byte304 bootOutputStop := make(chan bool)305 go func() {306 for {307 select {308 case out := <-inst.merger.Output:309 bootOutput = append(bootOutput, out...)310 case <-bootOutputStop:311 close(bootOutputStop)312 return313 }314 }315 }()316 // Wait for the qemu asynchronously.317 inst.waiterC = make(chan error, 1)318 go func() {319 err := qemu.Wait()320 inst.waiterC <- err321 }()322 // Wait for ssh server to come up.323 time.Sleep(5 * time.Second)324 start := time.Now()325 for {326 c, err := net.DialTimeout("tcp", fmt.Sprintf("localhost:%v", inst.port), 1*time.Second)327 if err == nil {328 c.SetDeadline(time.Now().Add(1 * time.Second))329 var tmp [1]byte330 n, err := c.Read(tmp[:])331 c.Close()332 if err == nil && n > 0 {333 break // ssh is up and responding334 }335 time.Sleep(3 * time.Second)336 }337 select {338 case err := <-inst.waiterC:339 inst.waiterC <- err // repost it for Close340 time.Sleep(time.Second) // wait for any pending output341 bootOutputStop <- true342 <-bootOutputStop343 return fmt.Errorf("qemu stopped:\n%v\n", string(bootOutput))344 default:345 }346 if time.Since(start) > 10*time.Minute {347 bootOutputStop <- true348 <-bootOutputStop349 return fmt.Errorf("ssh server did not start:\n%v\n", string(bootOutput))350 }351 }352 bootOutputStop <- true353 return nil354}355func (inst *instance) Forward(port int) (string, error) {356 return fmt.Sprintf("%v:%v", hostAddr, port), nil357}358func (inst *instance) Copy(hostSrc string) (string, error) {359 basePath := "/"360 if inst.image == "9p" {361 basePath = "/tmp"362 }363 vmDst := filepath.Join(basePath, filepath.Base(hostSrc))364 args := append(inst.sshArgs("-P"), hostSrc, inst.sshuser+"@localhost:"+vmDst)365 cmd := exec.Command("scp", args...)366 if inst.debug {367 Logf(0, "running command: scp %#v", args)368 cmd.Stdout = os.Stdout369 cmd.Stderr = os.Stdout370 }371 if err := cmd.Start(); err != nil {372 return "", err373 }374 done := make(chan bool)375 go func() {376 select {377 case <-time.After(3 * time.Minute):378 cmd.Process.Kill()379 case <-done:380 }381 }()382 err := cmd.Wait()383 close(done)384 if err != nil {385 return "", err386 }387 return vmDst, nil388}389func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command string) (<-chan []byte, <-chan error, error) {390 rpipe, wpipe, err := osutil.LongPipe()391 if err != nil {392 return nil, nil, err393 }394 inst.merger.Add("ssh", rpipe)395 args := append(inst.sshArgs("-p"), inst.sshuser+"@localhost", command)396 if inst.debug {397 Logf(0, "running command: ssh %#v", args)398 }399 cmd := exec.Command("ssh", args...)400 cmd.Stdout = wpipe401 cmd.Stderr = wpipe402 if err := cmd.Start(); err != nil {403 wpipe.Close()404 return nil, nil, err405 }406 wpipe.Close()407 errc := make(chan error, 1)408 signal := func(err error) {409 select {410 case errc <- err:411 default:412 }413 }414 go func() {415 select {416 case <-time.After(timeout):417 signal(vmimpl.TimeoutErr)418 case <-stop:419 signal(vmimpl.TimeoutErr)420 case err := <-inst.merger.Err:...

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1qemu.Close()2qemu.Close()3qemu.Close()4qemu.Close()5qemu.Close()6qemu.Close()7qemu.Close()8qemu.Close()9qemu.Close()10qemu.Close()11qemu.Close()12qemu.Close()13qemu.Close()14qemu.Close()15qemu.Close()16qemu.Close()17qemu.Close()18qemu.Close()19qemu.Close()20qemu.Close()21qemu.Close()22qemu.Close()23qemu.Close()24qemu.Close()

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 qemu, err := qemu.NewQemu("/usr/bin/qemu-system-x86_64", "qemu-system-x86_64", qemu.QMPConfig{Logger: qemu.QMPLogDiscard})4 if err != nil {5 panic(err)6 }7 if err := qemu.Start(); err != nil {8 panic(err)9 }10 if err := qemu.Close(); err != nil {11 panic(err)12 }13}

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 socket, err := qmp.NewSocket("/var/run/libvirt/qemu/instance-0000000f.sock")4 if err != nil {5 fmt.Println(err)6 }7 err = socket.Connect()8 if err != nil {9 fmt.Println(err)10 }11 q := qmp.NewQMP(socket)12 err = q.Initialize()13 if err != nil {14 fmt.Println(err)15 }16 err = q.Close()17 if err != nil {18 fmt.Println(err)19 }20}21import (22func main() {23 socket, err := qmp.NewSocket("/var/run/libvirt/qemu/instance-0000000f.sock")24 if err != nil {25 fmt.Println(err)26 }27 err = socket.Connect()28 if err != nil {29 fmt.Println(err)30 }31 q := qmp.NewQMP(socket)32 err = q.Initialize()33 if err != nil {34 fmt.Println(err)35 }36 result, err := q.ExecuteQMPCapabilities()37 if err != nil {38 fmt.Println(err)39 }40 fmt.Println(result)41 err = q.Close()42 if err != nil {43 fmt.Println(err)44 }45}

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 q, err := qemu.NewQemu("/usr/bin/qemu-system-x86_64", "qemu-system-x86_64", qemu.QEMU_DEFAULTS)4 if err != nil {5 panic(err)6 }7 err = q.Connect()8 if err != nil {9 panic(err)10 }11 err = q.Close()12 if err != nil {13 panic(err)14 }15 fmt.Println("Connection closed successfully")16}

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 qemu, err := qemu.NewQemu("/var/run/libvirt/qemu/vm1.sock", qemu.QMPConfig{})4 if err != nil {5 fmt.Println("Error in creating qemu object")6 }7 err = qemu.Connect()8 if err != nil {9 fmt.Println("Error in connecting qemu object")10 }11 err = qemu.Close()12 if err != nil {13 fmt.Println("Error in closing qemu object")14 }15}16import (17func main() {18 qemu, err := qemu.NewQemu("/var/run/libvirt/qemu/vm1.sock", qemu.QMPConfig{})19 if err != nil {20 fmt.Println("Error in creating qemu object")21 }22 err = qemu.Connect()23 if err != nil {24 fmt.Println("Error in connecting qemu object")25 }26 _, err = qemu.Run([]byte(`{"execute":"query-status"}`))27 if err != nil {28 fmt.Println("Error in running QMP command")29 }30}31{"return": {"status": "running", "singlestep": false, "running": true}}32import (33func main() {34 qemu, err := qemu.NewQemu("/var/run/libvirt/qemu/vm1.sock", qemu.QMPConfig{})35 if err != nil {36 fmt.Println("Error in creating qemu object")37 }38 err = qemu.Connect()39 if err != nil {40 fmt.Println("Error in connecting qemu object")41 }

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 q := qmp.NewQMP("/tmp/qmp-sock")4 err := q.Connect()5 if err != nil {6 fmt.Println(err)7 }8 err = q.Close()9 if err != nil {10 fmt.Println(err)11 }12}

Full Screen

Full Screen

Close

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 q := qemu.NewQemu()4 defer q.Close()5 fmt.Println("Hello, playground")6}7import (8func main() {9 q := qemu.NewQemu()10 q.Close()11 fmt.Println("Hello, playground")12}13import (14func main() {15 q := qemu.NewQemu()16 q.Close()17 q.Close()18 fmt.Println("Hello, playground")19}20import (21func main() {22 q := qemu.NewQemu()23 q.Close()24 q.Close()25 q.Close()26 fmt.Println("Hello, playground")27}28import (29func main() {30 q := qemu.NewQemu()31 q.Close()32 q.Close()33 q.Close()34 q.Close()35 fmt.Println("Hello, playground")36}37import (38func main() {39 q := qemu.NewQemu()40 q.Close()41 q.Close()42 q.Close()43 q.Close()44 q.Close()45 fmt.Println("Hello, playground")46}47import (48func main() {49 q := qemu.NewQemu()50 q.Close()51 q.Close()52 q.Close()53 q.Close()54 q.Close()55 q.Close()

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