Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
golang:sshfs-o重新连接在docker容器中使用时失败,并带有“读取目录:输入/输出错误”_Go_Docker_Reconnect_Sshfs - Fatal编程技术网

golang:sshfs-o重新连接在docker容器中使用时失败,并带有“读取目录:输入/输出错误”

golang:sshfs-o重新连接在docker容器中使用时失败,并带有“读取目录:输入/输出错误”,go,docker,reconnect,sshfs,Go,Docker,Reconnect,Sshfs,我正在尝试使用sshfs从docker容器装载位于主机上的目录。我遇到的问题是,一旦golang应用程序完成执行,挂载点就会消失,而假设它会持续执行,手动执行的相同步骤会产生持续的结果 ssh客户端代码: package main import ( //"bytes" "code.google.com/p/go.crypto/ssh" //"fmt" "io" "log" "os" ) var ( server = "172.17.42.1:49155" username = "root"

我正在尝试使用sshfs从docker容器装载位于主机上的目录。我遇到的问题是,一旦golang应用程序完成执行,挂载点就会消失,而假设它会持续执行,手动执行的相同步骤会产生持续的结果

ssh客户端代码:

package main

import (
 //"bytes"
"code.google.com/p/go.crypto/ssh"
//"fmt"
"io"
"log"
"os"
)

var (
server = "172.17.42.1:49155"
username = "root"
password = clientPassword("orobix2013")
)

type clientPassword string

func (p clientPassword) Password(user string) (string, error) {
return string(p), nil
}

type TerminalModes map[uint8]uint32

const (
VINTR = 1
VQUIT = 2
VERASE = 3
VKILL = 4
VEOF = 5
VEOL = 6
VEOL2 = 7
VSTART = 8
VSTOP = 9
VSUSP = 10
VDSUSP = 11
VREPRINT = 12
VWERASE = 13
VLNEXT = 14
VFLUSH = 15
VSWTCH = 16
VSTATUS = 17
VDISCARD = 18
IGNPAR = 30
PARMRK = 31
INPCK = 32
ISTRIP = 33
INLCR = 34
IGNCR = 35
ICRNL = 36
IUCLC = 37
IXON = 38
IXANY = 39
IXOFF = 40
IMAXBEL = 41
ISIG = 50
ICANON = 51
XCASE = 52
ECHO = 53
ECHOE = 54
ECHOK = 55
ECHONL = 56
NOFLSH = 57
TOSTOP = 58
IEXTEN = 59
ECHOCTL = 60
ECHOKE = 61
PENDIN = 62
OPOST = 70
OLCUC = 71
ONLCR = 72
OCRNL = 73
ONOCR = 74
ONLRET = 75
CS7 = 90
CS8 = 91
PARENB = 92
PARODD = 93
TTY_OP_ISPEED = 128
TTY_OP_OSPEED = 129
)

func main() {
// An SSH client is represented with a slete). Currently only
// the "password" authentication method is supported.
//
// To authenticate with the remote server you must pass at least one
// implementation of ClientAuth via the Auth field in ClientConfig.

config := &ssh.ClientConfig{
User: username,
Auth: []ssh.ClientAuth{
// ClientAuthPassword wraps a ClientPassword implementation
// in a type that implements ClientAuth.
ssh.ClientAuthPassword(password),
},
}
client, err := ssh.Dial("tcp", "172.17.42.1:49155", config)
if err != nil {
panic("Failed to dial: " + err.Error())
}

// Each ClientConn can support multiple interactive sessions,
// represented by a Session.
defer client.Close()
// Create a session
session, err := client.NewSession()
if err != nil {
log.Fatalf("unable to create session: %s", err)
}
defer session.Close()
// Set up terminal modes
modes := ssh.TerminalModes{
ECHO: 0, // disable echoing
TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
// Request pseudo terminal
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
log.Fatalf("request for pseudo terminal failed: %s", err)
}
//var b bytes.Buffer
//session.Stdout = &bi

stdin, _ := session.StdinPipe()

stdout, _ := session.StdoutPipe()

go io.Copy(os.Stdout, stdout)
go io.Copy(stdin, os.Stdin)
//go io.Copy(os.Stderr, stderr)
if err := session.Run("/bin/bash -c \"sshfs piotr@172.17.42.1:/home/piotr/helloworld/ /mnt -o idmap=user -o reconnect;touch /mnt/ofoo\""); err != nil {
panic("Failed to run: " + err.Error())
}
容器在后台运行-d,我通过ssh连接到它,确认mountpoint/mnt/仍然存在,这就是我得到的结果:

root@654b8fa08b9e:~# mount
none on / type aufs (rw,relatime,si=77b99811b9d139a9)
/dev/disk/by-uuid/7e1d6bab-b3f2-4ac3-8bff-0779f5bf40f2 on /etc/hostname type ext4 (ro,relatime,errors=remount-ro,data=ordered)
/dev/disk/by-uuid/7e1d6bab-b3f2-4ac3-8bff-0779f5bf40f2 on /etc/hosts type ext4 (ro,relatime,errors=remount-ro,data=ordered)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)
/dev/disk/by-uuid/7e1d6bab-b3f2-4ac3-8bff-0779f5bf40f2 on /.dockerinit type ext4 (ro,relatime,errors=remount-ro,data=ordered)
/dev/disk/by-uuid/7e1d6bab-b3f2-4ac3-8bff-0779f5bf40f2 on /etc/resolv.conf type ext4 (ro,relatime,errors=remount-ro,data=ordered)
devpts on /dev/tty1 type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
devpts on /dev/pts type devpts (rw,relatime,mode=600,ptmxmode=666)
devpts on /dev/ptmx type devpts (rw,relatime,mode=600,ptmxmode=666)
piotr@172.17.42.1:/home/piotr/helloworld/ on /mnt type fuse.sshfs (rw,nosuid,nodev,relatime,user_id=0,group_id=0,max_read=65536)
root@654b8fa08b9e:~# cd /mnt/
root@654b8fa08b9e:/mnt# ls
ls: reading directory .: Input/output error
root@654b8fa08b9e:/mnt# 
mount命令显示mountpoint在那里,但是当我尝试访问它时,我得到: ls:正在读取目录。:输入/输出错误

有人能告诉我怎么修吗?我花了相当多的时间在这方面,但没有成功,任何输入是非常欢迎的

如何测试它:

我已将我的测试docker容器上载到公共存储库,以便您现在可以自己运行和测试它:

首先,您需要在后台启动运行sshd的容器:

sudo docker run-i-t-privileged-dns=172.25.0.10-p22-d orobix/sshfs\u startup\u key2/bin/bash-c/usr/sbin/sshd-d

如果sshfs fuse系统需要privileged,我将使用dns选项指定我的dns服务器,因为我在本地网络中,您可能不需要这样做 imageorobix/ssfs_startuo_键2应自动从公共存储库中提取

一旦容器运行,您应该能够运行go代码。当然,必须更改ip地址

您可以手动将ssh连接到容器中,例如:

ssh root@172.17.42.1 -p 49153

同样,ip和端口也会不同。

您是否尝试过docker 0.8.1?它在运行“tmux”时解决了tty问题。也许它也能解决您的问题。

出于好奇:为什么要使用Go而不是shell脚本?如果改用shell脚本会发生什么?首先,我不擅长编写脚本,我喜欢用围棋编程。go的酷特性是,所有东西都可以放在一个可以交叉编译的二进制文件中。最后但并非最不重要的一点是,一位在bash方面比我更有经验的人告诉我,使用脚本处理配置文件非常麻烦,而在go中处理配置文件非常简单。也就是说,由于上面的代码失败了,而且手动执行也很有效,所以我现在正尝试使用bash脚本实现相同的目标。希望它能起作用!这是在golang nuts上提出的,由于PTY问题,有一些关于它的讨论。一个测试docker映像是的!我在golang nuts论坛上问了非常类似的问题,但是这个问题略有不同,因为它涉及sshfs-o重新连接选项。最后,这些问题总结了我用不同的方法解决同一问题的两种尝试。我之所以在这里发帖,是因为“golang nuts”这个问题已经存在了一段时间,其他人没有或很少提出意见,所以我决定尝试不同的论坛。到目前为止,使用-o重新连接似乎是最接近的解决方案,但它仍然失败,我不知道为什么。。。