Prepare disk image for dd command
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 1GCheck loop device support
Check your Linux kernel to enable loop back driver.
sudo modprobe loopCreate new loop device and take note of loop device.
sudo losetup -fLink our image with loop device for previously create device
sudo losetup /dev/loop0 my.imgCreate 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 bootmkfs.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
syncmv root/boot/* bootumount boot root
Now all files are in our disk image, we can release loop device and done
sudo losetup -d /dev/loop0Write disk image and expand partition
Afterward we can write this image to SD card.
dd bs=4M if=my.img of=/dev/mmcblk0 conv=fsyncTurn 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/mmcblk0In 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 rebootResize file system to fill up the partition. It should take awhile.
sudo resize2fs /dev/mmcblk0p2Check size now you will get all space of disk.
df -h