0

I'm building Linux From Scratch. Fun project. Anyways, I've created a 60 gigabyte raw disk image using dd and filled it with /dev/zero.

I can't find anything online about how to mount the image as an actual disk. Everything I read tells me that I need to mount each individual partition and not the disk itself.

Is there a way I could mount the 60 gigs of unpartitioned space as 1 device? Much like you would see an unformatted flash drive as sdb instead of sdb1 etc.

I have pretty much tried variants of: sudo mount -o loop disk /media/LFS

I always get this:

The device '/dev/loop0' doesn't seem to have a valid NTFS.
Maybe the wrong device is used? Or the whole disk instead of a
partition (e.g. /dev/sda, not /dev/sda1)? Or the other way around?
  • What methods did you try? – Anwar Dec 9 '16 at 8:50
  • Anwar Pretty much variants of: sudo mount -o loop disk /media/LFS – KI4JGT Dec 9 '16 at 8:53
  • is disk the name of your file. Is there a folder in /media/LFS? – Anwar Dec 9 '16 at 8:59
  • disk is the name of my file. There is no folder in /media/LFS – KI4JGT Dec 9 '16 at 9:19
  • /media/LFS should an empty folder if you want to mount on it – Anwar Dec 9 '16 at 9:25
1

You can not mount a disk or partition without having a filesystem on that. As the file disk contains all zeros there is not any filesystem on that.

First, to create a loop device on that file call

sudo losetup -f disk

which will create a loop device for the file 'disk'. With sudo losetup you will get a list of loop devices; note the one that is connected with your disk. I assume /dev/loop0from now on.

Next to create a valid filesystem, you have to call

sudo mkfs -t [type] /dev/loop0

where [type] means the filesystem type, e.g. ext4. For more options see man mkfs.

Now you are able to mount:

sudo mount /dev/loop0 /media/LFS

which should then be accessible as normal filesystem.

To disconnect, you have to unmount (sudo umount /media/LFS) and detach the loop device (sudo losetup -d /dev/loop0). From then on, you can mount the filesystem as you tried in your question (mounting with -o loop will create the loop device for you).

And no, you do not need to have the device partitioned. And you may of course mount a filesystem on a non-empty folder - but then you cannot access the previous contents of this folder before you unmount again.

Edit

If you want the image file to work like partitioned disks and be recognized by the kernel, you may follow the answer given in how to format.... In short:

Partition the image: # fdisk disk which will run fdisk as usual and you may create partitions.

Create Filesystems: As mkfs does not work with whole disks if they are partitioned (just may use the raw disk as one filesystem as stated above), you best use loop device with partprobeor kpartx to inform the kernel. The simplest way is to run # sudo losetup -f -P disk, which will create partition devices as e.g. /dev/loop0p1, /dev/loop0p2 etc. These may then be used by mkfs.

Use gparted: As gparted calls the default tools like parted and mkfs, the points above hold true as well. You can call # sudo gparted disk and then partition the disk, but you cannot format those partitions, as mkfs then asks for distinct images like disk1, disk2 etc. So you should use the loop device with gparted as well:

# sudo losetup --show -f -P disk
/dev/loop0
# sudo gparted /dev/loop0

(I used --show here so losetup will show which device is created). Now you are able to work with gparted as usual. The partitions, when created/formatted, will then be available to other applications like filemanagers (maybe you have to issue sudo partprobe /dev/loop0 for the kernel to recognize the partitions).

I did not manage to get the loop device shown in gparted when called without directly passing the name of the loop device, even after issuing partprobe or kpartx, e.g. when starting from the system menu. It only shows /dev/sdx devices. Maybe someone else is able to solve this issue.

  • How would I "plug" it into the system in a way that the disk would be seen by gparted? I am aware that I can use 'gparted disk' in the terminal but would like my computer to see it as an actual drive. – KI4JGT Dec 10 '16 at 5:05
  • I edited the answer for using partitioned images; this may not really answer the question in your comment, but I could not find how gparted is able to see loop devices (or mapped devices, which could be a solution as well). – ridgy Dec 10 '16 at 11:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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