11

I would like to create an .img file containing Ubuntu 13.10. I know how to get the .iso, but instructions for how to 'convert' it to .img I only found under Mac (see, for example, here) -- however, I work on Ubuntu 13.10. Which are the steps necessary to create the .img file?

Background information: I would like to use the .img to run it with Complete Linux Installer on Android. Complete Linux Installer already offers .img files (see here), but they are too small. Ubuntu only "sees" the size of the .img file which is far too small to be productive -- which is why I would like to create a larger .img file. Using resize2fs myImage.img 4G, I tried to make the image larger, but Ubuntu run from such an image still believes the .img is small, so my hope is that by creating a larger .img right away, Ubuntu "sees" the larger space. I know I'm on thin ice here...

  • http://askubuntu.com/questions/243628/cant-convert-iso-to-img-on-mac May help you? –  Dec 08 '13 at 21:08
  • I don't think so. It is (again) either based on Mac OS X (with hdiutils) or simply suggest to rename the .iso to .img (which I doubt is the correct approach). – Marius Hofert Dec 08 '13 at 21:41
  • 1
    Question does not make sense. The image files you linked appear to be already installed systems ready to run. You don't use the iso image to install Ubuntu into one of them. – psusi Dec 09 '13 at 04:19
  • My goal is to exactly get such an .img, just 'larger'. I thought one starts with the .iso (?) – Marius Hofert Dec 09 '13 at 06:27

3 Answers3

15

I would take a stab in the dark, but you are trying to make an img file from an iso, right? If that's the case then there isn't any difference between iso's and img's apart the extension:

There is no difference in the structure of ISO and IMG formats if the IMG file is uncompressed. It is possible for an IMG format file to be renamed with the ISO file extension and then opened in software that only recognizes the ISO file format. This is an effective way of accessing disc information in programs that do not handle the IMG format.

(source)

Wikipedia also seems to agree:

ISO images are another type of optical disc image files, which commonly use the .iso file extension, but sometimes use the .img file extension as well. They are similar to the raw optical disc images, but contain only one track with computer data obtained from an optical disc. They can not contain multiple tracks, nor audio or video tracks. They also do not contain the control headers and error correction fields of CD-ROM or DVD sectors that raw disc images usually store. Their internal format follows the structure of an optical disc file system, commonly ISO 9660 (for CDs) or UDF (for DVDs). The CUE/BIN and CCD/IMG formats, which usually contain raw disc images, can also store ISO images instead.

So, how to convert an iso to img? Just change the extension:

rename 's/\.iso(?=[^.iso]*$)/.img/g' *

Now, to create an img, you should use dd and save it into a file called img.

Braiam
  • 67,791
  • 32
  • 179
  • 269
  • Thank you for helping, Braiam. If renaming is the solution, why does this not work for people on Mac OS X (who use hdiutils for this; see the link of UncleNerdicus)? Concerning your last line, can you post more details about this? – Marius Hofert Dec 09 '13 at 16:34
  • @MariusHofert he's doing it wrong. Umm.. the last line only renames files that contains .iso to .img. I need it to make that only match the last .iso. Update – Braiam Dec 09 '13 at 17:07
  • Okay, thanks. So I just rename the .iso to .img. How do you then create an .img of, say, 6GB? With resize2fs? Well, that might be a new question, but I hope you know the answer, too. – Marius Hofert Dec 09 '13 at 17:31
  • @MariusHofert you can "create" a img file with dd. img are just a exact copy of every block of a filesystem. – Braiam Dec 09 '13 at 17:39
  • The syntax of dd is: dd if=infile.img of=outfile.img. I don't see how one could choose the size of outfile.img with dd (?). And if I use resize2fs, it seems that the (larger) file size is not 'seen'... – Marius Hofert Dec 09 '13 at 18:11
  • There is an argument count, but still outfile.img has the same size as infile.img – Marius Hofert Dec 09 '13 at 23:30
  • @MariusHofert dd will read exactly the same bytes of the devices/files you input, that's why it has the same size. You can create them, not convert iso into img with dd. – Braiam Dec 09 '13 at 23:44
  • I downloaded two files iso and img and file shows a difference: file us.iso us.iso: ISO 9660 CD-ROM filesystem data '8DET76US' (bootable), img file it.img it.img: DOS/MBR boot sector; partition 1 : ID=0x4, active, start-CHS (0x0,1,1), end-CHS (0x24,63,32), startsector 32, 75744 sectors – Martian2020 Dec 13 '21 at 12:06
  • @Martian2020 did you try renaming and trying file again? – Braiam Dec 13 '21 at 14:57
  • Yes, the output tells the same contents. I've just read advice to use geteltorito f1.iso > f1.img and on running it got the same result from running file on original and processed file as I've posted above. f1.img byte size is slightly smaller. – Martian2020 Dec 13 '21 at 21:14
2

Just rename the file. Any .iso file can be safely renamed to have a .img extension.

