This series of blog posts contains the following parts:
5. Creating an SDCard image (This part)
Part 5: SDCard Image
This is the last part of this series.
Here we will create an image that we can use to flash directly on an SDCard we then can boot from.
This is for those who don´t use Windows with MFGTool, but wan´t to use Linux to flash our created system.
There are different methods to achieve this.
One is creating an image from an already flashed system on the RIoTBoard onto an SDCard.
Not really an option since we then would need our system already flashed, which we don´t have.
The second method would be that we use an SDCard reader that´s connected to our building system.
And then do the same steps as MFGTool would do to flash it in Windows.
The other one is, that we create a virtual image file on our building system and mount it as a virtual disk.
And then also do the same steps as MFGTool would do to flash it in Windows.
Since both methods are almost the same and virtual machines often have problems with USB passthrough or like me having no physical access to the building system at all, we´re doing the third method here.
If you have an SDCard reader connected to your build system, you can skip to Step 5.
So let´s get started.
What you need are the previously created files.
These additional packages are not essential for this. But they make it a lot easier to create the image.
apt-get install qemu-utils kpartx
Choose a place where to store the image file.
Since all the files we need for this are stored in one folder let´s do it there.
Now we will create the image file.
You could use dd for this, but with the qemu-tools it´s a bit easier.
But what size should the file be?
Most 4 GB SDCards have about 3.7 GB of usable space.
So that should be the size right?
Well but what if we want to actually copy the image from our SDCard onto the RIoTBoards eMMC?
Looking at that it only has 3.6 GB of usable space.
So we will create an image file with 3600 MB.
qemu-img create sdcard.img 3600M
So if we now do this...
ls -lh sdcard.img
... we see the file is exactly 3.6 GB in size.
-rw-r--r-- 1 root root 3,6G Sep 18 23:21 sdcard.img
Now we need to partition the file.
Looking at our script we are using in MFGTool we find a simple little command that does the trick.
sfdisk --force -uM sdcard.img << EOF 10,,83 EOF
Now that we have a file we can use, we need to format the partition inside.
But how? It´s still only a file.
Well that is done through a mapping of a loopback device onto a specific part of the file.
Normally we would do this manually by executing something like "losetup -o 32256 --sizelimit 5160448 /dev/loop1 sdcard.img".
But then you would have to calculate that offset and the sizelimit for every partition you created.
And if you´re using several partitions that can be a real pain in the ass.
Luckily I´ve found a little tool that does that for us completely automatic.
And that´s kpartx.
All we have to do is this.
kpartx -av sdcard.img
After execution you see a line like this.
add map loop0p1 (253:0): 0 7148925 linear /dev/loop0 16065
If we had more than one partition we´d see more lines. One for every partition.
The interesting part here now is the mapping loop0p1.
We now have our device and will format and then mount it.
(If using an SDCard reader, replace /dev/mapper/loop0p1 with your device /dev/sdb or whatever it is recognized with from this point on.)
mkfs.ext4 -j /dev/mapper/loop0p1 mkdir -p /mnt/sdcard mount -t ext4 /dev/mapper/loop0p1 /mnt/sdcard
Now that the partition is mounted we can extract our file system, as well as the kernel and the modules.
Also we copy the bootscript into place.
tar --numeric-owner -C /mnt/sdcard -zxvf oneiric.tgz tar --numeric-owner -C /mnt/sdcard -zxvf linux-kernel.tgz cp boot.scr /mnt/sdcard/boot.scr
Unmount the partition and then remove the loopback mapping again with kpartx.
umount /mnt/sdcard kpartx -dv sdcard.img
Now we need to integrate u-boot into the file.
That can be done with dd. But if we do it just like in MFGTool we will only overwrite the file and all the stuff we´ve done so far would be gone.
So we have to do it like this.
dd if=u-boot.imx of=sdcard.img bs=512 seek=2 conv=notrunc
With "conv=notrunc" at the end, we tell dd to only overwrite the beginning of the file and leave the rest as is.
Let´s check if that worked.
ls -lh sdcard.img
If everything went right, the image file should still be 3.6 GB in size.
-rw-r--r-- 1 root root 3,6G Sep 18 23:52 sdcard.img
Now the image is done and ready to flash onto an SDCard.
Of course if we want to copy or download it we don´t want such a big file.
So let´s pack it into a zip file.
zip RiotboardLinux3.17SD.zip sdcard.img
And that´s it.
You now can flash the file with dd like this onto an SDCard.
dd if=sdcard.img of=/dev/sdb bs=4K
Or with Windows you can use a tool like Win32 Disk Imager to write it onto your SDCard.
Then put the SDCard in your RiotBoard.
Set the Dip Switches to 2,4,5,7 Off rest On for a normal SDCard or for the Micro SDCard to 2,4,5,8 Off rest On.
And that´s it for this series.
Hope you enjoyed this and are now able to create your own Linux system for the RIoTBoard.