Installing grub on virtual hard disks<br />Sukhvinder Singh<br />http://www.linkedin.com/in/s0001<br />sukh.chauhan@gmail....
Steps<br />This presentation demonstrates the process to create a new Qemu usable virtual disk capable of self boot using ...
Create a disk image<br />Create a disk image. You may create a file named disk.img, containing 40320 blocks ( a block has ...
Create a partition on the disk image<br />Expert command (m for help): m<br />Command action<br />   b   move beginning of...
Setup the loopback device<br />Using fdisk, find the starting sector of the recently created partition. The start field sh...
Create a file system<br />Format the disk and create an ext2 filesystem on the image<br />$ mkfs.ext2 /dev/loop0<br />Moun...
Install Grub<br />$ mkdir -p /mnt/boot/grub $ cp /boot/grub/stage1 /boot/grub/stage2 /mnt/boot/grub/ $ vi /mnt/boot/grub/g...
Setup Grub device-map<br />$ grub --device-map=/dev/null <br />device (hd0) disk.img <br />geometry (hd0) 40 16 63 <br />r...
Boot using Qemu<br />The following command will bring up the Qemu window and boot from the new disk.img. <br />$ qemu –hda...
For more information, contact:Sukhvinder Singhhttp://www.linkedin.com/in/s0001sukh.chauhan@gmail.com<br />
Upcoming SlideShare
Loading in...5
×

Installing grub on virtual hard disk images

10,896

Published on

This presentation demonstrates the process to create a new Qemu usable virtual disk capable of self boot using the popular Grub boot loader. Grub in turn can boot other kernels placed on the disk file system. The steps are applicable only for a GNU/Linux System. All tools used are readily available on all popular linux distros in use.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
10,896
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "Installing grub on virtual hard disk images"

  1. 1. Installing grub on virtual hard disks<br />Sukhvinder Singh<br />http://www.linkedin.com/in/s0001<br />sukh.chauhan@gmail.com<br />30 Aug 2010<br />
  2. 2. Steps<br />This presentation demonstrates the process to create a new Qemu usable virtual disk capable of self boot using the popular Grub boot loader. Grub in turn can boot other kernels placed on the disk file system. The steps are applicable only for a GNU/Linux System. All tools used are readily available on all popular linuxdistros in use. To demonstrate we’ll take ubuntu-desktop as the reference test platform.<br />
  3. 3. Create a disk image<br />Create a disk image. You may create a file named disk.img, containing 40320 blocks ( a block has a standard size of 512 bytes). This results in a disk image of size 20643840 bytes ( = 21 MB). The size used is only for demonstration purposes. You may use a different size based on your requirement.<br />40320 blocks<br />x<br />512 bytes/block<br />=<br />20643840 bytes<br />(21 MB) <br />$ dd if=/dev/zero of=disk.img count=40320<br />40320+0 records in<br />40320+0 records out<br />20643840 bytes (21 MB) copied, 0.175132 s, 118 MB/s<br />
  4. 4. Create a partition on the disk image<br />Expert command (m for help): m<br />Command action<br /> b move beginning of data in a partition<br /> c change number of cylinders (=40)<br /> d print the raw data in the partition table<br /> e list extended partitions<br /> f fix partition order<br /> g create an IRIX (SGI) partition table<br /> h change number of heads (=16)<br />i change the disk identifier<br /> m print this menu<br /> p print the partition table<br /> q quit without saving changes<br /> r return to main menu<br /> s change number of sectors/track (=63)<br /> v verify the partition table<br /> w write table to disk and exit<br />Use fdisk to create a partition on the disk image.<br />Set CHS (Cylinder-Head-Sectors) geometry of the disk<br />Create a primary partition<br />Set the OS identifier for the partition to 83 (Linux)<br />Calculate CHS:<br />The number of sectors in a cylinder is fixed to 63 ( a popular constant ), S = 63<br />Taking heads to an arbitrary value of 16, H=16 (another popular constant)<br />C * H * S = Number of blocks = 40320 <br />C * 16 * 63 = 40320 <br />C = 40 ; H = 16 ; S = 63<br />
  5. 5. Setup the loopback device<br />Using fdisk, find the starting sector of the recently created partition. The start field shows the starting sector of the partition. <br />There are about 32256 bytes before the first partition begins. <br />$ fdisk -u -l disk.img<br />Device Boot Start End Blocks Id System<br />disk.img1 63 40319 20128+ 83 Linux<br /><ul><li> Set up the loop device to point to the start sector (i.e. 32256 bytes )</li></ul>$ losetup -o 32256 /dev/loop0 disk.img<br />
  6. 6. Create a file system<br />Format the disk and create an ext2 filesystem on the image<br />$ mkfs.ext2 /dev/loop0<br />Mount the image<br />Mount the disk image to a directory on the local filesystem<br />$ mount /dev/loop0 /mnt<br />Detach the loopback device<br />$ losetup –d /dev/loop0<br />
  7. 7. Install Grub<br />$ mkdir -p /mnt/boot/grub $ cp /boot/grub/stage1 /boot/grub/stage2 /mnt/boot/grub/ $ vi /mnt/boot/grub/grub.conf title=MyKernel root (hd0,0) kernel /mykernel<br />$ mkdir -p /mnt/boot/grub $ cp /boot/grub/stage1 /boot/grub/stage2 /mnt/boot/grub/ $ vi /mnt/boot/grub/grub.conf title=MyKernel root (hd0,0) kernel /mykernel<br />$ mkdir -p /mnt/boot/grub $ cp /boot/grub/stage1 /boot/grub/stage2 /mnt/boot/grub/<br />Copy Grub files (stage1 and stage2) from your existing distro (in our case, ubuntu system) to the image file.<br />$ mkdir -p /mnt/boot/grub <br />$ cp /usr/lib/grub/i386-pc/stage1 /usr/lib/grub/i386-pc/stage2 /mnt/boot/grub/<br /><ul><li>Unmount the disk image</li></ul>$ umount /mnt<br />
  8. 8. Setup Grub device-map<br />$ grub --device-map=/dev/null <br />device (hd0) disk.img <br />geometry (hd0) 40 16 63 <br />root (hd0,0) <br />setup (hd0) <br />quit<br />
  9. 9. Boot using Qemu<br />The following command will bring up the Qemu window and boot from the new disk.img. <br />$ qemu –hda disk.img –m 128 <br />The grub boot code on the disk.img boots to stage2 and brings up a grub command prompt.<br />grub > <br />If you have placed a multiboot specification compliant kernel (for ex. linux kernel 2.6.* ) on the disk.img filesystem under /boot directory, the following command will bring up the kernel.<br />grub> kernel /boot/kernel.bin <br />grub> boot<br />
  10. 10. For more information, contact:Sukhvinder Singhhttp://www.linkedin.com/in/s0001sukh.chauhan@gmail.com<br />

×