How to use ssh method of qemu Package

Best Syzkaller code snippet using qemu.ssh

qemu.go

Source:qemu.go Github

copy

Full Screen

...41 cfg *Config42 image string43 debug bool44 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:421 cmd.Process.Kill()422 if cmdErr := cmd.Wait(); cmdErr == nil {423 // If the command exited successfully, we got EOF error from merger.424 // But in this case no error has happened and the EOF is expected.425 err = nil426 }427 signal(err)428 return429 }430 cmd.Process.Kill()431 cmd.Wait()432 }()433 return inst.merger.Output, errc, nil434}435func (inst *instance) sshArgs(portArg string) []string {436 args := []string{437 "-i", inst.sshkey,438 portArg, strconv.Itoa(inst.port),439 "-F", "/dev/null",440 "-o", "ConnectionAttempts=10",441 "-o", "ConnectTimeout=10",442 "-o", "BatchMode=yes",443 "-o", "UserKnownHostsFile=/dev/null",444 "-o", "IdentitiesOnly=yes",445 "-o", "StrictHostKeyChecking=no",446 "-o", "LogLevel=error",447 }448 if inst.debug {449 args = append(args, "-v")450 }451 return args452}453const initScript = `#! /bin/bash454set -eux455mount -t proc none /proc456mount -t sysfs none /sys457mount -t debugfs nodev /sys/kernel/debug/458mount -t tmpfs none /tmp459mount -t tmpfs none /var460mount -t tmpfs none /run461mount -t tmpfs none /etc462mount -t tmpfs none /root463touch /etc/fstab464mkdir /etc/network465mkdir /run/network466printf 'auto lo\niface lo inet loopback\n\n' >> /etc/network/interfaces467printf 'auto eth0\niface eth0 inet static\naddress 10.0.2.15\nnetmask 255.255.255.0\nnetwork 10.0.2.0\ngateway 10.0.2.1\nbroadcast 10.0.2.255\n\n' >> /etc/network/interfaces468printf 'auto eth0\niface eth0 inet6 static\naddress fe80::5054:ff:fe12:3456/64\ngateway 2000:da8:203:612:0:3:0:1\n\n' >> /etc/network/interfaces469ifup lo470ifup eth0471echo "root::0:0:root:/root:/bin/bash" > /etc/passwd472mkdir -p /etc/ssh473cp {{KEY}}.pub /root/474chmod 0700 /root475chmod 0600 /root/key.pub476mkdir -p /var/run/sshd/477chmod 700 /var/run/sshd478cat > /etc/ssh/sshd_config <<EOF479 Port 22480 Protocol 2481 UsePrivilegeSeparation no482 HostKey {{KEY}}483 PermitRootLogin yes484 AuthenticationMethods publickey485 ChallengeResponseAuthentication no486 AuthorizedKeysFile /root/key.pub487 IgnoreUserKnownHosts yes488 AllowUsers root489 LogLevel INFO490 TCPKeepAlive yes491 RSAAuthentication yes492 PubkeyAuthentication yes493EOF494/usr/sbin/sshd -e -D495/sbin/halt -f496`...

Full Screen

Full Screen

qemu-kernel.go

Source:qemu-kernel.go Github

copy

Full Screen

...14 "runtime"15 "strings"16 "syscall"17 "time"18 "golang.org/x/crypto/ssh"19)20func readUntilEOF(pipe io.ReadCloser, buf *[]byte) (err error) {21 bufSize := 102422 for err != io.EOF {23 stdout := make([]byte, bufSize)24 var n int25 n, err = pipe.Read(stdout)26 if err != nil && err != io.EOF {27 return28 }29 *buf = append(*buf, stdout[:n]...)30 }31 if err == io.EOF {32 err = nil33 }34 return35}36type arch string37const (38 // X86x64 is the qemu-system-x86_6439 X86x64 arch = "x86_64"40 // X86x32 is the qemu-system-i38641 X86x32 = "i386"42 // TODO add other43 unsupported = "unsupported" // for test purposes44)45// Kernel describe kernel parameters for qemu46type Kernel struct {47 Name string48 KernelPath string49 InitrdPath string50}51// System describe qemu parameters and executed process52type System struct {53 arch arch54 kernel Kernel55 drivePath string56 Cpus int57 Memory int58 debug bool59 gdb string // tcp::123460 noKASLR bool61 noSMEP bool62 noSMAP bool63 noKPTI bool64 // Timeout works after Start invocation65 Timeout time.Duration66 KilledByTimeout bool67 KernelPanic bool68 Died bool69 sshAddrPort string70 // accessible while qemu is running71 cmd *exec.Cmd72 pipe struct {73 stdin io.WriteCloser74 stderr io.ReadCloser75 stdout io.ReadCloser76 }77 Stdout, Stderr []byte78 // accessible after qemu is closed79 exitErr error80}81// NewSystem constructor82func NewSystem(arch arch, kernel Kernel, drivePath string) (q *System, err error) {83 if _, err = exec.LookPath("qemu-system-" + string(arch)); err != nil {84 return85 }86 q = &System{}87 q.arch = arch88 if _, err = os.Stat(kernel.KernelPath); err != nil {89 return90 }91 q.kernel = kernel92 if _, err = os.Stat(drivePath); err != nil {93 return94 }95 q.drivePath = drivePath96 // Default values97 q.Cpus = 198 q.Memory = 512 // megabytes99 return100}101func getRandomAddrPort() (addr string) {102 // 127.1-255.0-255.0-255:10000-50000103 ip := fmt.Sprintf("127.%d.%d.%d",104 rand.Int()%254+1, rand.Int()%255, rand.Int()%254)105 port := rand.Int()%40000 + 10000106 return fmt.Sprintf("%s:%d", ip, port)107}108func getRandomPort(ip string) (addr string) {109 // ip:1024-65535110 port := rand.Int()%(65536-1024) + 1024111 return fmt.Sprintf("%s:%d", ip, port)112}113func getFreeAddrPort() (addrPort string) {114 timeout := time.Now().Add(time.Second)115 for {116 if runtime.GOOS == "linux" {117 addrPort = getRandomAddrPort()118 } else {119 addrPort = getRandomPort("127.0.0.1")120 }121 ln, err := net.Listen("tcp", addrPort)122 if err == nil {123 ln.Close()124 return125 }126 if time.Now().After(timeout) {127 panic("Can't found free address:port on localhost")128 }129 }130}131func kvmExists() bool {132 if _, err := os.Stat("/dev/kvm"); err != nil {133 return false134 }135 file, err := os.OpenFile("/dev/kvm", os.O_WRONLY, 0666)136 if err != nil {137 if os.IsPermission(err) {138 return false139 }140 }141 file.Close()142 return true143}144func (q *System) panicWatcher() {145 for {146 time.Sleep(time.Second)147 if bytes.Contains(q.Stdout, []byte("Kernel panic")) {148 time.Sleep(time.Second)149 // There is no reason to stay alive after kernel panic150 q.Stop()151 q.KernelPanic = true152 return153 }154 }155}156func (q System) cmdline() (s string) {157 s = "root=/dev/sda ignore_loglevel console=ttyS0 rw"158 if q.noKASLR {159 s += " nokaslr"160 }161 if q.noSMEP {162 s += " nosmep"163 }164 if q.noSMAP {165 s += " nosmap"166 }167 if q.noKPTI {168 s += " nokpti"169 }170 return171}172// Start qemu process173func (q *System) Start() (err error) {174 rand.Seed(time.Now().UnixNano()) // Are you sure?175 q.sshAddrPort = getFreeAddrPort()176 hostfwd := fmt.Sprintf("hostfwd=tcp:%s-:22", q.sshAddrPort)177 qemuArgs := []string{"-snapshot", "-nographic",178 "-hda", q.drivePath,179 "-kernel", q.kernel.KernelPath,180 "-smp", fmt.Sprintf("%d", q.Cpus),181 "-m", fmt.Sprintf("%d", q.Memory),182 "-device", "e1000,netdev=n1",183 "-netdev", "user,id=n1," + hostfwd,184 }185 if q.debug {186 qemuArgs = append(qemuArgs, "-gdb", q.gdb)187 }188 if q.kernel.InitrdPath != "" {189 qemuArgs = append(qemuArgs, "-initrd", q.kernel.InitrdPath)190 }191 if (q.arch == X86x64 || q.arch == X86x32) && kvmExists() {192 qemuArgs = append(qemuArgs, "-enable-kvm", "-cpu", "host")193 }194 if q.arch == X86x64 && runtime.GOOS == "darwin" {195 qemuArgs = append(qemuArgs, "-accel", "hvf", "-cpu", "host")196 }197 qemuArgs = append(qemuArgs, "-append", q.cmdline())198 q.cmd = exec.Command("qemu-system-"+string(q.arch), qemuArgs...)199 if q.pipe.stdin, err = q.cmd.StdinPipe(); err != nil {200 return201 }202 if q.pipe.stdout, err = q.cmd.StdoutPipe(); err != nil {203 return204 }205 if q.pipe.stderr, err = q.cmd.StderrPipe(); err != nil {206 return207 }208 err = q.cmd.Start()209 if err != nil {210 return211 }212 go readUntilEOF(q.pipe.stdout, &q.Stdout)213 go readUntilEOF(q.pipe.stderr, &q.Stderr)214 go func() {215 q.exitErr = q.cmd.Wait()216 q.Died = true217 }()218 time.Sleep(time.Second / 10) // wait for immediately die219 if q.Died {220 err = errors.New("qemu died immediately: " + string(q.Stderr))221 }222 go q.panicWatcher()223 if q.Timeout != 0 {224 go func() {225 time.Sleep(q.Timeout)226 q.KilledByTimeout = true227 q.Stop()228 }()229 }230 return231}232// Stop qemu process233func (q *System) Stop() {234 // 1 00/01 01 01 SOH (Ctrl-A) START OF HEADING235 fmt.Fprintf(q.pipe.stdin, "%cx", 1)236 // wait for die237 time.Sleep(time.Second / 10)238 if !q.Died {239 q.cmd.Process.Signal(syscall.SIGTERM)240 time.Sleep(time.Second / 10)241 q.cmd.Process.Signal(syscall.SIGKILL)242 }243}244func (q System) ssh(user string) (client *ssh.Client, err error) {245 cfg := &ssh.ClientConfig{246 User: user,247 HostKeyCallback: ssh.InsecureIgnoreHostKey(),248 }249 client, err = ssh.Dial("tcp", q.sshAddrPort, cfg)250 return251}252// Command executes shell commands on qemu system253func (q System) Command(user, cmd string) (output string, err error) {254 client, err := q.ssh(user)255 if err != nil {256 return257 }258 defer client.Close()259 session, err := client.NewSession()260 if err != nil {261 return262 }263 bytesOutput, err := session.CombinedOutput(cmd)264 output = string(bytesOutput)265 return266}267// AsyncCommand executes command on qemu system but does not wait for exit268func (q System) AsyncCommand(user, cmd string) (err error) {269 client, err := q.ssh(user)270 if err != nil {271 return272 }273 defer client.Close()274 session, err := client.NewSession()275 if err != nil {276 return277 }278 return session.Run(fmt.Sprintf(279 "nohup sh -c '%s' > /dev/null 2> /dev/null < /dev/null &", cmd))280}281// CopyFile is copy file from local machine to remote through ssh/scp282func (q *System) CopyFile(user, localPath, remotePath string) (err error) {283 addrPort := strings.Split(q.sshAddrPort, ":")284 addr := addrPort[0]285 port := addrPort[1]286 cmd := exec.Command("scp", "-P", port,287 "-o", "StrictHostKeyChecking=no",288 "-o", "LogLevel=error",289 localPath, user+"@"+addr+":"+remotePath)290 output, err := cmd.CombinedOutput()291 if err != nil {292 return errors.New(string(output))293 }294 return295}296// CopyAndInsmod copy kernel module to temporary file on qemu then insmod it297func (q *System) CopyAndInsmod(localKoPath string) (output string, err error) {298 remoteKoPath := fmt.Sprintf("/tmp/module_%d.ko", rand.Int())299 err = q.CopyFile("root", localKoPath, remoteKoPath)300 if err != nil {301 return302 }303 return q.Command("root", "insmod "+remoteKoPath)304}305// CopyAndRun is copy local file to qemu vm then run it306func (q *System) CopyAndRun(user, path string) (output string, err error) {307 remotePath := fmt.Sprintf("/tmp/executable_%d", rand.Int())308 err = q.CopyFile(user, path, remotePath)309 if err != nil {310 return311 }312 return q.Command(user, "chmod +x "+remotePath+" && "+remotePath)313}314// Debug is for enable qemu debug and set hostname and port for listen315func (q *System) Debug(conn string) {316 q.debug = true317 q.gdb = conn318}319// SetKASLR is changing KASLR state through kernel boot args320func (q *System) SetKASLR(state bool) {321 q.noKASLR = !state322}323// SetSMEP is changing SMEP state through kernel boot args324func (q *System) SetSMEP(state bool) {325 q.noSMEP = !state326}327// SetSMAP is changing SMAP state through kernel boot args328func (q *System) SetSMAP(state bool) {329 q.noSMAP = !state330}331// SetKPTI is changing KPTI state through kernel boot args332func (q *System) SetKPTI(state bool) {333 q.noKPTI = !state334}335// GetKASLR is retrieve KASLR settings336func (q *System) GetKASLR() bool {337 return !q.noKASLR338}339// GetSMEP is retrieve SMEP settings340func (q *System) GetSMEP() bool {341 return !q.noSMEP342}343// GetSMAP is retrieve SMAP settings344func (q *System) GetSMAP() bool {345 return !q.noSMAP346}347// GetKPTI is retrieve KPTI settings348func (q *System) GetKPTI() bool {349 return !q.noKPTI350}351// GetSSHCommand returns command for connect to qemu machine over ssh352func (q System) GetSSHCommand() (cmd string) {353 addrPort := strings.Split(q.sshAddrPort, ":")354 addr := addrPort[0]355 port := addrPort[1]356 cmd = "ssh -o StrictHostKeyChecking=no"357 cmd += " -p " + port + " root@" + addr358 return359}...

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 key, err := qemu.NewSSHKey()4 if err != nil {5 log.Fatal(err)6 }7 instance, err := qemu.NewInstance(8 qemu.WithSSHKey(key),9 qemu.WithNetwork("user"),10 if err != nil {11 log.Fatal(err)12 }13 if err := instance.Start(); err != nil {14 log.Fatal(err)15 }16 time.AfterFunc(5*time.Second, func() {17 if err := instance.Stop(); err != nil {18 log.Fatal(err)19 }20 })21 if err := instance.Wait(); err != nil {22 log.Fatal(err)23 }24 config := instance.SSHConfig()25 client, err := ssh.Dial("tcp", net.JoinHostPort("localhost", strconv.Itoa(instance.SSHPort())), config)26 if err != nil {27 log.Fatal(err)28 }29 session, err := client.NewSession()30 if err != nil {31 log.Fatal(err)32 }33 defer session.Close()34 b, err := session.Output("echo hello world")35 if err != nil {36 log.Fatal(err)37 }38 fmt.Println(string(b))39 b, err = session.Output("ls -l")40 if err != nil {41 log.Fatal(err)42 }43 fmt.Println(string(b))44 b, err = session.Output("cat /etc/issue")45 if err != nil {46 log.Fatal(err)47 }48 fmt.Println(string(b))49 b, err = session.Output("uname -a")50 if err != nil {51 log.Fatal(err)52 }53 fmt.Println(string(b))

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c, err := qmp.NewSocket("qmp-sock")4 if err != nil {5 panic(err)6 }7 defer c.Disconnect()8 if err := c.Connect(); err != nil {9 panic(err)10 }11 cmd := &libvirt.Command{Command: "guest-info"}12 reply, err := c.Run(cmd)13 if err != nil {14 panic(err)15 }16 fmt.Println(reply)17}18&{guest-info 0xc0000a6000}19import (20func main() {21 c, err := qmp.NewTCP("

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 conn, err := qmp.NewSocketConnection("qmp-sock")4 if err != nil {5 fmt.Println(err)6 }7 defer conn.Close()8 q, err := qmp.NewQMP(conn)9 if err != nil {10 fmt.Println(err)11 }12 defer q.Shutdown()13 if err := q.Start(); err != nil {14 fmt.Println(err)15 }16 _, err = q.ExecuteQMPCapabilities()17 if err != nil {18 fmt.Println(err)19 }20 _, err = q.ExecuteQMPStop()21 if err != nil {22 fmt.Println(err)23 }24}

Full Screen

Full Screen

ssh

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")4 if err != nil {5 log.Fatal(err)6 }7 if err := q.Connect(); err != nil {8 log.Fatal(err)9 }10 if err := q.Shutdown(); err != nil {11 log.Fatal(err)12 }13 if err := q.Close(); err != nil {14 log.Fatal(err)15 }16}

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 q, err := qmp.NewSocketMonitor("unix", socket, nil)4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 defer q.Disconnect()9 err = q.Connect()10 if err != nil {11 fmt.Println(err)12 os.Exit(1)13 }14 err = q.ExecuteQMPCapabilities()15 if err != nil {16 fmt.Println(err)17 os.Exit(1)18 }19 err = q.ExecuteQMPStop()20 if err != nil {21 fmt.Println(err)22 os.Exit(1)23 }24 err = q.ExecuteQMPCont()25 if err != nil {26 fmt.Println(err)27 os.Exit(1)28 }29 err = q.ExecuteQMPSystemPowerdown()30 if err != nil {31 fmt.Println(err)32 os.Exit(1)33 }34 err = q.ExecuteQMPSystemReset()35 if err != nil {36 fmt.Println(err)37 os.Exit(1)38 }39 err = q.ExecuteQMPSystemWakeup()40 if err != nil {41 fmt.Println(err)42 os.Exit(1)43 }44 err = q.ExecuteQMPQuit()45 if err != nil {46 fmt.Println(err)47 os.Exit(1)48 }49 err = q.ExecuteQMPStop()50 if err != nil {51 fmt.Println(err)52 os.Exit(1)53 }54 err = q.ExecuteQMPSystemPowerdown()55 if err != nil {56 fmt.Println(err)57 os.Exit(1)58 }59 err = q.ExecuteQMPSystemReset()60 if err != nil {61 fmt.Println(err)62 os.Exit(1)63 }64 err = q.ExecuteQMPSystemWakeup()65 if err != nil {66 fmt.Println(err)67 os.Exit(1)68 }69 err = q.ExecuteQMPQuit()70 if err != nil {71 fmt.Println(err)72 os.Exit(1)73 }74 err = q.ExecuteQMPStop()75 if err != nil {76 fmt.Println(err)77 os.Exit(1)78 }

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