Skip to content

Commit 9a4dc7f

Browse files
committed
Add infos about diskimage.sh
1 parent 6ee0130 commit 9a4dc7f

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

Chapter-3/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,106 @@ struct multiboot_info {
6868
};
6969
```
7070

71+
You can use the command ```mbchk kernel.elf``` to valid your kernel.elf file with the multiboot standard. You also use the command ```nm -n kernel.elf``` to valid the offset of the differents objects in the ELF binary.
72+
73+
#### Create a disk image for our kernel and grub
74+
75+
The script [diskimage.sh](https://github.com/SamyPesse/How-to-Make-a-Computer-Operating-System/blob/master/src/sdk/diskimage.sh) will generate a hard disk image than can be used by QEMU.
76+
77+
The first step is to create a hard-disk image (c.img) using qemu-img:
78+
79+
```
80+
qemu-img create c.img 2M
81+
```
82+
83+
We need now to partition the disk using fdisk:
84+
85+
```
86+
fdisk ./c.img
87+
88+
# Switch to Expert commands
89+
> x
90+
91+
# Change number of cylinders (1-1048576)
92+
> c
93+
> 4
94+
95+
# Change number of heads (1-256, default 16):
96+
> h
97+
> 16
98+
99+
# Change number of sectors/track (1-63, default 63)
100+
> s
101+
> 63
102+
103+
# Return to main menu
104+
> r
105+
106+
# Add a new partition
107+
> n
108+
109+
# Choose primary partition
110+
> p
111+
112+
# Choose partition number
113+
> 1
114+
115+
# Choose first cylinder (1-4, default 1)
116+
> 1
117+
118+
# Choose last cylinder, +cylinders or +size{K,M,G} (1-4, default 4)
119+
> 4
120+
121+
# Toggle bootable flag
122+
> a
123+
124+
# Choose first partition for bootable flag
125+
> 1
126+
127+
# Write table to disk and exit
128+
> w
129+
```
130+
131+
We need now to atach the created partition to loop-device (which allow a file to be access like a block device) using losetup. The offset of the partition is passed as an argument and calculed using: **offset= start_sector * bytes_by_sector**.
132+
133+
Using ```fdisk -l -u c.img```, you get: 63 * 512 = 32356.
134+
135+
```
136+
losetup -o 32256 /dev/loop1 ./c.img
137+
```
138+
139+
We create a EXT2 filesystem on this new device using:
140+
141+
```
142+
mke2fs /dev/loop1
143+
```
144+
145+
We copy our files on a mounted disk:
146+
147+
```
148+
mount /dev/loop1 /mnt/
149+
cp -R bootdisk/* /mnt/
150+
umount /mnt/
151+
```
152+
153+
Install GRUB on the disk:
154+
155+
```
156+
grub --device-map=/dev/null << EOF
157+
device (hd0) ./c.img
158+
geometry (hd0) 4 16 63
159+
root (hd0,0)
160+
setup (hd0)
161+
quit
162+
EOF
163+
```
164+
165+
And finally we detach the loop device:
166+
167+
```
168+
losetup -d /dev/loop1
169+
```
170+
71171
#### See Also
72172

73173
* [GNU GRUB on wikipedia](http://en.wikipedia.org/wiki/GNU_GRUB)

0 commit comments

Comments
 (0)