- [Narrator] Welcome to the next video of section three,
using loopback files.
In the previous video,
we've learned about enumerating file type statistics.
In this video, we'll create logical disks inside
a file on your physical disk.
Loopback filesystems are very interesting components
of Linux-like systems.
We usually create filesystems on devices,
for example disk joint partitions.
These storage devices are available as device files,
such as slash dev slash device name.
In order to use the storage device filesystem,
we mount it at a directory called a mount point.
On the other hand,
loopback filesystems are those that we create in files,
rather than a physical device.
We can then mount those files
as filesystems at a mount point.
Let's see how to create an EXT four filesystem
on a file of size one gig.
The following command will create a file
that is one gig in size.
You can see that the size of
the created file exceeds one gigabyte.
This is because the hard disk is a block device,
and hence storage must be allocated
by integral multiples of block size.
Now, format the one gig file to EXT four
using the MKFS command as follows.
Check the file type using the following command.
Now you can mount the loopback file as follows.
The minus O loop,
additional options used to mount loopback filesystems.
This is actually a short method
where we don't have to manually attach it to any devices,
but internally it attaches
to a device called slash dev slash loop one or loop two.
We can do it manually as follows.
To umount, unmount,
use the following syntax, for example,
or alternatively, we can use a device filepath
as an argument to the umount command as.
Note the mount and umount command
should be executed as a root user,
since it's a privileged command.
Now, we'll see how it works.
First, we had to create a file
that will act as a loopback file.
For this, we used DD,
which is a generic command for copying raw data.
It starts copying data from the file specified
in its IF parameter
to the file specified in its OF parameter.
Additionally, we instruct DD to copy data
in blocks of size one gig,
and copy one such block.
Hence, creating a file size one gig.
The slash dev slash zero file is a special file
which always contains zero if you read from it.
We then use the mkfts.ext four command
to create an EXT four filesystem in the file.
A filesystem is needed because it provides a way
of storing files on a disk slash loopback file.
Finally, we used a mount command to mount
the loopback file to a mount point,
slash MNT slash loopback, in this case.
A mount point makes it possible for users to access
the files stored in a filesystem.
Before executing the mount command,
the mount point should be created using the MKDAR command.
We pass the option minus O loop to mount
to tell it we are passing it as a loopback file.
When mount knows it's operating on a loopback file,
it automatically set up a device
in slash dev corresponding to a loopback file,
and then mounts it.
If we wish to do it manually,
we use the LO setup command to set up device,
and then mount command to mount it.
Let's explore some possibilities
with loopback files and mounting.
Creating partitions inside loopback images.
Suppose you want to create a loopback file,
want to partition it,
and finally mount it as a subpartition.
In this case, we cannot use the mount minus O loop.
We have to manually set up device
and mount the partitions to it.
Partition a file with zeros dumped in it as follows.
Fdisk is a standard partitioning tool on Linux systems.
A very concise tutorial on creating partitions
using Fdisk is available at
www.tldp.org/howto/partition/fdisk_partitioning.html.
Make use it's U slash dev slash loop one,
instead of slash dev slash HTB
in the forementioned tutorial.
Create partitions in loopback.img
and mount the partition as follows.
Here slash dev slash loop two
will represent the first partition,
minus O is the offset flag,
32256 bytes are for a DOS partition scheme.
The first partition starts after an offset
of 32256 bytes from the start of the hard disk.
We can set up a second partition
by specifying the required offset.
After mounting, we can perform all regular operations
as we can on physical devices.
Quicker way to mount loopback disk images with partitions.
As we saw, we can manually pass partition offsets
to LO setup when we want to mount partitions
inside a loopback disk image.
However, there is a quicker way to mount
all the partitions inside such an image using kpartx.
This utility is not installed by default,
so you'll have to install it using your package manager.
This creates mappings from the partitions
in the disk image to devices in slash dev slash mapper,
which you can then mount.
For example, to mount the first partition,
use the following command.
When you're done with the devices,
and unmounting any mounted partitions using U-mount,
remove the mappings by flash changing immediately with sync.
While making changes on a mounted device,
they are not immediately written to the physical device.
They are only written when the buffer is full,
but we can force writing of changes immediately
by using the sync command as follows.
Awesome. We've successfully learned how
to use loopback files.
In the next video illustrates how to generate differences
between files highlighted with line numbers.