Prepare disk image for dd command

Ridnarong Promya
Jul 22, 2017 · 3 min read

If you are trying to use small computer board like Raspberry PI, you need to prepare SD card as a OS disk. To do this there are two way. First way is downloading disk image and write image to SD card by using dd command or other disk tools. Second way is downloading zip file that contain all files then copy to partitioned SD card.

Some OS only provide OS zip file, my colleague have difficulty with partitioning SD card is unable to follow the copy instruction. I end up with prepare disk image then she can use dd command to copy many SD card as her want. At the beginning I use dd to dump image form another SD card to raw disk image but this method consume disk space equal to SD card size.

Finally I found this (https://softwarebakery.com/shrinking-images-on-linux) article about loop back device that can simulate raw disk image into disk in /dev. Instead of dd from existing SD card, I design to use qemu-img command to create raw disk image at desired size then mount via loop back device, partition, copy OS file, unmount and done we have got preloaded disk image.

Create disk image

Create disk image by using qeume-img command depend on your OS size. In this example I create disk 1 GB raw image.

qemu-img create -f raw my.img 1G

Check loop device support

Check your Linux kernel to enable loop back driver.

sudo modprobe loop

Create new loop device and take note of loop device.

sudo losetup -f

Link our image with loop device for previously create device

sudo losetup /dev/loop0 my.img

Create partition and copy files

Now your can use partition tool like fdisk or gparted to modify partition in this image like usual disk then format the partition and mount to folder. In this example I create 2 partition which available at /dev/loop0p1 (for /boot partition) /dev/loop0p2 (for / partition).

mkfs.vfat /dev/loop0p1
mkdir boot
mount /dev/loop0p1 boot
mkfs.ext4 /dev/loop0p2
mkdir root
mount /dev/loop0p2 root

Copy or uncompressed your files to destination folder and unmounted. In this example I have shown archarm linux.

wget http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-2-latest.tar.gz
bsdtar -xpf ArchLinuxARM-rpi-2-latest.tar.gz -C root
sync
mv root/boot/* bootumount boot root

Now all files are in our disk image, we can release loop device and done

sudo losetup -d /dev/loop0

Write disk image and expand partition

Afterward we can write this image to SD card.

dd bs=4M if=my.img of=/dev/mmcblk0 conv=fsync

Turn on and boot by using SD card you will get working OS. But your SD have size equal to image file to expand partition to fill all your SD card space you can do by fdisk to remove and recreate partition will all remaining space.

fdisk /dev/mmcblk0

In fdisk

  • p print partition information, check for the second partition
  • d delete partition
  • enter to accept default partition number.
  • n create partition
  • enter to accept partition number
  • enter to accept first sector
  • enter to accept last sector
  • w to write change to disk and exit

Reboot to make your kernel read new partition table

sudo reboot

Resize file system to fill up the partition. It should take awhile.

sudo resize2fs /dev/mmcblk0p2

Check size now you will get all space of disk.

df -h
Ridnarong Promya

Written by

NoMethodError: undefined method `bio' for nil:NilClass

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade