Posted: . At: 9:35 AM. This was 4 years ago. Post ID: 13799
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to create a loopback filesystem on Linux and store data in it.


Linux can be used to create a nice filesystem inside a file. This can be used to store data on it. This could be used as a bootable filesystem, or whatever you wish.

Firstly, run this command to create a 512-megabyte file system.

4.4 Tue Nov 26 jason@Yog-Sothoth 0: $ dd if=/dev/zero bs=1M count=512 > out.img
512+0 records in
512+0 records out
536870912 bytes (537 MB, 512 MiB) copied, 0.232721 s, 2.3 GB/s

Then I create a new partition on the file-system. I firstly print the help of fdisk, so I know what to do.

4.4 Tue Nov 26 jason@Yog-Sothoth 0: $ sudo fdisk out.img 
1) All commands run with root privileges are always dangerous.
2) Never run commands on an environment you are not willing to destroy, or able to restore.
3) Do not become root until you know what you are going to do.
4) Be sure of your command and what is going to be affected by it.
[sudo] password for jason: 
 
Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
 
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x22eb7384.
 
Command (m for help): m
 
Help:
 
  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag
 
  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition
 
  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)
 
  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file
 
  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes
 
  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

Then press “n” to create a new primary partition. Then press “w” to save and exit.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-1048575, default 2048): 2048
Last sector, +sectors or +size{K,M,G,T,P} (2048-1048575, default 1048575): 
 
Created a new partition 1 of type 'Linux' and of size 511 MiB.
 
Command (m for help): w
The partition table has been altered.
Syncing disks.

Now, using the fdisk -lu command, the filesystem is visible.

4.4 Tue Nov 26 jason@Yog-Sothoth 0: $ sudo fdisk -lu out.img 
Disk out.img: 512 MiB, 536870912 bytes, 1048576 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: 0x22eb7384
 
Device     Boot Start     End Sectors  Size Id Type
out.img1         2048 1048575 1046528  511M 83 Linux

This cannot be mounted easily, as it is a loopback file-system. So we must setup a device for it first.

4.4 Tue Nov 26 jason@Yog-Sothoth 0: $ sudo losetup --partscan --show --find out.img 
/dev/loop11

Then format the device to allow us to copy files to it.

4.4 Tue Nov 26 jason@Yog-Sothoth 0: $ sudo mkfs.ntfs /dev/loop11
The partition start sector was not specified for /dev/loop11 and it could not be obtained automatically.  It has been set to 0.
The number of sectors per track was not specified for /dev/loop11 and it could not be obtained automatically.  It has been set to 0.
The number of heads was not specified for /dev/loop11 and it could not be obtained automatically.  It has been set to 0.
Cluster size has been automatically set to 4096 bytes.
To boot from a device, Windows needs the 'partition start sector', the 'sectors per track' and the 'number of heads' to be set.
Windows will not be able to boot from this device.
Initializing device with zeroes: 100% - Done.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.

Now I can mount the device and copy files to it. I do not need to be root to do this either.

4.4 Tue Nov 26 jason@Yog-Sothoth 0: $ sudo mount /dev/loop11 /mnt/contents/

This is how easy it is to create a loopback filesystem. This could be very useful, of course, if you want to get Windows to boot from this device, you would need extra steps, but there are other ways to create a proper filesystem on Linux for use with QEMU.

How to install Windows 7 in a QEMU KVM virtual machine and enjoy a fast Windows desktop in a window.
https://securitronlinux.com/bejiitaswrath/how-to-install-windows-7-in-a-qemu-kvm-virtual-machine-and-enjoy-a-fast-windows-desktop-in-a-window/.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.