The commands are from here: Mount single partition from image of entire disk (device)

About the first command' I need to "Calculate the offset from the start of the image to the partition start", but don't know what the numbers quoted in ** mean (512 in this example):

$ sudo fdisk -lu sda.img
Units = sectors of 1 * **512** = **512** bytes
Sector size (logical/physical): **512** bytes / **512** bytes
  Device Boot      Start         End      Blocks   Id  System
sda.img1   *          56     6400000     3199972+   c  W95 FAT32 (LBA)

about the second command, what is the "loop0":

sudo losetup -o 28672 /dev/loop0 sda.img

I looked there and it looked like a text file but it isn't, it also not a folder. what is that file? Can I choose another name to it? If yes, should I create it before?

How does the third command works? What does every part in it mean?:

sudo mount /dev/loop0 /mnt

I already successfully ran the commands to do what I wanted, it's just important to me to understand it more for next time and for helping other.

Thanks for your answers and teaching me (:


In the first command, the 512 number is referring to the block size of the image. When a file is written to a disk, it uses x amount of blocks. The remainder of any block not used is wasted space. This is why you sometimes see things listed as size (actual size of the file, folder, etc) and size on disk (amount of space actually taken up on disk, allowing for block size mismatch. Take, for example a 1234 byte file, if we were using 512 byte blocks, then it would actually take up 3 blocks, or 1536 bytes (512 * 3) and 302 bytes (1536 - 1234) bytes would be "wasted".

The loop0 is a loopback device. All files under /dev are actually devices (hence the name :-)). Linux treats devices as files which makes it very easy to accomplish some things when dealing with hardware. i.e. the following command will send an audio file directly to the soundcard to play (no software needed, neat!)

cat sound.wav > /dev/audio

Loopback devices are special pseudo or fake devices that Linux uses in cases like yours. What you are saying in that command is that you want Linux to treat your image file as a hard drive. This allows you to run any command that you would normally run on a hard drive, on your image file.

For the last command, we are simply mounting the file system on your fake drive (your image file) to a location on the local filesystem. With Linux, we use a flat directory structure, which is why every file location (including devices, network drives and external hard drives, etc) begins with '/', which is also referred to as the root of the filesystem. Mounting a filesystem simply puts it in a place accessible tot he rest of the system (somewhere under /).

Now, by default only a user called "root" can mount filesystems. The root user is basically the same as an administrator user on Windows. Someone who can do anything on the computer. In Ubuntu, the root user is hidden for security, so we need to use sudo. The sudo command, basically means "hey, make me the root user to run this one command".

mount here is the command that we are running as root (sudo), /dev/loop0 is our fake drive we created earlier, and /mnt is the location that we want the image to be accessible at. /mnt and /media are the standard locations in Linux where we mount filesystems, although this can be anywhere you want. It is best practice to create subfolder under /mnt or /media to mount to, instead of just /mnt. This is because you may have other filesystems mounted there, and if you mount over top of them, they will not be accessible! The only caveat to this, is that you need root privileges to create a folder here, so your last command should really be two commands, and look like this:

sudo mkdir /mnt/myimg
sudo mount /dev/loop0 /mnt/myimg

mkdir is just the command to make a directory.

| improve this answer | |
  • Wow! Thanks so much for your long answer, I'm feeling I Know it better now. you explained everything so good, and how the system is working with those commands. Thanks You again to help me so much, you're great! – yinon Jun 14 '12 at 17:27
  • No problem. I'm glad it was helpful. It's always nice to see people eager to learn! – reverendj1 Jun 14 '12 at 17:29
  • I'm having a bit problem now: I got: "losetup: /dev/loop0: device is busy" while running the second command (this time I got the error). should unmount another drives? or maybe change it too another loop? Thanks for your assitance – yinon Jun 15 '12 at 7:42
  • You would get this error if you still had your old image mounted. First, unmount it sudo umount /mnt. Replace /mnt with wherever you mounted it. No, detach it from the loop device sudo losetup -d /dev/loop0. If you wanted two different images mounted, then you could use /dev/loop1, /dev/loop2, etc. – reverendj1 Jun 15 '12 at 14:00
  • Sorry to not comment for thjis while, we didn't have the drive then. but we tried it all and everything have succeeded! Thank again for helping so much, you're great! – yinon Jun 29 '12 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.