Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Problem wit...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 14 of 29 Topic 26075 of 26955
Post > Topic >>

Re: Problem with SYS_mount

by Antoninus Twink <nospam@[EMAIL PROTECTED] > May 7, 2008 at 12:14 AM

On  6 May 2008 at 14:19, Walter Roberson wrote:
> ... and yes, people *do* make mistakes. For example, in the recent
> sys_MOUNT thread you were keen on, the answer you cited as "accurate"
> was *not* accurate.

There was indeed an inaccuracy in my answer, and it would have been a
courtesy to the OP and others reading this discussion if you'd pointed
it out instead of getting on your high-horse.

Specifically, I said:
> If you really want to work programatically, you can open one of the
> loop devices, perform an ioctl on it (request LOOP_SET_FD with extra
> argument "/home/sam/myfile.img"), then use mount(2).

This is wrong: the extra argument should be a file descriptor, not a
path name.

Here's a knock-up example of mounting a file programatically with a loop
device (of course it needs to be run with sufficient privileges to set up
the loop device and mount it).


#include <stdio.h>
#include <errno.h>

#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <linux/loop.h>

#define FILE_TO_MOUNT "/tmp/foo.iso"
#define MOUNT_PATH "/mnt"

int main(int argc, char **argv)
{
  char dev[]="/dev/loop0";
  int i, fd, fd2;
  struct stat statbuf;
  struct loop_info loopinfo;

  /* find a free loop device */
  for(i=0; i<8; i++) {
    dev[9]='0'+i;
    if( stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode) )
      if((fd = open (dev, O_RDONLY)) >= 0) {
        if(ioctl(fd, LOOP_GET_STATUS, &loopinfo) && (errno == ENXIO))
            break; /* got one! */
        close(fd);
      }
    fd=-1;
  }
  if(fd==-1) {
    fprintf(stderr, "%s: couldn't find free loop device\n", *argv);
    return 1;
  }

  /* set up the loop device */
  if((fd2=open(FILE_TO_MOUNT, O_RDONLY)) < 0) {
    fprintf(stderr, "%s: failed to open " FILE_TO_MOUNT "\n", *argv);
    return 2;
  }

  if(ioctl(fd, LOOP_SET_FD, (void *) fd2)) {
    fprintf(stderr, "%s: ioctl failed on %s\n", *argv, dev);
    return 3;
  }

  /* do the mount */
  if(mount(dev, MOUNT_PATH, "iso9660", MS_RDONLY, NULL)) {
    fprintf(stderr, "%s: failed to mount %s on " MOUNT_PATH "\n", *argv,
dev);
    if (ioctl (fd, LOOP_CLR_FD, 0))
      fprintf(stderr, "%s: failed to clear %s\n", *argv, dev);
    return 4;
  }

  /* pause */
  printf("Mounted " FILE_TO_MOUNT " at " MOUNT_PATH " using %s\n"
      "Press enter to umount...\n", dev);
  getchar();

  /* do the umount */
  if(umount(MOUNT_PATH)) {
    fprintf(stderr, "%s: failed to umount %s on " MOUNT_PATH "\n", *argv,
dev);
    if (ioctl (fd, LOOP_CLR_FD, 0))
      fprintf(stderr, "%s: failed to clear %s\n", *argv, dev);
    return 5;
  }

  /* clean up */
  close(fd2);
  if (ioctl (fd, LOOP_CLR_FD, 0)) {
    fprintf(stderr, "%s: failed to clear %s\n", *argv, dev);
    return 6;
  }
  close(fd);

  fprintf(stderr, "Umounted %s and cleared %s\n", MOUNT_PATH, dev);
  return 0;
}
 




 29 Posts in Topic:
Problem with SYS_mount
Sam Joseph <spamtrap@[  2008-05-05 22:14:10 
Re: Problem with SYS_mount
Antoninus Twink <nospa  2008-05-05 23:33:53 
Re: Problem with SYS_mount
Eric Sosman <Eric.Sosm  2008-05-05 17:47:28 
Re: Problem with SYS_mount
richard@[EMAIL PROTECTED]  2008-05-05 21:56:08 
Re: Problem with SYS_mount
Ian Collins <ian-news@  2008-05-06 10:24:50 
Re: Problem with SYS_mount
richard@[EMAIL PROTECTED]  2008-05-05 23:01:18 
Re: Problem with SYS_mount
Ian Collins <ian-news@  2008-05-06 11:05:29 
Re: Problem with SYS_mount
richard@[EMAIL PROTECTED]  2008-05-05 23:27:11 
Re: Problem with SYS_mount
CBFalconer <cbfalconer  2008-05-05 19:30:31 
Re: Problem with SYS_mount
richard@[EMAIL PROTECTED]  2008-05-05 23:55:50 
Re: Problem with SYS_mount
gazelle@[EMAIL PROTECTED]  2008-05-05 22:11:58 
Re: Problem with SYS_mount
jacob navia <jacob@[EM  2008-05-06 07:45:39 
Re: Problem with SYS_mount
gazelle@[EMAIL PROTECTED]  2008-05-06 13:47:59 
Re: Problem with SYS_mount
Antoninus Twink <nospa  2008-05-07 00:14:47 
Re: Problem with SYS_mount
Sam Joseph <spamtrap@[  2008-05-07 08:13:02 
Re: Problem with SYS_mount
gazelle@[EMAIL PROTECTED]  2008-05-07 09:19:55 
Re: Problem with SYS_mount
CBFalconer <cbfalconer  2008-05-07 12:11:02 
Re: Problem with SYS_mount
roberson@[EMAIL PROTECTED  2008-05-07 16:57:50 
Re: Problem with SYS_mount
gazelle@[EMAIL PROTECTED]  2008-05-07 18:17:04 
Re: Problem with SYS_mount
Eric Sosman <Eric.Sosm  2008-05-07 13:22:07 
Re: Problem with SYS_mount
Antoninus Twink <nospa  2008-05-05 23:50:41 
Re: Problem with SYS_mount
Eligiusz Narutowicz<el  2008-05-06 14:15:42 
Re: Problem with SYS_mount
Ian Collins <ian-news@  2008-05-07 07:10:39 
Re: Problem with SYS_mount
Eligiusz Narutowicz<el  2008-05-06 15:50:37 
Re: Problem with SYS_mount
roberson@[EMAIL PROTECTED  2008-05-06 14:19:18 
Re: Problem with SYS_mount
Nick Keighley <nick_ke  2008-05-06 07:16:28 
Re: Problem with SYS_mount
CBFalconer <cbfalconer  2008-05-06 19:47:53 
Re: Problem with SYS_mount
Eligiusz Narutowicz<el  2008-05-07 12:08:36 
Re: Problem with SYS_mount
CBFalconer <cbfalconer  2008-05-07 12:06:53 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 2:22:17 CDT 2008.