1. 程式人生 > >linux下將一個檔案mount為一個檔案系統

linux下將一個檔案mount為一個檔案系統

This is called mounting a loopback device.

3 steps.
  1. Creating the file that can be mounted as a file system
  2. Assigning the file as a block device
  3. Mounting the block device to a path
Creating the file that can be mounted as a file system Use the dd command to create an empty file of the size which you want your file system.   $ dd if=/dev/zero of=my_fs bs=1024 count=30720
of – parameter specifies the name of the file bs – size of the block (represents x no of KB. You can specify x no of MB using xM: i.e. “bs=1M”) count – how many blocks to be present (thus the size of the file system = bs*count) if – from which device the file content initially should be filled with. But still the file that gets created is not a proper filesystem yet. you need to format it using some file system type. In order to do that first lets assign the file to a loop back device (the next step) Assigning the file as a block device
In linux a file system has to be a block device in order for it to be managed as a file system. In this particular case we use a set of defined loop back devices for our purpose. eg: /dev/loop0 up to /dev/loop8 in order to check if a particular loop back device is already being used use the command,   $ losetup /dev/loop0
If its already being used you will see it mentioned in the output of the above command. Once you find a free loop back device to attach/assign the file to it   $ losetup /dev/loop0 my_fs This command needs to be issued as a sudoer user. Now lets format the loopback device we just attached.  $ mkfs -t ext3 -m 1 -v /dev/loop0 Effectively we are creating a filesystem inside a file using the above command. I’m formatting it as ext3 file system. You may have to use sudo again. Mounting the block device to a path The hard work is done. Now to just mount.   $ mount -t ext3 /dev/loop0 /mnt/fs_mount_location/ You may ommit the “-t ext3” parameter in this case since usually linux file systems are automatically detected at mount. You may have to sudo the above command.  Thats it actually. Once you are done you should clear up after you ofcourse. to do that just 2 commands   $ umount /mnt/fs_mount_location/ to unmount. you may need sudo again   $ losetup -d /dev/loop0 to detach the file from the loop back device. Need sudo again. Advertisements