Tuesday, July 31, 2012

HOWTO: Create and use .IMG files from the CLI

Now that Bodhi's second stable release for the desktop is settling down, I am going to be putting a bit of focus on our ARM releases for the next couple of weeks. ARM images typically are provided in the form of .IMG files. Using a downloaded .IMG file is fairly simple. You can write it to a drive of your choice with a single command:

sudo dd if=myfile.img of=/path/to/drive

One thing worth noting though is that /path/to/drive should not include any partition number. An example path would be something like:

/dev/sdb

Note that writing large images can take a good deal of time depending on the speed of your drive and that this command will not give you any feedback until it finishes.

Now, perhaps you are like myself and have some interest in creating/distributing .IMG files of your own. Creating image files is also fairly easily and uses the same dd command. An example of how to create an image file is:

sudo dd if=/path/to/drive of=image.img

Note that this command copies the entire contents of the drive - meaning if your drive is large your .IMG file will be equally large! Now, what do you do if you only want to copy part of your drive? Simply add one argument to the above command of course! For example to only copy the first two gigs of data on a drive to a .IMG file use:

sudo dd if=/path/to/drive of=image.img bs=1M count=2048


I am by no means an expert at using dd, but if you run into any issues feel free to drop a comment below and I'll do my best to help you out.

Cheers,
~Jeff Hoogland

7 comments:

  1. You have the last two backwards. count=1M will only copy 1M of data. bs=2048 will read in blocks of 2048 bytes, which will speed up the transfer, but not limit the amount of total data.

    The correct lines should be:
    sudo dd if=/path/to/drive of=image.img bs=1M
    and
    sudo dd if=/path/to/drive of=image.img bs=1M count=2048

    This will copy the entire contents or ~2G (2048 * 1M) respectively.

    ReplyDelete
    Replies
    1. Err I just used the command as I have written and it gave me the results I described...

      Delete
    2. There really doesn't seem to be a need for that "count" at all, actually. If you leave it out, it'll just get the whole drive. I use DD all the time to backup USB drives.

      Count is best used in this lovely command to backup your MBR.

      sudo dd if=/dev/sda of=mbr.img bs=512 count=1

      Delete
    3. Jeff, gruemaster is right:

      bs = block size
      count = how many blocks

      Your second command told dd to copy 1000000 blocks of default size (512 bytes), so you'll copy 512 MB at most.

      If your device happens to be bigger than 512 MB, you'll get a broken .IMG :)

      Oh, and to get feedback from dd, you can open a second terminal and 'kill -SIGUSR1' dd's PID, which can be obtained with 'pgrep dd' or 'pidof dd' for example.

      When dd receives SIGUSR1, it prints to stdout how many blocks it has read and written, time elapsed and average speed, very useful info.

      Cheers.

      Delete
    4. You both are correct - that second command was a typo on my part that is fixed now.

      Delete
  2. can someone explain me how to resize to a minimal size the image I've created with dd from a modified personalize bohdi sd card ?? thanks in advance

    ReplyDelete
    Replies
    1. To my knowledge you need to define the image size at creation - not afterwards.

      Delete