List all mount points in kernel module

691 Views Asked by At

I'm trying to list all the mount points in a kernel module. The method in this answer doesn't list all the mount point, and this answer is for older kernel version. I'm working on kernel version 4.15.0.

I found this method (iterate_mounts) and wrote this simple kernel module.

int mount_f(struct vfsmount *mnt, void *arg)
{
  char *tmp, *path;
  if(mnt == NULL || mnt->mnt_root == NULL)
  {
    return 0;
  }
  tmp = (char*)__get_free_page(GFP_KERNEL);
  path = dentry_path_raw(mnt->mnt_root, tmp, PAGE_SIZE);
  if(!IS_ERR(path))
  {
    pr_info("mounted path=%s\n", path);
  }
  free_page((unsigned long)tmp);
  return 0;
}

int init_module(void)
{
  struct path path;
  struct vfsmount *root_mnt;
  int err;
  err = kern_path("/", 0, &path);
  if (err)
  {
    return -1;
  }

  root_mnt = path.mnt;
  path_put(&path);
  if (IS_ERR(root_mnt))
  {
    return -1;
  }

  iterate_mounts(mount_f, NULL, root_mnt);
  return 0;
}

but it doesn't print the correct path (it prints just "/" for all mounts). Am I missing something here?

Also, which methods gets called when a new mount is mounted or unmounted? I'm assuming for unmounting (do_umount) gets called?

Edit: another question, how can I identify the mount if it's removable or not? any flags in the struct vfsmount or struct super_block that can help me identify that?

0

There are 0 best solutions below