Create a file
1. First step is to create a file of desired size. The following command will create a file that is 1 GB in size:# dd if=/dev/zero of=loopbackfile.img bs=100M count=10
10+0 records in
10+0 records out
1048576000 bytes (1.0 GB) copied, 1.26748 s, 827 MB/s
# du -sh loopbackfile.img
1000M loopbackfile.img
Create the loop device
1. Next step is to create a loop device with the file. Use the command “losetup” to create a loop device “loop0”# losetup -fP loopbackfile.img
-f – find the first unused loop device. If a file argument is present, use this device. Otherwise, print its name.
-P – force kernel to scan partition table on newly created loop device.
2. To print the loop device generated using the above command use “losetup -a”.
# losetup -a
/dev/loop0: [64769]:4199216 (/root/loopbackfile.img)
Create the filesystem
1. Now lets create a ext4 filesystem on the loopback device.# mkfs.ext4 /root/loopbackfile.img
mke2fs 1.42.9 (28-Dec-2013)
/root/loopbackfile.img is not a block special device.
Proceed anyway? (y,n) y
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
64000 inodes, 256000 blocks
12800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=262144000
8 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
Mount the loopback filesystem
1. We can now mount the loopback filesystem onto a directory. The “-o loop” additional option is used to mount loopback filesystems.# mkdir /loopfs
# mount -o loop /dev/loop0 /loopfs
# df -hP /loopfs/
Filesystem Size Used Avail Use% Mounted on
/dev/loop1 969M 2.5M 900M 1% /loopfs
# mount | grep loopfs
/dev/loop0 on /loopfs type ext4 (rw,relatime,seclabel,data=ordered)
Removing loop device
If you want remove the new filesystem, use the following steps:1. Umount and delete the directory /loopfs
umount /loopfs
rmdir /loopfs
# losetup -d /dev/loop0
# rm /root/loopbackfile.img
Enable Encryption on loop filesystem
‘losetup’ also allows to enable data encryption in order to get a crypted filesystem. The syntax to created a encrypted loop device si as shown below:# losetup [ -e encryption ] loop_device file
- NONE use no encryption (default).
- XOR use a simple XOR encryption.
- DES use DES encryption.
0 comments:
Post a Comment