1
0
mirror of synced 2026-01-21 01:48:09 +00:00

2005-12-18 03:32:58 by steve

Added --size, --swap, and --mirror handling via patch from
 Radu Spineanu [radu /at/ debian.org]
This commit is contained in:
steve 2005-12-18 03:32:58 +00:00
parent a50161f0ab
commit 3c30385cf7

View File

@ -48,15 +48,15 @@
# --
# http://www.steve.org.uk/
#
# $Id: xen-create-image,v 1.2 2005-12-17 15:21:32 steve Exp $
# $Id: xen-create-image,v 1.3 2005-12-18 03:32:58 steve Exp $
#
use strict;
use File::Copy;
use File::Temp qw/ tempdir /;
use Getopt::Long;
#
# Options set on the command line.
#
@ -74,7 +74,10 @@ my $GATEWAY; # set with '--gateway=dd.dd.dd.dd'
my $NETMASK="255.255.255.0"; # set with '--mask=dd.dd.dd.dd'
my $BROADCAST="192.168.1.255"; # set with '--broadcase=ddd.dd.dd.d'
my $NETWORK="192.168.1.0"; # set with '--network=dd.dd.dd.dd'
my $DHCP=0; # This setting overides the other options
my $DHCP=0; # This setting overides the other network options
my $MIRROR="http://ftp.us.debian.org/debian"; # set with '--mirror=http://www.etc.com"'
my $SIZE="2000M"; # set with '--size=1000M[b] / 1G[b]"
my $SWAP_SIZE="128M"; # set with '--swapsize=... like --size"
#
# Parse options.
@ -87,7 +90,10 @@ GetOptions(
"broadcast=s", \$BROADCAST,
"network=s", \$NETWORK,
"dir=s", \$DIR,
"dhcp", \$DHCP
"dhcp", \$DHCP,
"mirror=s", \$MIRROR,
"size=s", \$SIZE,
"swapsize=s", \$SWAP_SIZE
);
@ -125,7 +131,8 @@ my $swap = $DIR . '/domains/' . $HOSTNAME . "/swap.img" ;
# Create swapfile and initialise it.
#
print "Creating swapfile : $swap\n";
`/bin/dd if=/dev/zero of=$swap bs=1024k count=128 >/dev/null 2>/dev/null`;
$SWAP_SIZE =~ s/Mb*$//i;
`/bin/dd if=/dev/zero of=$swap bs=1024k count=$SWAP_SIZE >/dev/null 2>/dev/null`;
print "Initializing swap file\n";
`/sbin/mkswap $swap`;
print "Done\n";
@ -134,7 +141,8 @@ print "Done\n";
# Create disk file and initialise it.
#
print "Creating disk image: $image\n";
`/bin/dd if=/dev/zero of=$image bs=2M count=1 seek=1024 >/dev/null 2>/dev/null`;
$SIZE =~ s/Mb*$/k/i;
`/bin/dd if=/dev/zero of=$image bs=$SIZE count=1 seek=1024 >/dev/null 2>/dev/null`;
print "Creating EXT3 filesystem\n";
`/sbin/mkfs.ext3 -F $image`;
print "Done\n";
@ -144,13 +152,14 @@ print "Done\n";
# Now mount the image, in a secure temporary location.
#
my $dir = tempdir( CLEANUP => 1 );
`mount -t ext3 -o loop $image $dir`;
`mount -t xfs -o loop $image $dir`;
#
# Test that the mount worked
#
if ( ! -d $dir . "/lost+found" )
{
my $mount = `/bin/mount`;
if ( ! $mount =~ /$image/) {
print "Something went wrong trying to mount the new filesystem\n";
exit;
}
@ -159,7 +168,7 @@ if ( ! -d $dir . "/lost+found" )
# Install the base system.
#
print "Running debootstrap to install the system. This will take a while!\n";
`debootstrap --arch i386 sarge $dir http://ftp.us.debian.org/debian`;
`debootstrap sarge $dir $MIRROR`;
print "Done\n";
#
@ -181,14 +190,12 @@ print APT<<E_O_APT;
# Stable
deb http://http.us.debian.org/debian sarge main contrib non-free
deb-src http://http.us.debian.org/debian sarge main contrib non-free
deb $MIRROR sarge main contrib non-free
#
# Security updates
#
deb http://security.debian.org/ stable/updates main contrib non-free
deb-src http://security.debian.org/ stable/updates main contrib non-free
E_O_APT
@ -297,7 +304,7 @@ fixupInittab();
#
# Now unmount the image.
#
`umount $dir`;
`umount $dir >/tmp/ceva 2>&1`;
#
@ -404,6 +411,37 @@ EOF
exit;
}
#
# Make sure we have a valid size
#
if ( !(($SIZE =~ /^\d+[GM]b*$/i) && ($SWAP_SIZE =~ /^\d+[GM]b*$/i)) )
{
print "Invalid size formats. Please use something like:\n";
print " --size=128Mb\n";
print " --size=2Gb\n";
exit;
}
# Convert Gb -> Mb
if ( $SIZE =~ /^(\d+)Gb*$/i )
{
$SIZE = $1 * 1024 . "M";
}
if ( $SWAP_SIZE =~ /^(\d+)Gb*$/i )
{
$SWAP_SIZE = $1 * 1024 . "M";
}
#
# Check mirror format
#
if (!($MIRROR =~ /^http/i))
{
print "Please enter a valid mirror.\n";
exit;
}
#
# Only one of DHCP / IP is required.