This is how I made my "regular" filesystem:
$ touch container
$ mkfs.ext4 container 20000
How can I mount it using C function mount(2) ?
Actually mount tells me :
Block device required
Is there a way to create a filesystem in C ?
The problem is that the mount
library call does not set up a loop device. In order to mount your container, you would have needed to pass -o loop
to the mount
command, which effectively includes what losetup
does (i.e. makes /dev/loopN
point to your file and mounts that). When using the library call, you need to simulate that, i.e. you need to set up a loop device first.
To determine exactly what you want need to do, try:
strace mount -t ext4 -o loop container /mnt
and have a look at the system calls. You can see that what's happening is that first it makes a loop device (e.g. /dev/loop0
using the system call equivalent of losetup
), then it mounts that loop device.
Here are the key lines from the strace
output:
First allocate the loop device:
stat("/dev/loop", 0x7fffe90b00b0) = -1 ENOENT (No such file or directory)
open("/dev/loop0", O_RDONLY) = 3
fstat(3, {st_mode=S_IFBLK|0660, st_rdev=makedev(7, 0), ...}) = 0
ioctl(3, LOOP_GET_STATUS, {number=39736224, offset=0x6f726763, encrypt_type=0x732f7075 /* LO_CRYPT_??? */, encrypt_key_size=1702130553, flags=LO_FLAGS_READ_ONLY|LO_FLAGS_AUTOCLEAR|LO_FLAGS_PARTSCAN|0x63206460, name="group rw,noexec,nosuid,nodev,none,name=systemd 0 0", encrypt_key="s 0 0\0\0e-agent.devices 0 0\0\0\320\177\0\0", ...}) = -1 ENXIO (No such device or address)
close(3) = 0
open("/home/amb/so/container", O_RDWR) = 3
open("/dev/loop0", O_RDWR) = 4
readlink("/home", 0x7fffe90adfa0, 4096) = -1 EINVAL (Invalid argument)
readlink("/home/amb", 0x7fffe90adfa0, 4096) = -1 EINVAL (Invalid argument)
readlink("/home/amb/so", 0x7fffe90adfa0, 4096) = -1 EINVAL (Invalid argument)
readlink("/home/amb/so/container", 0x7fffe90adfa0, 4096) = -1 EINVAL (Invalid argument)
ioctl(4, LOOP_SET_FD, 0x3) = 0
close(3) = 0
ioctl(4, LOOP_SET_STATUS64, {offset=0, number=0, flags=LO_FLAGS_AUTOCLEAR, file_name="/home/amb/so/container", ...}) = 0
ioctl(4, LOOP_GET_STATUS64, {offset=0, number=0, flags=LO_FLAGS_AUTOCLEAR, file_name="/home/amb/so/container", ...}) = 0
and only then do a mount
:
mount("/dev/loop0", "/mnt", "ext4", MS_MGC_VAL, NULL) = 0
I would however advise you to shell out to mount
unless you have a good reason not to. This ensures /etc/mtab
etc. is maintained.