Virtual Filesystem


Table of Contents

Virtual Filesystem: A Linux Filesystem From An Ordinary File 
Synopsis 
Implementing Encrypted Home Directories 
a vanilla loopback filesystem 
encrypted filesystem 
Additional Pull Quotes 
Portable Loopback-Encrypted-Filesystem 
Portable Loopback-Encrypted-Filesystem 
Portable Loopback-Encrypted-Filesystem 
Portable Loopback-Encrypted-Filesystem 

Virtual Filesystem: A Linux Filesystem From An Ordinary File 

http://freshmeat.net/articles/view/1387/

by Mike Chirico, in Tutorials - Nov 27th 2004

Also available at http://linuxgazette.net/109/chirico.html

Latest Update: http://prdownloads.sourceforge.net/souptonuts/README_Virtual_FS.html?download Date: Tue Jun 14 11:45:52 EDT 2005

Synopsis 

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".

First, 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, create a directory that will serve as a mount point for the loopback device.

$ mkdir fs

Last step, 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.

grep loop /proc/mounts

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.

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

Next we start with ACL […]