Tuesday, March 25, 2008

Creating a Virtual File System and Mounting it with a Loopback Device.

Creating a Virtual File System and Mounting it with a Loopback Device.




 STEP 1 (Construct a 10MB file)

$ dd if=/dev/zero of=/tmp/disk-image count=20480

By default dd uses block of 512 so the size will be 20480*512

STEP 2 (Make an ext2 or ext3 file system) -- ext2 shown here.

$ mke2fs -q

or if you want ext3

$ mkfs -t ext3 -q /tmp/disk-image

yes, you can even use reiser, but you'll need to create a bigger
disk image. Something like "dd if=/dev/zero of=/tmp/disk-image count=50480".

$ mkfs -t reiserfs -q /tmp/disk-image

Hit yes for confirmation. It only asks this because it's a file


STEP 3 (Create a directory "virtual-fs" and mount. This has to be done as root)

$ mkdir /virtual-fs
$ mount -o loop=/dev/loop0 /tmp/disk-image /virtual-fs

SPECIAL NOTE: if you mount a second device you will have to increase the
loop count: loop=/dev/loop1, loop=/dev/loop2, ... loop=/dev/loopn

Now it operates just like a disk. This virtual filesystem can be mounted
when the system boots by adding the following to the "/etc/fstab" file. Then,
to mount, just type "mount /virtual-fs".

/tmp/disk-image /virtual-fs ext2 rw,loop=/dev/loop0 0 0

STEP 4 (When done, umount it)

$ umount /virtual-fs


SPECIAL NOTE: If you are using Fedora core 2, in the /etc/fstab you can take
advantage of acl properties for this mount. Note the acl next to the
rw entry. This is shown here with ext3.

/tmp/disk-image /virtual-fs ext3 rw,acl,loop=/dev/loop1 0 0

Also, if you are using Fedora core 2 and above, you can mount the file
on a cryptoloop.

$ dd if=/dev/urandom of=disk-aes count=20480


$ modprobe loop
$ modprobe cryptoloop
$ modprobe aes

$ losetup -e aes /dev/loop0 disk-aes
$ mkfs -t ext2 /dev/loop0
$ mount -o loop,encryption=aes disk-aes


If you do not have Fedora core 2, then, you can build the kernel from source
with some of the following options (not complete, yet)
reference:
http://cvs.sourceforge.net/viewcvs.py/cpearls/cpearls/src/posted_on_sf/acl/ehd.pdf?rev=1.1&view=log

Cryptographic API Support (CONFIG_CRYPTO)
generic loop cryptographic (CONFIG_CRYPTOLOOP)
Cryptographic ciphers (CONFIG_CIPHERS)
Enable one or more ciphers (CONFIG CIPHER .*) such as AES.


HELPFUL INFORMATION: It is possible to bind mount partitions, or associate the
mounted partition to a directory name.

# mount --bind /virtual-fs /home/mchirico/vfs

Also, if you want to see what filesystems are currently mounted, "cat" the
file "/etc/mtab"

$ cat /etc/mtab



Example with reiserfs file system

Assume you have a reisers files system created from a disk file, which means you have done something like the following:

# dd if=/dev/zero of=disk-rfs count=102400
# losetup /dev/loop4 ./disk-rfs
# mkfs -t reiserfs /dev/loop4
# mkdir /fs2
# mount -o loop,acl ./disk-rfs /fs2

Now, you can run reiserfstune. But, first you will need to umount fs2

# umount /fs2
# reiserfstune ./disk-rfs

Or you can run the debug command

# debugreiserfs -J ./disk-rfs

Now, suppose you run through a lot of the debug options on
http://www.namesys.com/ and you destroy this file.

You can recreate the file and delete the loop device.

# dd if=/dev/zero of=disk-rfs count=102400
# losetup -d /dev/loop4
# mount -o loop,acl ./disk-rfs /fs2

Now, try working with some of the ACL options - you can only do this
with the latest kernel and tools -- Fedora Core 2 will work.

Assume you have 3 users, donkey, chirico and bozo2. You can give
everyone rights to this file system as follows:

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

No comments: