util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Can't figure out how to use mnt_table_get_root_fs from libmount
@ 2012-09-08 18:07 Amit
  2012-09-08 20:19 ` Amit
  2012-09-10  8:23 ` Karel Zak
  0 siblings, 2 replies; 4+ messages in thread
From: Amit @ 2012-09-08 18:07 UTC (permalink / raw)
  To: util-linux

Hello,

I have a simple program using libmount that simply gets the path of the
root filesystem. I can't seem to get it to work. I always get a
segmentation fault. There is probably something basic that I am missing
but can't figure it out.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

#include <libmount.h>

int main(int argc, char *argv[])
{
    struct libmnt_context *cxt;
    struct libmnt_table *tab;
    struct libmnt_fs *fs;

    /* Enable debugging */
    mnt_init_debug(0xffff);

    /* Create new mount context */
    cxt = mnt_new_context();
    if (!cxt)
        printf("Error creating new mount context\n");

    /* A mount table */
    if (mnt_context_get_mtab(cxt, &tab) < 0)
        printf("Error getting mtab\n");

    /* Get the root filesystem */
    if (mnt_table_get_root_fs(tab, &fs) == -1)
        printf("Error getting root fs from mount table\n");

    const char *src = mnt_fs_get_source(fs);

    char *root = mnt_pretty_path(src, NULL);
    printf("root filesystem source (pretty) %s\n", root);

    return 0;
}


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Can't figure out how to use mnt_table_get_root_fs from libmount
  2012-09-08 18:07 Can't figure out how to use mnt_table_get_root_fs from libmount Amit
@ 2012-09-08 20:19 ` Amit
  2012-09-10  8:23 ` Karel Zak
  1 sibling, 0 replies; 4+ messages in thread
From: Amit @ 2012-09-08 20:19 UTC (permalink / raw)
  To: util-linux

Amit <amit.uttam@...> writes:

> 
> Hello,
> 
> I have a simple program using libmount that simply gets the path of the
> root filesystem. I can't seem to get it to work. I always get a
> segmentation fault. There is probably something basic that I am missing
> but can't figure it out.

[snip]

Using the following function works better.

fs = mnt_table_find_target(tab, "/", MNT_ITER_BACKWARD);

This is on until-linux 2.20.1 on debian wheezy.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Can't figure out how to use mnt_table_get_root_fs from libmount
  2012-09-08 18:07 Can't figure out how to use mnt_table_get_root_fs from libmount Amit
  2012-09-08 20:19 ` Amit
@ 2012-09-10  8:23 ` Karel Zak
  2012-09-17  0:21   ` Amit Uttamchandani
  1 sibling, 1 reply; 4+ messages in thread
From: Karel Zak @ 2012-09-10  8:23 UTC (permalink / raw)
  To: Amit; +Cc: util-linux

On Sat, Sep 08, 2012 at 06:07:51PM +0000, Amit wrote:
> Hello,
> 
> I have a simple program using libmount that simply gets the path of the
> root filesystem. I can't seem to get it to work. I always get a
> segmentation fault. There is probably something basic that I am missing
> but can't figure it out.
> 
> #include <stdlib.h>
> #include <string.h>
> #include <stdio.h>
> #include <assert.h>
> 
> #include <libmount.h>
> 
> int main(int argc, char *argv[])
> {
>     struct libmnt_context *cxt;
>     struct libmnt_table *tab;
>     struct libmnt_fs *fs;
> 
>     /* Enable debugging */
>     mnt_init_debug(0xffff);
> 
>     /* Create new mount context */
>     cxt = mnt_new_context();
>     if (!cxt)
>         printf("Error creating new mount context\n");
> 
>     /* A mount table */
>     if (mnt_context_get_mtab(cxt, &tab) < 0)
>         printf("Error getting mtab\n");
> 
>     /* Get the root filesystem */
>     if (mnt_table_get_root_fs(tab, &fs) == -1)


   if (mnt_table_get_root_fs(tab, &fs) != 0)

Sorry, the docs for the function is incorrect. Almost all libmount
functions return zero on success and less than zero on error.


Note that mnt_table_get_root_fs() uses mountpoint Id and parent Id
from /proc/self/mountinfo to found root fs.

It means that you have to use the mountinfo file -- on systems with
regular mtab file the function mnt_context_get_mtab() parses /etc/mtab
rather than the mountinfo file.

  tab = mnt_new_table_from_file("/proc/self/mountinfo");
  if (tab && mnt_table_get_root_fs(tab, &fs) == 0)
     ....

the struct libmnt_context is unnecessary in this case.

The mnt_table_get_root_fs() is mostly designed for application where
you need to sort mountpoints to a tree. (for example findmnt(8))

If you not sure than mnt_table_find_target() is probably better
choice.

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Can't figure out how to use mnt_table_get_root_fs from libmount
  2012-09-10  8:23 ` Karel Zak
@ 2012-09-17  0:21   ` Amit Uttamchandani
  0 siblings, 0 replies; 4+ messages in thread
From: Amit Uttamchandani @ 2012-09-17  0:21 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux

On Mon, Sep 10, 2012 at 10:23:06AM +0200, Karel Zak wrote:

[snip]

> The mnt_table_get_root_fs() is mostly designed for application where
> you need to sort mountpoints to a tree. (for example findmnt(8))
> 
> If you not sure than mnt_table_find_target() is probably better
> choice.
> 

Thanks for the reply. mnt_table_find_target works wonderfully.

Also, I am currently using libmount from util-linux 2.20.1. Would you
recommend this for production? Currently, debian wheezy uses util-linux
2.20.1.

Thanks,
Amit

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-09-17  0:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-08 18:07 Can't figure out how to use mnt_table_get_root_fs from libmount Amit
2012-09-08 20:19 ` Amit
2012-09-10  8:23 ` Karel Zak
2012-09-17  0:21   ` Amit Uttamchandani

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).