views:

90

answers:

1

I've have been desperately searching for a tutorial on how to use the mount() function properly (they are very good at hiding this knowledge).

I need to use it to MNT_UPDATE " / "(/dev/disk0s2 in this case) in single user mode but I can't find an example or tutorial on how to use the function.

ps. Please no "Use system()" pps. I know /sbin/mount exists for a reason, I'm doing this for education purposes

+2  A: 

As you have probably already read in the man page, you use it like this:

int ret = mount("hfs", "/", MNT_UPDATE, some_data);

The trick is what goes into "data". It is a filesystem-specific structure that can be found by grepping for the filesystem name in system headers. For example, for HFS it's in /usr/include/hfs/hfs_mount.h:

struct hfs_mount_args {
    char     *fspec; /* <--- This is the device to mount */
    uid_t     hfs_uid;
    gid_t     hfs_gid;
    mode_t    hfs_mask;
    u_int32_t hfs_encoding;
    struct    timezone hfs_timezone;
    int       flags;
    int       journal_tbuffer_size;
    int       journal_flags;
    int       journal_disable;
};
Alex B
for fspec would it be hfs_mount_args->fspec = "/dev/disk0s2"?
<code> struct hfs_mount_args some_data; some_data.fspec = strdup("/dev/disk0s2"); int res = mount("hfs", "/", MNT_UPDATE , NSLog(@"Error:%d %s",res,strerror(errno));</code>gives me Error:0 No such file or directory
I'm sorry I don't know the code tags on this forum
@edlundquist: it's not exactly a forum :) Anyways, code should be surrounded by backticks, \`like this\`, which does `like this`. For the error code, a return value of `0` from `mount` indicates success, so maybe it did work and `errno` was set by something else previously.
zneak
@edlundquist, you have a success return code (and errno is undefined in this case, so it should be ignored). What happens to your mount point?
Alex B
It gains read right privileges which is what I wanted, but now it seems like the only partition I can mount is disk0s2, attempting to mount disk0s3 returns an -1 if attempt to mount it at /Volumes/testpartition
@edlundquist what's in errno in the second case?
Alex B
returns -1 errno is Invalid Address
@edlundquist man page states that errno is set to EFAULT if you pass an invalid pointer for your dir argument.
Alex B
the directory testpartition exists i'm not sure why it wouldn't mount it
@edlundquist, I meant pointer pointing to invalid memory, rather than anything to do with a directory itself. I'd appreciate a code sample.
Alex B
`struct hfs_mount_args some_data;` `int options = 0; ` `int result = 0;` `some_data.fspec = strdup("/dev/disk0s3");` `options = MNT_UPDATE; ` `result = mount("hfs", "/Volumes/Software", options ,` `NSLog(@"%d %s",result, strerror(errno));`
@edlundquist You don't seem to initialise other members of `struct hfs_mount_args`, so they may contain garbage.
Alex B
I went through and initialized everything in the struct and it still fails to mount, I get -1 Invalid Argument