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

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

need to generate regular initramfs (#338)

  • Property svn:executable set to *
File size: 12.8 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 regular initramfs.",
325    "/usr/sbin/chroot $LIBDIR /usr/sbin/mkinitramfs -o /boot/initramfs-$KERNREV $KERNREV",
326    );
327
328$Bccd->run_test(
329    "system",
330    "",
331    "Created diskless initramfs.",
332    "/usr/sbin/chroot $LIBDIR /usr/sbin/mkinitramfs -d /etc/initramfs-tools.diskless -o /var/lib/tftpboot/initramfs-$KERNREV $KERNREV",
333    );
334
335$Bccd->run_test(
336    "fcopy",
337    "",
338    "Copied boot kernel to diskless kernel.",
339    "$LIBDIR/boot/vmlinuz-2.6.31.12_aufs",
340    "$LIBDIR/var/lib/tftpboot/vmlinuz-2.6.31.12_aufs",
341    );
342
343$Bccd->run_test(
344    "system",
345    "",
346    "Altering non-root account $USER directory for liberation.",
347    "/usr/sbin/chroot $LIBDIR /usr/sbin/usermod -d /bccd/home/$USER $USER",
348    );
349
350$Bccd->run_test(
351    "fmove",
352    "",
353    "Replacing exports file.",
354    "$LIBFETCH/exports",
355    "$LIBDIR/etc/exports",
356    );
357
358$Bccd->run_test(
359    "fwrite",
360    "",
361    "Made us LIBERATED.",
362    "w",
363    "$LIBDIR/etc/$PROJECT-stage",
364    "LIBERATED",
365    );
366
367$Bccd->run_test(
368    "unlink",
369    "0",
370    "Removed old ssh keys.",
371    "<$LIBDIR/root/.ssh/id_dsa*>",
372    );
373
374$Bccd->run_test(
375    "chmod",
376    "",
377    "chmod 0700 $LIBDIR/root/.ssh/",
378    0700,
379    "$LIBDIR/root/.ssh/",
380    );
381
382$Bccd->run_test(
383    "system",
384    "",
385    "Generated passwordless root ssh keys.",
386    "ssh-keygen -t dsa -q -f $LIBDIR/root/.ssh/id_dsa -N ''",
387    );
388
389$Bccd->run_test(
390    "fwrite",
391    "",
392    "Appended public key to root authorized key list.",
393    "a",
394    "$LIBDIR/root/.ssh/authorized_key",
395    $Bccd->snarf_file("$LIBDIR/root/.ssh/id_dsa.pub"),
396    );
397
398if( !(-d $LIBDIR."/".$PROJDIR) ) {
399    $Bccd->run_test(
400        "mkpath",
401        "",
402        "mkdir $LIBDIR/$PROJDIR",
403        "$LIBDIR/$PROJDIR",
404        );
405}
406
407$Bccd->run_test(
408    "mkpath",
409    "",
410    "mkdir $NEWHOME/.ssh/ for new user.",
411    "$NEWHOME/$USER/.ssh/",
412    );
413
414$Bccd->run_test(
415    "chmod",
416    "",
417    "chmod 0700 $NEWHOME/.ssh/ for new user.",
418    0700,
419    "$NEWHOME/$USER/.ssh/",
420    );
421
422$Bccd->run_test(
423    "rsync",
424    "",
425    "Copying homes to $NEWHOME.",
426    "/UNIONFS/home/*",
427    "$NEWHOME/",
428    );
429
430if( !( -f "$NEWHOME/$USER/.ssh/id_dsa") ) {
431    $Bccd->run_test(
432        "system",
433        "",
434        "Generated passwordless user ssh keys in $NEWHOME/$USER/.ssh/.",
435        "ssh-keygen -t dsa -q -f $NEWHOME/$USER/.ssh/id_dsa -N ''",
436        );
437}
438
439$Bccd->run_test(
440    "fwrite",
441    "",
442    "Appended public key to authorized user key list in $NEWHOME/$USER/.ssh/authorized_keys.",
443    "a",
444    "$NEWHOME/$USER/.ssh/authorized_keys",
445    $Bccd->snarf_file("$NEWHOME/$USER/.ssh/id_dsa.pub"),
446    );
447
448$Bccd->run_test(
449    "fwrite",
450    "",
451    "Generated valid /proc/mounts.",
452    "w",
453    "$LIBDIR/proc/mounts",
454    "/dev/$PROJECT/slash / ext3 rw,data=ordered 0 0",
455    );
456
457foreach my $dir ( "/", "/lib" ) {
458    $Bccd->run_test(
459        "chmod",
460        "",
461        "Set $dir to perms 0755.",
462        "0755",
463        "$dir",
464        );
465}
466
467# Stuff to change on both master and slave
468
469foreach my $script ( "ssh", "nfs-common", "tftpd-hpa" ) {
470    $Bccd->run_test(
471        "system",
472        "",
473        "Set $script to start on both master and slave nodes.",
474        "/usr/sbin/chroot $LIBDIR /usr/sbin/update-rc.d $script start 15 2 3 .",
475        );
476}
477
478$Bccd->run_test(
479    "rsync",
480    "",
481    "Copied diskless stuff into place.",
482    "$LIBFETCH/diskless/*",
483    "$LIBDIR/diskless/",
484    );
485
486# Remove libfetch directory
487
488$Bccd->run_test(
489    "rmtree",
490    "",
491    "Removed pre-fetch liberation directory.",
492    "$LIBFETCH",
493    );
494
495# Post diskless
496
497$Bccd->run_test(
498    "fwrite",
499    "",
500    "Opened $LIBDIR/etc/fstab",
501    "a",
502    "$LIBDIR/etc/fstab",
503    "$SLASHDEV / ext3 rw 0 0\n$SWAPDEV none swap    sw 0 0\n$BOOTDEV /boot ext3 defaults 1 2\n",
504    );
505
506if( !(-d "$LIBDIR/nfs") ) {
507    $Bccd->run_test(
508        "mkpath",
509        "",
510        "mkdir $LIBDIR/nfs",
511        "$LIBDIR/nfs"
512        );
513}
514
515# Stuff to change in project directory
516
517$Bccd->run_test(
518    "mkpath",
519    "",
520    "mkdir $LIBDIR/$PROJDIR/etc/network",
521    "$LIBDIR/$PROJDIR/etc/network/",
522    );
523
524$Bccd->run_test(
525        "fcopy",
526        "",
527        "Copied $LIBDIR/etc/init.d/bccd-nics -> $LIBDIR/$PROJDIR/etc/init.d/bccd-nics.",
528        "$LIBDIR/etc/init.d/bccd-nics",
529        "$LIBDIR/$PROJDIR/etc/init.d/bccd-nics",
530        );
531
532
533$Bccd->run_test(
534        "regexsub_file",
535        "",
536        "Disabled bccd-nic-probe in diskless nodes.",
537        "$LIBDIR/$PROJDIR/etc/init.d/bccd-nics",
538        "/bin/bccd-nic-probe",
539        "",
540        );
541
542$Bccd->run_test(
543    "fmove",
544    "",
545    "Moved interfaces.compute to $LIBDIR/$PROJDIR/etc/network",
546    "$LIBDIR/etc/network/interfaces.compute",
547    "$LIBDIR/$PROJDIR/etc/network/interfaces",
548    );
549
550$Bccd->run_test(
551    "mkpath",
552    "",
553    "Created $LIBDIR/$PROJDIR/etc/init.d.",
554    "$LIBDIR/$PROJDIR/etc/init.d",
555    );
556
557$Bccd->run_test(
558    "chmod",
559    "",
560    "Set libdir/$PROJDIR/etc/init.d to 0755.",
561    0755,
562    "$LIBDIR/$PROJDIR/etc/init.d",
563    );
564
565$Bccd->run_test(
566    "fmove",
567    "",
568    "mv $LIBDIR/etc/init.d/networking $LIBDIR/$PROJDIR/networking.",
569    "$LIBDIR/etc/init.d/networking",
570    "$LIBDIR/$PROJDIR/etc/init.d/networking",
571    );
572
573$Bccd->run_test(
574    "system",
575    "",
576    "Set hostname to come from eth1 in $LIBDIR.",
577    "sed -e 's/ifconfig eth0/ifconfig eth1/g' < $LIBDIR/$PROJDIR/etc/init.d/networking > $LIBDIR/etc/init.d/networking",
578    );
579
580$Bccd->run_test(
581    "system",
582    "",
583    "127.0.0.1 no longer points to hostname.",
584    "perl -wpli -e 's/^(127.0.0.1\\s+localhost).*\$/\$1/g' $LIBDIR/etc/hosts",
585    );
586
587$Bccd->run_test(
588    "chmod",
589    "",
590    "chmod 0755 $LIBDIR/etc/init.d/networking.",
591    0755,
592    "$LIBDIR/etc/init.d/networking",
593    );
594
595$Bccd->run_test(
596    "system",
597    "",
598    "chown'd $NEWHOME/$USER to $USER:$USER",
599    "chown -R $USER:$USER $NEWHOME/$USER/",
600    );
601
602$Bccd->run_test(
603    "chmod",
604    "",
605    "Setting permissions for $NEWHOME to 0755.",
606    0755,
607    "$NEWHOME",
608    );
609
610# Stuff to change on master node
611
612foreach my $script ( "nfs-kernel-server", "nfs-common", "dhcp3-server" ) {
613    $Bccd->run_test(
614        "system",
615        "",
616        "Enabled $script in $LIBDIR.",
617        "/usr/sbin/chroot $LIBDIR /usr/sbin/update-rc.d $script start 25 2 3 .",
618        );
619}
620
621print "Passed ".$Bccd->get_passed()." out of ".$Bccd->get_total()." total tests.\n";
622
623if($opts{'updatedb'}) {
624    my $dbh = open_db("$opts{'updatedb'}");
625    if( $Bccd->get_passed() < $Bccd->get_total() ) {
626        insert_results($TESTNAME,$STAGE,$SVNREV,$Bccd->snarf_file("$tmpdir/allout"),"FAILURE",$dbh);
627    } else {
628        insert_results($TESTNAME,$STAGE,$SVNREV,$Bccd->snarf_file("$tmpdir/allout"),"SUCCESS",$dbh);
629    }
630    $Bccd->close_stdio();
631    $dbh->disconnect;
632}
633
634exit $Bccd->get_total()-$Bccd->get_passed();
Note: See TracBrowser for help on using the repository browser.