Projects Linux-Vserver Script vserver-mount-fs
From 5dollarwhitebox.org Media Wiki
Contents |
[edit]
Summary
I created this script to assist with Linux-Vserver virtual servers created on LVM Partitions. The problem was that, once the filesystem is mounted, you need to attach the vserver's '/dev/hdv1' to a real vroot block device in order for quotas to work.
Download:
- vserver-mount-fs v0.01: http://5dollarwhitebox.org/media/projects/linux/vserver-mount-fs/vserver-mount-fs.tar.gz
[edit]
Related Articles
- Linux-Vserver:
[edit]
Usage
The usage is very basic:
SYNTAX: vserver-mount-fs [mount | umount] vserver_name Mount the LVM, Attach a Vroot Device, etc # vserver-mount-fs mount vserver1 Then you can use the standard linux-vserver utils: # vserver vserver1 start # vserver vserver1 stop Umount the partition when done: # vserver-mount-fs umount vserver1
[edit]
The Scripts
[edit]
vserver-mount-fs
#!/bin/bash
# $id: vserver-mount-fs, v 0.01 2006/01/25
# Script to assist with: linux-vserver/LVM/Quota/Vroot
# Copyright (C) 2005,2006 BJ Dierkes <wdierkes@5dollarwhitebox.org>
#
# 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 (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#
#
# The Purpose:
#
# The purpose of this script is to setup logical volumes (LVM)
# before and after starting the vservers using the util-vserver init scripts
#
# The Setup:
#
# This script looks for the file "$VSERVER_CONF/[vserver_name]/apps/init/lv_exists"
# Logically, if it finds this file then that means that the vserver is running on a
# LV (Logical Volume) and therefore we should execute the script. Secondly, within the file
# hold the Logical Volume path that this LV belongs to. For Example "/dev/vg0". The script
# uses this variable to perform the mount operations and etc.
#
VRUN_DIR=/var/run/vservers
VSERVER_BASEDIR=/var/lib/vservers
VSERVER_CONF=/etc/vservers
function showHelp()
{
echo ""
echo "$0: Used to mount Logical Volumes with linux-vserver virtual servers"
echo ""
echo "usage: ./vserver-mount-fs [mount | umount] [vserver_name] "
echo ""
echo "example: ./vserver-mount-fs mount vserver1"
echo ""
exit 0
}
case "$1" in
--help)
showHelp $0
;;
(mount | umount)
OPTION_MOUNT=$1
VSERVER=$2
if [ $OPTION_MOUNT == "mount" ]; then
VG_DIR=`cat ${VSERVER_CONF}/${VSERVER}/apps/init/lv_exists`
MOUNTED_DEV=`cat /proc/mounts | grep -w ${VG_DIR}/${VSERVER}`
MOUNTED_DIR=`cat /proc/mounts | grep -w ${VSERVER_BASEDIR}/${VSERVER}`
if [ -e "$VRUN_DIR/${VSERVER}" ]; then
echo "${VRUN_DIR}/${VSERVER} exists! Is $VSERVER currently running??? => I can't mount that Logical Volume!"
echo ""
exit 1
elif [ "$MOUNTED_DEV" != "" ]; then
echo "${VG_DIR}/${VSERVER} is currently mounted! => exitting now..."
exit 1
elif [ "$MOUNTED_DIR" != "" ]; then
echo "${VSERVER_BASEDIR}/${VSERVER} is currently mounted! => exitting now..."
exit 1
elif [ ! -e $VG_DIR/${VSERVER} ]; then
echo "Wow! The \"$2\" Logical Volume doesn't exist => exitting now!"
exit 1
else
echo "Setting up $VSERVER Logical Volume . . . "
e2fsck -p ${VG_DIR}/${2} && \
mount ${VG_DIR}/${VSERVER} ${VSERVER_BASEDIR}/${VSERVER} && \
rm -f ${VSERVER_BASEDIR}/${VSERVER}/dev/hdv1 && \
vrsetup /dev/vroot/${VSERVER} ${VG_DIR}/${VSERVER} && \
cp -fa /dev/vroot/${VSERVER} ${VSERVER_BASEDIR}/${VSERVER}/dev/hdv1
echo "[ DONE ]"
fi
elif [ $OPTION_MOUNT = "umount" ]; then
VG_DIR=`cat ${VSERVER_CONF}/${VSERVER}/apps/init/lv_exists`
MOUNTED_DEV=`cat /proc/mounts | grep -w ${VG_DIR}/${VSERVER}`
MOUNTED_DIR=`cat /proc/mounts | grep -w ${VSERVER_BASEDIR}/${VSERVER}`
MOUNTED_PROC=`cat /proc/mounts | grep -w ${VG_DIR}/${VSERVER}/proc`
MOUNTED_PTS=`cat /proc/mounts | grep -w ${VSERVER_BASEDIR}/${VSERVER}/dev/pts`
echo""
if [ -e "$VRUN_DIR/${VSERVER}" ]; then
echo "${VRUN_DIR}/${VSERVER} exists! Is $VSERVER currently running??? => I can't umount a busy volume!"
echo ""
exit 1
elif [ "$MOUNTED_DEV" = "" ]; then
echo "${VG_DIR}/${VSERVER} is not currently mounted! => exitting now..."
exit 1
elif [ ! -e $VG_DIR/${VSERVER} ]; then
echo "Wow! The \"$2\" Logical Volume doesn't exist => exitting now!"
exit 1
else
echo ""
echo "Breaking down the $VSERVER Logical Volume . . . "
mount -o remount,ro ${VG_DIR}/${VSERVER}
umount ${VG_DIR}/${VSERVER}
if [ "$MOUNTED_PROC" != "" ]; then
echo "ok"
#umount ${VG_DIR}/${VSERVER}/proc
fi
if [ "$MOUNTED_PTS" != "" ]; then
echo "ok"
#umount ${VG_DIR}/${VSERVER}/dev/pts
fi
vrsetup -d /dev/vroot/${VSERVER}
echo "[ DONE ]"
fi
fi
;;
*)
echo "ERROR => $0: \"$1\" is not a valid switch"
showHelp $0
exit 2
;;
esac
exit 0
[edit]
The INIT Script: mount-lvm-vservers
In addition to the main script, I've written an INIT script in PERL to determine on boot what vservers have LVM partitions, and whether to mount them or not determined by the vserver's 'mark' found in '/etc/vservers/vserver_name/apps/init/mark'.
#!/usr/bin/perl -w
# $id: mount-lvm-vservers, v 0.01 2006/01/25
# Script to assist with: linux-mount-fs
# Copyright (C) 2005,2006 BJ Dierkes <wdierkes@5dollarwhitebox.org>
#
# 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 (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#
# The Purpose:
#
# The purpose of this INIT script is to setup logical volumes (LVM)
# before the util-vserver scripts start up the vservers.
# It is using the vserver-mount-fs script to do all the mount operations
#
#
# The Setup:
#
# Basically, this script parses the dir $vserver_conf for all directories [vserver names]
# and then proceeds to look for the file $vserver_conf/vserver_name/apps/init/lv_exists
# If a Logical Volume Exists then it runs the vserver_mount_fs command to mount it
# and do all the crazy vroot stuff.
#
# The script also parses each vserver dir for the file 'apps/init/mark' which tells us the
# vserver's mark. In my case I mark all of my vservers as production, development, or testing.
# Therefore, in this script it only mounts the fs for vservers that are production or development
# based on the vserver's mark
#
# This INIT script needs to be config'd to run before the util-vserver init scripts
#
$vserver_conf = '/etc/vservers';
$vserver_mount_fs = "/usr/local/bin/vserver-mount-fs";
$case = $ARGV[0];
opendir(DIR,"$vserver_conf") || die "$!: Could not open $vserver_conf!\n";
@contents = readdir(DIR);
close DIR;
if (!$case) {
print "USAGE:\n\n";
print "./mount-lvm-vservers [ start | stop ]\n\n";
} elsif ($case eq "start") {
foreach $item (@contents) {
if ( -e "$vserver_conf/$item/apps/init/mark") {
$mark = `/bin/cat $vserver_conf/$item/apps/init/mark`;
chomp($mark);
if ($mark eq "development" || $mark eq "production") {
if ( -e "$vserver_conf/$item/apps/init/lv_exists") {
print "Logical Volume exists for vserver $item... attempting to mount\n";
if (system("$vserver_mount_fs mount $item")) {
print "The command failed with exit status $?\n\n";
};
}
} else {
print "${item}'s mark is not 'production' or 'development'... not mounting\n\n";
}
}
}
} elsif ($case eq "stop") {
foreach $item (@contents) {
if ( -e "$vserver_conf/$item/apps/init/lv_exists") {
print "Logical Volume exists for vserver $item... attempting to mount\n";
if (system("$vserver_mount_fs umount $item")) {
print "The command failed with exit status $?\n\n";
};
}
}
}
exit 0;
