Filesystem inside a file, hide a partition or file system in free space, create an ISO file of a directory

How to make a file system inside a file

dd if=/dev/zero of=bigfile count=10 bs=1M

# ext2 version
mke2fs ./bigfile
mount ./bigfile /mnt/tmp/ -o loop

# reiserfs version
mkreiserfs -f ./bigfile -b 512
mount ./bigfile /mnt/tmp/ -o loop -t reiserfs

How to hide a partition in free space on a block device

This will create a template image for the partition to hide using dd, then this image will be transferred to the free space and mounted as a loop device.

Find a suitable block device with unused space.

If the device is not completely empty, find a suitable location that is marked as free space, go by size to find the location in MB or GB where it should be safe to begin the partition.

Check cfdisk for an overview: cfdisk /dev/sdXY

For example, if a disk with a total of 10GB is partitioned as:
/dev/sda1 5GB
Free space 5GB

Then the Free space of 5GB is usable here.

Create the partition as a file - this makes one that is 10 MB:
dd if=/dev/zero of=bigfile bs=1M count=10

Format the partition:
mkfs.ext4 ./bigfile

Transfer the empty partition to the empty space on the disk beginning at 6 GB on the disk, note the block size and seek part, without them it will overwrite sda:
dd if=bigfile of=/dev/sda bs=1M seek=6144

Prepare a directory for the mount point of the partition:
mkdir -p /mnt/hidden

Mount the partition starting at 6 GB:
mount /dev/sda /mnt/hidden -o loop,offset=6442450944

The partition is now available at /mnt/hidden

Unmount as usual with:
umount /mnt/hidden

Hide a file system with multiple partitions in free space

This will use a loop device to select a byte range on a block device that can be managed as if the range was a regular block device. Viewing the block device in a partition manager should show free space where the data is. This is similar to the previous method, but it will not require copying in a template image file.

Same as above, find a suitable block device and a size.

Mount a part of block device /dev/sda, beginning 6 GB in and with a size of 100 MB on loop device /dev/loop0:
losetup -o6442450944 --sizelimit=104857600 /dev/loop0 /dev/sda

Option 1 - a single partition in the loop device

Create the file system in the loop device:
mkfs.ext4 /dev/loop0

Create mount point:
mkdir -p /mnt/hidden

Mount it:
mount /dev/loop0 /mnt/hidden

Unmount it:
umount /dev/loop0

Remove loop device:
losetup -d /dev/loop0

To remount it, repeat:
losetup -o6442450944 --sizelimit=104857600 /dev/loop0 /dev/sda
mount /dev/loop0 /mnt/hidden

Option 2 - a file system in the loop device

Create one or more partitions using cfdisk:
cfdisk /dev/loop0

The partitions will be named /dev/loop0p1..N

Use partprobe (apt install parted if missing) to scan for the partitions in the loop device:
partprobe /dev/loop0

Format a partition:
mkfs.ext4 /dev/loop0p1

Make a directory for the mount point:
mkdir -p /mnt/hidden

Mount a partition:
mount /dev/loop0p1 /mnt/hidden

Unmount a partition:
mount /mnt/hidden

Unmount the loop device:
losetup -d /dev/loop0

Or unmount all loop devices:
losetup -D

To re-mount it repeat:

losetup -o6442450944 --sizelimit=104857600 /dev/loop0 /dev/sda
partprobe /dev/loop0
mount /dev/loop0p1 /mnt/hidden

Create an ISO file of a directory

# Create an ISO of a directory, -J makes joliet - filenames more than 8.3
mkisofs -o thefile.iso -J directory

# Mount an ISO
mount -o loop -t iso9660 thefile.iso /mnt/test

This is a personal note. Last updated: 2021-01-16 04:27:26.


Don't forget to pay my friend a visit too. Joakim