August 22

Linux: Virtual Filesystem: Building A Linux Filesystem From An Ordinary File

You can take a disk file, format it as an ext2, ext3, or reiser filesystem, and then mount it, just like a physical drive. It’s then possible to read and write files to this newly-mounted device. You can also copy the complete filesystem, since it is just a file, to another computer. If security is an issue, read on. This article will show you how to encrypt the filesystem and mount it with ACL (Access Control Lists), which gives you rights beyond the traditional read (r), write (w), and execute (x) permissions for the three user groups “file”, “owner”, and “other”.

This is an excellent way to investigate different filesystems without having to reformat a physical drive, which means you avoid the hassle of moving all your data. This method is quick — very quick compared to preparing a physical device. You can then read and write files to the mounted device, but what is truly great about this technique is that you can explore different filesystems such as reiserfs, ext3, or ext2 without having to purchase an additional physical drive. Since the same file can be mounted on more than one mount point, you can investigate sync rates.

Creating a filesystem in this manner allows you to set a hard limit on the amount of space used, which, of course, will be equal to the file size. This can be an advantage if you need to move this information to other servers. Since the contents cannot grow beyond the file, you can easily keep track of how much space is being used.

First, you want to create a 20MB file by executing the following command:

      $ dd if=/dev/zero of=disk-image count=40960
      40960+0 records in
      40960+0 records out

You created a 20 MB file because, by default, dd uses a block size of 512 bytes. That makes the size: 40960*512=20971520.

      $ ls -l disk-image
      -rw-rw-r--    1 chirico  chirico  20971520 Sep  3 14:24 disk-image

Next, to format this as an ext3 filesystem, you just execute the following command:

      $ /sbin/mkfs -t ext3 -q disk-image
      mke2fs 1.32 (09-Nov-2002)
      disk-image is not a block special device.
      Proceed anyway? (y,n) y

You are asked whether to proceed because this is a file, and not a block device. That is OK. We will mount this as a loopback device so that this file will simulate a block device.

Next, you need to create a directory that will serve as a mount point for the loopback device.

      $ mkdir fs

You are now one step away from the last step. You just want to find out what the next available loopback device number is. Normally, loopback devices start at zero (/dev/loop0) and work their way up (/dev/loop1, /dev/loop2, … /dev/loopn). An easy way for you to find out what loopback devices are being used is to look into /proc/mounts, since the mount command may not give you what you need.

      $ cat /proc/mounts

      rootfs / rootfs rw 0 0
      /dev/root / ext3 rw 0 0
      /proc /proc proc rw,nodiratime 0 0
      none /sys sysfs rw 0 0
      /dev/sda1 /boot ext3 rw 0 0
      none /dev/pts devpts rw 0 0
      /proc/bus/usb /proc/bus/usb usbdevfs rw 0 0
      none /dev/shm tmpfs rw 0 0

On my computer, I have no loopback devices mounted, so I’m OK to start with zero. You must do the next command as root, or with an account that has superuser privileges.

      # mount -o loop=/dev/loop0 disk-image fs

That’s it. You just mounted the file as a device. Now take a look at /proc/mounts, you will see this is using /dev/loop0.

      $ cat /proc/mounts

      rootfs / rootfs rw 0 0
      /dev/root / ext3 rw 0 0
      /proc /proc proc rw,nodiratime 0 0
      none /sys sysfs rw 0 0
      /dev/sda1 /boot ext3 rw 0 0
      none /dev/pts devpts rw 0 0
      /proc/bus/usb /proc/bus/usb usbdevfs rw 0 0
      none /dev/shm tmpfs rw 0 0
      /dev/loop0 /home/chirico/junk/fs ext3 rw 0 0

You can now create new files, write to them, read them, and do everything you normally would do on a disk drive. First, I’ll give access to the chirico account.

      # chown -R chirico.chirico /home/chirico/junk/fs

Now, under the chirico account, it is possible to create files.

      $ cd /home/chirico/fs
      $ mkdir one two three
      $ ls -l

      total 15
      drwx------    2 chirico  chirico     12288 Sep  3 14:28 lost+found
      drwxrwxr-x    2 chirico  chirico      1024 Sep  3 14:34 one
      drwxrwxr-x    2 chirico  chirico      1024 Sep  3 14:34 three
      drwxrwxr-x    2 chirico  chirico      1024 Sep  3 14:34 two

      $ df -h

      Filesystem            Size  Used Avail Use% Mounted on
      /dev/sda2              17G   11G  4.6G  71% /
      /dev/sda1              99M   83M   11M  89% /boot
      none                   62M     0   62M   0% /dev/shm
                             20M  1.1M   18M   6% /home/chirico/junk/fs

If you need to umount the filesystem, as root, just issue the umount command. If you need to free the loopback device, execute the losetup command with the -d option. You can execute both commands as follows:

      # umount /home/chirico/junk/fs
      # losetup -d /dev/loop0

Using RWX — The Old Way To Collaborate

Before we get started with ACL, how would you set up rights on the filesystem so that users could create and save documents that others could modify? For instance, let’s say that users chirico and sporkey are collaborating on a project together.

Well, you have to add everyone to the same group. You would execute commands like these:.

      # groupadd sharefs
      # chown -R root.sharefs /home/chirico/junk/fs
      # chmod 2775 /home/chirico/junk/fs
      # usermod -G sharefs sporkey
      # usermod -G sharefs chirico

Note that if these changes do not take effect for your users (for example, if they were logged in when you executed the commands), they’ll have to log out and log in again or execute the “$ newgrp sharefs” command. No big deal, right? Well, keep reading, and see how ACL avoids this step.

