• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to create partitions inside loopback device

by admin

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)
How to create virtual block device (loop device/filesystem) in Linux

Filed Under: CentOS/RHEL 5, CentOS/RHEL 6, CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. How To Add New Disk to An Existing Diskgroup on RAC Cluster or Standalone ASM Configuration
  2. debchange: command not found
  3. whois Command Examples in Linux
  4. Status Commands in Linux – date, ps, who, uptime, finger, rup, ruser
  5. How to Audit File Access on Linux
  6. How To Limit/Restrict FTP Commands On Vsftpd Services (CentOS/RHEL 6,7)
  7. What are the mount options to improve ext4 filesystem performance in Linux
  8. partx Command Examples in Linux
  9. uflash: command not found
  10. md5sum: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • protonvpn-cli Command Examples in Linux
  • protonvpn-cli connect Command Examples
  • procs Command Examples in Linux
  • prlimit: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright