From d59049e5316c28e407799c029741bb690683fad8 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Wed, 15 Feb 2023 11:20:30 +0000 Subject: [PATCH] qemu: add CopyDirectory (via scp) --- qemu/qemu-kernel.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/qemu/qemu-kernel.go b/qemu/qemu-kernel.go index 339a97e..823f635 100644 --- a/qemu/qemu-kernel.go +++ b/qemu/qemu-kernel.go @@ -352,16 +352,25 @@ func (q System) AsyncCommand(user, cmd string) (err error) { "nohup sh -c '%s' > /dev/null 2> /dev/null < /dev/null &", cmd)) } -// CopyFile is copy file from local machine to remote through ssh/scp -func (q *System) CopyFile(user, localPath, remotePath string) (err error) { +func (q System) scp(user, localPath, remotePath string, recursive bool) (err error) { addrPort := strings.Split(q.sshAddrPort, ":") addr := addrPort[0] port := addrPort[1] - cmd := exec.Command("scp", "-P", port, + args := []string{ + "-P", port, "-o", "StrictHostKeyChecking=no", "-o", "LogLevel=error", - localPath, user+"@"+addr+":"+remotePath) + } + + if recursive { + args = append(args, "-r") + } + + args = append(args, localPath, user+"@"+addr+":"+remotePath) + + cmd := exec.Command("scp", args...) + output, err := cmd.CombinedOutput() if err != nil { return errors.New(string(output)) @@ -370,6 +379,16 @@ func (q *System) CopyFile(user, localPath, remotePath string) (err error) { return } +// CopyFile from local machine to remote via scp +func (q System) CopyFile(user, localPath, remotePath string) (err error) { + return q.scp(user, localPath, remotePath, false) +} + +// CopyDirectory from local machine to remote via scp +func (q System) CopyDirectory(user, localPath, remotePath string) (err error) { + return q.scp(user, localPath, remotePath, true) +} + // CopyAndInsmod copy kernel module to temporary file on qemu then insmod it func (q *System) CopyAndInsmod(localKoPath string) (output string, err error) { remoteKoPath := fmt.Sprintf("/tmp/module_%d.ko", rand.Int())