This file is indexed.

/usr/share/perl5/Lxctl/set.pm is in lxctl 0.3.1+debian-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package Lxctl::set;

use strict;
use warnings;
use autodie qw(:all);

use Getopt::Long;
use Digest::SHA qw(sha1_hex);
use Linux::LVM;

use Lxc::object;

use LxctlHelpers::helper;
use LxctlHelpers::config;

my %options = ();

my $yaml_conf_dir;
my $lxc_conf_dir;
my $root_mount_path;
my $templates_path;
my $vg;
my $config = new LxctlHelpers::config;


sub mac_create
{
	my ($self, $data) = @_;

	my $mac = sha1_hex($data);
	$mac =~ s/(..)(..)(..)(..).*/F0:$1:$2:$3:$4/;	
	return $mac;
}

sub set_hostname
{
	my $self = shift;

	defined($options{'hostname'}) or return;
	print "Setting hostname: $options{'hostname'}\n";

	open(my $hostname_file, '>', "$root_mount_path/$options{'contname'}/rootfs/etc/hostname");

	seek $hostname_file,0,0;

	print $hostname_file $options{'hostname'};

	close $hostname_file;

	my $searchdomain = $options{'searchdomain'};
	if (!defined($options{'searchdomain'})) {
		if ( -e "$root_mount_path/$options{'contname'}/rootfs/etc/resolv.conf" ) {
			$searchdomain = $self->{'helper'}->get_config("$root_mount_path/$options{'contname'}/rootfs/etc/resolv.conf", 'search');
		} else {
			$searchdomain = $config->get_option_from_main('set', 'SEARCHDOMAIN');
		}
	}

	$self->{'helper'}->change_config("$root_mount_path/$options{'contname'}/rootfs/etc/hosts", '127.0.0.1', "$options{'hostname'}.$searchdomain $options{'hostname'} localhost");

	return;
}

sub set_ipaddr
{
	my $self = shift;

	defined($options{'ipaddr'}) or return;
	if ($options{'ipaddr'} =~ m/\d+\.\d+\.\d+\.\d+\/(\d+)/ ) {
		my $netmask = $self->{'helper'}->cidr2ip($1);
		$options{'netmask'} = $netmask;
	}

	print "Setting IP: $options{'ipaddr'}\n";

	$self->{'helper'}->change_config("$root_mount_path/$options{'contname'}/rootfs/etc/network/interfaces", 'address', $options{'ipaddr'});

	return;
}

sub set_macaddr
{
	my $self = shift;

	if (defined($options{'macaddr'})) {
		print "Setting MAC: $options{'macaddr'}\n";
		$self->{'lxc'}->set_conf($options{'contname'}, "lxc.network.hwaddr", $options{'macaddr'});
		return;	
	}
	defined($options{'contname'}) or return;

	my $mac = "01:" . $self->mac_create($options{'contname'});
	print "Setting MAC: $mac\n";
	$self->{'lxc'}->set_conf($options{'contname'}, "lxc.network.hwaddr", $mac);
	$options{'macaddr'} = $mac;
	return;
} 

sub set_netmask
{
	my $self = shift;

	defined($options{'netmask'}) or return;

	print "Setting netmask: $options{'netmask'}\n";

	$self->{'helper'}->change_config("$root_mount_path/$options{'contname'}/rootfs/etc/network/interfaces", 'netmask', $options{'netmask'});

	return;
}

sub set_mtu
{
	my $self = shift;

	defined($options{'mtu'}) or return;

	print "Setting mtu: $options{'mtu'}\n";

	$self->{'helper'}->change_config("$root_mount_path/$options{'contname'}/rootfs/etc/network/interfaces", 'mtu', $options{'mtu'});
	$self->{'helper'}->change_config("$lxc_conf_dir/$options{'contname'}/config", 'lxc.network.mtu = ', $options{'mtu'});

	return;
}

