forked from skorokithakis/apt-btrfs-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfstab.py
80 lines (68 loc) · 2.66 KB
/
fstab.py
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
# Copyright (C) 2011 Canonical
#
# Author:
# Michael Vogt
#
# 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.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import print_function, unicode_literals
import os
class FstabEntry(object):
""" a single fstab entry line """
@classmethod
def from_line(cls, line):
# split up
args = line.partition("#")[0].split()
# use only the first 7 args and ignore anything after them, mount
# seems to do the same, see bug #873411 comment #7
return FstabEntry(*args[0:6])
def __init__(self, fs_spec, mountpoint, fstype, options, dump=0, passno=0):
# uuid or device
self.fs_spec = fs_spec
self.mountpoint = mountpoint
self.fstype = fstype
self.options = options
self.dump = dump
self.passno = passno
def __repr__(self):
return "<FstabEntry '%s' '%s' '%s' '%s' '%s' '%s'>" % (
self.fs_spec, self.mountpoint, self.fstype,
self.options, self.dump, self.passno)
class Fstab(list):
""" a list of FstabEntry items """
def __init__(self, fstab="/etc/fstab"):
super(Fstab, self).__init__()
with open(fstab) as fstab_file:
for line in (l.strip() for l in fstab_file):
if line == "" or line.startswith("#"):
continue
try:
entry = FstabEntry.from_line(line)
except ValueError:
continue
self.append(entry)
def get_supported_btrfs_root_fstab_entry(self):
""" return the supported btrfs root FstabEntry or None """
for entry in self:
if (
entry.mountpoint == "/" and
entry.fstype == "btrfs" and
"subvol=@" in entry.options):
return entry
return None
def uuid_for_mountpoint(self, mountpoint, fstab="/etc/fstab"):
""" return the device or UUID for the given mountpoint """
for entry in self:
if entry.mountpoint == mountpoint:
return entry.fs_spec
return None