1 |
package UML::Clone; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use UML::Config; |
7 |
use UML::Utils; |
8 |
|
9 |
sub get_cmd_dd { |
10 |
|
11 |
my $of = shift; |
12 |
my $fsize = shift; # megabyte |
13 |
my $bs = shift; # kilobyte |
14 |
my $count = $fsize * 1024 / $bs; |
15 |
$bs .= 'k'; |
16 |
|
17 |
return "dd if=/dev/zero of=$of bs=$bs count=$count"; |
18 |
|
19 |
} |
20 |
|
21 |
|
22 |
sub DirectoryStructure { |
23 |
my $vhost_name = shift; |
24 |
my $vhost = get_host_cfg($vhost_name); |
25 |
my $basedir = get_host_basepath($vhost_name); |
26 |
my $cmd = " |
27 |
mkdir -p $basedir/rootfs |
28 |
mkdir -p $basedir/datafs |
29 |
mkdir -p $basedir/mnt/rootfs |
30 |
mkdir -p $basedir/mnt/datafs |
31 |
mkdir -p $basedir/tmp |
32 |
"; |
33 |
`$cmd`; |
34 |
} |
35 |
|
36 |
sub RootFS { |
37 |
my $vhost_name = shift; |
38 |
my $skel_name = shift; |
39 |
|
40 |
my $skel_rootfs_file = UML::Config::get_skel_rootfs_file($skel_name); |
41 |
my $skel_rootfs_path = UML::Config::get_skel_rootfs_path($skel_name); |
42 |
|
43 |
my $vhost_main_basepath = get_host_basepath($vhost_name); |
44 |
my %vhost_cfg = %{get_host_cfg($vhost_name)}; |
45 |
my $rootfs = $vhost_main_basepath . '/rootfs/' . $vhost_cfg{'rootfs'}; |
46 |
|
47 |
my $targetfile = "$vhost_main_basepath/rootfs/$vhost_cfg{'rootfs'}"; |
48 |
if (-f $targetfile) { |
49 |
print "rootfs already exists (\"$targetfile\"), will not overwrite it!", "\n"; |
50 |
return; |
51 |
} |
52 |
|
53 |
my $cmd; |
54 |
#$cmd = "cp $skel_rootfs_path $targetfile.gz && gunzip $targetfile.gz"; |
55 |
$cmd = "tar -xzf $skel_rootfs_path.tar.gz --directory=$vhost_main_basepath/rootfs/ && tune2fs -c 0 -i 0 $targetfile"; |
56 |
askCmd('do it', $cmd); |
57 |
} |
58 |
|
59 |
sub DataFS { |
60 |
my $vhost_name = shift; |
61 |
my $skel_name = shift; |
62 |
|
63 |
my $skel_datafs_file = UML::Config::get_skel_datafs_file($skel_name); |
64 |
my $skel_datafs_path = UML::Config::get_skel_datafs_path($skel_name); |
65 |
|
66 |
my $vhost_main_basepath = UML::Config::get_host_basepath($vhost_name); |
67 |
my %vhost_cfg = %{get_host_cfg($vhost_name)}; |
68 |
my $datafs = $vhost_main_basepath . '/datafs/' . $vhost_cfg{'datafs'}; |
69 |
|
70 |
if (-f $datafs) { |
71 |
print "datafs already exists (\"$datafs\"), will not overwrite it!", "\n"; |
72 |
return; |
73 |
} |
74 |
|
75 |
my $cmd; |
76 |
|
77 |
my $cmd_dd = get_cmd_dd($datafs, $vhost_cfg{'datafs_size'}, 4); |
78 |
if ($vhost_cfg{'datafs_type'} eq 'reiserfs') { |
79 |
$cmd = "$cmd_dd && mkreiserfs -f -f $datafs"; |
80 |
} |
81 |
if ($vhost_cfg{'datafs_type'} eq 'ext3') { |
82 |
$cmd = "$cmd_dd && mke2fs -j -F $datafs && tune2fs -c 0 -i 0 $datafs"; |
83 |
} |
84 |
#$cmd = "tar -xzf $skel_datafs_path --directory=$vhost_main_basepath/datafs/"; |
85 |
askCmd('do it', $cmd); |
86 |
} |
87 |
|
88 |
sub SwapFS { |
89 |
my $vhost_name = shift; |
90 |
|
91 |
my $vhost_main_basepath = UML::Config::get_host_basepath($vhost_name); |
92 |
my $swapfs = "$vhost_main_basepath/swap"; |
93 |
|
94 |
if (-f $swapfs) { |
95 |
print "swapfs already exists (\"$swapfs\"), will not create a new one!", "\n"; |
96 |
return; |
97 |
} |
98 |
|
99 |
my $cmd; |
100 |
my $cmd_dd = get_cmd_dd($swapfs, 128, 1024); |
101 |
|
102 |
$cmd = "$cmd_dd && mkswap $swapfs"; |
103 |
askCmd('do it', $cmd); |
104 |
|
105 |
} |
106 |
|
107 |
sub Backup { |
108 |
my $vhost_name = shift; |
109 |
my $backupBase = '/home/uml/backup'; |
110 |
my $date = `date +%Y%m%d-%H%M`; |
111 |
$date =~ s/\n//g; |
112 |
my $backupTar = $vhost_name . '_' . $date . '.tar.gz'; |
113 |
#my $basedir = $cfg{'main'}{'path_base'} . '/' . $cfg{'hosts'}{$vhost_name}{'path_rel'}; |
114 |
my $basedir = get_host_basepath($vhost_name); |
115 |
|
116 |
my $backupSource = $basedir . '/' . $vhost_name; |
117 |
my $backupTarget = $backupBase . '/' . $backupTar; |
118 |
|
119 |
if (-d $backupSource) { |
120 |
my $cmd = "tar -cz --directory=$backupSource/.. --file=$backupTarget $vhost_name/"; |
121 |
#print "$cmd", "\n"; |
122 |
`$cmd`; |
123 |
} else { |
124 |
print "could not backup, source does not exist!", "\n"; |
125 |
} |
126 |
} |
127 |
|
128 |
sub PackAndRemove { |
129 |
my $vhost_name = shift; |
130 |
my $basedir = get_host_basepath($vhost_name); |
131 |
my $cmd = "rm -rf $basedir"; |
132 |
print "\n"; |
133 |
if (ask("remove \"$basedir\" via \"$cmd\"?")) { |
134 |
print "backing up vhost", "\n"; |
135 |
#Backup($vhost_name); |
136 |
print "removing vhost", "\n"; |
137 |
`$cmd`; |
138 |
} |
139 |
} |
140 |
|
141 |
1; |