~ubuntu-branches/ubuntu/karmic/computer-janitor/karmic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# fstab_plugin.py - modify /etc/fstab to be similar to fresh install
# Copyright (C) 2008  Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import fstab

import computerjanitor
import computerjanitorapp
_ = computerjanitorapp.setup_gettext()


class FstabCruftBase(computerjanitor.Cruft):

    """Base class for cruft in the fstab file."""

    def __init__(self, fstab_line):
        self.fstab_line = fstab_line


class RelatimeCruft(FstabCruftBase):

    """Cruft consisting of a missing 'relatime' option for a filesystem."""
        
    def get_prefix(self):
        return "relatime"
        
    def get_prefix_description(self):
        return "fstab mount option relatime missing"
        
    def get_shortname(self):
        return self.fstab_line.directory
        
    def get_description(self):
        return _("The 'relatime' mount option is missing for filesystem "
                "mounted at %s") % self.fstab_line.directory
                
    def cleanup(self):
        if "relatime" not in self.fstab_line.options:
            self.fstab_line.options += ["relatime"]



# FIXME: this should either be like the
#    def _rewriteFstab(self) in DistUpgradeQuirks()
# or not exit at all
class Scd0Cruft(FstabCruftBase):

    """Rewrite iso9660 fs devices as/dev/cdrom in fstab."""

    def get_prefix(self):
        return "scd0"
        
    def get_prefix_description(self):
        return "/dev/scd0 should be /dev/cdrom"
        
    def get_shortname(self):
        return "/dev/scd0"
        
    def get_description(self):
        return _("The '/dev/scd0' device should be '/dev/cdrom' in fstab.")
                
    def cleanup(self):
        if self.fstab_line.device == "/dev/scd0":
            self.fstab_line.device = "/dev/cdrom"


class FstabPlugin(computerjanitor.Plugin):

    """Plugin to modify /etc/fstab.
    
    This plugin will add the relatime mount option to /etc/fstab
    to those ext2 and ext3 filesystems that are missing it.
    
    In the future, it may do other things. We'll see. The goal is
    to provide a way to add tweaks to /etc/fstab upon upgraded that
    would be there if the system was installed from scratch.
    
    """
    
    allowed_fstypes = ["ext2", "ext3"]
    
    def __init__(self):
        self.fstab_filename = "/etc/fstab"
        self.fstab = None
        
    def is_relatime_cruft(self, line):
        return (line.fstype in self.allowed_fstypes and
                "relatime" not in line.options and
                "noatime" not in line.options)

    def is_scd0_cruft(self, line):
        # FIXME: deactivated for now, see above reason why
        return False
        #return line.device == "/dev/sc0"

    def get_cruft(self):
        tests = [(self.is_relatime_cruft, RelatimeCruft),
                 (self.is_scd0_cruft, Scd0Cruft)]

        self.fstab = fstab.Fstab()
        self.fstab.read(self.fstab_filename)

        cruft = []
        for line in self.fstab.lines:
            for test, klass in tests:
                if test(line):
                    cruft.append(klass(line))
        return cruft

    def post_cleanup(self):
        self.fstab.write(self.fstab_filename)