fstab: Refactor to be able to add ssd-specific options
This commit is contained in:
parent
c64e229142
commit
87367d5abd
@ -18,6 +18,7 @@
|
|||||||
# along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
# along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
import libcalamares
|
import libcalamares
|
||||||
|
|
||||||
@ -30,6 +31,7 @@ HEADER = """# /etc/fstab: static file system information.
|
|||||||
#
|
#
|
||||||
# <file system> <mount point> <type> <options> <dump> <pass>"""
|
# <file system> <mount point> <type> <options> <dump> <pass>"""
|
||||||
|
|
||||||
|
# Turn Parted filesystem names into fstab names
|
||||||
FS_MAP = {
|
FS_MAP = {
|
||||||
"fat": "vfat",
|
"fat": "vfat",
|
||||||
"linuxswap": "swap",
|
"linuxswap": "swap",
|
||||||
@ -41,49 +43,119 @@ def mkdir_p(path):
|
|||||||
os.makedirs(path)
|
os.makedirs(path)
|
||||||
|
|
||||||
|
|
||||||
def generate_fstab_line(partition):
|
def is_ssd_disk(disk_name):
|
||||||
fs = partition["fs"]
|
filename = os.path.join("/sys/block", disk_name, "queue/rotational")
|
||||||
mount_point = partition["mountPoint"]
|
if not os.path.exists(filename):
|
||||||
|
# Should not happen unless sysfs changes, but better safe than sorry
|
||||||
|
return False
|
||||||
|
with open(filename) as f:
|
||||||
|
return f.read() == "0\n"
|
||||||
|
|
||||||
fs = FS_MAP.get(fs, fs)
|
|
||||||
|
|
||||||
if not mount_point and not fs == "swap":
|
def disk_name_for_partition(partition):
|
||||||
return
|
name = os.path.basename(partition["device"])
|
||||||
|
return re.sub("[0-9]+$", "", name)
|
||||||
|
|
||||||
options = "defaults"
|
|
||||||
|
|
||||||
if mount_point == "/":
|
class FstabGenerator(object):
|
||||||
check = 1
|
def __init__(self, partitions, root_mount_point, mount_options,
|
||||||
elif mount_point:
|
ssd_extra_mount_options):
|
||||||
check = 2
|
self.partitions = partitions
|
||||||
else:
|
self.root_mount_point = root_mount_point
|
||||||
check = 0
|
self.mount_options = mount_options
|
||||||
|
self.ssd_extra_mount_options = ssd_extra_mount_options
|
||||||
|
self.ssd_disks = set()
|
||||||
|
self.root_is_ssd = False
|
||||||
|
|
||||||
return "UUID={} {:<14} {:<7} {:<10} 0 {}".format(
|
def run(self):
|
||||||
partition["uuid"],
|
self.find_ssd_disks()
|
||||||
mount_point or "none",
|
self.generate_fstab()
|
||||||
fs,
|
self.create_mount_points()
|
||||||
options,
|
return None
|
||||||
check)
|
|
||||||
|
|
||||||
|
def find_ssd_disks(self):
|
||||||
|
disks = {disk_name_for_partition(x) for x in self.partitions}
|
||||||
|
self.ssd_disks = {x for x in disks if is_ssd_disk(x)}
|
||||||
|
|
||||||
|
def generate_fstab(self):
|
||||||
|
# Create fstab
|
||||||
|
mkdir_p(os.path.join(self.root_mount_point, "etc"))
|
||||||
|
fstab_path = os.path.join(self.root_mount_point, "etc", "fstab")
|
||||||
|
with open(fstab_path, "w") as fl:
|
||||||
|
print(HEADER, file=fl)
|
||||||
|
for partition in self.partitions:
|
||||||
|
dct = self.generate_fstab_line_info(partition)
|
||||||
|
if dct:
|
||||||
|
self.print_fstab_line(dct, file=fl)
|
||||||
|
|
||||||
|
if self.root_is_ssd:
|
||||||
|
# Mount /tmp on a tmpfs
|
||||||
|
dct = dict(
|
||||||
|
device="tmpfs",
|
||||||
|
mount_point="/tmp",
|
||||||
|
fs="tmpfs",
|
||||||
|
options="defaults,noatime,mode=1777",
|
||||||
|
check=0,
|
||||||
|
)
|
||||||
|
self.print_fstab_line(dct, file=fl)
|
||||||
|
|
||||||
|
def generate_fstab_line_info(self, partition):
|
||||||
|
fs = partition["fs"]
|
||||||
|
mount_point = partition["mountPoint"]
|
||||||
|
disk_name = disk_name_for_partition(partition)
|
||||||
|
is_ssd = disk_name in self.ssd_disks
|
||||||
|
|
||||||
|
fs = FS_MAP.get(fs, fs)
|
||||||
|
|
||||||
|
if not mount_point and not fs == "swap":
|
||||||
|
return None
|
||||||
|
|
||||||
|
options = self.mount_options.get(fs, self.mount_options["default"])
|
||||||
|
if is_ssd:
|
||||||
|
extra = self.ssd_extra_mount_options.get(fs)
|
||||||
|
if extra:
|
||||||
|
options += "," + extra
|
||||||
|
|
||||||
|
if mount_point == "/":
|
||||||
|
check = 1
|
||||||
|
elif mount_point:
|
||||||
|
check = 2
|
||||||
|
else:
|
||||||
|
check = 0
|
||||||
|
|
||||||
|
if mount_point == "/":
|
||||||
|
self.root_is_ssd = is_ssd
|
||||||
|
|
||||||
|
return dict(
|
||||||
|
device="UUID=" + partition["uuid"],
|
||||||
|
mount_point=mount_point or "none",
|
||||||
|
fs=fs,
|
||||||
|
options=options,
|
||||||
|
check=check)
|
||||||
|
|
||||||
|
def print_fstab_line(self, dct, file=None):
|
||||||
|
line = "{:41} {:<14} {:<7} {:<10} 0 {}".format(
|
||||||
|
dct["device"],
|
||||||
|
dct["mount_point"],
|
||||||
|
dct["fs"],
|
||||||
|
dct["options"],
|
||||||
|
dct["check"])
|
||||||
|
print(line, file=file)
|
||||||
|
|
||||||
|
def create_mount_points(self):
|
||||||
|
for partition in self.partitions:
|
||||||
|
if partition["mountPoint"]:
|
||||||
|
mkdir_p(self.root_mount_point + partition["mountPoint"])
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
partitions = libcalamares.globalstorage.value("partitions")
|
gs = libcalamares.globalstorage
|
||||||
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
|
conf = libcalamares.job.configuration
|
||||||
|
partitions = gs.value("partitions")
|
||||||
|
root_mount_point = gs.value("rootMountPoint")
|
||||||
|
|
||||||
# Create fstab
|
mount_options = conf["mountOptions"]
|
||||||
mkdir_p(os.path.join(root_mount_point, "etc"))
|
ssd_extra_mount_options = conf.get("ssdExtraMountOptions", {})
|
||||||
fstab_path = os.path.join(root_mount_point, "etc", "fstab")
|
|
||||||
with open(fstab_path, "w") as fl:
|
|
||||||
print(HEADER, file=fl)
|
|
||||||
for partition in partitions:
|
|
||||||
line = generate_fstab_line(partition)
|
|
||||||
if line:
|
|
||||||
print(line, file=fl)
|
|
||||||
|
|
||||||
# Create mount points
|
generator = FstabGenerator(partitions, root_mount_point,
|
||||||
for partition in partitions:
|
mount_options, ssd_extra_mount_options)
|
||||||
if partition["mountPoint"]:
|
return generator.run()
|
||||||
mkdir_p(root_mount_point + partition["mountPoint"])
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
@ -4,3 +4,12 @@ interface: "python"
|
|||||||
requires: []
|
requires: []
|
||||||
script: "main.py"
|
script: "main.py"
|
||||||
configuration:
|
configuration:
|
||||||
|
mountOptions:
|
||||||
|
default: defaults,noatime
|
||||||
|
btrfs: defaults,noatime,space_cache,autodefrag
|
||||||
|
ssdExtraMountOptions:
|
||||||
|
ext4: discard
|
||||||
|
jfs: discard
|
||||||
|
xfs: discard
|
||||||
|
swap: discard
|
||||||
|
btrfs: discard,compress=lzo
|
||||||
|
Loading…
Reference in New Issue
Block a user