[ previous ] [ next ] [ threads ]
 
 From:  "Michael Iedema" <iedemam at pluto dot dsu dot edu>
 To:  <m0n0wall at lists dot m0n0 dot ch>
 Subject:  m0n0image.php - simple builder script
 Date:  Fri, 9 Jan 2004 11:25:23 -0600
Greetings,
I know this has done before, but I'll post it anyway.  I have very
minimal shell scripting experience, but after reading a few articles on
using php as a shell scripting language I thought I'd give it a shot.

m0n0image.php will extract and compress m0n0wall images.  Invoked with a
-x flag, it decompresses the image into a DEVEL_imagename directory with
the contents of the image laid out inside.  Invoked with a -c flag, it
recompresses a DEVEL_imagename folder into a bootable image.

Many thanks to Rudi for the excellent hackers guide.  Most of these
commands should look pretty familiar :)

I've test built and run a few images with it on my 4511 and so far no
problems, but this is my first attempt so something is sure to be amiss
(permissions come to mind).

Please let me know if you have any comments or suggestions of cleaner
methods to accomplish this, I'm sure there are some, but this ended up
decent looking to me.

To get an clean version of this which hasn't been mangled by e-mail it
can be downloaded from
http://mrkasai.undergroup.com/m0n0wall/m0n0image.tgz

Thanks,
--Michael I

------------------------------------------------------------------------
-
#!/usr/local/bin/php
<?php
/*
 * m0n0image.php (c)2004-2005
 *
 * michael iedema - iedemam at pluto dot dsu dot edu
 *
 * simple script made to ease the build process for m0n0wall
 *
 * -x extracts contents into DEVEL_imagename
 * -c compresses contents of a DEVEL_imagename directory into a bootable
image
 *
 * usage: ./m0n0image.php [-x,-c] imagename.img
 */
 
$version = "0.1.0";

if((!$argv[1]) || (!$argv[2]) || (($argv[1] != "-x") && ($argv[1] !=
"-c")))
{
  print("m0n0image.php v$version      (C)2004 michael iedema
<iedemam at pluto dot dsu dot edu>");
  print("\nusage: $argv[0] [-x,-c] imagename.img\n");
  print("\t-x\textract contents of image into DEVEL_imagename\n");
  print("\t-c\tcompress DEVEL_imagename folder into a bootable
image\n\n");
}
elseif($argv[1] == "-x")
{
  $image = substr("$argv[2]", 0, -4);
  $image_dir = "DEVEL_".$image;
  
  $commands = array(
    /* directory creation */
    "mkdir $image_dir",
    "mkdir $image_dir/fs",
    "mkdir $image_dir/kern",
    /* rename image and decompress */
    "mv $image.img $image.bin.gz",
    "gzip -d $image.bin.gz",
    /* setup our virtual device & mount on /mnt */
    "vnconfig -s labels -c vn0 $image.bin",
    "mount /dev/vn0a /mnt",
    /* copy out our kernel.gz, mfsroot.gz, boot, and conf */
    "cp -p /mnt/kernel.gz $image_dir/kern/",
    "cp -p /mnt/mfsroot.gz $image_dir",
    "cp -Rp /mnt/boot $image_dir/boot",
    "cp -Rp /mnt/conf $image_dir/conf",
    /* unmount and destroy the virtual device */
    "umount /mnt",
    "vnconfig -u vn0",
    /* rezip our image and rename back to .img */
    "gzip -9 $image.bin",
    "mv $image.bin.gz $image.img",
    /* unzip our mfsroot.gz */
    "gzip -d $image_dir/mfsroot.gz",
    /* setup our virtual device & mount on /mnt */
    "vnconfig -s labels -c vn0 $image_dir/mfsroot",
    "mount /dev/vn0c /mnt",
    /* copy out our mfsroot into our devel/fs directory */
    "cp -Rp /mnt/* $image_dir/fs",
    /* unmount, destroy and clean up our mfsroot virtual device */
    "umount /mnt",
    "vnconfig -u vn0",
    "rm $image_dir/mfsroot");

  /* loop through and execute with pretty dots denoting completed
commands */
  print("extracting.");

  foreach($commands as $command)
  {
    exec($command);
    print(".");
  }
  
  print("\nextraction complete\n");
}
elseif($argv[1] == "-c")
{
  $directory = $argv[2];

  /* removes trailing slash of a directory path */
  if(($directory[strlen($directory)-1])=="/")
  {
    $directory = substr("$directory", 0, -1);
  }

  $commands = array(
  /* make a temp directory to do all this within */
  "mkdir tmp",
  /* create an image file for the root file system */
  "dd if=/dev/zero of=tmp/mfsroot.bin bs=1k count=10240 2>/dev/null",
  "vnconfig -s labels -c vn0 tmp/mfsroot.bin",
  "disklabel -rw vn0 auto 2>/dev/null",
  "newfs -b 8192 -f 1024 /dev/vn0c 2>dev/null",
  /* mount up our image and copy in our root file system */
  "mount /dev/vn0c /mnt",
  "cp -Rp $directory/fs/* /mnt",
  /* unmount and destroy our virtual device */
  "umount /mnt",
  "vnconfig -u vn0",
  /* compress and rename our root file system */
  "gzip -9 tmp/mfsroot.bin",
  "mv tmp/mfsroot.bin.gz tmp/mfsroot.gz",
  /* build our memory filesystem image */
  "dd if=/dev/zero of=tmp/$directory.bin bs=1k count=6144 2>/dev/null",
  "vnconfig -s labels -c vn0 tmp/$directory.bin",
  "disklabel -BR vn0 label.proto",
  "newfs -b 8192 -f 1024 /dev/vn0a",
  /* mount our empty image, and copy everything into it */
  "mount /dev/vn0a /mnt",
  "cp -Rp $directory/boot /mnt",
  "cp -Rp $directory/conf /mnt",
  "cp -p tmp/mfsroot.gz $directory/kern/kernel.gz /mnt",
  /* unmount and destroy our virtual device */
  "umount /mnt",
  "vnconfig -u vn0",
  /* compress and rename our image to DEVEL_originalimagename */
  "gzip -9 tmp/$directory.bin",
  "mv tmp/$directory.bin.gz $directory.img",
  /* clean up after ourselves */
  "rm -rf tmp",
  "rm label.proto");


  /* write out our label prototype */
  $fd = fopen("label.proto", "w");
  $labelproto=<<<EOD
# /dev/vn0c:
type: unknown
disk: amnesiac
label:
flags:
bytes/sector: 512
sectors/track: 32
tracks/cylinder: 64
sectors/cylinder: 2048
cylinders: 5
sectors/unit: 10240
rpm: 3600
interleave: 1
trackskew: 0
cylinderskew: 0
headswitch: 0           # milliseconds
track-to-track seek: 0  # milliseconds
drivedata: 0

8 partitions:
#        size   offset    fstype   [fsize bsize bps/cpg]
  a:    10240        0    4.2BSD     1024  8192    26   # (Cyl.    0 -
4)
  c:    10240        0    unused        0     0         # (Cyl.    0 -
4)
EOD;
  fwrite($fd, $labelproto);
  fclose($fd);


  print("compressing.");

  foreach($commands as $command)
  {
    exec($command);
    print(".");
  }

  print("\ncompression complete\n");


}
?>