sub set_ifname
{
	my $self = shift;
	defined($options{'ifname'}) or return;

	if ($options{'ifname'} eq "mac") {
		$options{'ifname'} = $options{'mac'};
		if (!defined($options{'ifname'})) {
			$options{'ifname'} = $config->get_option_from_yaml("$yaml_conf_dir/$options{'contname'}.yaml", "", "macaddr");
		}

		if (!defined($options{'ifname'})) {
			$options{'ifname'} = $self->{'lxc'}->get_conf($options{'contname'}, "lxc.network.hwaddr");
			$options{'macaddr'} = $options{'ifname'};
		}

		$options{'ifname'} =~ s/^..:(.*)/$1/;
		$options{'ifname'} =~ s/://g;

		$options{'ifname'} = "veth" . $options{'ifname'};
	} elsif ($options{'ifname'} eq "ip") {
		if (!defined($options{'ipaddr'})) {
			$options{'ipaddr'} = $config->get_option_from_yaml("$yaml_conf_dir/$options{'contname'}.yaml", "", "ipaddr");
			die "No IP address specified" if (!defined($options{'ipaddr'}));
		}
		$options{'ifname'} = $options{'ipaddr'};
		$options{'ifname'} =~ s/\d+\.\d+\.(\d+).(\d+)/$1$2/g;
		$options{'ifname'} = "lxc" . $options{'ifname'};
	}

	print "Setting interface (host part) name to $options{'ifname'}\n";	

	my $size = bytes::length($options{'ifname'});

	die "Wow... that thing is big... too big for me! Maximum I know how to handle is 15 bytes\n" if ($size > 15);

	my $old_name;
	my $step = 1;
	eval {
		$old_name = $config->get_option_from_yaml("$yaml_conf_dir/$options{'contname'}.yaml", "", "ifname") or die;
		$step++;
		system("ip link set $old_name down");
		$step++;
		system("ip link set $old_name name $options{'ifname'}");
		$step++;
		system("ip link set $options{'ifname'} up");
		$step++;
		1;
	} or do {
		print "THIS IS NOT FATAL: Failed to do step $step, can't change ifname of interface in runtime. Please, restart container manualy.\n";
	};

	$self->{'helper'}->change_config("$lxc_conf_dir/$options{'contname'}/config", 'lxc.network.link', $options{'ifname'});
	return;
}

sub set_defgw
{
	my $self = shift;

	defined($options{'defgw'}) or return;

	print "Setting gateway: $options{'defgw'}\n";

	$self->{'helper'}->change_config("$root_mount_path/$options{'contname'}/rootfs/etc/network/interfaces", 'gateway', $options{'defgw'});

	return;
}

sub set_dns
{
	my $self = shift;

	defined($options{'dns'}) or return;

	print "Setting DNS: $options{'dns'}\n";

	$self->{'helper'}->change_config("$root_mount_path/$options{'contname'}/rootfs/etc/resolv.conf", 'nameserver', $options{'dns'});

	return;
}

sub set_searchdomain
{
	my $self = shift;

	defined($options{'searchdomain'}) or return;

	print "Setting search domain: $options{'searchdomain'}\n";

	$self->{'helper'}->change_config("$root_mount_path/$options{'contname'}/rootfs/etc/resolv.conf", 'search', $options{'searchdomain'});

	my $hostname = $self->{'helper'}->get_config("$root_mount_path/$options{'contname'}/rootfs/etc/hostname", "");

	$self->{'helper'}->change_config("$root_mount_path/$options{'contname'}/rootfs/etc/hosts", '127.0.0.1', "$hostname.$options{'searchdomain'} $hostname localhost");

	return;
}

sub set_userpasswd
{
	my $self = shift;

	defined($options{'userpasswd'}) or return;

	print "Setting password for user: $options{'userpasswd'}\n";

	die "Failed to change password!\n\n"
		if system("echo '$options{'userpasswd'}' | chroot $root_mount_path/$options{'contname'}/rootfs/ chpasswd");

	return;
}

sub set_rootsz
{
	my $self = shift;

	defined($options{'rootsz'}) or return;

	$options{'roottype'} ||= 'lvm';
	if (lc($options{'roottype'}) eq 'file') {
		print "set rootsz is unsupported for root in file\n\n";
		return;
	}

	$options{'rootsz'} =~ m/^[+-]?\d+[.]?\d*[bBsSkKmMgGtTpPeE]/ or die "Bad size!";

	my %lvm_info = get_lv_info("/dev/$vg/$options{'contname'}");
	my $desired_size;

	my $tmp = $lvm_info{'size'};
	if ($options{'rootsz'} =~ m/^[+](.+)+/) {
		print "SET: $1\n";
		$desired_size = $self->{'lxc'}->convert_size($1, $lvm_info{'size_unit'}, 0);
		$desired_size += $lvm_info{'size'};
	} elsif ($options{'rootsz'} =~ m/^-(.*)+/) {
		print "Shrinking is not supported yet\n";
		$desired_size = $self->{'lxc'}->convert_size($1, $lvm_info{'size_unit'}, 0);
		$desired_size = $lvm_info{'size'} - $desired_size;
		if ($desired_size <= 0) {
			print "Can't resize this much!\n";
			return;
		}
	} else {
		$desired_size = $self->{'lxc'}->convert_size($options{'rootsz'}, $lvm_info{'size_unit'}, 0);
	}

	if ($desired_size == $lvm_info{'size'}) {
		print "Already desired size, exiting...\n";
		return;
	} elsif ($desired_size < $lvm_info{'size'}) {
		print "Shrinking is not supported yet\n";
		return;
	}

	$desired_size .= $lvm_info{'size_unit'};

        $options{'rootsz'} = $desired_size;

	print "Setting root size: $desired_size\n";

	(system("lvextend -L $desired_size /dev/$vg/$options{'contname'}") == 0) or die "Failed to extend logical volume.\n\n";
	(system("resize2fs /dev/$vg/$options{'contname'}") == 0) or die "Failed to resize filesystem.\n\n";

	return;
}

