How to create partitions inside loopback device?

In most of the situations you would simply create a loopback device using “losetup” and mount it using the “-o loopback” options. But if you want to create a loopback file, want to partition it, and finally mount a subpartition, this option can not be used. Let see how you can create partitions inside a loopback image.


Creating loopback device


1. First create a file of size around 1GB using “dd” command.

# 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


2. Create a loopback device on top of the file created in the above step.

# losetup -fP loopbackfile.img


3. To print the loop device generated using the above command use “losetup -a”.

# losetup -a
/dev/loop0: [64769]:4199216 (/root/loopbackfile.img)


Creating partitions inside loopback image using fdisk


1. Use the fdisk command to create partitions on the loopback device /dev/loop0. A primary parition of size 500MB is created as shown below.

# fdisk /dev/loop0
Welcome to fdisk (util-linux 2.23.2).

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
Building a new DOS disklabel with disk identifier 0x4d455ea1.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2047999, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2047999, default 2047999): +500M
Partition 1 of type Linux and of size 500 MiB is set


2. Save the partition table and exit out of the fdisk utility.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


Creating filesystem and mounting it


1. Create the ext4 filesystem on the /dev/loop0p1 partition created in the above step.

# mkfs.ext4 /dev/loop0p1
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done                            
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
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


2. Mount the filesystem on the desired directory.

# mkdir /loopfs
# mount -o loop /dev/loop0p1 /loopfs


3. Verify the size of mount point and the filesystem type.

# df -hP  /loopfs
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop1      477M  2.3M  445M   1% /loopfs


# mount | grep loopfs
/dev/loop0p1 on /loopfs type ext4 (rw,relatime,seclabel,data=ordered)


Was this answer helpful? 0 Users Found This Useful (0 Votes)