Using the Linux loop device
create a disk for mounting
A 1GB disk:
# dd bs=1024k count=1000 if=/dev/zero of=/storage/disk1 (or faster (no zeros used): truncate -s1000M /storage/disk1)
Partition:
# fdisk /storage/disk1
Result (i.e. with one partition):
Device Boot Start End Blocks Id System /storage/disk1p1 2048 2047999 1022976 83 Linux
use loop device for mounting
Source: http://unix.stackexchange.com/questions/87183/creating-formatted-partition-from-nothing
If on Linux, when loading the loop module, make sure you pass a max_part option to the module so that the loop devices are partitionable.
Check the current value:
cat /sys/module/loop/parameters/max_part
If it’s 0:
modprobe -r loop # unload the module modprobe loop max_part=31
To make this setting persistent, add the following line to /etc/modprobe.conf or to a file in /etc/modprobe.d if that directory exists on your system:
options loop max_part=31
If modprobe -r loop fails because “Module loop is builtin”, you’ll need to add loop.max_part=31 to your kernel command line and reboot. If your bootloader is Grub2, add to it to the value of GRUB_CMDLINE_LINUX in etc/default/grub.
Use losetup to create a loopback device
# losetup /dev/loop0 /storage/disk1 # fdisk -l /dev/loop0 Device Boot Start End Blocks Id System /dev/loop0p1 2048 2047999 1022976 83 Linux
Create filesystem
# mkfs.ext3 -m0 /dev/loop0p1 # mount /dev/loop0p1 /mnt
Check:
# df -h /mnt Filesystem Size Used Avail Use% Mounted on /dev/loop0p1 984M 18M 967M 2% /mnt
Ok!