forked from brho/akaros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-bootable-grub-hdd.txt
196 lines (146 loc) · 6.01 KB
/
make-bootable-grub-hdd.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
make-bootable-grub-hdd.txt
Barret Rhoden
2013-02-22
This document explains how to make a hard disk image file, install grub on the
image, and load your kernel (Akaros in the examples).
Initial Setup:
--------------------------
Commands that begin with # need root access. Commands beginning with $ should
be done as your development user.
You need the loop module loaded with max_part=10 (or something more than the
number of partions you are making on an image file).
# modprobe loop max_part=10
If your loop device is compiled into the kernel, add the kernel parameter "loop.max_part=10"
For example, once you partition an image file and connect it to a loopback
device (e.g., loop1), you will see the following devices:
/dev/loop1
/dev/loop1p1
/dev/loop1p2
..etc
Build your Image File:
--------------------------
This makes an image of size 268MB, which is 256MiB):
$ dd if=/dev/zero of=mnt/hdd.img bs=512 count=1 seek=524287
Connect to the image via a loopback device:
# losetup /dev/loop0 mnt/hdd.img
Fdisk the device:
# fdisk /dev/loop0
Create a new linux partition
Note it has 524288 sectors (1 + the seek offset)
Also note the partition begins at 2048, not 512 like it used to (changes
to fdisk, probably to get away from 512 byte alignments)
Disconnect and reconnect the loopback device, so we now can see the
partitions:
# losetup -d /dev/loop0
# losetup /dev/loop0 mnt/hdd.img
# ls /dev/loop0*
/dev/loop0 /dev/loop0p1
Make the filesystem:
# mkfs /dev/loop0p1
Create a mount point for your image file (as your user);
$ mkdir mnt/hdd/
Mount and chown. The chown only needs to be done the first time you mount the
device.
# mount /dev/loop0p1 mnt/hdd/
# chown -R brho:brho mnt/hdd/
Install Grub on the Image file:
--------------------------
This assumes legacy grub: for gentoo, emerge sys-boot/grub-static to get the
legacy grub. I glanced at grub2, but don't particularly want to mess with
that.
Set up the grub1 files and directories (assuming you still have the image
mounted and have access to stage1 and stage2 files).
$ mkdir -p mnt/hdd/boot/grub
$ cp /boot/grub/stage1 /boot/grub/stage2 /boot/grub/menu.lst mnt/hdd/boot/grub/
Edit menu.lst. Here's one similar to mine that works with Akaros:
default 0
timeout 5
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
terminal --timeout=5 serial console
title=Akaros
root (hd0,0)
kernel /kernel
Now put the kernel on the mounted image. You can do this whenever, btw, and
you'll do this whenever you want to update the kernel (my Makelocal has a
target that does this). So feel free to do this later.
$ cp obj/kern/kernel mnt/hdd/kernel
Actually install grub on the device. Do this as a regular user (not root), to
limit damage in case you mess up (e.g., accidentally write to /dev/sda instead
of your image file)
$ /sbin/grub --device-map=/dev/null
Enter the commands at the grub> prompt. I've included the output you
should see if things are going well:
grub> device (hd0) mnt/hdd.img
device (hd0) mnt/hdd.img
grub> root (hd0,0)
root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
setup (hd0)
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/e2fs_stage1_5" exists... no
Running "install /boot/grub/stage1 (hd0) /boot/grub/stage2 p /boot/grub/menu.lst "... succeeded
Done.
grub> quit
quit
That's it. Whenever you reboot, you'll need to recreate the loopback device
and remount the image at mnt/hdd/. Check out my "kvm-up.sh" script for how I
do this (it's basically just losetup, sleep, and mount).
Whenever you update the kernel, cp it into mnt/hdd/kernel, and sync. The sync
is necessary so the image file / backing store gets updated right away. If
you don't do this, your VM might see the old version of kernel if you run the
VM right away (before the FS naturally syncs). Check out my Makelocal target
for kvm for how to do this.
Old Stuff:
--------------------------
I originally wrote this back in 2009. It works for older versions of fdisk
and has some acrobatics with loopback devices that will help if you can't use
the max_part parameter to the loop module. Also, it has some examples with
using bochs
# make a 8MB image. picked these values so there is 1 cyl (minimum, it seems)
dd if=/dev/zero of=mnt/hdd.img bs=512 count=16065
losetup /dev/loop1 mnt/hdd.img
fdisk /dev/loop1
# determine the offset, in sectors
fdisk -ul /dev/loop1
# mult the sector offset by 512, since losetup offsets by bytes
# this will have us point loop2 to the partition on the disk
losetup -o 32256 /dev/loop2 /dev/loop1
mkfs /dev/loop2
mount /dev/loop2 mnt/hdd/
# copy over grub info
mkdir -p mnt/hdd/boot/grub
cp -r /boot/grub/stage1 /boot/grub/stage2 /boot/grub/menu.lst mnt/hdd/boot/grub
cp -r the_kernel mnt/hdd/
# edit accordingly
vi mnt/hdd/boot/grub/menu.lst
grub --device-map=/dev/null
# in here:
# important to not use the /dev/loop1, since there is a bug in grub
# use the image instead, since it bypasses whatever checks fail later
device (hd0) mnt/hdd.img
root (hd0,0)
setup (hd0) # make sure you don't do (hd0,0). it'll still work, but not the way you want
kvm mnt/hdd.img
# or
bochs -q 'ata0-master: type=disk, mode=flat, path="./mnt/hdd.img", cylinders=1, heads=255, spt=63'
# to use a floppy image (made similarly)
bochs -q 'floppya: 1_44=mnt/floppy.img, status=inserted' 'boot:a'
# to easily edit, keep the hdd image mounted and just copy in your kernel or
# whatever
# list the loops, delete them with -d to keep things nice and clean
losetup -a
losetup -o 32256 /dev/loop0 mnt/hdd.img
mount /dev/loop0 mnt/hdd
chown -R brho:brho mnt/hdd
# you'll need to make sure changes to the mnt/hdd take effect immediately
# if you want to run a VM right away with the .img
sync
Notes:
--------------------------
http://www.linuxjournal.com/article/4622
http://sig9.com/bochs-grub
http://web2.clarkson.edu/projects/itl/honeypot/ddtutorial.txt
http://www.mail-archive.com/bug-grub@gnu.org/msg09648.html
http://www.omninerd.com/articles/Installing_GRUB_on_a_Hard_Disk_Image_File