TL;DR

ISO vs. IMG

A raw disk image is a binary file that contains a sector-by-sector binary copy of the binary data stored on a storage device. A raw disk image is a complete snapshot of the source medium, including the MBR, GUID OR Apple partition table.

An ISO image is a raw disk image that contains a sector-by-sector copy of the binary data stored on an optical disk. ISO images contains the binary data of an optical media formatted in the ISO-9660 filesystem.

ISO images form a subset of raw disk images. An ISO image (.iso extension) can be renamed to a raw disk image file (.img extension) but the opposite isn't automatically true unless the disk image contains ISO-9660-formatted data. A raw disk image that contains ext4-formatted data isn't an ISO image.

Quite often though, ISO images are used as bootable mediums if they contain a MBR boot sector or an ESP boot partition. The BIOS/UEFI firmware will be able to execute the bootloader code and boot the system.

I downloaded an Ubuntu 22.04 bootable ISO for a quick demo, probing for the filetype shows:

root@ubuntu:~# file ./ubuntu-22.04-desktop-amd64.iso
ubuntu-22.04-desktop-amd64.iso: DOS/MBR boot sector; GRand Unified Bootloader, stage1 version 0x79, boot drive 0xbb, stage2 address 0x8e70, 1st sector stage2 0xb8db31c3, stage2 segment 0x201

The ISO image uses a GPT (GUID partition table) layout that includes a protective MBR. The protective MBR contains the bootstrap code for the first-stage bootloader, which is just a tiny executable code used to load the second-stage bootloader (i.e. GRUB) responsible for loading the kernel in the legacy BIOS boot process.

The second-stage bootloader is stored in the ESP (EFI system partition) and newer computers that have a UEFI-compliant firmware can directly execute the second-stage bootloader by probing the disk to find the ESP.

The protective MBR also contains a partition table that defines a single stub partition with the size of the entire disk. This prevents software that don't understand the GPT layout to see the disk as empty.

Exploring the image's content

To explore the image's content, we can attach the ISO image to a loop device.

A loop device is a block device that maps its data blocks not to a physical storage device but to the blocks of a regular file in the filesystem or to another block device. This is useful to provide a block device for a raw image stored in the filesystem.

You can list the loop devices with ls -l /dev/loop*. The special character device /dev/loop-control is the kernel driver for loop devices. It's used to dynamically find a free loop device to use, and to add and remove loop devices from the system.

Attach the ISO image to an available loop device:

root@ubuntu:~# losetup -f --show -P ./ubuntu-22.04-desktop-amd64.iso
/dev/loop0

List the partitions:

root@ubuntu:~# fdisk -l /dev/loop0
Disk /dev/loop0: 3.41 GiB, 3654957056 bytes, 7138588 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: A09DB2B8-B5F6-43AE-AFB3-91E0A90189A1

Device Start End Sectors Size Type /dev/loop0p1 64 7129427 7129364 3.4G Microsoft basic data /dev/loop0p2 7129428 7137923 8496 4.2M EFI System /dev/loop0p3 7137924 7138523 600 300K Microsoft basic data

Mount the ISO-9660 and EFI partitions:

root@ubuntu:~# mkdir -p /mnt/{iso,esp}
root@ubuntu:~# mount -o ro /dev/loop0p1 /mnt/iso
root@ubuntu:~# mount /dev/loop0p2 /mnt/esp

Note: ISO-9660 is a read-only filesystem so the partition must be mounted with the ro option.

You can verify the mounted filesystems with the following:

root@ubuntu:~# df -T /dev/loop0p{1,2}
Filesystem     Type    1K-blocks    Used Available Use% Mounted on
/dev/loop0p1   iso9660   3564682 3564682         0 100% /mnt/iso
/dev/loop0p2   vfat         4214    3222       992  77% /mnt/esp

To unmount the partitions and deallocate the loop device, run:

root@ubuntu:~# umount loop0p{1,2}
root@ubuntu:~# losetup -d /dev/loop0
emandret
  • 553
1

Disclamer: I do not state that below correctly answer the question, but google search found me that Q&A and I hope my answer will help to solve similar issues.

I've found an advice to convert iso to img to boot from USB. I was not able to boot from USB with written downloaded ISO. Some time later I found an advice to convert to img via:

geteltorito f1.iso > f1.img

And indeed I was able to boot from USB where I've written img file. The files were different:

$ file 6muj31uc.iso
6muj31uc.iso: ISO 9660 CD-ROM filesystem data '6muj31us' (bootable)
$ file 6muj31uc.img  
6muj31uc.img: DOS/MBR boot sector; partition 1 : ID=0x4, active, start-CHS (0x0,1,1), end-CHS (0xe,63,32), startsector 32, 30688 sectors

Boot image sounds like img file:

man geteltorito

geteltorito is a Perl script which extracts the initial/default El Torito boot image from a CD if one exists.