기본 콘텐츠로 건너뛰기

마운트 정보 읽어오기

Linux에서 마운트 정보를 읽어보자. getmntent()가 바로 그것이다. 아쉽게도 POSIX, ANSI, IEEE 표준이 아니며, 리눅스SunOS, HP-UX 등에서 사용할 수 있으며, FreeBSD에서는 getmntinfo()로부터 알아낼 수 있다. 이 함수는 내부 버퍼를 사용하기 때문에 쓰레드에 안전하지 않으며, getmntent_r()으로 쓰레드에 대응할 수 있다.

자, 먼저 getmntent()를 하기 앞서, 마운트 정보를 읽고 쓸 수 있도록 핸들을 얻어와야하는데 그것을 하는 것이 setmntent()이고, 다 쓴 핸들을 닫을 때 쓰는 것이 endmntent()이다. 만약 중간에 마운트 정보를 바꿨으면, endmntent()할 때 비로소 적용된다. 자세한 것은 man페이지를 참조하고, 간단하게 C++로 마운트 목록을 보여주는 프로그램을 짜보았다.
#include <iostream>
#include <list>
#include <string>
#include <cstdio>
#include <errno.h>
using namespace std;
#include <mntent.h>

typedef struct me_type
{
    string fsname;
    string dir;
    string type;
    string opts;
    int freq;
    int passno;
} me_type;

typedef list<me_type> me_list;

bool
getMountList(me_list& ml)
{
    ml.clear();
    struct mntent* mnt;
    const char* table = "/etc/mtab"; // 부팅할 때 정보를 읽으려면 /etc/fstab로 수정한다.
    FILE* fp;

    fp = setmntent(table, "r");
    if ( !fp )
    {
        return false;
    }

    me_type me;

    while ( (mnt=getmntent(fp) ) )
    {
        me.fsname = mnt->mnt_fsname;
        me.dir = mnt->mnt_dir;
        me.type = mnt->mnt_type;
        me.opts = mnt->mnt_opts;
        me.freq = mnt->mnt_freq;
        me.passno = mnt->mnt_passno;
        ml.push_back(me);
    }// while

    endmntent(fp);

    return true;
}

int
main(int argc, char* argv[])
{
    me_list mel;
    if ( !getMountList(mel) )
    {
        cout << strerror(errno) << endl;
    }

    me_list::const_iterator ib(mel.begin());
    me_list::const_iterator ie(mel.end());

    while ( ib != ie )
    {
        cout << "ib->fsname: " << ib->fsname << endl;
        cout << "ib->dir: " << ib->dir << endl;
        cout << "ib->opts: " << ib->opts << endl;
        cout << "ib->freq: " << ib->freq << endl;
        cout << "ib->passno: " << ib->passno << endl;
        ++ ib;
    }

    return 0;
}

댓글

이 블로그의 인기 게시물

설치한 패키지에서 RPM 추출하기

오래된 패키지를 관리할 저장소가 없어졌고, 기존 패키지로 다른 서버를 세팅해야할 일이 생겼다면 RPM의 리패키지 기능을 이용해보자. $ rpm -e --repackage [PACKAGE_NAME] 위와 같이 리패키지하면, /var/spool/repackage/ 에 생성한 RPM파일이 있다. :-)

Winget 해시 무시하기

가끔씩 Winget 에서 패키지를 다운로드 했을 때, "설치 관리자 해시가 일치하지 않습니다." 오류가 뜰 때가 있다. 보안 이슈가 있지만, 그냥 무시하고 싶을 때, 아래 순서로 무시해준다. 관리자 권한 winget settings --enable InstallerHashOverride 설치 winget install --ignore-security-hash --id NirSoft.NirCmd