blob: a5515e304765fce7ea16c76605eca2b28ef3aa22 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
#!/usr/bin/perl -T
package VServer;
our $VERSION = '1.2';
use strict;
use warnings;
sub get_context_id($) {
my $vserver = shift;
if ($vserver =~ /^([-a-z0-9._]*)$/) {
$vserver = $1;
};
my $dir = "/etc/vservers/$vserver";
return unless -d $dir;
open(my $context, '<', "$dir/context") || return undef;
my $cid = undef;
while (<$context>) {
if ($_ =~ m/([0-9]*)/) {
$cid = $1;
last;
}
}
close $context;
return $cid;
}
sub get_proc_dir($) {
my $context = shift;
my $dir = "/proc/virtual/$context/";
return $dir if (-d $dir);
return undef;
}
sub get_config_dir($) {
my $context = shift;
my $dir = qx(/usr/sbin/vuname --xid $context --get context 2> /dev/null);
return undef unless ($? eq 0);
chomp($dir);
return $dir;
}
sub get_name($) {
my $context = shift;
my $dir = get_config_dir($context) || return undef;
my $name = undef;
open(my $file, "<", "$dir/name") || return undef;
while (<$file>) {
chomp($name = $_);
last;
}
close($file);
return $name;
}
1;
__END__
=pod
=head1 NAME
VServer - little perl helpers for handling linux-vserver
=head1 SYNOPSIS
use VServer;
my $id = VServer::get_context_id("name");
=head1 DESCRIPTION
This module contains some functions for interfacing linux-vserver
with perl. It does not use the libvserver library, but reading files
in I</proc/virtual/> and I</etc/vservers/> and executing lightwight
external tools like I<vuname>.
=head2 Methods
=over 4
=item B<get_context_id($vserver_name)>
Returns the context id for a vserver given by its name. If an
invalid name is supplied or any other error occurs, B<undef>
is returned.
=item B<get_proc_dir($context)>
Build the directory in C</proc/virtual/> for the given context id.
The directory is build with the following template:
I</proc/virtual/$context/>. If the resulting directory does not exist
(e.g. because the vserver is not running), B<undef> is returned.
=item B<get_config_dir($context)>
Returns the config directory for the given context id. This is done
by calling C</usr/sbin/vuname>. If any error occurs, B<undef> is
returned.
=item B<get_name($context)>
Returns the name of the vserver specified by the given context id.
This uses B<get_config_dir($context)> and reading the name file
inside the config directory. If any error occurs, B<undef> is
returned.
=back
=head1 AUTHORS
Alexander Sulfrian <alexander@sulfrian.net>
=cut
|