L4Ka:Create disk image(fd,hd)

http://l4ka.org/projects/pistachio/ia32/gettingstarted.php

Environment Redhat9
Linux redhat9 2.4.20-8
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

In fact, this version gcc won’t compile the kernel correctly. I compile the kernel & user program
(grabmem kickstart l4test pingpong sigma0 x86-kernel)
in a remote mechine, which has the following environment:
Linux 2.6.23-ARCH
gcc version 4.2.2


1. Create a floopy disk image
a. create a directory "fdsource" in current directory, and copy the following files.
[jfo@redhat9 build]$ find fdsource
fdsource
fdsource/boot
fdsource/boot/grub
fdsource/boot/grub/stage1
fdsource/boot/grub/stage2
fdsource/boot/grub/e2fs_stage1_5
fdsource/boot/grub/menu.lst
fdsource/boot/grabmem
fdsource/boot/kickstart
fdsource/boot/l4test
fdsource/boot/pingpong
fdsource/boot/sigma0
fdsource/boot/x86-kernel


note that the grub files stage1, stage2 & e2fs_stage1_5 is copied from grub-0.97, which is built myself.

b. edit grub conf file "menu.lst"
[jfo@redhat9 build]$ cat fdsource/boot/grub/menu.lst
root (fd0,0)
default=0
timeout=3
serial –port=0x3f8 –speed=115200
terminal –timeout=0 serial
title L4Ka::Pistachio
        kernel /boot/kickstart
        module /boot/x86-kernel
        module /boot/sigma0
        module /boot/pingpong


note: please change the name of this configuration file to "menu.lst", "grub.conf" just won’t work!!!

c. use the following script to generate a floppy disk image
[jfo@redhat9 build]$ cat gen_fd_img.sh
#!/bin/sh
# generate floppy disk image

FD=a.img
GRUB=/opt/grub/sbin/grub      # install grub-0.9.7 in this directory
rm $FD
dd if=/dev/zero of=$FD bs=512 count=2880
mke2fs $FD
echo "######################################"
echo -e "tmke2fs successfull!"
echo "######################################"
echo

TMPDIR=_tmp
rm -rf $TMPDIR
mkdir $TMPDIR
sudo mount $FD $TMPDIR -o loop
sudo chmod 777 $TMPDIR
cp -a fdsource/ $TMPDIR
sudo umount $TMPDIR
rmdir $TMPDIR

cat <<EOF | $GRUB –batch –device-map=/dev/null
device (fd0) $FD
root (fd0)
setup (fd0)
quit
EOF

d. run you floppy image
[jfo@redhat9 build]$ $QEMU/qemu -nographic -fda a.img

of course you can also run it using bochs

2. Create a hard disk image
a. prepare a hd image
the hard disk image’s geometry is:
cylinders=16, heads=16, spt=63, total 16128 sectors, 8257536 bytes ~= 8M
[jfo@redhat9 build]$ dd if=/dev/zero of=c.img bs=512 count=16128

