Develop OS on VMs
时间:2009-06-28 来源:qingfenglala
Develop OS on VMs
作者: falcon 发表日期: 2008-03-31 12:31 复制链接
build a bootable ISO image file of linux system and use it on VMs
-- develop OS on VMs
falcon <[email protected]>
2008-03-24
this is a document about how to build a bootable ISO image file with mkisofs and
isolinux in ubuntu 7.10 and use it on VMs, the detail steps are:
1. build the ISO image file
1.1 install mkisofs, syslinux
mkisofs is a tool for building an ISO file, isolinux is a bootloader for linux.
$ apt-get install mkisofs syslinux
1.2 prepare the necessary resources
$ mkdir source
$ mkdir source/isolinux
$ cp /usr/lib/syslinux/isolinux.bin source/isolinux
$ cp /boot/vmlinuz-`uname -r` source/vmlinuz
$ cp /boot/initrd.img-`uname -r` source/initrd.img
here, we just use the current vmlinuz and initrd.img, if you want to build your
own linux kernel and filesystem, please compile the linux kernel yourself to
generate a new vmlinuz(bzImage) and make your own filesystem with busybox. if
you want to know more about how to compile the linux kernel, and build the mini
filesystem with busybox, please refer to the references[3,4].
1.3 configure the isolinux boot loader
check the size of initrd.img, the size is 7.0M, the 8M(8192kb) is engogh for it.
$ ls -lh source/initrd.img
-rw-r--r-- 1 falcon falcon 7.0M 2008-03-24 13:56 source/initrd.img
configure isolinux
$ vim source/isolinux/isolinux.cfg
$ cat source/isolinux/isolinux.cfg
default linux
prompt 1
timeout 600
label linux
kernel /vmlinuz
append initrd=/initrd.img load_ramdisk=1 prompt_ramdisk=0 ramdisk_size=8192 rw root=/dev/ram0
1.4 build the iso file
$ mkdir target
$ mkisofs -o target/minios.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table source/
the iso file will be stored in target as target/minios.iso
2. boot the iso file on an VM(virtual machine)
before burning the ISO file to a cd-rom, you can check it via booting on an VM.
here will introduce two open source VMs, one is Virtualbox[5], another is
Qemu[6].
the steps for using them are the same, at first, An system image file should be
created, and then a specific device should be indicated to boot this
system,such as a cd-rom, floppy, of course, a virtual device is also supported,
for example, a floppy image file or an ISO image file.
2.1 use Virtualbox
install virtualbox
$ apt-get install virtualbox-ose
add the current user to the group vboxusers
$ adduser $USER vboxusers
insert the relative modules to kernel
$ /etc/init.d/vboxdrv start
start virtualbox, follow the prompt, and boot a system from the iso file
$ virtulbox
if this not work, please reboot your system to make sure the user have been
added to the vboxusers group.
2.2 use Qemu
if we want to use qemu, it should be installed firstly
$ apt-get install qemu
now, we can boot the iso file
$ qemu -m 128M -cdrom minios.iso -boot d -no-kqemu
if you want to boot a floppy image file, how?
$ qemu -m 64 -boot a -fda boot.img -no-kqemu
in virtualbox, booting a floppy image file is like booting an ISO file.
that is all, is it useful? yes, of course, you will have no need to reboot your
system like before, and test the embedded system on current host system. what's
more? if you are writing your own bootloader, you just need to create a flopy
image[7], and test it via booting it on these VMs.
if you want to speed up the qemu, please install kqemu, the basic steps are:
$ sudo apt-get install kqemu-source kernel-package linux-source build-essential
$ cd /usr/src/linux
$ sudo module-assistant prepare kqemu
$ sudo module-assistant build kqemu
$ sudo module-assistant install kqemu
$ sudo depmod -a
$ sudo modprobe kqemu
$ sudo mknod /dev/kqemu c 250 0
$ sudo chmod 666 /dev/kqemu
now, there is no need to use the -no-kqemu option when using qemu and the
running speed of qemu will be accelerated.
3. build the network between VMs and the HOST system/Internet
there are several methods to built network between VMs and HOST system, such as
through NAT, Router and Bridge. here will just introduce the method with
Bridge for this is available to Virtualbox and Qemu.
3.1 install the basic tools
for controlling the virtual network device(TUN/TAP), we should install
uml-utilities, for controlling the bridge device, we should install
bridge-utils.
$ apt-get install uml-utilities bridge-utils
3.2 configure the network on HOST system
here are two scripts for starting and stoping the network configuration on HOST
OS.
$ cat vmnet_up.sh
#!/bin/bash
USER="$USER"
IP="Please Configure An IPaddress to you HOST system Here"
GW="Please Set the Default GateWay Address Here"
tunctl -t tap1 -u $USER
brctl addbr br0
ifconfig eth0 0.0.0.0 promisc
brctl addif br0 eth0
# static configuration
#ifconfig br0 $IP netmask 255.255.255.0 up
#route add default gw $GW dev br0
# dynamic configuration
dhclient br0
brctl addif br0 tap1
ifconfig tap1 up
chmod 0666 /dev/net/tun
$ cat vmnet_down.sh
#!/bin/bash
#IP="Please Configure An IPaddress to you HOST system Here"
#GW="Please Set the Default GateWay Address Here"
ifconfig tap1 down
tunctl -d tap1
ifconfig br0 down
brctl delbr br0
# for static configuration
# ifconfig eth0 $IP netmask 255.255.255.0
# route add default gw $GW
# for dynamica configuration
dhclient
if we want to start the network configuration on HOST system, please set the
necessary variables(i.e user,ipaddress,default gateway) and issue the following
command.
$ sudo vmnet_up.sh
correspondingly, you can stop it via vmnet_down.sh
3.3 configure it on VMs
for using Bridge method to build the network, we should also configure the VMs
themselves to support the relative device.
3.3.1 Virtualbox
After starting Virtualbox, you can select one of the existing VM and then press
the "Setting" button to do some configurations, afterward, choose the "Network"
menu on the left side, and then, you will find the following inforamtion.
Enable Network Adapter
Cable Connected
choose the above two options, and then set the "Attached to" as "HOST
Interface", set the "interface name" as "tap1".
3.3.2 Qemu
if you want to enable the bridge method in Qemu, just start it with the
additional options:
-net tap,vlan=0,ifname=tap1
4. debug Linux kernel via VMs
it's very simple to debug Linux Kernel via Qemu[9], of course, we can debug it
via Virtualbox[8], here just give a demo about how to debug it via Qemu. the
detail steps are:
4.1 compile linux kernel with -g option
for debugging Linux kernel, we should compile it using gcc with the -g option,
simply, we can check it whether there is a line in the
linux-<version>/Makefile like this:
CFLAGS += -g
after compiling Linux Kernel, a compressed linux kernel image file will be
stored as linux-<version>/arch/<ARCH>/boot/bzImage,the original one will be
stored as linux-<version>/vmlinux.
4.2 boot linux kernel with Qemu
if you never create a virtual disk image file before, please create one like
this:
$ qemu-create linux.img 1G
and then boot the kernel using qemu with the -S option.
$ qemu -kernel /path/to/bzImage -hda linux.img -append "root=/dev/hda" -S
after qemu is running, a black screen will appear which is the interface of the
linux system, please switch it to the qemu command line via typing the
following command to start the gdb server:
$ gdbserver 1234
afterward, you can start a new terminal on your HOST system and execute gdb to
debug the linux kernel.
$ gdb /path/to/vmlinux
(gdb) target remote localhost:1234
have fun to debug it :-)
5. conclusion
as the above steps we have doen, we find that it's very convenient to develop
OS on VMs, here we just introduce how to develop the existing os(linux kernel)
on an indicated cpu architecture.
what's more? we can develop our own os and even just a boot loader for
different cpu architectures on VMs, because some VMs can emulate different cpu
architectures and peripherals, Qemu is one of such VMs.
if you want to know more about VMs, you can access this link[10].
play with VMs perhaps is the best choice for a poor but aspirant
student because of most of them are open source, free and powerful.
References:
[1] 使用isolinux制作Linux启动光盘
http://www.linuxdiyf.com/viewarticle.php?id=16752
[2] ISOLINUX Document
http://syslinux.zytor.com/iso.php
[3] Compile linux kernel 2.6
http://linuxgazette.net/111/krishnakumar.html
[4] Build filesystem with busybox
Build a mini filesystem in linux with BusyBox
[5] Use Virtualbox
http://zpq2004.javaeye.com/blog/165654
[6] Use Qemu
http://www.ibm.com/developerworks/cn/linux/l-qemu/index.html
[7] Writing x86 PC Bootloader With Free Software
http://solrex.blogspot.com/2007/07/writing-x86-pc-bootloader-with-free.html
[8] Debug linux via Virtualbox+kgdb
http://www.devfront.com/?q=node/302
[9] Debug Linux via Qemu+gdb
http://www.yuanma.org/data/2007/0329/article_2466.htm
http://www.ljfind.com/post/98990618/
http://stealthextraction.info/index.php?hl=f5&q=uggc%3A%2F%2Fvffnevf.oybtfcbg.pbz%2F2007%2F12%2Fqbjaybnq-yvahk-xreary-fbheprpbqr-sebz.ugzy
http://kgdb.linsyssoft.com/quickstart.htm
[10] Virtual Machine
http://en.wikipedia.org/wiki/Virtual_machine
[11] KGDB Patchs
http://kgdb.linsyssoft.com/cvs.htm
http://www.popies.net/kgdb/index.html
http://sourceforge.net/project/showfiles.php?group_id=5073
build a bootable ISO image file of linux system and use it on VMs
-- develop OS on VMs
falcon <[email protected]>
2008-03-24
this is a document about how to build a bootable ISO image file with mkisofs and
isolinux in ubuntu 7.10 and use it on VMs, the detail steps are:
1. build the ISO image file
1.1 install mkisofs, syslinux
mkisofs is a tool for building an ISO file, isolinux is a bootloader for linux.
$ apt-get install mkisofs syslinux
1.2 prepare the necessary resources
$ mkdir source
$ mkdir source/isolinux
$ cp /usr/lib/syslinux/isolinux.bin source/isolinux
$ cp /boot/vmlinuz-`uname -r` source/vmlinuz
$ cp /boot/initrd.img-`uname -r` source/initrd.img
here, we just use the current vmlinuz and initrd.img, if you want to build your
own linux kernel and filesystem, please compile the linux kernel yourself to
generate a new vmlinuz(bzImage) and make your own filesystem with busybox. if
you want to know more about how to compile the linux kernel, and build the mini
filesystem with busybox, please refer to the references[3,4].
1.3 configure the isolinux boot loader
check the size of initrd.img, the size is 7.0M, the 8M(8192kb) is engogh for it.
$ ls -lh source/initrd.img
-rw-r--r-- 1 falcon falcon 7.0M 2008-03-24 13:56 source/initrd.img
configure isolinux
$ vim source/isolinux/isolinux.cfg
$ cat source/isolinux/isolinux.cfg
default linux
prompt 1
timeout 600
label linux
kernel /vmlinuz
append initrd=/initrd.img load_ramdisk=1 prompt_ramdisk=0 ramdisk_size=8192 rw root=/dev/ram0
1.4 build the iso file
$ mkdir target
$ mkisofs -o target/minios.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table source/
the iso file will be stored in target as target/minios.iso
2. boot the iso file on an VM(virtual machine)
before burning the ISO file to a cd-rom, you can check it via booting on an VM.
here will introduce two open source VMs, one is Virtualbox[5], another is
Qemu[6].
the steps for using them are the same, at first, An system image file should be
created, and then a specific device should be indicated to boot this
system,such as a cd-rom, floppy, of course, a virtual device is also supported,
for example, a floppy image file or an ISO image file.
2.1 use Virtualbox
install virtualbox
$ apt-get install virtualbox-ose
add the current user to the group vboxusers
$ adduser $USER vboxusers
insert the relative modules to kernel
$ /etc/init.d/vboxdrv start
start virtualbox, follow the prompt, and boot a system from the iso file
$ virtulbox
if this not work, please reboot your system to make sure the user have been
added to the vboxusers group.
2.2 use Qemu
if we want to use qemu, it should be installed firstly
$ apt-get install qemu
now, we can boot the iso file
$ qemu -m 128M -cdrom minios.iso -boot d -no-kqemu
if you want to boot a floppy image file, how?
$ qemu -m 64 -boot a -fda boot.img -no-kqemu
in virtualbox, booting a floppy image file is like booting an ISO file.
that is all, is it useful? yes, of course, you will have no need to reboot your
system like before, and test the embedded system on current host system. what's
more? if you are writing your own bootloader, you just need to create a flopy
image[7], and test it via booting it on these VMs.
if you want to speed up the qemu, please install kqemu, the basic steps are:
$ sudo apt-get install kqemu-source kernel-package linux-source build-essential
$ cd /usr/src/linux
$ sudo module-assistant prepare kqemu
$ sudo module-assistant build kqemu
$ sudo module-assistant install kqemu
$ sudo depmod -a
$ sudo modprobe kqemu
$ sudo mknod /dev/kqemu c 250 0
$ sudo chmod 666 /dev/kqemu
now, there is no need to use the -no-kqemu option when using qemu and the
running speed of qemu will be accelerated.
3. build the network between VMs and the HOST system/Internet
there are several methods to built network between VMs and HOST system, such as
through NAT, Router and Bridge. here will just introduce the method with
Bridge for this is available to Virtualbox and Qemu.
3.1 install the basic tools
for controlling the virtual network device(TUN/TAP), we should install
uml-utilities, for controlling the bridge device, we should install
bridge-utils.
$ apt-get install uml-utilities bridge-utils
3.2 configure the network on HOST system
here are two scripts for starting and stoping the network configuration on HOST
OS.
$ cat vmnet_up.sh
#!/bin/bash
USER="$USER"
IP="Please Configure An IPaddress to you HOST system Here"
GW="Please Set the Default GateWay Address Here"
tunctl -t tap1 -u $USER
brctl addbr br0
ifconfig eth0 0.0.0.0 promisc
brctl addif br0 eth0
# static configuration
#ifconfig br0 $IP netmask 255.255.255.0 up
#route add default gw $GW dev br0
# dynamic configuration
dhclient br0
brctl addif br0 tap1
ifconfig tap1 up
chmod 0666 /dev/net/tun
$ cat vmnet_down.sh
#!/bin/bash
#IP="Please Configure An IPaddress to you HOST system Here"
#GW="Please Set the Default GateWay Address Here"
ifconfig tap1 down
tunctl -d tap1
ifconfig br0 down
brctl delbr br0
# for static configuration
# ifconfig eth0 $IP netmask 255.255.255.0
# route add default gw $GW
# for dynamica configuration
dhclient
if we want to start the network configuration on HOST system, please set the
necessary variables(i.e user,ipaddress,default gateway) and issue the following
command.
$ sudo vmnet_up.sh
correspondingly, you can stop it via vmnet_down.sh
3.3 configure it on VMs
for using Bridge method to build the network, we should also configure the VMs
themselves to support the relative device.
3.3.1 Virtualbox
After starting Virtualbox, you can select one of the existing VM and then press
the "Setting" button to do some configurations, afterward, choose the "Network"
menu on the left side, and then, you will find the following inforamtion.
choose the above two options, and then set the "Attached to" as "HOST
Interface", set the "interface name" as "tap1".
3.3.2 Qemu
if you want to enable the bridge method in Qemu, just start it with the
additional options:
-net tap,vlan=0,ifname=tap1
4. debug Linux kernel via VMs
it's very simple to debug Linux Kernel via Qemu[9], of course, we can debug it
via Virtualbox[8], here just give a demo about how to debug it via Qemu. the
detail steps are:
4.1 compile linux kernel with -g option
for debugging Linux kernel, we should compile it using gcc with the -g option,
simply, we can check it whether there is a line in the
linux-<version>/Makefile like this:
CFLAGS += -g
after compiling Linux Kernel, a compressed linux kernel image file will be
stored as linux-<version>/arch/<ARCH>/boot/bzImage,the original one will be
stored as linux-<version>/vmlinux.
4.2 boot linux kernel with Qemu
if you never create a virtual disk image file before, please create one like
this:
$ qemu-create linux.img 1G
and then boot the kernel using qemu with the -S option.
$ qemu -kernel /path/to/bzImage -hda linux.img -append "root=/dev/hda" -S
after qemu is running, a black screen will appear which is the interface of the
linux system, please switch it to the qemu command line via typing the
following command to start the gdb server:
$ gdbserver 1234
afterward, you can start a new terminal on your HOST system and execute gdb to
debug the linux kernel.
$ gdb /path/to/vmlinux
(gdb) target remote localhost:1234
have fun to debug it :-)
5. conclusion
as the above steps we have doen, we find that it's very convenient to develop
OS on VMs, here we just introduce how to develop the existing os(linux kernel)
on an indicated cpu architecture.
what's more? we can develop our own os and even just a boot loader for
different cpu architectures on VMs, because some VMs can emulate different cpu
architectures and peripherals, Qemu is one of such VMs.
if you want to know more about VMs, you can access this link[10].
play with VMs perhaps is the best choice for a poor but aspirant
student because of most of them are open source, free and powerful.
References:
[1] 使用isolinux制作Linux启动光盘
http://www.linuxdiyf.com/viewarticle.php?id=16752
[2] ISOLINUX Document
http://syslinux.zytor.com/iso.php
[3] Compile linux kernel 2.6
http://linuxgazette.net/111/krishnakumar.html
[4] Build filesystem with busybox
Build a mini filesystem in linux with BusyBox
[5] Use Virtualbox
http://zpq2004.javaeye.com/blog/165654
[6] Use Qemu
http://www.ibm.com/developerworks/cn/linux/l-qemu/index.html
[7] Writing x86 PC Bootloader With Free Software
http://solrex.blogspot.com/2007/07/writing-x86-pc-bootloader-with-free.html
[8] Debug linux via Virtualbox+kgdb
http://www.devfront.com/?q=node/302
[9] Debug Linux via Qemu+gdb
http://www.yuanma.org/data/2007/0329/article_2466.htm
http://www.ljfind.com/post/98990618/
http://stealthextraction.info/index.php?hl=f5&q=uggc%3A%2F%2Fvffnevf.oybtfcbg.pbz%2F2007%2F12%2Fqbjaybnq-yvahk-xreary-fbheprpbqr-sebz.ugzy
http://kgdb.linsyssoft.com/quickstart.htm
[10] Virtual Machine
http://en.wikipedia.org/wiki/Virtual_machine
[11] KGDB Patchs
http://kgdb.linsyssoft.com/cvs.htm
http://www.popies.net/kgdb/index.html
http://sourceforge.net/project/showfiles.php?group_id=5073
相关阅读 更多 +