|
Written by Jani Reinikainen
|
|
Nov 10, 2007 at 06:21 PM |
|
This is a script for quickly creating new Vservers for a Linux-Vserver patched system.
#!/usr/bin/perl
use strict;
use warnings;
# A script for quickly creating new Vservers for a Linux-Vserver system.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# By JB Consulting Oy Ab / Jani Reinikainen [ jani (at) jbc (dot) fi ]
# Version 0.4.0 (10. November 2007). http://jbc.fi/
my $vserverpath = '/var/lib/vservers';
my $distrib = 'gutsy';
if (!$ARGV[0]) {
die("Need at least one argument (vserver name)! Usage:\n ./newvserver.pl [name] [IP]\n");
}
my $ip = '';
if ($ARGV[1]) {
$ip = $ARGV[1];
}
# Check that a vserver with this name is not already running
my $output = `vserver-stat | grep $ARGV[0]\$`;
if ($output ne '') {
print "Vserver with name $ARGV[0] already running! Stop vserver? [y/N] ";
my $choice = ;
chop($choice);
if ($choice eq 'Y' || $choice eq 'y') {
print "Attempting to stop vserver $ARGV[0].\n";
`vserver $ARGV[0] stop`;
} else {
print "Not stopping vserver. Aborting.\n";
exit();
}
}
# Check if vserver path exists
if (-e "$vserverpath/$ARGV[0]") {
print "$vserverpath/$ARGV[0] already exists! Move $vserverpath/$ARGV[0] to $vserverpath/$ARGV[0].old? [y/N] ";
my $choice = ;
chop($choice);
if ($choice eq 'Y' || $choice eq 'y') {
print "Attempting to move old vserver files out of the way.\n";
`mv $vserverpath/$ARGV[0] $vserverpath/$ARGV[0].old`;
if (-e "$vserverpath/$ARGV[0]") {
print "Move seems to have failed. Aborting.\n";
exit();
}
} else {
print "Not moving old vserver files. Aborting '$choice'.\n";
exit();
}
}
# Check if configuration files with this name exist
if (-e "/etc/vservers/$ARGV[0]") {
print "Configuration files exist in /etc/vservers/$ARGV[0]. Move to /etc/vservers/$ARGV[0].old? [y/N] ";
my $choice = ;
chop($choice);
if ($choice eq 'Y' || $choice eq 'y') {
print "Attempting to move old configuration files.\n";
`mv /etc/vservers/$ARGV[0] /etc/vservers/$ARGV[0].old`;
if (-e "/etc/vservers/$ARGV[0]") {
print "Move seems to have failed. Aborting.\n";
exit();
}
} else {
print "Not moving old configuration files. Aborting.\n";
exit();
}
}
# Install base distribution
system("vserver $ARGV[0] build -n $ARGV[0] --hostname $ARGV[0] --interface eth0:$ip -m debootstrap -- -d $distrib");
# Symlink IP to hostname for quick reference
print "Symlinking $ip to $ARGV[0]\n";
chdir($vserverpath);
system("ln -s $ARGV[0]/ $ip");
# Generate hosts
system("echo '$ip\t$ARGV[0]' > $vserverpath/$ARGV[0]/etc/hosts");
# Generate sources.list for APT
system("echo 'deb http://fi.archive.ubuntu.com/ubuntu/ $distrib main restricted universe multiverse' > $vserverpath/$ARGV[0]/etc/apt/sources.list");
system("echo 'deb http://fi.archive.ubuntu.com/ubuntu/ $distrib-updates main restricted' >> $vserverpath/$ARGV[0]/etc/apt/sources.list");
system("echo 'deb http://fi.archive.ubuntu.com/ubuntu/ $distrib-backports main restricted universe multiverse' >> $vserverpath/$ARGV[0]/etc/apt/sources.list");
system("echo 'deb http://security.ubuntu.com/ubuntu $distrib-security main restricted' >> $vserverpath/$ARGV[0]/etc/apt/sources.list");
# Install language pack to avoid errors about locales
chroot("$vserverpath/$ARGV[0]");
system("apt-get update; apt-get install -y --force-yes language-pack-en-base");
# Remove crippled shell
system("rm /bin/sh");
system("ln -s /bin/bash /bin/sh");
# Start vserver while we're at it?
print "All done! Do you wish to start the vserver now? [Y/n] ";
my $choice = ;
chop($choice);
if ($choice ne 'N' || $choice ne 'n') {
print "Attempting to start vserver $ARGV[0].\n";
`vserver $ARGV[0] start`;
}
Some almost always useless stuff gets installed as well, so I usually do this inside the newly created Vserver (example is for Ubuntu Gutsy, i.e. 7.10):
# apt-get --purge remove alsa-base alsa-utils wpasupplicant wireless-tools pcmciautils xkb-data mii-diag module-init-tools initramfs-tools ifupdown ethtool dhcp3-client dhcp3-common makedev usbutils update-inetd pciutils laptop-detect eject
|
|
Last Updated ( Nov 11, 2007 at 06:47 PM )
|
Copyright © 2007 Jani Reinikainen. All rights reserved.
Permission granted to replicate information found on these pages, provided that all copyright headers/footers remain intact.