如何以编程方式获取syscall.Mount()的文件系统类型 [英] How to get the file system type for syscall.Mount() programmatically

查看:330
本文介绍了如何以编程方式获取syscall.Mount()的文件系统类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数 Linux syscall.Mount 需要文件系统类型.

The function Linux syscall.Mount requires a file system type.

如果您尝试使用文件系统auto运行它,如下所示:

If you try to run it with the file system auto, like this:

func main(){
    if err := syscall.Mount("/dev/sda1", "/mnt1", "auto", 0, "w"); err != nil {
        log.Printf("Mount(\"%s\", \"%s\", \"auto\", 0, \"rw\")\n","/dev/sda1","/mnt1")
        log.Fatal(err)
    }
}

它将失败,并显示no such device. 此处已经描述了.

It will fail with no such device. It was already described here that Linux syscall.Mount just wraps mount(2), which doesn't itself support the concept of an "auto" fstype.

我知道如何使用bash查找它:

I know how to find it using bash:

root@ubuntu:~/go/src# blkid /dev/sda1
/dev/sda1: UUID="527c895c-864e-4f4c-8fba-460754181173" TYPE="ext4" PARTUUID="db5c2e63-01"

root@ubuntu:~/go/src# file -sL /dev/sda1
/dev/sda1: Linux rev 1.0 ext4 filesystem data, UUID=527c895c-864e-4f4c-8fba-460754181173 (needs journal recovery) (extents) (large files) (huge files)

在两种情况下,您都获得ext4文件系统类型.

In both cases you get the ext4 file system type.

在Go中用ext4替换auto将解决问题,但是我感兴趣的是,如何使用Go获取文件系统类型,例如/dev/sda1?

Replacing auto with ext4 in Go will solve the problem, but what I am interested in is, how can I use Go to get the file system type of, for example, /dev/sda1?

是否存在类似于blkidfile的功能,可以显示设备的文件系统类型?

Is there a function similar to blkid or file that can show the file system type of the device?

推荐答案

您尝试使用 blkid.go#L101 ).您只需获取Blkid()函数返回的地图的键名,然后在您的API中重用它

Did you try using the package blkid ? It seems to work out of the box as it internally implements the blkid shell command underneath (see blkid.go#L101). You can just get the key name for the map returned from the Blkid() function and reuse it in your API

package main

import (
    "fmt"
    blkid "github.com/LDCS/qslinux/blkid"
)

func main() {
    rmap := blkid.Blkid(false)
    var key string
    var result *blkid.Blkiddata

    for key, result = range rmap {
        fmt.Printf("Devname: %q\n", key)
    }

    fmt.Printf("Uuid_=%q\n", result.Uuid_)
    fmt.Printf("Uuidsub_=%q\n", result.Uuidsub_)
    fmt.Printf("Type_=%q\n", result.Type_)
    fmt.Printf("Label_=%q\n", result.Label_)
    fmt.Printf("Parttype_=%q\n", result.Parttype_)
    fmt.Printf("Partuuid_=%q\n", result.Partuuid_)
    fmt.Printf("Partlabel_ =%q\n", result.Partlabel_)
}

Blkiddata结构包含与默认Linux版本相同的所有信息

The Blkiddata struct contains all the information as with the default Linux version

type Blkiddata struct {
    Devname_   string
    Uuid_      string
    Uuidsub_   string
    Type_      string
    Label_     string
    Parttype_  string
    Partuuid_  string
    Partlabel_ string
}

只需使用以下模块即可

go get github.com/LDCS/qslinux/blkid

它还实现了其他Linux utils系列-dfdmidecodeetcfstabetchostsetcserviceetcshadowetcuserhpmdnmappartedscsismartctltgtd.参见模块github.com/LDCS/qslinux

It also implements the other family of Linux utils namely - df, dmidecode, etcfstab, etchosts, etcservice, etcshadow, etcuser, hp, md, nmap, parted, scsi, smartctl and tgtd. See module github.com/LDCS/qslinux

这篇关于如何以编程方式获取syscall.Mount()的文件系统类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