引言
最近在做操作系統(tǒng)方面的研究,不得不去下一個(gè)虛擬機(jī) 來用。由于我不使用Windows,所以選擇很少了。因?yàn)樵贚inux上,差不多就只有兩款好的 虛擬機(jī)——Bochs和Qemu。這兩個(gè)之中,Bochs是用C++編寫,能在很多平臺(tái)上使用,而且不論什么平臺(tái), 它都不倚賴主機(jī)的指令系統(tǒng),總是模擬x86。所以我選擇了Bochs。但是,我驚訝地發(fā)現(xiàn) 關(guān)于如何在Linux上使用Bochs的資料很少。Bochs自帶的文檔中講得也不很明確,而且還 是英文的。所以我寫了這篇文章。
安裝
到Bochs的主頁: http://bochs.sourceforge.net 上下載最新的Bochs RPM安裝包。(寫這篇文章時(shí),最新的版本是2.2.1。)以root的身份安裝:
#rpm -ivh bochs-2.2.1-1.i586.rpm其實(shí)RPM安裝包給你裝上了四個(gè)新程序:bochs,bochs-dlx,bximage,bxcommit, 以及相關(guān)文檔。安裝完畢后,你可以先試試bochs-dlx,它是DLX Linux的一個(gè)demo。
$ bochs-dlx馬上,你就會(huì)看到一個(gè)X11窗口。
---------------------------------------------------------------
DLX Linux Demo, for Bochs x86 Emulator
---------------------------------------------------------------
Checking for bochs binary...ok
Checking for DLX linux directory...ok
Checking for /usr/bin/gzip...ok
Checking for /home/wangcong/.bochsdlx directory...ok
Entering /home/wangcong/.bochsdlx
Running bochs
00000000000i[APIC?] local apic in initializing
========================================================================
Bochs x86 Emulator 2.2.1
Build from CVS snapshot on July 8, 2005
========================================================================
00000000000i[ ] LTDL_LIBRARY_PATH not set. using compile time default '/usr/lib/bochs/plugins'
00000000000i[ ] BXSHARE not set. using compile time default '/usr/share/bochs'
00000000000i[ ] reading configuration from bochsrc.txt
00000000000i[ ] lt_dlhandle is 0x8bac490
00000000000i[PLGIN] loaded plugin libbx_x.la
00000000000i[ ] installing x module as the Bochs GUI
00000000000i[ ] using log file bochsout.txt

