Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, i want to use the mount() function to mount an external usb drive. I see that its prototype is: Code: int mount(const char *source, const char *target, const char ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    9

    Mounting a usb drive with mount()

    Hi,
    i want to use the mount() function to mount an external usb drive.
    I see that its prototype is:
    Code:
    int mount(const char *source, const char *target,
                     const char *filesystemtype, unsigned long mountflags,
                     const void *data);
    It implies specifying a certain filesystemtype.
    Before i was simply using the mount shell command (calling it with "system"):that doesn't satisfy me,since i would like to gain more control on the mounting process,but it's simpler,cause it doesn't need me to specify the filesystemtype of the usb disk.

    So:
    Since usb disks can have various filesystems (usually they are FAT,but they might be also NTFS),i want my application to be capable to mount the drive indipendently from the usb disk's file system.Is there some way to do that? I noticed some interesting filesystems like usbfs and autofs,but it doesn't seem to work.
    Any advice?
    Thanks in advance!

  2. #2
    Just Joined!
    Join Date
    Mar 2009
    Posts
    9
    While working on it i used the following command:
    Code:
    int b;
    b=mount("/dev/sdb1","/media/myusbdisk","vfat",0,"");
    The mounting succeeds (i can see the mounted device on my dekstop,and i can browse its contents if i open /media/myusbdisk,but i don't see the mounted device with the usual commands i use to check it (i.e. df -m,mount).I am able,instead,to see the device with fdisk -l.

    Why this?

  3. #3
    Just Joined!
    Join Date
    Feb 2009
    Posts
    45
    Answer:

    Quote Originally Posted by Zipi
    Since usb disks can have various filesystems (usually they are FAT,but they might be also NTFS),i want my application to be capable to mount the drive indipendently from the usb disk's file system.Is there some way to do that?
    This is only a guess: the «mount» CLI command guesses the filesystem before it tries to mount it. This is highly likely, because when I mount a filesystem without specifying a filesystem, an «open()» is performed on the device block node before the mount:
    Code:
    $> strace mount /dev/usbstick/somestick /mnt/usbstick 2>&1
    […]
    readlink("/dev/usbstick", 0xbf8e5c2b, 4096) = -1 EINVAL (Invalid argument)
    readlink("/dev/usbstick/somestick", 0xbf8e5c2b, 4096) = -1 EINVAL (Invalid argument)
    […]
    open("/dev/usbstick/somestick", O_RDONLY)  = 3
    […]
    ioctl(3, BLKGETSIZE64, 0xbf8e6978)      = 0
    lseek(3, 2066677760, SEEK_SET)          = 2066677760
    read(3, "\337\331n\270\251\201\375\325\364\314}\212)\246\n\2727\274}T}qK\376y\307\376\343[@\320v"..., 4096) = 4096
    lseek(3, 0, SEEK_SET)                   = 0
    read(3, "\353X\220mkdosfs\0\0\2\10 \0\2\0\0\0\0\370\0\0>\0@\0\0\0\0\0"..., 69632) = 69632
    […]
    close(3)                                = 0
    […]
    mount("/dev/usbstick/somestick", "/mnt/usbstick", "vfat"..., MS_MGC_VAL, NULL) = 0
    in contrast to:
    Code:
    $> strace mount -t vfat /dev/usbstick/somestick /mnt/usbstick 2>&1
    […]
    readlink("/dev/usbstick", 0xbf877bbb, 4096) = -1 EINVAL (Invalid argument)
    readlink("/dev/usbstick/somestick", 0xbf877bbb, 4096) = -1 EINVAL (Invalid argument)
    […]
    mount("/dev/usbstick/somestick", "/mnt/usbstick", "vfat"..., MS_MGC_VAL, NULL) = 0

    Quote Originally Posted by Zipi
    While working on it i used the following command:
    Code:
    int b;
    b=mount("/dev/sdb1","/media/myusbdisk","vfat",0,"");
    The mounting succeeds (i can see the mounted device on my dekstop,and i can browse its contents if i open /media/myusbdisk,but i don't see the mounted device with the usual commands i use to check it (i.e. df -m,mount).I am able,instead,to see the device with fdisk -l.
    This is because the «mount» CLI command performs one extra action: it registeres the mounted filesystem within the file «/etc/mtab»¹. It is this file that tools like «df» and «mount» get their information as for which filesystems are mounted². «fdisk» does not use «/etc/mtab» but rather performs itʼs own query.

    Footnotes:
    1. mtab - Wikipedia, the free encyclopedia
    2. This can easily be verified:
      Code:
      $> strace df 2>&1 | grep mtab
      open("/etc/mtab", O_RDONLY)             = 3
      $> strace mount 2>&1 | grep mtab
      open("/etc/mtab", O_RDONLY|O_LARGEFILE) = 3

  4. #4
    Just Joined!
    Join Date
    Mar 2009
    Posts
    9
    Thank you so much! Your replies are always very helpful!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...