作者:王聰
http://wangcong.org/articles/bochs.html
引言
最近在做操作系統方面的研究,不得不去下一個虛擬機 來用。由於我不使用Windows,所以選擇很少了。因為在Linux上,差不多就只有兩款好的 虛擬機——Bochs和Qemu。這兩個之中,Bochs是用C++編寫,能在很多平台上使用,而且不論什么平台, 它都不倚賴主機的指令系統,總是模擬x86。所以我選擇了Bochs。但是,我驚訝地發現 關於如何在Linux上使用Bochs的資料很少。Bochs自帶的文檔中講得也不很明確,而且還 是英文的。所以我寫了這篇文章。
安裝
到Bochs的主頁: http://bochs.sourceforge.net 上下載最新的Bochs RPM安裝包。(寫這篇文章時,最新的版本是2.2.1。)以root的身份安裝:
#rpm -ivh bochs-2.2.1-1.i586.rpm其實RPM安裝包給你裝上了四個新程序:bochs,bochs-dlx,bximage,bxcommit, 以及相關文檔。安裝完畢后,你可以先試試bochs-dlx,它是DLX Linux的一個demo。
$ bochs-dlx馬上,你就會看到一個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啟動程序
/usr/bin/bximage
Bochs帶的制作磁盤鏡像文件的工具
/usr/bin/bxcommit
把redolog放進flat磁盤鏡像文件中去的交互工具
/usr/share/doc/bochs/bochsrc-sample.txt
Bochs配置文件的例子
/usr/share/bochs/BIOS-bochs-*
ROM BIOS鏡像文件
/usr/share/bochs/VGABIOS-*
與VGA BIOS鏡像文件相關的文件
/usr/bin/bochs-dlx
啟動Bochs中DLX linux的程序
/usr/share/bochs/dlxlinux/
DLX Linux的目錄,包含它的磁盤鏡像文件和配置文件
/usr/share/bochs/keymaps/*.map
X11和SDL的keymap列表
命令bohcs可以接受4個參數:-q 加載配置文件后跳過開始菜單;-f configfile 識別指定的配制文件;-n 不加載配置文件;-h 打印幫助信息;-qf configfile 相當於同時使用-q和-f兩個選項。使用Bochs,你必須有一個配置文件,或者你通 過-f參數指定,或者讓Bochs自己搜索。配置文件的作用是告訴Bochs到哪里尋找鏡像 文件,模擬層如何工作等。Bochs搜索配置文件的順序是:
-
.bochsrc 在當前目錄中</TT CLASS="FILENAME">
-
bochsrc 在當前目錄中
-
bochsrc.txt 在當前目錄中</TT CLASS="FILENAME">
-
.bochsrc 在用戶的主目錄中
-
bochsrc 在 /etc 目錄中
以dlx-linux的配置文件為例,講一下如何定制你需要的配置文件。dlx-linux的 配置文件是/usr/share/doc/bochs目錄中的bochsrc-sample.txt。它的內容 如下:
###############################################################
# 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的內存。注意:后面的數字最大為2048,但是因為主機系統的限制,在 大多數系統上,Bochs甚至不能支持1024M。
romimage: file=$BXSHARE/BIOS-bochs-latest, address=0xf0000指明ROM的鏡像文件在哪,開機時ROM BIOS被加載到哪里。$BXSHARE是環境變量,指明 Bochs的share目錄,在Linux上為/usr/share/bochs。這一般不用改動。下面的vgaromimage 指明VGA ROM的鏡像文件位置。
floppya: 1_44=floppya.img, status=insertedfloppya是第一軟驅,floppyb是第二軟驅。后面標明的是軟驅鏡像文件的位置,軟盤 是否插入。可以根據你的需要做適當的修改。
floppyb: 1_44=floppyb.img, status=inserted
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14打開ata0通道,另外還有ata1,ata2,ata3。對於每一個通道,都必須指明兩個IO地址和IRQ。 ata0是默認被打開的,它的ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14。ata0-master 是主ata0通道,后面指明它附帶的設備是硬盤,硬盤鏡像文件路徑,硬盤柱面數,磁頭數等參數。
ata0-master: type=disk, path="hd10meg.img", cylinders=306, heads=4, spt=17
boot: c指明啟動次序,可以是cdrom,floppy,disk。不過由於歷史的原因a和c也能被接受。 也可以這樣來寫:
boot: cdrom, floppy, disk
mouse: enabled=0指明鼠標是否可見。
keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-us.map打開對虛擬US鍵盤的重映射,keymap必須被指明。
更多的配置文件選項說明見/usr/share/doc/bochs/user/bochsrc.html。 如何編寫鍵盤的keymap列表見/usr/share/doc/bochs/user/keymap.html。
制作磁盤鏡像
bximage是Bochs自帶的制作磁盤鏡像的工具,具有 良好的交互性,使用很方便。運行bximage,你會看到:
========================================================================詢問我們是建立一個軟盤鏡像還是硬盤鏡像,默認的是硬盤。我們直接按回車,選擇硬盤 鏡像。
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?我們想建立一個簡單的鏡像,選擇默認的flat。回車。
Please type flat, sparse or growing. [flat]
Enter the hard disk size in megabytes, between 1 and 32255想要多少Mbyte的大小?鍵入你想要的大小。
[10]
I will create a 'flat' hard disk image with在上一步中輸入“1”后,顯示相關的硬盤信息。詢問想要什么鏡像文件名字?默認的是 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.建立完畢。然后你就可以使用這個鏡像,用Bochs做實驗了。
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還有強大的調試功能,詳細請參閱用戶 手冊第8章。Bochs主頁上還有一些做好的系統鏡像,比如:OpenBSD,FreeDOS等。 不妨下載一個來試試。 Bochs的確是一款優秀的工具,我推薦給每一位做操作系統研究的朋友。