顯示的是VGA BIOS信息,然后是加載Linux的信息。以root身份登錄,試試DLX Linux。安裝成功。
使用
下面是一些重要的文件:
/usr/bin/bochs | Bochs啟動(dòng)程序 |
/usr/bin/bximage | Bochs帶的制作磁盤鏡像文件的工具 |
/usr/bin/bxcommit | 把redolog放進(jìn)flat磁盤鏡像文件中去的交互工具 |
/usr/share/doc/bochs/bochsrc-sample.txt | Bochs配置文件的例子 |
/usr/share/bochs/BIOS-bochs-* | ROM BIOS鏡像文件 |
/usr/share/bochs/VGABIOS-* | 與VGA BIOS鏡像文件相關(guān)的文件 |
/usr/bin/bochs-dlx | 啟動(dòng)Bochs中DLX linux的程序 |
/usr/share/bochs/dlxlinux/ | DLX Linux的目錄,包含它的磁盤鏡像文件和配置文件 |
/usr/share/bochs/keymaps/*.map | X11和SDL的keymap列表 |
命令bohcs可以接受4個(gè)參數(shù):-q 加載配置文件后跳過開始菜單;-f configfile 識(shí)別指定的配制文件;-n 不加載配置文件;-h 打印幫助信息;-qf configfile 相當(dāng)于同時(shí)使用-q和-f兩個(gè)選項(xiàng)。使用Bochs,你必須有一個(gè)配置文件,或者你通 過-f參數(shù)指定,或者讓Bochs自己搜索。配置文件的作用是告訴Bochs到哪里尋找鏡像 文件,模擬層如何工作等。Bochs搜索配置文件的順序是:
-
-
bochsrc 在當(dāng)前目錄中
-
-
.bochsrc 在用戶的主目錄中
-
bochsrc 在 /etc 目錄中
###############################################################以#開頭的每一行都是注釋。注釋寫得很明白。
# bochsrc.txt file for DLX Linux disk image.
###############################################################
# how much memory the emulated machine will have
megs: 32
# filename of ROM images
romimage: file=$BXSHARE/BIOS-bochs-latest, address=0xf0000
vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest
# what disk images will be used
floppya: 1_44=floppya.img, status=inserted
floppyb: 1_44=floppyb.img, status=inserted
# hard disk
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
ata0-master: type=disk, path="hd10meg.img", cylinders=306, heads=4, spt=17
# choose the boot disk.
boot: c
# default config interface is textconfig.
#config_interface: textconfig
#config_interface: wx
#display_library: x
# other choices: win32 sdl wx carbon amigaos beos macintosh nogui rfb term svga
# where do we send log messages?
log: bochsout.txt
# disable the mouse, since DLX is text only
mouse: enabled=0
# enable key mapping, using US layout as default.
#
# NOTE: In Bochs 1.4, keyboard mapping is only 100% implemented on X windows.
# However, the key mapping tables are used in the paste function, so
# in the DLX Linux example I'm enabling keyboard_mapping so that paste
# will work. Cut&Paste is currently implemented on win32 and X windows only.
keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-us.map
#keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-fr.map
#keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-de.map
#keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-es.map
megs: 32指明想要模擬32M的內(nèi)存。注意:后面的數(shù)字最大為2048,但是因?yàn)橹鳈C(jī)系統(tǒng)的限制,在 大多數(shù)系統(tǒng)上,Bochs甚至不能支持1024M。
romimage: file=$BXSHARE/BIOS-bochs-latest, address=0xf0000指明ROM的鏡像文件在哪,開機(jī)時(shí)ROM BIOS被加載到哪里。$BXSHARE是環(huán)境變量,指明 Bochs的share目錄,在Linux上為/usr/share/bochs。這一般不用改動(dòng)。下面的vgaromimage 指明VGA ROM的鏡像文件位置。
floppya: 1_44=floppya.img, status=insertedfloppya是第一軟驅(qū),floppyb是第二軟驅(qū)。后面標(biāo)明的是軟驅(qū)鏡像文件的位置,軟盤 是否插入。可以根據(jù)你的需要做適當(dāng)?shù)男薷摹?
floppyb: 1_44=floppyb.img, status=inserted
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14打開ata0通道,另外還有ata1,ata2,ata3。對(duì)于每一個(gè)通道,都必須指明兩個(gè)IO地址和IRQ。 ata0是默認(rèn)被打開的,它的ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14。ata0-master 是主ata0通道,后面指明它附帶的設(shè)備是硬盤,硬盤鏡像文件路徑,硬盤柱面數(shù),磁頭數(shù)等參數(shù)。
ata0-master: type=disk, path="hd10meg.img", cylinders=306, heads=4, spt=17
boot: c指明啟動(dòng)次序,可以是cdrom,floppy,disk。不過由于歷史的原因a和c也能被接受。 也可以這樣來寫:
boot: cdrom, floppy, disk
mouse: enabled=0指明鼠標(biāo)是否可見。
keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-us.map打開對(duì)虛擬US鍵盤的重映射,keymap必須被指明。
更多的配置文件選項(xiàng)說明見/usr/share/doc/bochs/user/bochsrc.html。 如何編寫鍵盤的keymap列表見/usr/share/doc/bochs/user/keymap.html。
制作磁盤鏡像
bximage是Bochs自帶的制作磁盤鏡像的工具,具有 良好的交互性,使用很方便。運(yùn)行bximage,你會(huì)看到:
========================================================================詢問我們是建立一個(gè)軟盤鏡像還是硬盤鏡像,默認(rèn)的是硬盤。我們直接按回車,選擇硬盤 鏡像。
bximage
Disk Image Creation Tool for Bochs
$Id: bximage.c,v 1.25.2.1 2005/07/06 20:40:00 vruppert Exp $
========================================================================
Do you want to create a floppy disk image or a hard disk image?
Please type hd or fd. [hd]
What kind of image should I create?我們想建立一個(gè)簡單的鏡像,選擇默認(rèn)的flat。回車。
Please type flat, sparse or growing. [flat]
Enter the hard disk size in megabytes, between 1 and 32255想要多少M(fèi)byte的大小?鍵入你想要的大小。
[10]
I will create a 'flat' hard disk image with在上一步中輸入“1”后,顯示相關(guān)的硬盤信息。詢問想要什么鏡像文件名字?默認(rèn)的是 c.img。輸入hd.img回車。
cyl=2
heads=16
sectors per track=63
total sectors=2016
total size=0.98 megabytes
What should I name the image?
[c.img]
Writing: [] Done.建立完畢。然后你就可以使用這個(gè)鏡像,用Bochs做實(shí)驗(yàn)了。
I wrote 1032192 bytes to hd.img.
The following line should appear in your bochsrc:
ata0-master: type=disk, path="hd.img", mode=flat, cylinders=2, heads=16, spt=63
另外Bochs還有強(qiáng)大的調(diào)試功能,詳細(xì)請(qǐng)參閱用戶 手冊(cè)第8章。Bochs主頁上還有一些做好的系統(tǒng)鏡像,比如:OpenBSD,F(xiàn)reeDOS等。 不妨下載一個(gè)來試試。 Bochs的確是一款優(yōu)秀的工具,我推薦給每一位做操作系統(tǒng)研究的朋友。