From ef238be46150e932c65f2bd67aa4325924df2720 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 12 Feb 2014 03:33:59 +0100 Subject: get_context_id_by_dir: add function --- lib/VServer.pm | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/VServer.pm b/lib/VServer.pm index c0ba688..b586a9e 100644 --- a/lib/VServer.pm +++ b/lib/VServer.pm @@ -1,7 +1,7 @@ #!/usr/bin/perl -T package VServer; -our $VERSION = '1.3'; +our $VERSION = '1.4'; use strict; use warnings; @@ -29,6 +29,22 @@ sub get_context_id_by_name($) { return $cid; } +sub get_context_id_by_dir($) { + my $dir = shift; + my $id = undef; + + open(my $info, "<", "$dir/info") || return undef; + while (<$info>) { + if ($_ =~ m/^ID:\s*([0-9.]+)$/) { + $id = $1; + last; + } + } + close($info); + + return $id; +} + sub get_proc_dir($) { my $context = shift; my $dir = "/proc/virtual/$context/"; @@ -93,6 +109,14 @@ Returns the context id for a vserver given by its name. If an invalid name is supplied or any other error occurs, B is returned. +=item B + +Returns the context id for a vserver given by the path in the +I tree. This could be done by parsing the path, +but the function uses the I file and parsing the C line. +If the I file is not found or does not contain a line starting +with C, B is returned. + =item B Build the directory in C for the given context id. -- cgit v1.2.3-1-g7c22