My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
QEmuSDCardImage  
How to make, and use a Emdebian qemu "SD" image.
qemu, img, nbd, kpartx
Updated Jan 12, 2012 by buser...@gmail.com

Introduction

QEmu has great tools to manipulate disk images, but sometime it's not easy to know how to make the first one, the one that you have to make from "outside" on the host.

Here's a quick howto on how to make a fake SD card using the root debian file system available in the "Download" page, and use it in qemu.

Note that this is very similar to the way one makes a real SD card.

Making the Image File

Thats easy as pie. Just pick a size over 120MB or so, thats probably the smallest size you want your image to me. Anything bigger will work. See qemu-img --help for details.

 michel@moskva:~/qemu/qemu-trunk.git$ *./qemu-img create mini2440/mini2440_sd.img 256M* 

Formatting 'mini2440/mini2440_sd.img', fmt=raw, size=262144 kB

Then, make a "network block device" out of it. The process will sit in the background, and avcept just one connection on the oort 1024 (default). See qemu-nbd --help for details.

 michel@moskva:~/qemu/qemu-trunk.git$ *./qemu-nbd mini2440/mini2440_sd.img & * 

Now, we need to tell linux to "attach" itself to it, and create a virtual block device. First you need to have nbd-client installed on your linux, here's how to install it for debian derivative (and ubuntu

 michel@moskva:~/qemu/qemu-trunk.git$ *sudo aptitude install nbd-client* 

Load the necessary kernel modules

You also have to load the necessary modules from the kernl, in case they are not already loaded:

 michel@moskva:~/qemu/qemu-trunk.git$ *sudo modprobe nbd* 

michel@moskva:~/qemu/qemu-trunk.git$ *sudo modprobe dm-mod*

"Connect" to the disk

Then, you can now connect to your disk !

 michel@moskva:~/qemu/qemu-trunk.git$ sudo nbd-client localhost 1024 /dev/nbd0
 Negotiation: ..size = 262144KB
 bs=1024, sz=262144
 

Re/Partition the disk Image

Now, we know that the image is not partitioned and anything, but let's assume here that you already did it before, and that in fact it's partitioned and you want to 'see' them. nbd-client doesn't create the partition block devices for you, which is most annoying.

Anyway, lets now partition our "disk". This step is very similar to what is explained in Emdebian wiki page so the gorry details are skipped for brievety purpose.

michel@moskva:~/qemu/qemu-trunk.git$ sudo fdisk /dev/nbd0
 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
 Building a new DOS disklabel with disk identifier 0x6b164d18.
 Changes will remain in memory only, until you decide to write them.
 After that, of course, the previous content won't be recoverable.
 

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): o Building a new DOS disklabel with disk identifier 0xe620d3e2. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-32, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-32, default 32): +50MB

Command (m for help): t Selected partition 1 Hex code (type L to list codes): b Changed system type of partition 1 to b (W95 FAT32)

Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (8-32, default 8): Using default value 8 Last cylinder, +cylinders or +size{K,M,G} (8-32, default 32): Using default value 32

Command (m for help): p

Disk /dev/nbd0: 268 MB, 268435456 bytes 255 heads, 63 sectors/track, 32 cylinders Units = cylinders of 16065 512 = 8225280 bytes Disk identifier: 0xe620d3e2

Device Boot Start End Blocks Id System /dev/nbd0p1 1 7 56196 b W95 FAT32 /dev/nbd0p2 8 32 200812+ 83 Linux

Command (m for help): w The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 22: Invalid argument. The kernel still uses the old table. The new table will be used at the next reboot.

WARNING: If you have created or modified any DOS 6.x partitions, please see the fdisk manual page for additional information. Syncing disks.

Here we need to tell the system that that block device has partitions on. Being a "virtual" disk the hist system doesn't know about the partition themselves.

We use kpartx for that, you might have to install it with :

 michel@moskva:~/qemu/qemu-trunk.git$ *sudo aptitude install kpartx* 

Now use kpartx to find where the partitions are, and be able to mount them

