09:58 am: Virtual Filesystem (Build it fm an ordinary file)
http://freshmeat.net/articles/view/1387/[snip] from the above website
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
/home/chirico/junk/disk-image
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