操作系統實驗報告-熟悉實驗環境


實驗材料准備

下載實驗包hit-oslab-linux-20110823.tar.gz(包含linux-0.11源碼、bochs虛擬機等):

https://cms.hit.edu.cn/pluginfile.php/3329/mod_folder/content/6/hit-oslab/hit-oslab-linux-20110823.tar.gz?forcedownload=1

下載gcc-3.4-ubuntu.tar.gz(編譯linux-0.11需要用到的低版本gcc):

https://cms.hit.edu.cn/pluginfile.php/3329/mod_folder/content/6/hit-oslab/gcc-3.4-ubuntu.tar.gz?forcedownload=1

實驗環境設置(ubuntu-16.04.1-desktop-amd64)

實驗材料

在~(用戶Home文件夾)下創建workspace文件夾,將hit-oslab-linux-20110823.tar.gz解壓到workspace下:

mkdir workspace
cd workspace
tar xzf hit-oslab-linux-20110823.tar.gz

gcc-3.4安裝

解壓gcc-3.4-ubuntu.tar.gz到/opt下:

cd /opt 
tar xzf ~/Downloads/gcc-3.4-ubuntu.tar.gz

安裝gcc-3.4(因為我的是64位系統,所以安裝參數為amd64;若為32位系統,參數應為i386):

cd gcc-3.4
sudo ./inst.sh amd64

as86、ld86安裝(用於編譯和鏈接linux-0.11/boot下的bootsect.s和setup.s,它們采用as86匯編語法;而linux-0.11下的其他匯編語言文件采用gas的語法AT&T)

搜索包含as86、ld86的包:

apt-cache search as86 ld86

執行結果:

bin86 - 16-bit x86 assembler and loader

安裝bin86:

sudo apt-get install bin86

64位系統需要安裝32位兼容庫:

sudo apt-get install libc6-dev-i386 

C語言編譯環境

sudo apt-get install build-essential

安裝IDE

sudo apt-get install vim cscope exuberant-ctags

編譯內核

1 cd ~/workspace/oslab/linux-0.11
2 make

編譯中斷,輸出錯誤信息:

In file included from /usr/include/linux/fs.h:10,
                 from tools/build.c:28:
/usr/include/linux/ioctl.h:4:23: asm/ioctl.h: No such file or directory
In file included from /usr/include/linux/fs.h:11,
                 from tools/build.c:28:
/usr/include/linux/types.h:4:23: asm/types.h: No such file or directory
In file included from /usr/include/linux/types.h:8,
                 from /usr/include/linux/fs.h:11,
                 from tools/build.c:28:
/usr/include/linux/posix_types.h:35:29: asm/posix_types.h: No such file or directory
In file included from /usr/include/linux/fs.h:11,
                 from tools/build.c:28:
/usr/include/linux/types.h:27: error: syntax error before "__le16"
/usr/include/linux/types.h:28: error: syntax error before "__be16"
/usr/include/linux/types.h:29: error: syntax error before "__le32"
/usr/include/linux/types.h:30: error: syntax error before "__be32"
/usr/include/linux/types.h:31: error: syntax error before "__le64"
/usr/include/linux/types.h:32: error: syntax error before "__be64"
/usr/include/linux/types.h:34: error: syntax error before "__sum16"
/usr/include/linux/types.h:35: error: syntax error before "__wsum"
In file included from tools/build.c:28:
/usr/include/linux/fs.h:43: error: syntax error before "__u64"
/usr/include/linux/fs.h:45: error: syntax error before "minlen"
make: *** [tools/build] Error 1

google搜索得知是因為64位linux的asm目錄為:

/usr/include/x86_64-linux-gnu/asm

make找不到文件從而導致編譯中斷

解決方法(建立鏈接):

sudo ln -s /usr/include/x86_64-linux-gnu/asm /usr/include/asm

運行和調試

運行

運行編譯好的內核,在oslab目錄下:

./run

出現錯誤:

./bochs/bochs-gdb: error while loading shared libraries: libSM.so.6: cannot open shared object file: No such file or directory

打印動態鏈接配置:

ldconfig -p | grep libSM.so.6

libSM.so.6的鏈接信息:

libSM.so.6 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libSM.so.6

我們需要的是32位的。搜索庫對應的包名稱:

apt-file search libSM.so.6

打印結果:

libsm6: /usr/lib/x86_64-linux-gnu/libSM.so.6
libsm6: /usr/lib/x86_64-linux-gnu/libSM.so.6.0.1

得到其對應的包名為libsm6,安裝對應的32位庫:

sudo apt-get install libsm6:i386

再次嘗試,出現錯誤:

./bochs/bochs-gdb: error while loading shared libraries: libXpm.so.4: cannot open shared object file: No such file or directory

也可按以上方法解決,不過這里用了另一個工具:

dpkg-query -S libXpm.so.4

同理解決之,再次嘗試:

./run

成功運行!

調試

匯編級調試

./dbg-asm

操作參考Bochs使用手冊

C語言級調試

./dbg-c

Ctrl+Alt+T打開另一個終端窗口,進到~/workspace/oslab下執行:

./rungdb

出現兩次庫不存在錯誤,按照上面的方法安裝對應32位庫解決后運行成功:

操作參考GDB的Manuel:

man gdb

Ubuntu與Linux-0.11之間的文件交換

掛載Linux-0.11

./mount-hdc

mount-hdc是一個shell腳本:

1 #!/bin/sh
2 export OSLAB_PATH=$(dirname `which $0`)
3 mount -t minix -o loop,offset=1024 $OSLAB_PATH/hdc-0.11.img $OSLAB_PATH/hdc 

第1行 指明腳本使用的shell

第2行 將調用本腳本的目錄設置為oslab的目錄

第3行 根據mount的manual手冊,

    -t minix 表示要掛載的文件系統類型為minix

    -o[ption] 可以有3個參數(參考losetup的manual):

      loop 可以指定文件所映射的回環設備;若不指定,則由mount自動搜尋

      offset 指定文件或設備中數據起始位置在該文件或設備中的偏移

      sizelimit 從offset開始算起文件或設備的最大大小

   故此行將$OSLAB_PATH/hdc-0.11.img映射到mount搜尋到的設備中,文件系統為minix,數據起始偏移為1024byte,再掛載到$OSLAB_PATH/hdc目錄下。

這樣Ubuntu就可以在$OSLAB_PATH/hdc下訪問Linux-0.11的文件了。

卸載Linux-0.11

在oslab目錄下:

umount hdc

注意

1:不要在0.11內核運行的時候mount鏡像文件,否則可能會損壞文件系統。同理,也不要在已經mount的時候運行0.11內核;

2:在關閉Bochs之前,需要先在Linux-0.11的命令行運行“sync”,確保所有緩存數據都存盤后,再關閉Bochs。

參考

as86、ld86安裝

內核編譯asm不存在錯誤

loop device回環設備

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM