~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to mountallusers.py

Merge from object-publishing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# IVLE - Informatics Virtual Learning Environment
3
 
# Copyright (C) 2007-2008 The University of Melbourne
4
 
#
5
 
# This program is free software; you can redistribute it and/or modify
6
 
# it under the terms of the GNU General Public License as published by
7
 
# the Free Software Foundation; either version 2 of the License, or
8
 
# (at your option) any later version.
9
 
#
10
 
# This program is distributed in the hope that it will be useful,
11
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
# GNU General Public License for more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License
16
 
# along with this program; if not, write to the Free Software
17
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 
19
 
# Program: MountAllUsers
20
 
# Author:  William Grant
21
 
# Date:    4/7/2008
22
 
 
23
 
# Script to UnionFS-mount jails for all users on the system.
24
 
# Requires root to run.
25
 
 
26
 
import sys
27
 
import os
28
 
import common.db
29
 
import os.path
30
 
import conf
31
 
import optparse
32
 
import logging
33
 
 
34
 
p = optparse.OptionParser()
35
 
p.add_option('--verbose', '-v', action='store_true')
36
 
p.add_option('--unmount', '-u', action='store_true')
37
 
options, arguments = p.parse_args()
38
 
 
39
 
if options.verbose:
40
 
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
41
 
                        level=logging.DEBUG)
42
 
else:
43
 
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
44
 
                        level=logging.WARNING)
45
 
 
46
 
if os.getuid() != 0:
47
 
    print >> sys.stderr, "%s must be run as root" % sys.argv[0]
48
 
    sys.exit(1)
49
 
 
50
 
try:
51
 
    db = common.db.DB()
52
 
    list = db.get_users()
53
 
    res = db.get_all('login', ['login', 'unixid'])
54
 
    def repack(flds):
55
 
        return (flds['login'], flds['unixid'])
56
 
    uids = dict(map(repack,res))
57
 
except Exception, message:
58
 
    logging.error(str(message))
59
 
    sys.exit(1)
60
 
 
61
 
logging.info("mass aufs mount started")
62
 
 
63
 
list.sort(key=lambda user: user.login)
64
 
for user in list:
65
 
    login = user.login
66
 
    # This is where we'll mount to...
67
 
    destdir = os.path.join(conf.jail_base, login)
68
 
    # ... and this is where we'll get the user bits.
69
 
    srcdir = os.path.join(conf.jail_src_base, login)
70
 
 
71
 
    try:
72
 
        if not options.unmount:
73
 
            if not os.path.exists(srcdir):
74
 
                logging.info("user %s doesn't have a jail - skipping" % login)
75
 
                continue
76
 
            if not os.path.exists(destdir):
77
 
                logging.debug("user %s had no mountpoint - creating" % login)
78
 
                os.mkdir(destdir)
79
 
        
80
 
            if os.system('/bin/mount -t aufs -o dirs=%s:%s=ro none %s'
81
 
                             % (srcdir,conf.jail_system,destdir)) == 0:
82
 
                logging.info("mounted user %s's jail." % login)
83
 
            else:
84
 
                logging.error("failed to mount user %s's jail!" % login)
85
 
        else:
86
 
            os.system('/bin/umount ' + destdir)
87
 
            logging.info("unmounted user %s's jail." % login)
88
 
    except Exception, message:
89
 
        logging.warning(str(message))
90
 
        continue
91
 
 
92
 
logging.info("mount completed successfully")