golang:sshfs -o reconnect在Docker容器中使用“读取目录时失败。:输入/输出错误”

时间:2013-10-23 18:41:05

标签: 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以确认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 -p 22 -d orobix / sshfs_startup_key2 / bin / bash -c“/ usr / sbin / sshd -D”

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

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

您可以使用例如:

手动ssh到容器中
ssh root@172.17.42.1 -p 49153

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

1 个答案:

答案 0 :(得分:0)

你有没有尝试过docker 0.8.1?它在运行'tmux'时解决了tty问题。也许它也可以解决你的问题。