if there is no sysfs in /etc/fstab, how sysfs is mounted? - filesystems

If I type $mount on shell, it shows several mount point
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sys on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
dev on /dev type devtmpfs (rw,nosuid,relatime,size=3030352k,nr_inodes=757588,mode=755)
....
....
tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=606608k,mode=700,uid=1000,gid=1000)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,relatime)
I searched web and it says i could setup mount point by editing fatab.(like sysfs /sys sysfs noauto 0 0)
but in my /etc/fstab, there is no such description about sysfs, it only has descrption about /dev/sda1.
Then, how does my linux understand sysfs mount point? and who mounts it?

FYI:
Does sysfs procfs devtmpfs mounted by kernel?
systemd tries to mount, but earlier than that, initramfs mount it.

Related

Identify removable devices from mount point in kernel module

I'm writing a kernel module and I want to know if a given mount point is associated with a removable device. Is there any way to do that?
For example, when I list all the mounts using cat /proc/mounts I get something like this:
/dev/nvme0n1p7 / ext4 rw,relatime,errors=remount-ro 0 0
/dev/nvme0n1p2 /boot/efi vfat rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0 0
/dev/sdb1 /media/mosa/DELLRESTORE vfat rw,nosuid,nodev,relatime,uid=1001,gid=1001,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro 0 0
...
So I have 3 mount points /, /boot/efi, and /media/mosa/DELLRESTORE.. the 3rd one is a removable device.
I want a way in a kernel module, given a mount point (e.g. /media/mosa/DELLRESTORE) tell me if it's associated with a removable device or not.

How to get mount point for a given path in custom linux kernel module

As the title says I have a problem with my custom kernel module. My goal is to retrieve the root mount point of a given path. Just like using df:
df "/tmp/some_dir/some_file"
vvvvvvvv out vvvvvvvv
Filesystem 1K-blocks Used Available Use% Mounted on <---
tmpfs 8125932 120 8125812 1% /tmp <----
I was trying to achieve it with path struct by using both mnt and dentry fields but when I printed mnt_root or superblock root it always returned '/' so not associated mounting point.
Maybe I'm just digging in the wrong place. I don't know if path/dentry/etc... are suitable structures for these operations (I'm beginning my adventure with kernel modules and probably I'm overkilling it :D)
Kernel version: 5.6.2
Thanks in advice!
I have managed to reach my goal!
It turns out that there is follow_up function in namei.c which does exactly what I needed.

Mounting ext4 mounts as ext2 in linux 4.4.0 [RHEL]

commands I executed
mkfs .ext4 -F /dev/xxx0
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
524288 inodes, 2097152 blocks
104857 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
mount -o dax /dev/xxx0 /mnt/ext4-xxx0
mount output
/dev/xxx0 on /mnt/ext4-xxx0 type ext2 (rw,relatime,seclabel,block_validity,barrier,dax,user_xattr,acl)
dmesg output
[ 2917.044866] EXT4-fs (xxx0): DAX enabled. Warning: EXPERIMENTAL, use at your own risk
[ 2917.044869] EXT4-fs (xxx0): mounting ext2 file system using the ext4 subsystem
...
...
[ 2917.045459] EXT4-fs (xxx0): mounted filesystem without journal. Opts: dax
which subsequently results in failure of fallocate, as the ext2 is not supported fos ext2.
any suggestions/solution would be appreciated.
Provide the type as ext4 while mounting. Or else it will take default type (or) type mentioned in /etc/fstab.
# mount -o dax -t ext4 /dev/xxx0 /mnt/ext4-xxx0

Accessing file system through ioctl function

Recently, I developed a simple file system kernel module.
So, I needed to assign my own ioctl function (.unlocked_ioctl) to the file_operation structure to implement specific commands to my file system module. The Ext4 file system has its own ioctl function, for example.
Then, I created a file using the dd command and mounted it:
# mount -t myfs -o loop simple_file /mnt/
Everything works fine, but how can I access this file system using ioctl with a user space program?
I tryed to do ioctl(fd, MY_COMMAND_1, &my_struct_t); (where fd is the file descriptor of the dev file /dev/loop[0..7]), but it returns me Invalid argument.
If you open /dev/loop0, you're accessing a loop device, and therefore you're talking to the loop driver.
The ioctl handler that you've registered for your filesystem applies to files opened on a mounted filesystem.
fd = open("/mnt/something", O_RDWR);
ioctl(fd, MY_COMMAND_1, &my_struct_t);

Finding the description of the device connected to /dev/input/eventX

I have a program that is listening to a certain event file handle. Is there a file I can read to get details about the specific event's device that I am listening to?
Assuming that (a) you're on Linux and (b) you have sysfs mounted (typically on /sys), you can look at /sys/class/input/eventX. This will be a symlink into the device tree; this should provide you some device details. For example:
$ readlink /sys/class/input/event4
../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.1/input/input4/event4
For USB devices, you could then probably mount a usbfs filesystem and check out the devices file for more information.
Do you have access to the file descriptor or is this an external program? If this is your fd to the actual device, a list of ioctls provides you with most info you'll need. Have a look at print_device_info from evtest, it does exactly that:
http://cgit.freedesktop.org/evtest/tree/evtest.c#n753

Resources