linux下將一個檔案mount為一個檔案系統
阿新 • • 發佈:2019-01-30
This is called mounting a loopback device.
3 steps.- Creating the file that can be mounted as a file system
- Assigning the file as a block device
- Mounting the block device to a path
$ 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 $ losetup /dev/loop0
$ 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