sub set_cgroup
{
	my $self = shift;

	my ($name, $value) = @_;

	defined($options{$name}) or return;

	print "Setting $name: $options{$name}\n";

	# Commenting out for now. cpu.shares can be any val
#	$options{$name} =~ m/^\d+$/ or
#		die "Bad $name option!\n\n";

	eval {
		$self->{'lxc'}->set_cgroup($options{'contname'}, $value, $options{$name}, 1);

		$self->{'lxc'}->set_conf($options{'contname'}, "lxc.cgroup." . $value, $options{$name});
	} or do {
		print "$@";
		die "Failed to change $name share!\n\n";
	};

	return;
}

sub set_autostart
{
	my $self = shift;

	defined($options{'autostart'}) or return;
	my $autostart = $options{'autostart'};
	my $name = $options{'contname'};

	if ($autostart == 0) {
		print "Removing $name from autostart\n";
		$self->{'helper'}->modify_config("/etc/default/lxc", "CONTAINERS", $name, "");
	} else {
		print "Adding $name to autostart\n";
		$self->{'helper'}->modify_config("/etc/default/lxc", "CONTAINERS", "\"\$", " $name\"");
	}
}

sub set_tz()
{
	use File::Copy "cp";
	my $self = shift;

	defined($options{'tz'}) or return;

	print "Setting timesone: $options{'tz'}...\n";
	my $cont_root_path = "$root_mount_path/$options{'contname'}/rootfs";

	-e "$cont_root_path/usr/share/zoneinfo/$options{'tz'}" or die "No such timezone: $options{'tz'}!\n\n";

	cp("$cont_root_path/usr/share/zoneinfo/$options{'tz'}", "$cont_root_path/etc/localtime");
}

sub do
{
	my $self = shift;

	$options{'contname'} = shift
		or die "Name the container please!\n\n";

	die "Strange, rare name... I don't know how to deal with it.\n" if ($options{'contname'} =~ m/^-.*/);

	GetOptions(\%options, 'ipaddr=s', 'hostname=s', 'userpasswd=s', 
		'nameserver=s', 'searchdomain=s', 'rootsz=s', 
		'netmask|mask=s', 'defgw|gw=s', 'dns=s', 'cpus=s', 'cpu-shares=s', 'mem=s', 'io=s', 
		'macaddr=s', 'autostart=s', 'tz|timezone=s', 'mtu=i', 'ifname=s');

	if (defined($options{'mem'})) {
		$options{'mem'} = $self->{'lxc'}->convert_size($options{'mem'}, "B");
	}

	# Dirty hack. set_macaddr used from create and should be able to work without --maccaddr option.
	$self->set_macaddr() if defined($options{'macaddr'});
	$self->set_ipaddr();
	$self->set_netmask();
	$self->set_defgw();
	$self->set_dns();
	$self->set_hostname();
	$self->set_searchdomain();
	$self->set_userpasswd();
	$self->set_rootsz();
	$self->set_autostart();
	$self->set_tz();
	$self->set_mtu();
	$self->set_ifname();
	$self->set_cgroup('cpu-shares', 'cpu.shares');
	$self->set_cgroup('cpus', 'cpuset.cpus');
	$self->set_cgroup('mem', 'memory.limit_in_bytes');
	$self->set_cgroup('io', 'blkio.weight');

	$config->change_hash(\%options, "$yaml_conf_dir/$options{'contname'}.yaml");

	return;
}

sub new
{
	my $class = shift;	
	my $self = {};
	bless $self, $class;

	$self->{'lxc'} = Lxc::object->new;
	$self->{'helper'} = LxctlHelpers::helper->new;

	$root_mount_path = $self->{'lxc'}->get_roots_path();
	$templates_path = $self->{'lxc'}->get_template_path();
	$yaml_conf_dir = $self->{'lxc'}->get_config_path();
	$lxc_conf_dir = $self->{'lxc'}->get_lxc_conf_dir();
	$vg = $self->{'lxc'}->get_vg();

	%options = @_;

	return $self;
}

1;
__END__

=head1 AUTHOR

Anatoly Burtsev, E<lt>anatolyburtsev@yandex.ruE<gt>
Pavel Potapenkov, E<lt>ppotapenkov@gmail.comE<gt>
Vladimir Smirnov, E<lt>civil.over@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2011 by Anatoly Burtsev, Pavel Potapenkov, Vladimir Smirnov

This library is free software; you can redistribute it and/or modify
it under the same terms of GPL v2 or later, or, at your opinion
under terms of artistic license.

=cut