Changes between Version 17 and Version 18 of Developer/VirtualMachines/VirtualBox

Feb 5, 2018, 9:52:59 PM (17 months ago)
Tony Richardson



  • Developer/VirtualMachines/VirtualBox

    v17 v18  
    230230XXX: Determine if/how to run RTEMS network applications with the VirtualBox emulator.
     232= Booting RTEMS Applications from a Virtual Drive =
     234As an alternative to booting RTEMS applications from the virtual network via TFTP you can also boot them from a virtual drive.
     236== Creating the Disk Image ==
     2381) Create a new virtual machine with a fixed-size, VHD hard disk. Attach a SystemRescueCD image as the optical drive.
     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.) 
     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.
     251   C) Google SystemRescueCD to find the free CD.
     2532) 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.
     255   A) I just used the default options during the SystemRescueCD boot.
     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.
     262   C) The grub partition can be of any type (it won't be formatted),
     263   I just used the default ext4. 
     265   D) Aligning the partitions to a cylinder instead of a Megabyte
     266   prevents some mtools warnings that may occur later.
     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.
     2703) From a terminal window, mount the new partition by entering:
     273     mount /dev/sda2 /mnt/custom
     276   Install grub by entering:
     279     grub-install --boot-directory=/mnt/custom/boot /dev/sda
     282   An "ls /mnt/custom" command should now show a "boot" directory.
     283   After reading the note below, shut down the virtual machine.
     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:
     293     dd count=1 < /dev/sda2 | hexdump -C
     295   (When I do this is see the string "mkfs.fat" starting at the
     296   fourth byte in the sector.)
     2994) 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.
     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.)
     306== Accessing the Disk Image from the Host ==
     307There are several ways to read and write files to the disk image
     308from the host machine.  I will mention three.  (My preferred
     309method is mtools.)
     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:
     317  drive r:
     318        file="c:/Users/ar63/VirtualBox VMs/RTEMS/RTEMSGPT.vhd" offset=8225280
     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.
     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.
     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.
     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.
     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.
     351=== Loopback Device (Linux) ===
     352  Enter the commands:
     355   losetup -o 8225280 /dev/loop0 RTEMSGPT.vhd
     356   mount -t vfat /dev/loop0 /mnt
     359  or as a single command:
     362   mount -t vfat RTEMSGPT.vhd /mnt -o loop,offset=8225280
     365  You will need to find the correct offset to the start of the
     366  FAT partition.
     368== Using GRUB ==
     370To boot an RTEMS application named hello.exe from the grub command prompt:
     373        multiboot (hd0,2)/hello.exe
     374        boot
     377The (hd0,2) notation tells grub to look on the second partition of the first
     380It is a lot easier to use the grub menu.  Grub looks for a file named
     381grub.cfg in the boot/grub directory.  I find it more convenient to
     382keep my configuration file in the root directory of the image.  I do this
     383by creating a grub.cfg file in the boot/grub directory that looks like
     387    configfile (hd0,2)/grub.cfg
     390This causes grub to read a second configuration file (also named grub.cfg)
     391from the root directory of the image.  The grub.cfg file in the root
     392directory looks like this:
     395        menuentry "HELLO" {
     396          multiboot (hd0,2)/hello.exe
     397        }
     399        menuentry "tftp" {
     400          insmod chain
     401          set root=(fd0)
     402          chainloader +1
     403        }
     405        menuentry "reboot" {
     406          reboot
     407        }
     410You can edit this menu to add additional items.  The second entry
     411in the menu chainloads an etherboot image from an attached floppy.
     412This allows you to either boot RTEMS applications from the virtual disk or via TFTP.