2014-07-24 13:51:14 +02:00
|
|
|
#!/usr/bin/env python3
|
2015-02-18 15:06:10 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-08-25 16:05:56 +02:00
|
|
|
# === This file is part of Calamares - <https://calamares.io> ===
|
2014-07-24 13:51:14 +02:00
|
|
|
#
|
2020-08-24 16:36:47 +02:00
|
|
|
# SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
|
|
|
|
# SPDX-FileCopyrightText: 2016 Anke Boersma <demm@kaosx.us>
|
|
|
|
# SPDX-FileCopyrightText: 2018 Adriaan de Groot <groot@kde.org>
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2014-07-24 13:51:14 +02:00
|
|
|
#
|
2020-08-25 16:05:56 +02:00
|
|
|
# Calamares is Free Software: see the License-Identifier above.
|
2014-07-24 13:51:14 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
2016-01-13 22:11:55 +01:00
|
|
|
import shutil
|
2014-07-24 13:51:14 +02:00
|
|
|
|
|
|
|
import libcalamares
|
2018-02-20 10:49:51 +01:00
|
|
|
from libcalamares.utils import gettext_path, gettext_languages
|
|
|
|
|
|
|
|
import gettext
|
|
|
|
_translation = gettext.translation("calamares-python",
|
|
|
|
localedir=gettext_path(),
|
|
|
|
languages=gettext_languages(),
|
|
|
|
fallback=True)
|
|
|
|
_ = _translation.gettext
|
|
|
|
_n = _translation.ngettext
|
|
|
|
|
|
|
|
|
|
|
|
def pretty_name():
|
|
|
|
return _( "Unmount file systems." )
|
2014-07-24 13:51:14 +02:00
|
|
|
|
|
|
|
|
2014-07-29 14:53:07 +02:00
|
|
|
def list_mounts(root_mount_point):
|
2015-02-21 10:49:37 +01:00
|
|
|
""" List mount points.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param root_mount_point:
|
|
|
|
:return:
|
|
|
|
"""
|
2014-07-24 13:51:14 +02:00
|
|
|
lst = []
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2020-11-03 22:53:21 +01:00
|
|
|
root_mount_point = os.path.normpath(root_mount_point)
|
2014-07-29 14:53:07 +02:00
|
|
|
for line in open("/etc/mtab").readlines():
|
|
|
|
device, mount_point, _ = line.split(" ", 2)
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2020-11-03 22:53:21 +01:00
|
|
|
if os.path.commonprefix([root_mount_point, mount_point]) == root_mount_point:
|
2014-07-29 14:53:07 +02:00
|
|
|
lst.append((device, mount_point))
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-07-24 13:51:14 +02:00
|
|
|
return lst
|
|
|
|
|
|
|
|
|
2014-07-25 16:41:21 +02:00
|
|
|
def run():
|
2015-02-21 10:49:37 +01:00
|
|
|
""" Unmounts given mountpoints in decreasing order.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2014-07-29 14:53:07 +02:00
|
|
|
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
|
2016-01-21 11:31:45 +01:00
|
|
|
|
2016-01-21 12:09:42 +01:00
|
|
|
if(libcalamares.job.configuration and
|
2016-01-21 14:46:02 +01:00
|
|
|
"srcLog" in libcalamares.job.configuration and
|
|
|
|
"destLog" in libcalamares.job.configuration):
|
2016-01-21 12:09:42 +01:00
|
|
|
log_source = libcalamares.job.configuration["srcLog"]
|
|
|
|
log_destination = libcalamares.job.configuration["destLog"]
|
2018-01-03 11:01:38 +01:00
|
|
|
# Relocate log_destination into target system
|
|
|
|
log_destination = '{!s}/{!s}'.format(root_mount_point, log_destination)
|
|
|
|
# Make sure source is a string
|
|
|
|
log_source = '{!s}'.format(log_source)
|
2016-01-21 12:09:42 +01:00
|
|
|
|
|
|
|
# copy installation log before umount
|
2018-01-03 11:01:38 +01:00
|
|
|
if os.path.exists(log_source):
|
|
|
|
try:
|
|
|
|
shutil.copy2(log_source, log_destination)
|
|
|
|
except Exception as e:
|
2018-02-20 10:42:56 +01:00
|
|
|
libcalamares.utils.warning("Could not preserve file {!s}, "
|
2018-01-03 11:01:38 +01:00
|
|
|
"error {!s}".format(log_source, e))
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-07-29 14:53:07 +02:00
|
|
|
if not root_mount_point:
|
|
|
|
return ("No mount point for root partition in globalstorage",
|
|
|
|
"globalstorage does not contain a \"rootMountPoint\" key, "
|
|
|
|
"doing nothing")
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-07-29 14:53:07 +02:00
|
|
|
if not os.path.exists(root_mount_point):
|
|
|
|
return ("Bad mount point for root partition in globalstorage",
|
|
|
|
"globalstorage[\"rootMountPoint\"] is \"{}\", which does not "
|
|
|
|
"exist, doing nothing".format(root_mount_point))
|
|
|
|
|
|
|
|
lst = list_mounts(root_mount_point)
|
2014-07-24 13:51:14 +02:00
|
|
|
# Sort the list by mount point in decreasing order. This way we can be sure
|
|
|
|
# we unmount deeper dirs first.
|
2014-07-29 14:53:07 +02:00
|
|
|
lst.sort(key=lambda x: x[1], reverse=True)
|
2014-07-24 13:51:14 +02:00
|
|
|
|
2014-07-29 14:53:07 +02:00
|
|
|
for device, mount_point in lst:
|
2020-04-21 16:13:19 +02:00
|
|
|
# On success, no output; if the command fails, its output is
|
|
|
|
# in the exception object.
|
|
|
|
subprocess.check_output(["umount", "-lv", mount_point], stderr=subprocess.STDOUT)
|
2014-07-24 13:51:14 +02:00
|
|
|
|
2014-07-29 14:53:07 +02:00
|
|
|
os.rmdir(root_mount_point)
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-07-28 17:03:22 +02:00
|
|
|
return None
|