Thread: using sys/mount.h and mounting a USB thumb drive

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    28

    using sys/mount.h and mounting a USB thumb drive

    I am working on a small program written in C to mount and unmount a USB thumb drive on a Linux Ubuntu (Please see code below for mounting and unmounting). The problem I am facing is that each time I mount, I cannot unmount and I keep getting "Device or resource busy" error....

    Both operations work fine if I mount/unmount my USB from the command prompt using the following command:
    Code:
    mount -t vfat /dev/sda1 /media/usb0
    umount -f /media/usb0
    To mount my USB thumb drive using C code I write the following:
    Code:
    int mountUSB()
    {
    	int mountAttempts;
    	int status;	
    
    	mountAttempts = 0;
    	status = 0;
    	
    	do
    	{	
    		status = mount("/dev/sda1", "/media/usb0", "vfat", MS_MGC_VAL | MS_NOSUID, "");
    		if(status != SUCCESS)
    			sleep(1);
    		mountAttempts++;
    	}while(status != SUCCESS && mountAttempts < 3);
    	
    	printf("USB mount - status = %s\n", strerror(errno));
    }
    to unmount:
    Code:
    void umountUSB()
    {
    	int status;
    	status = umount("/media/usb0");
    	
    	printf("USB unmount - status = %s\n", strerror(errno));	
    }
    Can anyone tell me if I have the flags set correctly and whether or not I am using "vfat" correctly? I searched online but I couldn't find exact filesystem string descriptions.

    I also read the man page for mounting but not sure which flags I need to set to be able to read, write, delete, copy, and create files/directories.

    Any input is greatly appreciated.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    The flags look right, and "vfat" should be the same as when you use the command line, so it's all good.

    As for the umount...
    Are you doing anything between the mount and umount? If so, make sure you're not still using files in the mount point.
    If you're not, on the other hand, it's entirely possible that the OS is still reading information about the mounted drive, scanning the filesystem, etc. Maybe you should wait a second or two before umounting? Or try the MNT_FORCE flag in umount (specifically umount2, which should be available on Linux) to see if the problem still exists.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    28
    Thanks bernt,
    I played with it some more and to my surprise, adding MS_RDONLY flag during the mount makes it work - not sure why since I can still write to the USB as well as delete, change content and other operations....

    This is the new call that I perform and it seems to work:
    Code:
    status = mount("/dev/sda1", "/media/usb0", "vfat", MS_MGC_VAL | MS_RDONLY | MS_NOSUID, "");

    Thanks again for your comments.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. USB Linux - live or no?
    By sean in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-23-2008, 10:56 AM