In fact you can also use bochs’s tool bximage.exe to create a hd image, just set the image size to 8M,
and it will produce exactly the same result.
Both c.img will contain all `0’s, which can be verified in ultraEdit or od.

b. make partiton
bochs-2.3.6 contains a linux image "dlxlinux"
edit its configuration file bochsrc.bxrc, add the following line"
ata0-slave: type=disk, path="c.img", mode=flat, cylinders=16, heads=16, spt=63

run dlxlinux,
dlx:~# fdisk -l

Disk /dev/hda: 4 heads, 17 sectors, 306 cylinders
Units = cylinders of 68 512 bytes

   Device Boot Begin   Start     End Blocks   Id System
/dev/hda1           1       1     306   10395+ 83 Linux native

Disk /dev/hdb: 16 heads, 63 sectors, 16 cylinders           
// this is recognized because we configured it in bochsrc.bxrc
Units = cylinders of 1008 512 bytes

   Device Boot Begin   Start     End Blocks   Id System


there will be a new device /dev/hdb.
now it’s time to create patitions on /dev/hdb using fdisk
dlx:~# fdisk /dev/hdb
Warning: invalid flag 0000 of partition table 4 will be corrected by w(rite)

Command (m for help): p

Disk /dev/hdb: 16 heads, 63 sectors, 16 cylinders
Units = cylinders of 1008 512 bytes

   Device Boot Begin   Start     End Blocks   Id System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-16): 1
Last cylinder or +size or +sizeM or +sizeK (1-16): 16

Command (m for help): p

Disk /dev/hdb: 16 heads, 63 sectors, 16 cylinders
Units = cylinders of 1008 512 bytes

   Device Boot Begin   Start     End Blocks   Id System
/dev/hdb1           1       1      16    8032+ 83 Linux native

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
(Reboot to ensure the partition table has been updated.)
hdb: hdb1
Syncing disks.

WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
dlx:~#

Ok, you have made the partition table. Now you can mke2fs /dev/hdb1 under dlxlinux
dlx:~# mke2fs /dev/hdb1

go back to linux and verify c.img

[jfo@redhat9 build]$ fdisk c.img
You must set cylinders.
You can do this from the extra functions menu.

Command (m for help): p

Disk c.img: 0 MB, 0 bytes
16 heads, 63 sectors/track, 0 cylinders            //
this is a little bit of error, since c.img is a regular file, not a block divece
Units = cylinders of 1008 512 = 516096 bytes

Device Boot    Start       End    Blocks   Id System
c.img1             1        16      8032+ 83 Linux

Command (m for help): q



c. like fdsource, prepare for hdsource
[jfo@redhat9 build]$ find hdsource/
hdsource/
hdsource/boot
hdsource/boot/grub
hdsource/boot/grub/stage1
hdsource/boot/grub/stage2
hdsource/boot/grub/e2fs_stage1_5
hdsource/boot/grub/menu.lst
hdsource/boot/grabmem
hdsource/boot/kickstart
hdsource/boot/l4test
hdsource/boot/pingpong
hdsource/boot/sigma0
hdsource/boot/x86-kernel


[jfo@redhat9 build]$ cat hdsource/boot/grub/menu.lst
root (hd0,0)
default=0
timeout=3
serial –port=0x3f8 –speed=115200
terminal –timeout=0 serial
title L4Ka::Pistachio
        kernel /boot/kickstart
        module /boot/x86-kernel
        module /boot/sigma0
        module /boot/pingpong
        module /boot/l4test
        module /boot/grabmem


d. generate hd disk image using script "gen_hd_img.sh"
#!/bin/sh
# gen_hdimg.sh

HD=c.img
GRUB=/opt/grub/sbin/grub
#rm $HD
#dd if=/dev/zero of=$HD bs=512 count=32256
#mke2fs $HD
#echo "######################################"
#echo -e "tmke2fs successfull!"
#echo "######################################"
#echo

TMPDIR=
tmp
rm -rf $TMPDIR
mkdir $TMPDIR
sudo losetup /dev/loop0 $HD -o 32256
sudo mount /dev/loop0 $TMPDIR
sudo chmod 777 $TMPDIR
sudo rm -rf $TMPDIR/
cp -a hdsource/
$TMPDIR
sudo umount $TMPDIR
rmdir $TMPDIR
sudo losetup -d /dev/loop0

cat <<EOF | $GRUB –batch –device-map=/dev/null
device (hd0) $HD
root (hd0,0)
setup (hd0)
quit
EOF

There is an alternative way that is much easier, just run the following script:
#!/bin/sh
# gen_hd_img_fromscratch.sh

HD=c.img
GRUB=/opt/grub/sbin/grub
rm $HD
dd if=/dev/zero of=$HD bs=512 count=16128

cat << EOF | fdisk -C16 -H16 $HD
n
p
1
1
16
w
EOF

sudo losetup /dev/loop0 $HD -o 32256
sudo mke2fs /dev/loop0
sudo losetup -d /dev/loop0
echo "######################################"
echo -e "tmke2fs successfull!"
echo "######################################"
echo

TMPDIR=___tmp
rm -rf $TMPDIR
mkdir $TMPDIR
sudo losetup /dev/loop0 $HD -o 32256
sudo mount /dev/loop0 $TMPDIR
sudo chmod 777 $TMPDIR
sudo rm -rf $TMPDIR/
cp -a hdsource/
$TMPDIR
sudo umount $TMPDIR
rmdir $TMPDIR
sudo losetup -d /dev/loop0

cat <<EOF | $GRUB –batch –device-map=/dev/null
device (hd0) $HD
root (hd0,0)
setup (hd0)
quit
EOF



when the script runs to the command fdisk, do as follows:
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklab
el
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): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-16, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-16, default 16): 16

Command (m for help): p

Disk c.img: 0 MB, 0 bytes
16 heads, 63 sectors/track, 16 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

Device Boot    Start       End    Blocks   Id System
c.img1             1        16      8032+ 83 Linux

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 ioct
l for device.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.



[jfo@redhat9 build]$ qemu -nographic -hda c.img