戈朗 :sshfs -o reconnect fails when used inside docker container with "reading directory .: Input/output error"

标签 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"
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 进入它以确认挂载点 (/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”命令显示挂载点在那里,但是当我尝试访问它时,我得到: ls: 读取目录.: 输入/输出错误

有人可以告诉我如何解决这个问题吗?我在这上面花了很多时间但没有成功,非常欢迎任何意见!

如何测试:

我将我的测试 docker 容器上传到公共(public)存储库,因此您现在可以自己运行和测试它:

首先你需要在后台启动容器并运行sshd:

sudo docker run -i -t -privileged -dns=172.25.0.10 -p 22 -d orobix/sshfs_startup_key2/bin/bash -c "/usr/sbin/sshd -D"

sshfs fuse 系统需要特权才能工作,我使用 dns 选项指定我的 dns 服务器,因为我在本地网络中(您可能不需要) 镜像 (orobix/ssfs_startuo_key2) 应自动从公共(public)存储库中提取。

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

您可以手动通过 ssh 进入容器,例如:

ssh root@172.17.42.1 -p 49153

同样,ip 和端口将不同。

最佳答案

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

关于戈朗 :sshfs -o reconnect fails when used inside docker container with "reading directory .: Input/output error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19549809/

相关文章:

java - Spring Boot 2.1 和 Java 11 中的 Bean 生命周期

PHP mysqli 重新连接问题

callback - 如何对 interface{} 变量进行类型断言以测试它是否是函数?

go - 为什么 Go 中的包级别不允许短变量声明?

GOPATH 为空(Gogland IDE)

go - golang 中有 urlencode() 函数吗?

linux - 脚本测试以查看工具是否已加载?

docker - 如何在容器中与 docker 客户端正确交互

c++ - 切换游戏杆 USB 端口时使用 joyGetPosEx 失败

c# - TcpClient 自动重新连接