Thursday, March 22, 2012

Virtual file system in Linux

如果你想要在Linux系統中從一個既有的partition去虛擬另一個partition的話,Virtual file system是一個蠻不錯的小技巧。

Step1. 建立一個空檔:
[root@localhost ruckus]# cd /tmp/
[root@localhost tmp]# dd if=/dev/zero of=vs_file bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB) copied,0.767225 秒,700 MB/s
Step2. 將空檔格式化成虛擬的檔案系統:
[root@localhost tmp]# mke2fs -j vs_file
mke2fs 1.39 (29-May-2006)
vs_file is not a block special device.
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
64 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
Step3. 以一般掛載partition的方法掛載虛擬檔案系統:
[root@localhost tmp]# mount -o loop /tmp/vs_file /mnt/vf/

Step4. 驗證一下:
[root@localhost tmp]# mount | grep vs_file
/tmp/vs_file on /mnt/vf type ext3 (rw,loop=/dev/loop0)
[root@localhost tmp]# cd /mnt/vf/
[root@localhost vf]# ls
lost+found
[root@localhost vf]# touch test
[root@localhost vf]# ls
lost+found test

No comments: