代写 C shell ************************************************************************** *****

************************************************************************** *****
* HOWTO: Create a disk image file and mount it for use under Bochs * *** **
* NOTE: This assumes you have bochs installed on your system. If not, it can * * be downloaded from bochs.sourceforge.net * ************************************************************************** *****
1) Create a Bochs disk image by typing the following at the shell prompt (shown as a dollar symbol below) and then answer the on-screen questions (NOTE: here, we’ll create a 30MB disk image, for a virtual disk with 60 cylinders, 16 heads and 63 sectors per track. You can change this configuration to suit your needs. Default answers to on-screen questions are given in square brackets. You can hit return to accept these values.):
$ bximage ========================================================= ===============
bximage
Disk Image Creation Tool for Bochs
=========================================================
===============
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? Please type flat, sparse or growing. [flat]
Enter the hard disk size in megabytes, between 1 and 32255 [10] 30
I will create a ‘flat’ hard disk image with cyl=60
heads=16
sectors per track=63
total sectors=60480
total size=29.53 megabytes

What should I name the image? [c.img]
Writing: [] Done.
I wrote 30965760 bytes to c.img.
The following line should appear in your bochsrc:
ata0-master: type=disk, path=”c.img”, mode=flat, cylinders=60, heads=16,
spt=63
2) Set up a partition table in the virtual disk (NOTE: In this example, we’ll create a primary partition from which to boot a kernel image, and we’ll set its type to be a Linux ext2/ext3 native file system
paritition:
$ fdisk c.img
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won’t be recoverable.
You must set cylinders.
You can do this from the extra functions menu.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): x
Expert command (m for help): h
Number of heads (1-256, default 255): 16
Expert command (m for help): s
Number of sectors (1-63, default 63): 63
Warning: setting sector offset for DOS compatiblity
Expert command (m for help): c Number of cylinders (1-1048576): 60
Expert command (m for help): r
Command (m for help): n Command action
e extended
p primary partition (1-4)

p
Partition number (1-4): 1
First cylinder (1-60, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-60, default 60): Using default value 60
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 83
Command (m for help): a Partition number (1-4): 1
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 25: Inappropriate ioctl for device.
The kernel still uses the old table.
The new table will be used at the next reboot. Syncing disks.
3) Create an actual filesystem on the partition in the disk image (NOTE: here, we’ll create an ext2 native Linux file system using mke2fs. To
create other file systems, see ‘man mkfs’ on a Linux machine. You must be root user here).
The first step is to load the loop device module if it isn’t already part
of your kernel. Chances are, it may well be compiled into the core kernel image, so you can skip the ‘modprobe loop’ line below, which should otherwise be executed as a root user. You still need to perform the other three commands at the shell prompt, however. The most tricky part is working out the offset to ‘losetup’. In this example, it is 32256. You can find the actual value you need by typing at the shell ‘fdisk -u c.img’ followed by ‘p’ to print the partition table — the value under the
‘Start’ label is the sector offset for the beginning of the partition. You need to multiply this value by the sector size (typically 512 bytes) to
get the byte offset for ‘losetup’:
$ modprobe loop
$ losetup -o 32256 /dev/loop0 /path/to/c.img $ mke2fs /dev/loop0

mke2fs 1.36 (05-Feb-2005)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
7552 inodes, 30208 blocks
1510 blocks (5.00%) reserved for the super user First data block=1
4 block groups
8192 blocks per group, 8192 fragments per group 1888 inodes per group
Superblock backups stored on blocks:
8193, 24577
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 35 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
$ losetup -d /dev/loop0
4) Mount the disk image partition under the host OS (NOTE: In this example, we try to load the ext2 file system module in the kernel, if it isn’t already there. Chances are, you won’t have to do this if your system has it, or the appropriate file system functionality compiled into the core kernel. Here, we mount our virtual disk under /mnt/C to resemble a 30MB C drive):
$ modprobe ext2
$ mkdir /mnt/C
$ mount /path/to/c.img /mnt/C -text2 -o loop,offset=32256
5) Install grub onto the disk image (requires grub 0.95 or newer). This will be used as the bootloader to load a subsequent kernel image. We copy all the bootloader processing stages to the appropriate subdirectory of our virtual disk, and then we invoke the grub interactive shell to configure our bootloader boot script:
$ mkdir /mnt/C/boot
$ mkdir /mnt/C/boot/grub
$ cp /usr/share/grub/i386-redhat/{stage1,e2fs_stage1_5,stage2} /mnt/C/boot/grub
$ grub

grub> device (hd0) /path/to/c.img
grub> geometry (hd0) 60 16 63
drive 0x80: C/H/S = 60/16/63, The number of sectors = 60480, c.img
Partition num: 0, Filesystem type is ext2fs, partition type 0x83
grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
Checking if “/boot/grub/stage1” exists… yes
Checking if “/boot/grub/stage2” exists… yes
Checking if “/boot/grub/e2fs_stage1_5” exists… yes
Running “embed /boot/grub/e2fs_stage1_5 (hd0)”… 16 sectors are embedded.
succeeded
Running “install /boot/grub/stage1 (hd0) (hd0)1+16 p (hd0,0)/boot/grub/stage2
/boot/grub/grub.conf”… succeeded Done.
grub> quit
6) At this point you can copy your kernel image to /mnt/C/boot/. We’ll assume it’s a Linux kernel image called ‘vmlinuz’. You then need to create a suitable grub config file with a full pathname /mnt/C/boot/grub/menu.lst that contains something like this:
title My Linux Kernel root (hd0,0)
kernel /boot/vmlinuz
7) Edit the bochs configuration which you got with bochs. If you downloaded the bochs sources a config file is included in the soruce tree as “.bochsrc”. You need to modify the line that refers to your disk image as follows:
ata0-master: type=disk, path=/path/to/c.img, cylinders=60, heads=16, spt=63
8) Run “bochs -qf /path/to/bochsrc”, and with any luck the kernel will boot!
Good luck! You’ve just had an introduction to creating a virtual disk for use with Bochs, that you can use for booting your own kernel images.