最近在做操作系統方面的研究,不得不去下一個虛擬機 來用。由于我不使用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
下面是一些重要的文件:
/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列表 |
bochsrc 在當前目錄中
.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的內存。注意:后面的數字最大為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必須被指明。
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的確是一款優秀的工具,我推薦給每一位做操作系統研究的朋友。
實驗一 目的:熟悉實驗環境,認識Bochs虛擬機 內容: 1、下載并安裝Bochs 2.3.7,官方網站 http://bochs.sourceforge.net/ 2、下載DOS 6.22安裝光盤,參考地址 http://www.grqhd.com/downloads/hdd/dos622cd.iso 3、使用Bochs自帶工具制作10M硬盤鏡像若干、1.44M軟盤鏡像若干,備用。 4、練習修改Bochs配置文件,使用DOS 6.22安裝光盤將DOS安裝到硬盤和軟盤。 5、練習修改配置文件,分別實現從光盤、硬盤或軟盤啟動。 實驗二 目的:MBR的讀寫與分析 內容: 1、練習使用int 13功能獲取安裝了DOS 6.22系統的硬盤MBR、反匯編。 2、使用int 13功能將獲取的MBR,寫到空白的硬盤上。 3、修改MBR代碼,并測試其工作流程。 實驗三 目的:掌握分區表的數值意義 內容: 1、練習使用DOS 6.22自帶的分區工具FDisk,把一個空白硬盤分區,觀察MBR的變化。 2、使用FDisk把一個空白硬盤分一個主分區,自定義大小,其余分為擴展分區,擴展分區上分兩個邏輯分區,自定義大小。 3、說明以上分區表的數值含義。 實驗四 目的:DBR的讀寫與分析 內容: 1、獲取正常DOS 6.22系統盤上的DBR,并反匯編、閱讀。 2、比較空白硬盤經分區后,使用Format前后DBR的差別。 3、測試DBR的工作流程。 實驗五 目的:掌握FAT原理 內容: 1、以FAT12為例,分析BPB以及FAT、根目錄表。 2、利用以上原理,通過直接改寫磁盤扇區的辦法,在根目錄下生成一個文本文件,并寫入適當的內容。 |