More importantly, even though the old way worked for you, at some point, new users may need to be added to the project. What if some of these users only need a subset of the rights? For instance, you have developers, testers, managers, and a few special people. There are limits to what the rwx type rights can do. ACL solves a lot of these problems.

ACL, Reiserfs, and AES Encryption: The 2.6 Kernel

For the next steps, I will assume that you are running Red Hat Fedora Core 2. If not, reference the 2.6 kernel upgrade section below. Four things will be covered in this section:


  • Create A File With Random Data
  • Set Up An AES Encrypted Loopback Device With Password
  • Build A Reiser Filesystem On The Loopback Device
  • Mount With ACL Capabilities

Your installation of Fedora Core 2, by default, will be configured for loop, cryptoloop, and aes, but it is highly unlikely that you will have all of these modules loaded. So, execute the following commands to load these modules (you will need to do this as root):

      # modprobe loop
      # modprobe cryptoloop
      # modprobe aes

Next, create a directory to store the files. The Reiser filesystem will require more space than the ext3 filesystem.

      # mkdir /home/diskimg
      # cd /home/diskimg

Instead of creating the file zeroed out, like you did with the ext3 filesystem, this one is going to contain random bits, which may add a little extra security.

      # dd if=/dev/urandom of=disk-aes count=102400

We need to encrypt the loop device, so you need to use losetup. You will be prompted for a password, which you will need to remember when you mount the device.

      # losetup -e aes /dev/loop1 ./disk-aes

This step is new also. Instead of formating the file directly, you will format the loop device. The file stays encrypted. Again, you will be prompted to continue, so just enter “y”.

      # mkfs -t reiserfs /dev/loop1

      mkfs.reiserfs 3.6.13 (2003

      A pair of credits:
      Elena Gryaznova performed testing and benchmarking.

      The  Defense  Advanced  Research  Projects Agency (DARPA, is the
      primary sponsor of Reiser4.  DARPA  does  not  endorse  this project; it merely
      sponsors it.

      Guessing about desired format.. Kernel 2.6.8-1.521 is running.
      Format 3.6 with standard journal
      Count of blocks on the device: 12800
      Number of blocks consumed by mkreiserfs formatting process: 8212
      Blocksize: 4096
      Hash function used to sort names: "r5"
      Journal Size 8193 blocks (first block 18)
      Journal Max transaction length 1024
      inode generation number: 0
      UUID: 435e3495-5e2e-489d-bf55-1b5f9a44b670
              ALL DATA WILL BE LOST ON '/dev/loop1'!

      Continue (y/n):y
      Initializing journal - 0%....20%....40%....60%....80%....100%

      Tell your friends to use a kernel based on 2.4.18 or later, and especially not a
      kernel based on 2.4.9, when you use reiserFS. Have fun.

      ReiserFS is successfully created on /dev/loop1.

Create the mount point /fs, and mount this device. Note that you will be entering the acl option as well. Plus, you will prompted for a password.

      # mkdir /fs
      # mount -o loop,encryption=aes,acl ./disk-aes /fs

Ok, now take a look at the mount command. It should show up as the Reiser filesystem, encrypted, using ACL. Note that it says loop2; it mounted it on /dev/loop2, which is one above what losetup specified, /dev/loop1.

      $ mount
      /home/diskimg/disk-aes on /fs type reiserfs (rw,loop=/dev/loop2,encryption=aes,acl)

Exploring ACL

With ACL (Access Control Lists), you have finer control over access permissions. With the rwx permission scheme, you cannot easily change rights without creating new groups to handle the users. With ACL, you can set user permissions without creating a group, and individual users can add or remove access.

These rights are set with the setfacl command. The command below will give the users donkey, chirico, and bozo2 access to this new filesystem that we mounted. Again, I’m assuming that you are using Fedora Core 2, or some distribution that is set up for ACL.

# setfacl -R -m d:u:donkey:rwx,d:u:chirico:rwx,d:u:bozo2:rwx /fs

Next, create a few directories as one of the users. The example below was done as the user chirico.

      $ mkdir /fs/one
      $ touch /fs/one/stuff
      $ ls -l /fs/one/stuff
      -rw-rw----+ 1 chirico chirico 0 Sep  3 17:48 /fs/one/stuff

Notice the plus sign in the last line. It tells us a little about who has access. So, as user chirico, the getfacl command can be executed:

      $ getfacl /fs/one/stuff

      getfacl: Removing leading '/' from absolute path names
      # file: fs/one/stuff
      # owner: chirico
      # group: chirico
      user:chirico:rwx                #effective:rw-
      user:donkey:rwx                 #effective:rw-
      user:bozo2:rwx                  #effective:rw-
      group::r-x                      #effective:r--

We now see that donkey, chirico, and bozo2 have effective rights on this file. Chirico has enough rights to remove bozo2.

      $ setfacl -x u:bozo2 /fs/one/stuff
      $ getfacl /fs/one/stuff
      getfacl: Removing leading '/' from absolute path names
      # file: fs/one/stuff
      # owner: chirico
      # group: chirico

This is just scratching the surface of what can be done with ACL. For more information, see some of the references below.

2.6 Kernel Upgrade

This article will get you started with the 2.6 kernel if you are currently running Red Hat 8 or 9. You may want to take a look at it to see what is involved. If you decide to upgrade, you will need to configure your kernel for the following:


This is done in the .config file, and you can download my config file here. Just look for kernel- in the tar.gz.

In addition to upgrading the kernel, you will need the latest version of the Linux utilities. Currently, there is no need to patch this version. In the past, there was a patch, but this version worked fine for me.

You will also need the Reiser tools.

By: Mike Chirico

Copyright 2021. All rights reserved.

Posted August 22, 2011 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B