1 |
#!/usr/bin/perl |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
# path to modules |
7 |
use lib '/home/service/bin/umltools'; |
8 |
|
9 |
# path to config-file |
10 |
use lib '/home/service/etc'; |
11 |
|
12 |
END { |
13 |
print "\n"; |
14 |
} |
15 |
|
16 |
use Getopt::Long; |
17 |
use UML::Config; |
18 |
use UML::Clone; |
19 |
use UML::Utils; |
20 |
use ConfigPatcher::Main; |
21 |
use Fcntl ':mode'; |
22 |
|
23 |
sub usage { |
24 |
print "\n"; |
25 |
print " Usage: uml_setup.pl [OPTIONS] which may be ...", "\n"; |
26 |
print " --skel=<skel-name> ", "supply a skel-name to create virtual host from", "\n"; |
27 |
print " --vhost=<vhost-name> ", "supply a distingushed name for a virtual host", "\n"; |
28 |
print " --clear ", "clear a virtual host", "\n"; |
29 |
print " --create ", "create a virtual host", "\n"; |
30 |
print " --patch ", "patch a virtual host", "\n"; |
31 |
print " --build ", "tries a --create and --patch", "\n"; |
32 |
print " --rebuild ", "tries a --clear, --create and --patch", "\n"; |
33 |
print " --daemonize ", "creates proper scripts under /etc/init.d and -rc3.d/", "\n"; |
34 |
print " --ask ", "ask questions", "\n"; |
35 |
exit; |
36 |
} |
37 |
|
38 |
my $vhost_name; |
39 |
my $skel_name; |
40 |
my $bool_clear; |
41 |
my $bool_create; |
42 |
my $bool_patch; |
43 |
my $bool_build; |
44 |
my $bool_rebuild; |
45 |
my $bool_daemonize;; |
46 |
my $glbl_ask; |
47 |
my $args_parsed = GetOptions ( |
48 |
# "length=i" => \$length, # numeric |
49 |
# "file=s" => \$data, # string |
50 |
# "verbose" => \$verbose, # flag |
51 |
"skel=s" => \$skel_name, |
52 |
"vhost=s" => \$vhost_name, |
53 |
"clear" => \$bool_clear, |
54 |
"create" => \$bool_create, |
55 |
"patch" => \$bool_patch, |
56 |
"build" => \$bool_build, |
57 |
"rebuild" => \$bool_rebuild, |
58 |
"daemonize" => \$bool_daemonize, |
59 |
"ask" => \$glbl_ask, |
60 |
); |
61 |
|
62 |
my $bool_argsInvalid = 1; |
63 |
|
64 |
if ($bool_daemonize && $vhost_name) { |
65 |
$bool_argsInvalid = 0; |
66 |
} |
67 |
elsif ($bool_build && $skel_name && $vhost_name) { |
68 |
$bool_argsInvalid = 0; |
69 |
} |
70 |
elsif ($vhost_name && $skel_name && $bool_patch) { |
71 |
$bool_argsInvalid = 0; |
72 |
} |
73 |
|
74 |
if ($bool_argsInvalid) { |
75 |
usage(); |
76 |
} |
77 |
|
78 |
if ($glbl_ask) { |
79 |
our $DEBUG_STEP; |
80 |
$DEBUG_STEP = 1; |
81 |
$UML::Config::DEBUG_STEP = 1; |
82 |
} |
83 |
|
84 |
my $vhost = get_host_cfg($vhost_name); |
85 |
my %vhost = %{$vhost}; |
86 |
|
87 |
if ($bool_build) { |
88 |
logAction("building"); |
89 |
$bool_create = 1; |
90 |
$bool_patch = 1; |
91 |
} |
92 |
|
93 |
|
94 |
if ($bool_rebuild) { |
95 |
logAction("rebuilding"); |
96 |
$bool_clear = 1; |
97 |
$bool_create = 1; |
98 |
$bool_patch = 1; |
99 |
} |
100 |
|
101 |
|
102 |
if ($bool_clear) { |
103 |
logAction("=== clearing \"$vhost_name\""); |
104 |
UML::Clone::PackAndRemove($vhost_name); |
105 |
} |
106 |
|
107 |
|
108 |
if ($bool_create) { |
109 |
logAction("=== creating \"$vhost_name\""); |
110 |
|
111 |
logAction("building directory structure"); |
112 |
UML::Clone::DirectoryStructure($vhost_name); |
113 |
|
114 |
logAction("creating rootfs"); |
115 |
UML::Clone::RootFS($vhost_name, $skel_name); |
116 |
|
117 |
logAction("creating datafs"); |
118 |
UML::Clone::DataFS($vhost_name, $skel_name); |
119 |
|
120 |
logAction("creating swapfs"); |
121 |
UML::Clone::SwapFS($vhost_name); |
122 |
} |
123 |
|
124 |
|
125 |
if ($bool_patch) { |
126 |
logAction("=== patching \"$vhost_name\""); |
127 |
my $vhost_base = UML::Config::get_host_basepath($vhost_name); |
128 |
my $vhost_rootfs = UML::Config::get_host_rootfs($vhost_name); |
129 |
my $vhost_datafs = UML::Config::get_host_datafs($vhost_name); |
130 |
my $skel_base = UML::Config::get_skel_base($skel_name); |
131 |
|
132 |
my $cmd; |
133 |
|
134 |
# transfer patches from skel to vhost-base |
135 |
$cmd="cp $skel_base/patches/src/*.tar.gz $vhost_base/tmp/"; |
136 |
askCmd("do it", $cmd, "copying patches from skel/patches/ to vhost/tmp/"); |
137 |
# extract patches |
138 |
$cmd="cd $vhost_base/tmp/ && tar -xzf datafs.tar.gz && tar -xzf rootfs.tar.gz && rm datafs.tar.gz && rm rootfs.tar.gz"; |
139 |
askCmd("do it", $cmd, "extracting patches at vhost/tmp/"); |
140 |
|
141 |
# modify/prepare/configure patch-dir |
142 |
logAction("configuring patches"); |
143 |
%ConfigPatcher::Main::vhost = %vhost; |
144 |
preparePatches("$skel_base/patches/patchlist_rootfs_fs.txt", "$vhost_base/tmp/rootfs"); |
145 |
|
146 |
# pack packages again |
147 |
$cmd="cd $vhost_base/tmp && tar -czf ../mnt/rootfs.tar.gz rootfs/ && tar -czf ../mnt/datafs.tar.gz datafs/ && rm -r $vhost_base/tmp/*"; |
148 |
askCmd("do it?", $cmd, "packing patches"); |
149 |
|
150 |
$cmd = "mount $vhost_rootfs $vhost_base/mnt/rootfs/ -o loop"; |
151 |
askCmd("do it?", $cmd, "mounting rootfs"); |
152 |
$cmd="cd $vhost_base/mnt && tar -xzf rootfs.tar.gz"; |
153 |
askCmd("do it?", $cmd, "applying patches"); |
154 |
$cmd = "umount $vhost_rootfs"; |
155 |
askCmd("do it?", $cmd, "unmounting rootfs"); |
156 |
|
157 |
$cmd = "mount $vhost_datafs $vhost_base/mnt/datafs/ -o loop"; |
158 |
askCmd("do it?", $cmd, "mounting datafs"); |
159 |
$cmd="cd $vhost_base/mnt && tar -xzf datafs.tar.gz"; |
160 |
askCmd("do it?", $cmd, "applying patches"); |
161 |
$cmd = "umount $vhost_datafs"; |
162 |
askCmd("do it?", $cmd, "unmounting datafs"); |
163 |
|
164 |
} |
165 |
|
166 |
if ($bool_daemonize) { |
167 |
my $cmd; |
168 |
my $content; |
169 |
my $filename; |
170 |
|
171 |
my $runlevel = 3; |
172 |
my $uml_boot = "/bin/uml_boot"; |
173 |
my $daemon = "uml" . $vhost_name; |
174 |
|
175 |
my $initscript_path = "/etc/init.d/$daemon"; |
176 |
my $runlevel_base = "/etc/init.d/rc$runlevel.d"; |
177 |
|
178 |
my $owner = $vhost->{main}{owner}; |
179 |
|
180 |
# TODO: handle if above determined parameters are not set |
181 |
# try not to write out invalid shell scripts! |
182 |
|
183 |
# main init script |
184 |
$content = "#!/usr/bin/perl |
185 |
|
186 |
use strict; |
187 |
use warnings; |
188 |
my \$action = shift; |
189 |
if (!\$action) { |
190 |
print \"\$0 called without action argument!\", \"\\n\"; |
191 |
exit; |
192 |
} |
193 |
if (\$action eq \"start\") { |
194 |
system(\"$uml_boot --prepare --vhost=$vhost_name\"); |
195 |
system(\"$sudo -u $owner $uml_boot --start --vhost=$vhost_name\"); |
196 |
} |
197 |
if (\$action eq \"stop\") { |
198 |
system(\"$uml_boot --kill --vhost=$vhost_name\"); |
199 |
} |
200 |
"; |
201 |
|
202 |
$cmd = "echo '" . $content . "'" . " > $initscript_path"; |
203 |
askCmd("do it?", $cmd, "create $initscript_path"); |
204 |
chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, $initscript_path; |
205 |
|
206 |
# runlevel init scripts |
207 |
$filename = "$runlevel_base/S30$daemon"; |
208 |
$cmd = "test -e $filename && rm $filename; ln $initscript_path -s $filename"; |
209 |
askCmd("do it?", $cmd, "create $filename"); |
210 |
chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, $filename; |
211 |
|
212 |
$filename = "$runlevel_base/K01$daemon"; |
213 |
$cmd = "test -e $filename && rm $filename; ln $initscript_path -s $filename"; |
214 |
askCmd("do it?", $cmd, "create $filename"); |
215 |
chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, $filename; |
216 |
|
217 |
} |