linux下将一个文件mount为一个文件系统

原文:https://samindaw.wordpress.com/2012/03/21/mounting-a-file-as-a-file-system-in-linux/

Mounting a file as a file system in Linux

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

One Response to “Mounting a file as a file system in Linux”

  1. Nagilum Says:

    If you use truncate you can save some time and space. So for your 30MB file you can simply do:
    truncate -s 30M my_fs
    The file will be a sparse file which I believe you can also create using dd and skip=xx , but truncate seems a bit more straight forward to me.

Leave a Reply

:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值