登录
首页 >  Golang >  Go问答

使用Golang实现文件系统挂载

来源:stackoverflow

时间:2024-02-23 10:42:18 194浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《使用Golang实现文件系统挂载》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

linux下可以使用go实现挂载功能吗?挂载从前台转移的路径到本地路径?如

add_iptables "${shared_file_path}"

if [[ "x$domain" == "xnoDomain" ]]
then
    expect > /dev/null 2>&1 < /dev/null 2>&1 <

正确答案


您可以使用 go 将系统调用包装到几乎任何您想要的内容。

例如,在 nanobox-io/nanobox with util/provider/dockermachine_mount_windows.go 中(较大函数的摘录):

    // ensure cifs/samba utilities are installed
    cmd = []string{"sh", "-c", setupCifsUtilsScript()}
    if b, err := Run(cmd); err != nil {
        lumber.Debug("cifs output: %s", b)
        return fmt.Errorf("cifs:%s", err.Error())
    }

    // mount!
    // mount -t cifs -o sec=ntlmssp,username=USER,password=PASSWORD,uid=1000,gid=1000 //192.168.99.1/ /
    source := fmt.Sprintf("//192.168.99.1/nanobox-%s", appID)
    // mfsymlinks,
    config, _ := models.LoadConfig()
    additionalOptions := config.NetfsMountOpts

    // since the mount command inserts the user into the command string with
    // single quotes, we need to escape any single quotes from the real
    // username. As the command will be running in bash, the actual escape
    // sequence is a bit tricky. Each ' will be replaced with '"'"'.
    escapedUser := strings.Replace(user, "'", "'\"'\"'", -1)

    opts := fmt.Sprintf("nodev,sec=ntlmssp,user='%s',password='%s',uid=1000,gid=1000", escapedUser, pass)
    if additionalOptions != "" {
        opts = fmt.Sprintf("%s,%s", additionalOptions, opts)
    }

    cmd = []string{
        "sudo",
        "/bin/mount",
        "-t",
        "cifs",
        "-o",
        opts,
        source,
        host,
    }

    lumber.Debug("cifs mount cmd: %v", cmd)
    if b, err := Run(cmd); err != nil {
        lumber.Debug("mount output: %s", b)
        return fmt.Errorf("mount: output: %s err:%s", b, err.Error())
    }

到这里,我们也就讲完了《使用Golang实现文件系统挂载》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>