How to umount a file system being in kernel code - linux

I am in kernel code and trying to umount a filesytem, before going to unload the driver.
Before that I want to umount the filesystem.
system(umount -t .....) does not work here.
Can any one please tell me how to umount in kerenel mode.

checking source code of umount() system call at LXR you can find out how unmount is done in kernel, here are some internals you may find useful:
real_mount()
check_mnt()
do_umount()
mntput_no_expire()

I'd look at how the autofs subsystem, i.e. the kernel automounter does it.

Related

Access image files by FUSE in userspace only

I am writing some tools that operate on files stored inside a disk image file under a users control. FUSE tools like fuse2fs are used to access file systems inside of partitions of those image files.
However, mounting those filesystems, and even accessing parts of it still require superuser privileges (and a lot of kernel work), as FUSE will still register the filesystem in kernel space, provide an /etc/mtab entry and VFS mount of course.
FUSE allows some configuration to allow a non-privileged user to mount such FS, but fuse2fs still enforces user rights.
So I wonder if something can be done to stop for example fuse2fs exporting the fs to kernel space at all, and just access files in userspace. Like a wrapper, used like this:
fusewrapper --fuse-cmd="fuse2fs foo.img /bar" ls /bar/ # lists "/" of foo.img's ext4 FS
fusewrapper --user=root --group=root --fuse-cmd="fuse2fs foo.img /bar" cat /bar/baz # prints "/baz" contents from foo.img's ext4 fs as root.
where /bar is a virtual mount path only valid inside of the fusewrapper universe and ls / cat internal fusewrapper command to list or cat the named path.
This tool would only use the kernel for access to foo.img and stdin / out, and can still impersonate any user (eg. root).
How could such a tool work? What is needed to get rid of FUSEs non-userspace parts (eg. kernel interface) ?

Checking Current File System with Perl

I need my perl script to check the file system type of the computer it's running on. What is the easiest way to do this? (on Linux)
There is a linux command df -T to determine filesystem
You can invoke it from your script and parse the output:
my $filesystem_info = `df -T`;
The only reliable way to do what you want is (a) decide which mount you are talking about and (b) find its entry in /proc/mounts.
On Linux, /proc/mounts lists all mounted file systems. The format of each line is "device mount-point fs-type mount-options'. It is human-readable; cat /proc/mounts and you should get the idea.
(Note that /etc/fstab only lists the file systems that get auto-mounted at boot time. That can be different than what is mounted at the time the script runs for all sorts of reasons, most notably automounters. /proc/mounts is what you want.)
You can try parsing the /etc/fstab file to find it out.
Beware there might be multiple filesystems in this file, you have to pick the one you want.

Is there a way to wait until root filesystem is mounted?

I have a statically linked code(not a module) in kernel that should launch kernel thread after root file system is mounted. The problem is I don't know how to do this without modifying prepare_namespace() kernel function. I thought it's possible to do via initcalls but
they're executed before kernel takes care about rootfs.
Does anyone know the best way to do this?
UPDATE [1]: #BenVoigit suggested the following solution in comments:
Seems like you should open /proc/mounts and poll_wait on it. See the source for `mounts_poll'
UPDATE [2]: I looked at RSBAC patches, RSBAC modifies prepare_namespace() function to make some actions after filesystem is mounted. It seems to be the easiest way.
Well, current Linux images are too big to fit the PC boot sector. Modern bootloaders like grub will mount an small filesystem in RAM before the real one.
To understand what is happening under the hood, you can open the disk image located under /boot. For example, in Ubuntu:
mkdir test
cd test
zcat /boot/initrd.img-2.6.35-24-generic > image.cpio
cpio -i < image.cpio
vim init
In the end, it's just a bunch of shell scripts - the simplicity is almost poetic.

Any way to eliminate time lag with sshfs?

If I rsync a directory with some remote directory under an sshfs
management, even if the rsync process finishes synching the
directory contents, there seems to be a time lag for the sshfs
directory to get updated. It is unintuitive and makes some
automation process relying on sshfs contents unstable.
How do I eliminate the lag?
-o cache=no in your sshfs options will turn off caching.
Note: no should be lower case: -o cache=NO should give you an error!

Howto disable password request on file creation on newly formatted hard disk

I have just formatted an external hard disk using this command:
$ sudo mkfs -t ext3 /dev/sdc1
However, once this is done, whenever I want to create a directory
or file in this hard disk, it always ask for password (i.e. permission requirement).
Is there a way I can disable that?
Note that I can't format the partition with mkfs unless I use sudo.
Please advice.
If you mount the partition at /media/foo, then try this:
sudo chmod 1777 /media/foo
It'll act like /tmp does at that point. So any user can create a folder / file in there, but you can only delete your own files.
I would recommend you asking the same question at ServerFault. I think you'll get a better answer over there.

Resources