Linking /proc/mnt to /proc/mounts

I understand that /proc filesystem reflects the output of various processes. Unfortunately, I have a propietary (romdump) binary that expects the mount table to appear as /proc/mtd, while my Android device appears to output it as /proc/mounts.

I’ve tried creating a symbolic link, but clearly this only works for actual files or directories. How can I fool this binary to read the output from /proc/mounts instead of /proc/mtd?

Asked By: Paul Lammertsma

||

Have you tried a hard link?

ln /proc/mnt /proc/mounts
Answered By: Mark Cohen

The /proc filesystem exists in memory so to speak. See http://www.kernel.org/doc/Documentation/filesystems/proc.txt for more info. Links probably won’t work.

You might be able to use LD_PRELOAD to intercept the open() function. See https://stackoverflow.com/questions/426230/what-is-the-ld-preload-trick

You might also be able to edit the binary to change the file name from /proc/mnt to /proc_mnt and symlink that to /proc/mounts .

Another option would be to recompile the kernel and make /proc/mnt an alias for /proc/mounts .

Answered By: Chad Clark

I don’t know of a way of adding things to /proc outside of writing a module (or plain kernel code). Might be some utilities out there though.

If you can build and insert a module, then it’s pretty simple: you can just create another symlink (/proc/mounts is a symlink already).

Source (mnt_link.c):

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>

#define MODULE_VERS "0.0"
#define MODULE_NAME "mnt_link"

static int __init init_mnt_link(void)
{
    static struct proc_dir_entry *symlink;
    symlink = proc_symlink("mnt", NULL, "self/mounts");
    if(!symlink)
        return -ENOMEM;
    return 0;
}

static void __exit cleanup_mnt_link(void)
{
    remove_proc_entry("mnt", NULL);
}

module_init(init_mnt_link);
module_exit(cleanup_mnt_link);

MODULE_AUTHOR("U&L");
MODULE_LICENSE("CC-WIKI");
MODULE_DESCRIPTION("Create a /proc/mnt symlink to /proc/self/mounts");

Makefile:

obj-m := mnt_link.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

(This assumes you’ll be building for your current Linux system. To build something for Android, you can refer to: How do you create a loadable kernel module for Android?.)

Once you’ve loaded the module (insmod mnt_link.ko), you should get:

$ ls -l /proc/m*nt*
lrwxrwxrwx 1 root root 11 Nov 27 22:43 /proc/mnt -> self/mounts
lrwxrwxrwx 1 root root 11 Nov 27 22:43 /proc/mounts -> self/mounts

That being said, that utility of yours might very well be expecting something else than this symlink. (Perhaps it depends on another module being loaded to provide some information at that location.)

Use at your own risks.

Answered By: Mat

The easiest way to do it would be to change the binary:

sed s-/proc/mtd-/tmp/mntx- < romdump > romdump.new
ln -s /proc/mounts /tmp/mntx
./romdump.new

The trick here, since you’re editing a binary, is to make sure the original string /proc/mtd is the same length as the new string /tmp/mntx, so that you don’t change the size or location of anything in the binary.

This is not foolproof—it will not work if the binary builds up the path name in pieces rather than using a fixed string. But it’s likely to do the trick.

Answered By: Jim Paris
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.