michel@moskva:~/qemu/qemu-trunk.git$ *sudo kpartx -a /dev/nbd0* 

gpt: 0 slices dos: 4 slices nbd0p1 : 0 112392 /dev/nbd0 63 nbd0p2 : 0 401625 /dev/nbd0 112455

Now the blocks device you need are in /dev/mapper/nbd0p1 and /dev/mapper/nbd0p2

Install the system

You can then follow the tutorial on the Embedian wiki page on how to install the system on it. Here is a concise way to partition, and install the system :

 

michel@moskva:~/qemu/qemu-trunk.git$ *sudo mkfs.vfat /dev/mapper/nbd0p1*

michel@moskva:~/qemu/qemu-trunk.git$ *sudo mkfs.ext3 /dev/mapper/nbd0p2*

michel@moskva:~/qemu/qemu-trunk.git$ *sudo mount /dev/mapper/nbd0p2 ./disk*

michel@moskva:~/qemu/qemu-trunk.git$ *mkdir -p disk/boot*

michel@moskva:~/qemu/qemu-trunk.git$ *sudo mount /dev/mapper/nbd0p1 ./disk/boot*

michel@moskva:~/qemu/qemu-trunk.git$ *wget <INSERT GOOGLE URL>/emdebian-grip-090306-armel-lenny-installed.tar.bz2 *

michel@moskva:~/qemu/qemu-trunk.git$ *(cd disk; sudo tar jxf ../emdebian-grip-090306-armel-lenny-installed.tar.bz2) *

michel@moskva:~/qemu/qemu-trunk.git$ *sudo umount disk/boot disk*

Done !

Once done, unmounted and all that, just do a:

michel@moskva:~/qemu/qemu-trunk.git$ *sudo nbd-client -d /deb/nbd0* 
This will disconnect the drive, end qemu-nbd and you can then use the file as expected with qemu

Comment by kvshe...@gmail.com, Jan 21, 2010

when i am trying to run this with qemu, i am getting this:

root@kailas-laptop:/home/kailas/sdcard# ./run-qemu.sh qemu: fatal: Trying to execute code outside RAM or ROM at 0x30008000

Comment by curt...@gmail.com, Jan 25, 2010

kvshetye: Use this to start quemu (stay in the top source dir where the configure script is)

./mini2440/mini2440_start.sh

Then you will probably see this line earlier in the output:

./mini2440/mini2440_start.sh: line 16: qemu-img: command not found

I ran this manually (You could probably edit the script as well to use an absolute path for qemu-img, as its not in your path if you don't install qemu):

./qemu-img create mini2440/mini2440_snapshots.img 100MB

And then copy a u-boot.bin into the mini2440 directory.

You should be good to go then.

Comment by col...@icomuk.co.uk, Jun 15, 2011

Hi

I am using this image with QEMU to test some code for the Mini2440 board

I have created the Image ok but when i run the startup script and type bootm in u-boot it error with the below

TCP cubic registered NET: Registered protocol family 17 s3c2410-rtc s3c2410-rtc: setting system clock to 2011-06-15 13:35:55 UTC (1308144955) VFS: Mounted root (jffs2 filesystem) on device 31:3. Freeing init memory: 132K Warning: unable to open an initial console. Kernel panic - not syncing: No init found. Try passing init= option to kernel.

Please could you help me im stuck and have been for a couple of days and dont know what to try next

Thanks

Colin

Comment by olivier...@gmail.com, Aug 23, 2011

Colint : can you give us your bootargs used ? it might help... It seems like the kernel is looking for an yaffs2 partition. If you follow this process is not what you want.

Try this : "setenv bootargs console=ttySAC0,115200 noinitrd root=/dev/mmcblk0p2 rootfstype=ext3 rootwait=1 mini2440=0tb init=/linuxrc"

Not sure about init=/linuxrc as I no longer use emdebian but check in the rootfs

Comment by rnga...@gmail.com, Feb 9, 2012

sudo nbd-client -d /deb/nbd0

-> should be /dev/nbd0, not /deb/nbd0


Sign in to add a comment
Powered by Google Project Hosting