Linux disk partitions using loopback file and loop device

1. Create a 1GB loopback file:

  • dd if=/dev/zero of=imgfile bs=1M count=1024

Note: For single partition, you can skip the following steps and directly run the commands:
mkfs.ext4 imgfile; mkdir mnt; mount imgfile mnt

2. Create partitions:
Let us create a 512 MB primary partition and a 512 MB extended partition. Inside the extended partition, we will create two logical partitions each of 256 MB size.

  1. fdisk imgfile
  2. Enter n to create a new primary partition
  3. Partition type -> p
  4. Partition number -> press Enter (to select default value)
  5. First sector -> press Enter (to select default value)
  6. Last sector -> +512MB
  7. Enter n again to create a new extended partition
  8. Partition type -> e
  9. Partition number -> press Enter (to select default value)
  10. First sector -> press Enter (to select default value)
  11. Last sector -> press Enter (To assign all remaining size i.e 512 MB)
  12. Enter n again to create 1st logical partition
  13. Partition type -> l
  14. Partition number -> press Enter (to select default value)
  15. First sector -> press Enter (to select default value)
  16. Last sector -> +256MB
  17. Enter n again and follow the above steps 13-16 to create 2nd logical partition
  18. Enter p to print the partition table
  19. Enter w to save and exit

3. Print partitions:

  • partprobe -s imgfile
  • parted imgfile print; fdisk -l imgfile  

4. Assign the loopback file to a loopback device:

  • losetup -f imgfile
  • losetup -l; ls -l /dev/loop*
  • fdisk -l /dev/loop0parted /dev/loop0 print

5. Format partitions:

  • partprobe -s /dev/loop0
  • mkfs -t ext4 /dev/loop0p1; mkfs.ext4 /dev/loop0p5; mkfs -t xfs /dev/loop0p5
  • parted /dev/loop0 print

6. Mount loopback file partitions:

  • mkdir mnt1 mnt2 mnt3
  • mount /dev/loop0p1 mnt1; mount /dev/loop0p5 mnt2; mount /dev/loop0p6 mnt3
  • chmod a+rw mnt1 mnt2 mnt3
  • df -hT

7. Unmount partitions and delete loopback file (optional): 

  • umount mnt1 mnt2 mnt3
  • losetup -d /dev/loop0
  • rm imgfile

About Ishtiaque

I am IBM Certified Infrastructure Systems Architect, Linux Foundation Certified System Administrator, Oracle Certified Programmer in Java and Web Component Developer, and TOGAF 9 certified with over 10 years of support and development experience in IBM middleware software and Java. Additionally, have a sound grip in databases and OpenStack administration. I hold the following certifications: IBM Certified Infrastructure Systems Architect Linux Foundation Certified System Administrator (LFCS) TOGAF 9 Certified Oracle Certified Expert, Java EE6 Web Component Developer Oracle Certified Professional – Java 6 Programmer ITIL v3 Foundation Certified IBM Certified Solution Architect – Cloud Computing Infrastructure V1 IBM Certified System Administrator – WebSphere Portal V8, V7, V6.1, V6 IBM Certified System Administrator – WebSphere Application Server V7, V6.1 IBM Certified System Administrator – AIX V7 IBM Certified System Administrator – WebSphere MQ V7 IBM Certified Deployment Professional – Business Process Manager Advanced V7.5 IBM Certified Solution Advisor – Cloud Computing Architecture V3 IBM Certified Solution Developer – WebSphere Portal V5.1
This entry was posted in Linux and tagged . Bookmark the permalink.