| 231 | |
| 232 | = Booting RTEMS Applications from a Virtual Drive = |
| 233 | |
| 234 | As an alternative to booting RTEMS applications from the virtual network via TFTP you can also boot them from a virtual drive. |
| 235 | |
| 236 | == Creating the Disk Image == |
| 237 | |
| 238 | 1) Create a new virtual machine with a fixed-size, VHD hard disk. Attach a SystemRescueCD image as the optical drive. |
| 239 | |
| 240 | A) I created a 32-bit Ubuntu compatible machine. (It has to have |
| 241 | enough resources to run Linux from the SystemRescueCD. You can |
| 242 | decrease the amount of some of the resources used by the machine |
| 243 | after the disk is created.) |
| 244 | |
| 245 | B) You can probably use a dynamically sized disk if you only |
| 246 | want to mount the disk from Windows. A VHD fixed-size disk |
| 247 | is just a hard disk image with a footer, so you can mount |
| 248 | it in Linux and/or access it with mtools (or other hard disk |
| 249 | image manipulation tools). I created a 50 MB hard disk. |
| 250 | |
| 251 | C) Google SystemRescueCD to find the free CD. |
| 252 | |
| 253 | 2) Boot the virtual machine from the SystemRescueCD. Startup the graphical interface by typing "startx" at the command prompt. Start the "GParted" tool from the "System" menu. Create a "gpt" partition table on the disk (from the Device menu). Create a new 1 MB primary, ext4 partition for grub (the boot manager). Align the partition to a cylinder. Create a second primary, FAT32 partition that takes up the remainder of the space. Again, align the partition to a cylinder (not necessary if you don't intend to use mtools). Be sure to apply the operations by clicking on the big green check mark. Right-click on the first partition (/dev/sda1), select "Manage Flags" and click on "bios_grub". Right-click on the second partition (/dev/sda2) and format to FAT32. Close GParted. |
| 254 | |
| 255 | A) I just used the default options during the SystemRescueCD boot. |
| 256 | |
| 257 | B) The default partition table type is "msdos", use the drop-down |
| 258 | menu to change this to "gpt". An msdos (MBR) partition table works |
| 259 | fine also and in that case you only need to create a single FAT32 |
| 260 | partition, I just decided to go with the newer gpt table. |
| 261 | |
| 262 | C) The grub partition can be of any type (it won't be formatted), |
| 263 | I just used the default ext4. |
| 264 | |
| 265 | D) Aligning the partitions to a cylinder instead of a Megabyte |
| 266 | prevents some mtools warnings that may occur later. |
| 267 | |
| 268 | E) You can choose filesystems other than FAT, but you want to choose something that is understood by both GRUB and the host operating system. |
| 269 | |
| 270 | 3) From a terminal window, mount the new partition by entering: |
| 271 | |
| 272 | {{{ |
| 273 | mount /dev/sda2 /mnt/custom |
| 274 | }}} |
| 275 | |
| 276 | Install grub by entering: |
| 277 | |
| 278 | {{{ |
| 279 | grub-install --boot-directory=/mnt/custom/boot /dev/sda |
| 280 | }}} |
| 281 | |
| 282 | An "ls /mnt/custom" command should now show a "boot" directory. |
| 283 | After reading the note below, shut down the virtual machine. |
| 284 | |
| 285 | A) In order to use mtools you need to know the byte offset to the |
| 286 | start of the FAT partition. You can probably calculate this |
| 287 | offset using information from GParted or sfdisk. I find it |
| 288 | easier to open the disk file in a binary editor and search for |
| 289 | a particular string in the boot sector. The following command |
| 290 | will display the boot sector in hex and ASCII: |
| 291 | |
| 292 | {{{ |
| 293 | dd count=1 < /dev/sda2 | hexdump -C |
| 294 | }}} |
| 295 | (When I do this is see the string "mkfs.fat" starting at the |
| 296 | fourth byte in the sector.) |
| 297 | |
| 298 | |
| 299 | 4) Use the VirtualBox Virtual Media Manager to change the type of the VHD disk from "normal" to "immutable". This will prevent VirtualBox from opening the disk image with write-access. This detaches the disk from the Virtual Machine, so you will need to reattach it. Remove the SystemRescueCD from the machine. Restart the Virtual Machine and verify that it boots to a grub prompt. |
| 300 | |
| 301 | A) Changing the type to "immutable" allows you to open the disk image |
| 302 | with some other tool while also using it in VirtualBox. (Although |
| 303 | read-only is perhaps more desirable, VirtualBox will not allow you to use |
| 304 | the read-only type with hard disk images.) |
| 305 | |
| 306 | == Accessing the Disk Image from the Host == |
| 307 | There are several ways to read and write files to the disk image |
| 308 | from the host machine. I will mention three. (My preferred |
| 309 | method is mtools.) |
| 310 | |
| 311 | === mtools (Windows, Linux) === |
| 312 | Download the mtools archive. I use mtools from Windows, but you |
| 313 | need a UNIX build environment. I use cygwin. My .mtoolsrc file |
| 314 | looks like this: |
| 315 | |
| 316 | {{{ |
| 317 | drive r: |
| 318 | file="c:/Users/ar63/VirtualBox VMs/RTEMS/RTEMSGPT.vhd" offset=8225280 |
| 319 | }}} |
| 320 | |
| 321 | I found the offset by opening the VHD file in a binary editor and |
| 322 | searching for a string (mkfs.fat) in the boot sector. The offset |
| 323 | should be to the beginning of the boot sector not the string! The |
| 324 | offset should be a multiple of 512, so find the nearest multiple |
| 325 | of 512 below the string location. |
| 326 | |
| 327 | The mtools work directly with the fat file system on the image. |
| 328 | There is no need to mount the image. mdir gives a directory |
| 329 | listing of the current directory on the image. mcd changes |
| 330 | current directories on the image. mcopy copies files to and |
| 331 | from the image. mtype displays the contents of a file. There |
| 332 | is no medit, but you can copy a file from the image to the host, |
| 333 | edit it and then copy it back. |
| 334 | |
| 335 | I have not been able to get mtools to work properly when built from |
| 336 | MSYS or MSYS2. The directory tools and the tools that copy to the |
| 337 | image seem to work fine, but I can't copy from the image to the host |
| 338 | or use mtype to display the contents on the disk image. |
| 339 | |
| 340 | === Disk Management Tool (Windows) === |
| 341 | Use the Disk Management Tool to attach the VHD to the system (from |
| 342 | the Action menu). Use the Windows Explorer to copy/edit files on |
| 343 | the disk image. |
| 344 | |
| 345 | Write-caching may cause files written to the image from the host to |
| 346 | not be immediately visible in the Virtual Machine. However, when I |
| 347 | try to turn write-caching off, Windows disconnects the image. It |
| 348 | is probably safest to detach the image after accessing it from the host. |
| 349 | Sysinternals provides a "sync" command that can help with this issue. |
| 350 | |
| 351 | === Loopback Device (Linux) === |
| 352 | Enter the commands: |
| 353 | |
| 354 | {{{ |
| 355 | losetup -o 8225280 /dev/loop0 RTEMSGPT.vhd |
| 356 | mount -t vfat /dev/loop0 /mnt |
| 357 | }}} |
| 358 | |
| 359 | or as a single command: |
| 360 | |
| 361 | {{{ |
| 362 | mount -t vfat RTEMSGPT.vhd /mnt -o loop,offset=8225280 |
| 363 | }}} |
| 364 | |
| 365 | You will need to find the correct offset to the start of the |
| 366 | FAT partition. |
| 367 | |
| 368 | == Using GRUB == |
| 369 | |
| 370 | To boot an RTEMS application named hello.exe from the grub command prompt: |
| 371 | |
| 372 | {{{ |
| 373 | multiboot (hd0,2)/hello.exe |
| 374 | boot |
| 375 | }}} |
| 376 | |
| 377 | The (hd0,2) notation tells grub to look on the second partition of the first |
| 378 | disk. |
| 379 | |
| 380 | It is a lot easier to use the grub menu. Grub looks for a file named |
| 381 | grub.cfg in the boot/grub directory. I find it more convenient to |
| 382 | keep my configuration file in the root directory of the image. I do this |
| 383 | by creating a grub.cfg file in the boot/grub directory that looks like |
| 384 | this: |
| 385 | |
| 386 | {{{ |
| 387 | configfile (hd0,2)/grub.cfg |
| 388 | }}} |
| 389 | |
| 390 | This causes grub to read a second configuration file (also named grub.cfg) |
| 391 | from the root directory of the image. The grub.cfg file in the root |
| 392 | directory looks like this: |
| 393 | |
| 394 | {{{ |
| 395 | menuentry "HELLO" { |
| 396 | multiboot (hd0,2)/hello.exe |
| 397 | } |
| 398 | |
| 399 | menuentry "tftp" { |
| 400 | insmod chain |
| 401 | set root=(fd0) |
| 402 | chainloader +1 |
| 403 | } |
| 404 | |
| 405 | menuentry "reboot" { |
| 406 | reboot |
| 407 | } |
| 408 | |
| 409 | }}} |
| 410 | You can edit this menu to add additional items. The second entry |
| 411 | in the menu chainloads an etherboot image from an attached floppy. |
| 412 | This allows you to either boot RTEMS applications from the virtual disk or via TFTP. |
| 413 | |
| 414 | |
| 415 | |
| 416 | |