内核调试 · 2014-08-03 0

Bochs调试linux内核环境搭建笔记(3)

近期不小心把系统弄崩了,重装了系统,使用了ubuntu 14.04,顺便准备研究Grub 2.0引导linux的方式,发现制作bochs引导镜像着实不容易,倒也不是操作困难,而是grub命令变更比较大,一时半会还不熟,折腾了好久才算是弄好。特此留下笔记以备后忘。

环境信息

Ubuntu 14.04
Grub 2.02

1、创建img镜像文件
root@Jean-vm:/home/jeanleo/bochs# dd if=/dev/zero of=disk.img count=$((63*16*100))
100800+0 records in
100800+0 records out
51609600 bytes (52 MB) copied, 0.699557 s, 73.8 MB/s
2、使用fdisk对img镜像文件进行分区操作
root@Jean-vm:/home/jeanleo/bochs# fdisk disk.img 
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x379f62f8.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): m
——m是帮助命令
Command action

   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition’s system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): x
——切入专家模式。
Expert command (m for help): c
Number of cylinders (1-1048576, default 6): 100
——设置cylinders柱面
Expert command (m for help): h
Number of heads (1-256, default 255): 16
——设置heads
Expert command (m for help): s
Number of sectors (1-63, default 63): 63
——设置每个track的sector数
Expert command (m for help): r
——返回至普通模式
Command (m for help): n
——创建新分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2): 1
——创建基础分区,分区数为1
First sector (2048-100799, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-100799, default 100799): 
Using default value 100799
——设置起始和结束的sector,直接回车采用默认的即可

Command (m for help): a
Partition number (1-4): 1
——设置可引导的分区
Command (m for help): w
The partition table has been altered!
——将分区表写入磁盘并退出

Syncing disks.
3、检查一下格式化的结果
root@Jean-vm:/home/jeanleo/bochs# fdisk -lu disk.img 

Disk disk.img: 51 MB, 51609600 bytes
16 heads, 63 sectors/track, 100 cylinders, total 100800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x379f62f8

   Device Boot      Start         End      Blocks   Id  System
disk.img1   *        2048      100799       49376   83  Linux
4、kpartx在linux上不是默认安装的,需要另外安装一下。
root@Jean-vm:/home/jeanleo/bochs# sudo apt-get install kpartx
Reading package lists… Done
Building dependency tree       
Reading state information… Done
kpartx is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 53 not upgraded.
5、将disk.img映射至loop设备,同时会在/dev/mapper目录下生成分区设备
root@Jean-vm:/home/jeanleo/bochs# kpartx -av disk.img 
add map loop1p1 (252:0): 0 98752 linear /dev/loop1 2048
通过查看kpartx的执行结果,可以知道disk.img映射到了哪个设备上,该设备文件类似loop*p1,
除此之外,同样可以通过查看/dev/mapper目录的文件名来得知。
root@Jean-vm:/home/jeanleo/bochs# ls -l /dev/mapper/
total 0
crw——- 1 root root  10, 236  7月 17 06:23 control
brw-r–r– 1 root root 252,   0  7月 19 13:26 loop1p1
6、将设备初始化为文件系统
root@Jean-vm:/home/jeanleo/bochs# mkfs /dev/mapper/loop1p1 
mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: done                            
warning: 223 blocks unused.

Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
12384 inodes, 49153 blocks
2456 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=50593792
6 block groups
8192 blocks per group, 8192 fragments per group
2064 inodes per group
Superblock backups stored on blocks: 
8193, 24577, 40961

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done
8、将设备挂载到目录/mnt/img/,这里是使用以前已经创建好的目录,可以自行创建。
root@Jean-vm:/home/jeanleo/bochs# mount /dev/mapper/loop1p1 /mnt/img/
9、将grub安装到镜像中。
root@Jean-vm:/home/jeanleo/bochs# grub-install –no-floppy –boot-directory=/mnt/img /dev/loop1
Installing for i386-pc platform.
Installation finished. No error reported.
10、安装内核,将linux内核拷贝到镜像中。
root@Jean-vm:/home/jeanleo/bochs# cp /boot/vmlinuz-3.14.12 /mnt/img/vmlinuz
11、配置grub.cfg文件。
root@Jean-vm:/home/jeanleo/bochs# vim /mnt/img/grub/grub.cfg

grub.cfg文件内容为:
# Timeout for menu
set timeout=10
# Set default boot entry as Entry0
set default=0
# Entry0- Load Linux kernel
menuentry “Linux” { 
set root=(hd0,1)
linux /vmlinuz root=/dev/hda1
}

12、卸载设备。
root@Jean-vm:/home/jeanleo/bochs# umount /mnt/img/
root@Jean-vm:/home/jeanleo/bochs# kpartx -d disk.img 
loop deleted : /dev/loop1

13、往后需要更新内核的时候,设备挂载操作:
root@Jean-vm:/home/jeanleo/bochs# kpartx -av disk.img 
add map loop1p1 (252:0): 0 98752 linear /dev/loop1 2048
root@Jean-vm:/home/jeanleo/bochs# mount /dev/mapper/loop1p1 /mnt/img/

bochsrc文件配置:
megs: 2000
romimage: file=$BXSHARE/BIOS-bochs-latest
vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest
ata0-master: type=disk, path=”disk.img”, cylinders=100, heads=16, spt=63
boot: c
log: bochsout.txt
mouse: enabled=0
cpu: ips=15000000
clock: sync=both