source: /cluster/svnroot/bccd-ng/branches/skylar-devel/packages/liberate.pl @ 2552

Last change on this file since 2552 was 2552, checked in by skylar, 10 years ago

wrong directory (#338)

  • Property svn:executable set to *
File size: 12.6 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Pod::Usage;
6use Getopt::Long;
7use Bccd;
8use Bccd::TestDb qw/open_db insert_results/;
9use File::Temp;
10use Cwd;
11use Readonly;
12use POSIX;
13use Carp;
14
15my %opts;
16my($rc,$out,$rammb,$diskmb,$swapmb,$bootmb,$nouid,$nogid,
17   $Bccd,$ft,$tmpdir,$cwd);
18my @homes;
19
20Readonly my $KERNREV => (POSIX::uname())[2];
21
22GetOptions(
23           'libdev=s' => \$opts{'libdev'},
24           'nocleanup' => \$opts{'nocleanup'},
25           'updatedb=s' => \$opts{'updatedb'},
26           "debug" => \$opts{'debug'},
27           "info" => \$opts{'info'},
28           'help' => \$opts{'help'}
29           );
30
31if($opts{'help'}) {
32    pod2usage(1);
33}
34
35if(!$opts{'libdev'}) {
36    pod2usage(2);
37}
38
39$Bccd = new Bccd();
40Readonly my $PROJECT  => "bccd";
41Readonly my $USER     => $PROJECT;
42Readonly my $STAGE    => $Bccd->get_stage();
43Readonly my $TESTNAME => "liberation";
44Readonly my $SVNREV   => $Bccd->get_rev();
45Readonly my $SWAPDEV  => "/dev/$PROJECT/swap";
46Readonly my $SLASHDEV => "/dev/$PROJECT/slash";
47Readonly my $LIBDIR   => "/liberate";
48Readonly my $LIBFETCH => "/root/liberate";
49Readonly my $BOOTDIR  => $LIBDIR."/boot";
50Readonly my $MTAB     => $LIBDIR."/etc/mtab";
51Readonly my $PROJDIR  => "/diskless/$PROJECT"; # Project directory for diskless
52Readonly my $NEWHOME  => "$LIBDIR/bccd/home/";
53Readonly my $BOOTDEV  => $opts{'libdev'}."1";
54Readonly my $SPLASH_FILE => "bccd_logo_new.png";
55
56if($opts{'info'}) {
57    $Bccd->set_debug('INFO');
58}
59
60if($opts{'debug'}) {
61    $Bccd->set_debug('DEBUG');
62}
63
64if($opts{'updatedb'}) {
65    $ft = new File::Temp();
66    if($opts{'nocleanup'}) {
67        $tmpdir = $ft->tempdir("$PROJECT",CLEANUP => 0);
68    } else {
69        $tmpdir = $ft->tempdir("$PROJECT",CLEANUP => 1);
70    }
71    $Bccd->redirect_stdio("$tmpdir");
72}
73
74if(!(-b $opts{'libdev'})) {
75    croak "not a block device!\n";
76}
77
78$Bccd->run_test(
79    "system",
80    "",
81    "Unmounting all auto-mounted filesystems.",
82    "/usr/sbin/invoke-rc.d bccd-mount-devs stop",
83    );
84
85($rammb,$rc) = $Bccd->run_test(
86    "system",
87    "",
88    "Getting total system RAM.",
89    "/bin/cat /proc/meminfo",
90    );
91
92if($rammb =~ /MemTotal:\s+(\d+) kB/) {
93    $rammb = $1 / 1024;
94} else {
95    croak "/proc/meminfo unparseable.\n";
96}
97
98($diskmb,$rc) = $Bccd->run_test(
99    "system",
100    "",
101    "Getting libdev size.",
102    "/sbin/fdisk -l $opts{'libdev'}",
103    );
104
105if($diskmb =~ /Disk $opts{'libdev'}.*, (\d+) bytes/) {
106    $diskmb = $1 / (1024*1024);
107} else {
108    croak "Couldn't get $opts{'libdev'} size.\n";
109}
110
111$swapmb = $rammb*2;
112
113$bootmb = 200;
114
115if($diskmb-$swapmb < 0) {
116    croak "Too little disk space for /boot!\n";
117}
118
119$Bccd->run_test(
120    "rm_lvm",
121    "",
122    "Removed all LVM data from all attached disks.",
123    "",
124    );
125
126$Bccd->run_test(
127    "system",
128    "",
129    "Zeroed partition table for $opts{'libdev'}.",
130    "dd if=/dev/zero of=$opts{'libdev'} bs=512 count=1",
131    );
132
133$Bccd->run_test(
134    "system",
135    "",
136    "Cleared partition table for $opts{'libdev'}.",
137    "/sbin/parted -s -- $opts{'libdev'} mklabel msdos",
138    );
139
140$Bccd->run_test(
141    "system",
142    "",
143    "Setup /boot for $opts{'libdev'}.",
144    "/sbin/parted -s -- $opts{'libdev'} mkpart primary ext2 0 $bootmb",
145    );
146
147$Bccd->run_test(
148    "system",
149    "",
150    "Setup partition for LVM PV.",
151    "/sbin/parted -s -- $opts{'libdev'} mkpart primary ext2 ".($bootmb+1)." $diskmb",
152    );
153
154$Bccd->run_test(
155    "system",
156    "",
157    "Set LVM PV partition type.",
158    "fdisk $opts{'libdev'} < $LIBFETCH/fdisk_lvm.input",
159    );
160
161$Bccd->run_test(
162    "system",
163    "",
164    "Created LVM PV on $opts{'libdev'}2",
165    $Bccd->get_lvmroot()."/pvcreate  $opts{'libdev'}2",
166    );
167
168$Bccd->run_test(
169    "system",
170    "",
171    "Created VG $PROJECT with $opts{'libdev'}2",
172    $Bccd->get_lvmroot()."/vgcreate $PROJECT $opts{'libdev'}2",
173    );
174
175$Bccd->run_test(
176    "system",
177    "",
178    "Created swap LV within VG $PROJECT .",
179    $Bccd->get_lvmroot()."/lvcreate -n swap -L ".$swapmb."M $PROJECT",
180    );
181
182$Bccd->run_test(
183    "system",
184    "",
185    "Created / LV within VG $PROJECT.",
186    $Bccd->get_lvmroot()."/lvcreate -n slash -l ".$Bccd->get_free_pe_count()." $PROJECT",
187    );
188
189$Bccd->run_test(
190    "system",
191    "",
192    "Marked swap LV for swap use.",
193    "mkswap $SWAPDEV",
194    );
195
196$Bccd->run_test(
197    "system",
198    "",
199    "Made filesystem for /.",
200    "mke2fs -j -J size=100 $SLASHDEV",
201    );
202
203$Bccd->run_test(
204    "system",
205    "",
206    "Made filesystem for /boot.",
207    "mke2fs -j $BOOTDEV",
208    );
209
210if( !(-d $LIBDIR) ) {
211    $Bccd->run_test(
212        "mkpath",
213        "",
214        "mkdir $LIBDIR.",
215        "$LIBDIR",
216        );
217}
218
219$Bccd->run_test(
220    "system",
221    "",
222    "Mounted liberating / at $LIBDIR.",
223    "mount $SLASHDEV $LIBDIR",
224    );
225
226if( !(-d "$LIBDIR/aufs") ) {
227    $Bccd->run_test(
228        "mkpath",
229        "",
230        "mkdir $LIBDIR/aufs.",
231        "$LIBDIR/aufs",
232        );
233}
234
235if( !(-d $BOOTDIR) ) {
236    $Bccd->run_test(
237        "mkpath",
238        "",
239        "mkdir $BOOTDIR.",
240        "$BOOTDIR",
241        );
242}
243
244$Bccd->run_test(
245    "system",
246    "",
247    "Mounted liberating /boot at $BOOTDIR.",
248    "mount $BOOTDEV $BOOTDIR",
249    );
250
251# Diskless copy
252
253$Bccd->run_test(
254    "chdir",
255    "",
256    "cd /UNIONFS.",
257    "/UNIONFS",
258    );
259
260$Bccd->run_test(
261    "system",
262    "",
263    "Copied /UNIONFS to $LIBDIR",
264    "bash -c \"tar --exclude=./dev/* --exclude=./proc/* --exclude=./sys/* -cf - . | (cd $LIBDIR; tar xvf -)\"",
265    );
266
267$cwd = getcwd();
268
269$Bccd->run_test(
270    "chdir",
271    "",
272    "cd $LIBDIR/dev",
273    "$LIBDIR/dev",
274    );
275
276$Bccd->run_test(
277    "system",
278    "",
279    "Making devices in $LIBDIR/dev.",
280    "MAKEDEV generic",
281    );
282
283$Bccd->run_test(
284    "chdir",
285    "",
286    "cd to old directory.",
287    "$cwd",
288    );
289
290$Bccd->run_test(
291    "fwrite",
292    "",
293    "Made valid mtab entries.",
294    "a",
295    "$MTAB",
296    "$SLASHDEV / ext3 rw 0 0 \n $BOOTDEV /boot ext3 rw 0 0",
297    );
298
299$Bccd->run_test(
300    "system",
301    "",
302    "Installed grub into MBR.",
303    "/usr/sbin/chroot $LIBDIR /usr/sbin/grub-install $opts{'libdev'}",
304    );
305
306$Bccd->run_test(
307        "system",
308        "",
309        "Made PNM splash screen.",
310        "/usr/bin/convert $LIBFETCH/$SPLASH_FILE $LIBDIR/boot/grub/bccd_logo_new.xpm.gz",
311        );
312
313$Bccd->run_test(
314    "fcopy",
315    "",
316    "Installed grub menu.",
317    "$LIBFETCH/grub_menu.lst",
318    "$LIBDIR/boot/grub/menu.lst",
319    );
320
321$Bccd->run_test(
322    "system",
323    "",
324    "Created diskless initramfs files.",
325    "/usr/sbin/chroot $LIBDIR /usr/sbin/mkinitramfs -d /etc/initramfs-tools.diskless -o /var/lib/tftpboot/initramfs-$KERNREV $KERNREV",
326    );
327
328$Bccd->run_test(
329    "fcopy",
330    "",
331    "Copied boot kernel to diskless kernel.",
332    "$LIBDIR/boot/vmlinuz-2.6.31.12_aufs",
333    "$LIBDIR/var/lib/tftpboot/vmlinuz-2.6.31.12_aufs",
334    );
335
336$Bccd->run_test(
337    "system",
338    "",
339    "Altering non-root account $USER directory for liberation.",
340    "/usr/sbin/chroot $LIBDIR /usr/sbin/usermod -d /bccd/home/$USER $USER",
341    );
342
343$Bccd->run_test(
344    "fmove",
345    "",
346    "Replacing exports file.",
347    "$LIBFETCH/exports",
348    "$LIBDIR/etc/exports",
349    );
350
351$Bccd->run_test(
352    "fwrite",
353    "",
354    "Made us LIBERATED.",
355    "w",
356    "$LIBDIR/etc/$PROJECT-stage",
357    "LIBERATED",
358    );
359
360$Bccd->run_test(
361    "unlink",
362    "0",
363    "Removed old ssh keys.",
364    "<$LIBDIR/root/.ssh/id_dsa*>",
365    );
366
367$Bccd->run_test(
368    "chmod",
369    "",
370    "chmod 0700 $LIBDIR/root/.ssh/",
371    0700,
372    "$LIBDIR/root/.ssh/",
373    );
374
375$Bccd->run_test(
376    "system",
377    "",
378    "Generated passwordless root ssh keys.",
379    "ssh-keygen -t dsa -q -f $LIBDIR/root/.ssh/id_dsa -N ''",
380    );
381
382$Bccd->run_test(
383    "fwrite",
384    "",
385    "Appended public key to root authorized key list.",
386    "a",
387    "$LIBDIR/root/.ssh/authorized_key",
388    $Bccd->snarf_file("$LIBDIR/root/.ssh/id_dsa.pub"),
389    );
390
391if( !(-d $LIBDIR."/".$PROJDIR) ) {
392    $Bccd->run_test(
393        "mkpath",
394        "",
395        "mkdir $LIBDIR/$PROJDIR",
396        "$LIBDIR/$PROJDIR",
397        );
398}
399
400$Bccd->run_test(
401    "mkpath",
402    "",
403    "mkdir $NEWHOME/.ssh/ for new user.",
404    "$NEWHOME/$USER/.ssh/",
405    );
406
407$Bccd->run_test(
408    "chmod",
409    "",
410    "chmod 0700 $NEWHOME/.ssh/ for new user.",
411    0700,
412    "$NEWHOME/$USER/.ssh/",
413    );
414
415$Bccd->run_test(
416    "rsync",
417    "",
418    "Copying homes to $NEWHOME.",
419    "/UNIONFS/home/*",
420    "$NEWHOME/",
421    );
422
423if( !( -f "$NEWHOME/$USER/.ssh/id_dsa") ) {
424    $Bccd->run_test(
425        "system",
426        "",
427        "Generated passwordless user ssh keys in $NEWHOME/$USER/.ssh/.",
428        "ssh-keygen -t dsa -q -f $NEWHOME/$USER/.ssh/id_dsa -N ''",
429        );
430}
431
432$Bccd->run_test(
433    "fwrite",
434    "",
435    "Appended public key to authorized user key list in $NEWHOME/$USER/.ssh/authorized_keys.",
436    "a",
437    "$NEWHOME/$USER/.ssh/authorized_keys",
438    $Bccd->snarf_file("$NEWHOME/$USER/.ssh/id_dsa.pub"),
439    );
440
441$Bccd->run_test(
442    "fwrite",
443    "",
444    "Generated valid /proc/mounts.",
445    "w",
446    "$LIBDIR/proc/mounts",
447    "/dev/$PROJECT/slash / ext3 rw,data=ordered 0 0",
448    );
449
450foreach my $dir ( "/", "/lib" ) {
451    $Bccd->run_test(
452        "chmod",
453        "",
454        "Set $dir to perms 0755.",
455        "0755",
456        "$dir",
457        );
458}
459
460# Stuff to change on both master and slave
461
462foreach my $script ( "ssh", "nfs-common", "tftpd-hpa" ) {
463    $Bccd->run_test(
464        "system",
465        "",
466        "Set $script to start on both master and slave nodes.",
467        "/usr/sbin/chroot $LIBDIR /usr/sbin/update-rc.d $script start 15 2 3 .",
468        );
469}
470
471$Bccd->run_test(
472    "rsync",
473    "",
474    "Copied diskless stuff into place.",
475    "$LIBFETCH/diskless/*",
476    "$LIBDIR/diskless/",
477    );
478
479# Remove libfetch directory
480
481$Bccd->run_test(
482    "rmtree",
483    "",
484    "Removed pre-fetch liberation directory.",
485    "$LIBFETCH",
486    );
487
488# Post diskless
489
490$Bccd->run_test(
491    "fwrite",
492    "",
493    "Opened $LIBDIR/etc/fstab",
494    "a",
495    "$LIBDIR/etc/fstab",
496    "$SLASHDEV / ext3 rw 0 0\n$SWAPDEV none swap    sw 0 0\n$BOOTDEV /boot ext3 defaults 1 2\n",
497    );
498
499if( !(-d "$LIBDIR/nfs") ) {
500    $Bccd->run_test(
501        "mkpath",
502        "",
503        "mkdir $LIBDIR/nfs",
504        "$LIBDIR/nfs"
505        );
506}
507
508# Stuff to change in project directory
509
510$Bccd->run_test(
511    "mkpath",
512    "",
513    "mkdir $LIBDIR/$PROJDIR/etc/network",
514    "$LIBDIR/$PROJDIR/etc/network/",
515    );
516
517$Bccd->run_test(
518        "fcopy",
519        "",
520        "Copied $LIBDIR/etc/init.d/bccd-nics -> $LIBDIR/$PROJDIR/etc/init.d/bccd-nics.",
521        "$LIBDIR/etc/init.d/bccd-nics",
522        "$LIBDIR/$PROJDIR/etc/init.d/bccd-nics",
523        );
524
525
526$Bccd->run_test(
527        "regexsub_file",
528        "",
529        "Disabled bccd-nic-probe in diskless nodes.",
530        "$LIBDIR/$PROJDIR/etc/init.d/bccd-nics",
531        "/bin/bccd-nic-probe",
532        "",
533        );
534
535$Bccd->run_test(
536    "fmove",
537    "",
538    "Moved interfaces.compute to $LIBDIR/$PROJDIR/etc/network",
539    "$LIBDIR/etc/network/interfaces.compute",
540    "$LIBDIR/$PROJDIR/etc/network/interfaces",
541    );
542
543$Bccd->run_test(
544    "mkpath",
545    "",
546    "Created $LIBDIR/$PROJDIR/etc/init.d.",
547    "$LIBDIR/$PROJDIR/etc/init.d",
548    );
549
550$Bccd->run_test(
551    "chmod",
552    "",
553    "Set libdir/$PROJDIR/etc/init.d to 0755.",
554    0755,
555    "$LIBDIR/$PROJDIR/etc/init.d",
556    );
557
558$Bccd->run_test(
559    "fmove",
560    "",
561    "mv $LIBDIR/etc/init.d/networking $LIBDIR/$PROJDIR/networking.",
562    "$LIBDIR/etc/init.d/networking",
563    "$LIBDIR/$PROJDIR/etc/init.d/networking",
564    );
565
566$Bccd->run_test(
567    "system",
568    "",
569    "Set hostname to come from eth1 in $LIBDIR.",
570    "sed -e 's/ifconfig eth0/ifconfig eth1/g' < $LIBDIR/$PROJDIR/etc/init.d/networking > $LIBDIR/etc/init.d/networking",
571    );
572
573$Bccd->run_test(
574    "system",
575    "",
576    "127.0.0.1 no longer points to hostname.",
577    "perl -wpli -e 's/^(127.0.0.1\\s+localhost).*\$/\$1/g' $LIBDIR/etc/hosts",
578    );
579
580$Bccd->run_test(
581    "chmod",
582    "",
583    "chmod 0755 $LIBDIR/etc/init.d/networking.",
584    0755,
585    "$LIBDIR/etc/init.d/networking",
586    );
587
588$Bccd->run_test(
589    "system",
590    "",
591    "chown'd $NEWHOME/$USER to $USER:$USER",
592    "chown -R $USER:$USER $NEWHOME/$USER/",
593    );
594
595$Bccd->run_test(
596    "chmod",
597    "",
598    "Setting permissions for $NEWHOME to 0755.",
599    0755,
600    "$NEWHOME",
601    );
602
603# Stuff to change on master node
604
605foreach my $script ( "nfs-kernel-server", "nfs-common", "dhcp3-server" ) {
606    $Bccd->run_test(
607        "system",
608        "",
609        "Enabled $script in $LIBDIR.",
610        "/usr/sbin/chroot $LIBDIR /usr/sbin/update-rc.d $script start 25 2 3 .",
611        );
612}
613
614print "Passed ".$Bccd->get_passed()." out of ".$Bccd->get_total()." total tests.\n";
615
616if($opts{'updatedb'}) {
617    my $dbh = open_db("$opts{'updatedb'}");
618    if( $Bccd->get_passed() < $Bccd->get_total() ) {
619        insert_results($TESTNAME,$STAGE,$SVNREV,$Bccd->snarf_file("$tmpdir/allout"),"FAILURE",$dbh);
620    } else {
621        insert_results($TESTNAME,$STAGE,$SVNREV,$Bccd->snarf_file("$tmpdir/allout"),"SUCCESS",$dbh);
622    }
623    $Bccd->close_stdio();
624    $dbh->disconnect;
625}
626
627exit $Bccd->get_total()-$Bccd->get_passed();
Note: See TracBrowser for help on using the repository browser.