Use A File As A Linux Block Device
Just like when creating a SWAP file, you can create a file on a disk and present it as a block device. The block device would have a maximum file size of the backing file, and (as long as it’s not in use) be moved around like a normal file. For example, I could create a 1GB file on the filesystem and make Linux treat the file as a disk mounted in /dev/. And guess what – that’s what we’re going to do.
Create a file and filesystem to use as a block device
First off, use dd to create a 1GB file on an existing disk that we’ll use for our storage device:
dd if=/dev/zero of=/root/diskimage bs=1M count=1024
Then ‘format’ the file to give it the structure of a filesystem. For this example we’re going to use ext4 but you could choose any filesystem that meets your needs.
You’ll be promoted with Proceed anyway?. Type y and press return to proceed with the process.
mke2fs 1.42.5 (29-Jul-2012) /root/diskimage is not a block special device. Proceed anyway? (y,n) y Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536 inodes, 262144 blocks 13107 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done
Mounting a loop device
Before mounting the file we need to check that there is a free /dev/loopX loopback device that we can use to represent our new block device.
Run the below command, and if there is any output then check if it’s one of your loop devices, which will more than likely reference /dev/loop as the mounted device. If you do have a reference to our loop device then see the below section on Unmounting a loop device, or choose a number higher than the highest listed loop device, for example: usually there are several loop devices, starting with loop0 and going up in value to loop1, loop2, and so on.
cat /proc/mounts | grep /dev/loop
Once you have the file that you’d like to mount and a free loop device then you can go ahead and mount the file as a block device. You have two options:
- Mount the file as a block device only
- Mount the file as a block device and mount the filesystem of it on a local mount point (eg. /mnt/mymountpoint).
For option 1; to only mount the file as a device in /dev/, run the below command and change /root/diskimage to the path of the file you’d like to mount. loop0 can also be incremented as explained above.
losetup /dev/loop0 /root/diskimage
If you’d like this to be remounted after a machine reboot then add the above line to the rc.local file.
losetup /dev/loop0 /root/diskimage
For option 2; to mount the file and the filesystem on it, use the mount command. You must have already created the mount point locally before running the command, as you would when mounting a disk or NFS share.
Then run the mount command and specify the loop device, the path of the file and the path to mount the filesystem on:
mount -o loop=/dev/loop0 /root/diskimage /mnt/mymountpoint
To check the file has been mounted you can use the df command:
df -h | grep mymountpoint /dev/loop0 976M 1.3M 924M 1% /mnt/mymountpoint
Unmounting a loop device
If you’ve mounted the filesystem on the block device using the mount command then make sure it’s unmounted before proceeding.
To then free the loop0 device (or which ever loop device you’ve used) you’ll need the losetup command with the d switch.
File As Disk
next we must put a filesystem on it Note u can create different filesystem formats ‘mkfs.
[s0x45ker--_(+_+)_--SysAdmin ~]$ mkfs.ext4 imagefile mke2fs 1.45.6 (20-Mar-2020) Discarding device blocks: done Creating filesystem with 262144 4k blocks and 65536 inodes Filesystem UUID: 3ba526cc-875d-4315-ab6a-534fe7ac58ff Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done
Create Mount Point
Now lets mount the filesystem, firstly by creating a directory as mointpoint
[s0x45ker--_(+_+)_--SysAdmin ~]$ mkdir mntpoint # mount the filesystem [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo mount imagefile mntpoint/ # list the mount point [s0x45ker--_(+_+)_--SysAdmin ~]$ df -h | grep mntpoint /dev/loop0 976M 2.6M 907M 1% /home/s0x45ker/mntpoint # unmount the filesystem [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo umount mntpoint/ # returns nothing [s0x45ker--_(+_+)_--SysAdmin ~]$ df -h | grep mntpoint [s0x45ker--_(+_+)_--SysAdmin ~]$ # delete the directory [s0x45ker--_(+_+)_--SysAdmin ~]$ rmdir mntpoint [s0x45ker--_(+_+)_--SysAdmin ~]$
Using: ‘losetup’
check man pages of losetup and parted once again, you can reuse imagefile or create another one using ‘dd’
Check Free Loop Device
# display free loop device [s0x45ker--_(+_+)_--SysAdmin ~]$ losetup -f /dev/loop0
Setup Loop
# create an imagefile from it [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo losetup /dev/loop0 imagefile
Create Partition
# make partition label [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo parted -s /dev/loop0 mklabel msdos # create 3 partitions on the disk [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo parted -s /dev/loop0 unit MB mkpart primary ext4 0 256 Warning: The resulting partition is not properly aligned for best performance: 1s % 2048s != 0s [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo parted -s /dev/loop0 unit MB mkpart primary ext4 256 512 Warning: The resulting partition is not properly aligned for best performance: 500001s % 2048s != 0s [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo parted -s /dev/loop0 unit MB mkpart primary ext4 512 1024 Warning: The resulting partition is not properly aligned for best performance: 1000001s % 2048s != 0s
View Partition
# view the partitions [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo fdisk -l /dev/loop0 Disk /dev/loop0: 1 GiB, 1073741824 bytes, 2097152 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xcf49ab35 Device Boot Start End Sectors Size Id Type /dev/loop0p1 1 500000 500000 244.1M 83 Linux /dev/loop0p2 500001 1000000 500000 244.1M 83 Linux /dev/loop0p3 1000001 2000000 1000000 488.3M 83 Linux [s0x45ker--_(+_+)_--SysAdmin ~]$ ls -l /dev/loop0* brw-rw----. 1 root disk 7, 0 Apr 12 21:22 /dev/loop0 brw-rw----. 1 root disk 259, 0 Apr 12 21:22 /dev/loop0p1 brw-rw----. 1 root disk 259, 1 Apr 12 21:22 /dev/loop0p2 brw-rw----. 1 root disk 259, 2 Apr 12 21:22 /dev/loop0p3
Place Filesystem
# put filesystem on partitions [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo mkfs.ext4 /dev/loop0p1 mke2fs 1.45.6 (20-Mar-2020) Discarding device blocks: done Creating filesystem with 250000 1k blocks and 62744 inodes Filesystem UUID: 3b8ebc72-3a3f-46d3-9989-03f24184d248 Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185 Allocating group tables: done Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo mkfs.ext4 /dev/loop0p2 mke2fs 1.45.6 (20-Mar-2020) Discarding device blocks: done Creating filesystem with 250000 1k blocks and 62744 inodes Filesystem UUID: b6f68d7d-7aac-4bd5-86de-b40807f44a74 Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185 Allocating group tables: done Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo mkfs.ext4 /dev/loop0p3 mke2fs 1.45.6 (20-Mar-2020) Discarding device blocks: done Creating filesystem with 499713 1k blocks and 125416 inodes Filesystem UUID: 7029e10e-db19-4328-bb0b-c2162a2d2d7b Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done
Create Mountpoint
# creating directories for mounting [s0x45ker--_(+_+)_--SysAdmin ~]$ mkdir mnt1 mnt2 mnt3 # mount the filesystem on repective directories [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo mount /dev/loop0p1 mnt1 [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo mount /dev/loop0p2 mnt2 [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo mount /dev/loop0p3 mnt3 # check wether it exists [s0x45ker--_(+_+)_--SysAdmin ~]$ df -Th | grep "/dev/loop0*" /dev/loop0p1 ext4 233M 2.1M 215M 1% /home/s0x45ker/mnt1 /dev/loop0p2 ext4 233M 2.1M 215M 1% /home/s0x45ker/mnt2 /dev/loop0p3 ext4 465M 2.3M 434M 1% /home/s0x45ker/mnt3
Delete and Remove
# delete and remove after use [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo umount mnt1 mnt2 mnt3 [s0x45ker--_(+_+)_--SysAdmin ~]$ rmdir mnt* [s0x45ker--_(+_+)_--SysAdmin ~]$ sudo losetup -d /dev/loop0