News:

If you need help with OS/2 visit http://forum.os2world.com

Main Menu

Practical example: VPC to VBox disk converter

Started by jep, 2008.04.21, 11:35:54

Previous topic - Next topic

jep

Marked as: Normal
Hello,

here's some code to convert Virtual PC vhd hard disk images to Virtual Box Harddisk images. The script use the executable shipped with Virtual Box to convert virtual disks.

Note: This example use a technique for self installation and configuration, where it save data to itself (in the EA) and to create a Program Object in the VBox folder. You can then drag & drop a vhd file on the program object and create a new file suitable for VBox.

The script assume that the new file will use as much space as the old one, which may not be quite right, but you can manually comment that part out if you want.

/* VPC/2 to VBox virtual harddrive converter  */
'@ECHO OFF'
PARSE SOURCE . . prog

CALL RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
CALL SysLoadFuncs

CALL SysCls

CALL SysGetEA prog, 'VBOX', 'f_path' /* Read Extended Attribute (EA) from this .cmd-file  */
IF LENGTH( f_path ) = 0 | DIRECTORY( f_path ) = '' THEN /* Nothing set or firectory not found? */
DO
   PARSE VALUE VALUE( 'PATH',, 'OS2ENVIRONMENT' ) WITH '\OS2\SYSTEM' -2 boot_drive +2 /* Get boot drive */
   IF LENGTH( boot_drive ) = 0 THEN boot_drive = 'C:' /* Set C: and continue if not found */
   f_size = STREAM( boot_drive||'\config.sys', 'C', 'QUERY SIZE' ) /* Get size of config.sys */
   IF LENGTH( f_size ) = 0 THEN CALL Err_Msg 3 /* No size = not found? */
   f_in = CHARIN( boot_drive||'\config.sys', 1, f_size ) /* Read whole config.sys */
   IF LENGTH( f_in ) = 0 THEN CALL Err_Msg 4 /* Ehhh, not possbile, locked? */
   IF POS( 'VBOXDRV.SYS', TRANSLATE( f_in ) ) > 0 THEN
   DO
      PARSE VALUE TRANSLATE( f_in ) WITH f_path'\VBOXDRV.SYS'. /* Data before vbox driver */
      f_size = LASTPOS( 'DEVICE=', f_path ) /* Look for last DEVICE= statment */
      IF f_size = 0 THEN CALL Err_Msg 5 /* No offset from beginning? NOt possible, must be wrong! */
      f_path = SUBSTR( f_path, f_size + 7 ) /* Get path to vbox */
      DROP f_in f_size boot_drive /* Free memory for variables used */
      SAY 'Creating Program object in the Virtual Box folder...'
      IF SysCreateObject( 'WPProgram', 'VHD to VMDK Converter', '<VBOXFOLDER>', 'EXENAME='||prog||';PARAMETERS="%**P\%**F" "[Save to (same as vhd by default):]"', 'U' ) THEN
         CALL SysPutEA prog, 'VBOX', f_path /* Create Icon and set EA so we don't need to go through this again */
   END
   ELSE CALL Err_Msg 5 /* VBox driver not found, so it's not isntalled! */
END

IF LENGTH( f_path ) = 0 THEN CALL Err_Msg 5 /* Path to VBox found  */
IF DIRECTORY( f_path ) = '' THEN CALL Err_Msg 6 /* Possible to access it? */

PARSE VALUE TRANSLATE( ARG(1) ) WITH from_vpc'.VHD'.' 'to_vbox'.VMDK'. /* Get files to convert */
from_vpc = STRIP( from_vpc,, '"' ) /* Strip characters */
IF LENGTH( from_vpc ) = 0 THEN CALL Err_Msg 1 /* No input file specified? Tell how to use it */
to_vbox = STRIP( to_vbox,, '"' ) /* Strip unwanted characters before and after filename */
IF LENGTH( to_vbox ) = 0 THEN to_vbox = from_vpc /* No output file specified? Hmm, should just have a new extension and file format then */
PARSE VALUE SysDriveInfo( FILESPEC( 'D', to_vbox ) ) WITH . disk_free . /* Get information about free space where we'll place the the new vbox file */
IF disk_free < STREAM( from_vpc||'.VHD', 'C', 'QUERY SIZE' ) THEN CALL Err_Msg 2 /* Less free space than size of vhd file? Then it's not even worth trying (Comment out?) */

'@qemu-img convert '||from_vpc||'.vhd -O vmdk '||to_vbox||'.vmdk' /* Convert vhd file to vmdk */
RETURN rc

Err_Msg:
SELECT
   WHEN ARG(1) = 1 THEN DO
      f_size = LASTPOS( '\', prog )
      prog = SUBSTR( prog, f_size )
      PARSE VALUE prog WITH prog'.'.
      PARSE VALUE prog with .'\'prog'.'.
      SAY 'Usage: '||prog||' x:\path\to\vpc\file.vhd <y:\path\to\vbox\file.vmdk>'
   END
   WHEN ARG(1) = 2 THEN
      SAY 'There is not enough free space on the target drive to convert the VBox file'
   WHEN ARG(1) = 3 THEN
      SAY "Couldn't find config.sys"
   WHEN ARG(1) = 4 THEN
      SAY "Couldn't read config.sys"
   WHEN ARG(1) = 5 THEN
      SAY "Can't find Virtual Box installation"
   WHEN ARG(1) = 6 THEN
      SAY "Can't access the folder where Virtual Box should be installed ( "||f_path||" )"
   OTHERWISE
   SAY 'An unknown error occured, return code: -'||ARG(1)
END
   SAY ''
   SAY 'Press any Key to Exit'
   PARSE PULL
EXIT -ARG(1)