1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | ############################################################################# |
---|
4 | # smbmounts.pl: Setup Automounter to mount via SMB for federated experiments |
---|
5 | # $Id: smbmount.Linux.pl,v 1.1 2008-05-18 00:09:31 faber Exp $ |
---|
6 | # |
---|
7 | |
---|
8 | $SHARE=shift || "USERS"; |
---|
9 | $ADDR=shift || `cat /usr/local/etc/emulab/bossnode`; |
---|
10 | $PUSER=shift || "jhickey"; |
---|
11 | $PNAME=shift || "emulab-ops"; |
---|
12 | $FSTAB="/etc/fstab"; |
---|
13 | $HOMEROOT="/users"; |
---|
14 | $PROJROOT="/proj"; |
---|
15 | |
---|
16 | my $share = 1; |
---|
17 | |
---|
18 | chomp $ADDR; |
---|
19 | |
---|
20 | # ADDR is probably a DNS name (and we assume so if it has letters. mount cifs |
---|
21 | # wants an IP address, so this hex dereferences the DNS name and converts the |
---|
22 | # first address into the string representation of the IP. It is, perhaps, a |
---|
23 | # but terse. |
---|
24 | if ($ADDR =~ /[a-z]/ ) { |
---|
25 | my @addrs = (gethostbyname($ADDR))[4]; |
---|
26 | $ADDR = join(".", unpack("C4", @addrs[0])) |
---|
27 | if @addrs; |
---|
28 | } |
---|
29 | |
---|
30 | |
---|
31 | $TMCC="/usr/local/etc/emulab/tmcc"; |
---|
32 | $UMOUNT="/bin/umount"; |
---|
33 | $MOUNT="/bin/mount"; |
---|
34 | $MKDIR="/bin/mkdir"; |
---|
35 | $CP="/bin/cp"; |
---|
36 | |
---|
37 | print "Unmounting all nfs and cifs filesystems.\n"; |
---|
38 | system("$UMOUNT -a -f -t nfs,cifs"); # or die("Failed to unmount NFS"); |
---|
39 | |
---|
40 | # Backup the fstab so we can run multiple times |
---|
41 | if (! -f "$FSTAB.bak") { |
---|
42 | system("$CP $FSTAB $FSTAB.bak") && die("Unable to backup $FSTAB"); |
---|
43 | } else { |
---|
44 | system("$CP $FSTAB.bak $FSTAB") && die("Unable to restore $FSTAB"); |
---|
45 | } |
---|
46 | |
---|
47 | # open(TM, "$TMCC accounts windows |") or die("Failed to execute TMCC"); |
---|
48 | open(TM, "/usr/local/federation/etc/userconf")or die("Failed to open userconf"); |
---|
49 | open(FSTAB,">>$FSTAB") or die("Cannot Open File $FSTAB"); |
---|
50 | |
---|
51 | print FSTAB "\n# SMB Configuration Generated by smbmount.pl\n"; |
---|
52 | |
---|
53 | while(<TM>) { |
---|
54 | /ADDUSER/ && do { |
---|
55 | /LOGIN=(\S+)/ && do { $user = $1; }; |
---|
56 | /WPSWD=(\S+)/ && do { $pswd = $1; }; |
---|
57 | /UID=(\S+)/ && do { $uid = $1; }; |
---|
58 | /GID=(\S+)/ && do { $gid = $1; }; |
---|
59 | /HOMEDIR=(\S+)/ && do { $homedir = $1; }; |
---|
60 | |
---|
61 | open(PWDFILE, ">/tmp/$user.cifs_creds") || |
---|
62 | warn "Can't create credentials for $user:$!\n"; |
---|
63 | print PWDFILE "username=$user\npassword=$pswd\n"; |
---|
64 | close(PWDFILE); |
---|
65 | chmod(0600, "/tmp/$user.cifs_creds") == 1 || |
---|
66 | warn "Credential file /tmp/$user.cifs_creds may have " . |
---|
67 | "bad permissions:$!\n"; |
---|
68 | |
---|
69 | print FSTAB "//$SHARE/$user\t"; |
---|
70 | print FSTAB "$homedir\tcifs\t"; |
---|
71 | print FSTAB "auto,rw,credentials=/tmp/$user.cifs_creds,ip=$ADDR\t0\t0\n"; |
---|
72 | |
---|
73 | # |
---|
74 | # If we are PUSER, the user assigned to mount the project |
---|
75 | # directory, then we add in the mount for that. The share is |
---|
76 | # called proj-$PNAME where PNAME is the name of the project at |
---|
77 | # the mothership. We need a second map for this since we will |
---|
78 | # be mounting under /proj. We should really not use share in |
---|
79 | # federation. |
---|
80 | # |
---|
81 | |
---|
82 | if($user =~ m/$PUSER/i) { |
---|
83 | print FSTAB "//$SHARE/proj-$PNAME\t"; |
---|
84 | print FSTAB "$PROJROOT/$PNAME\tcifs\t"; |
---|
85 | print FSTAB "rw,credentials=/tmp/$user.cifs_creds,ip=$ADDR\t0\t0\n"; |
---|
86 | if ( $share) { |
---|
87 | print FSTAB "//$SHARE/share\t"; |
---|
88 | print FSTAB "/share\tcifs\t"; |
---|
89 | print FSTAB "rw,credentials=/tmp/$user.cifs_creds,ip=$ADDR\t0\t0\n"; |
---|
90 | |
---|
91 | mkdir("/share") unless -d "/share"; |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | close(FSTAB); |
---|
98 | |
---|
99 | print("Mounting the cifs versions of everything\n"); |
---|
100 | system("$MOUNT -a -t cifs"); |
---|