source: calamares/trunk/fuentes/src/modules/umount/main.py @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 3.5 KB
Line 
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# === This file is part of Calamares - <https://github.com/calamares> ===
5#
6#   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
7#   Copyright 2016, Anke Boersma <demm@kaosx.us>
8#   Copyright 2018, Adriaan de Groot <groot@kde.org>
9#
10#   Calamares is free software: you can redistribute it and/or modify
11#   it under the terms of the GNU General Public License as published by
12#   the Free Software Foundation, either version 3 of the License, or
13#   (at your option) any later version.
14#
15#   Calamares is distributed in the hope that it will be useful,
16#   but WITHOUT ANY WARRANTY; without even the implied warranty of
17#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18#   GNU General Public License for more details.
19#
20#   You should have received a copy of the GNU General Public License
21#   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
22
23import os
24import subprocess
25import shutil
26
27import libcalamares
28from libcalamares.utils import gettext_path, gettext_languages
29
30import gettext
31_translation = gettext.translation("calamares-python",
32                                   localedir=gettext_path(),
33                                   languages=gettext_languages(),
34                                   fallback=True)
35_ = _translation.gettext
36_n = _translation.ngettext
37
38
39def pretty_name():
40    return _( "Unmount file systems." )
41
42
43def list_mounts(root_mount_point):
44    """ List mount points.
45
46    :param root_mount_point:
47    :return:
48    """
49    lst = []
50
51    for line in open("/etc/mtab").readlines():
52        device, mount_point, _ = line.split(" ", 2)
53
54        if mount_point.startswith(root_mount_point):
55            lst.append((device, mount_point))
56
57    return lst
58
59
60def run():
61    """ Unmounts given mountpoints in decreasing order.
62
63    :return:
64    """
65    root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
66
67    if(libcalamares.job.configuration and
68       "srcLog" in libcalamares.job.configuration and
69       "destLog" in libcalamares.job.configuration):
70        log_source = libcalamares.job.configuration["srcLog"]
71        log_destination = libcalamares.job.configuration["destLog"]
72        # Relocate log_destination into target system
73        log_destination = '{!s}/{!s}'.format(root_mount_point, log_destination)
74        # Make sure source is a string
75        log_source = '{!s}'.format(log_source)
76
77        # copy installation log before umount
78        if os.path.exists(log_source):
79            try:
80                shutil.copy2(log_source, log_destination)
81            except Exception as e:
82                libcalamares.utils.warning("Could not preserve file {!s}, "
83                                       "error {!s}".format(log_source, e))
84
85    if not root_mount_point:
86        return ("No mount point for root partition in globalstorage",
87                "globalstorage does not contain a \"rootMountPoint\" key, "
88                "doing nothing")
89
90    if not os.path.exists(root_mount_point):
91        return ("Bad mount point for root partition in globalstorage",
92                "globalstorage[\"rootMountPoint\"] is \"{}\", which does not "
93                "exist, doing nothing".format(root_mount_point))
94
95    lst = list_mounts(root_mount_point)
96    # Sort the list by mount point in decreasing order. This way we can be sure
97    # we unmount deeper dirs first.
98    lst.sort(key=lambda x: x[1], reverse=True)
99
100    for device, mount_point in lst:
101        subprocess.check_call(["umount", "-lv", mount_point])
102
103    os.rmdir(root_mount_point)
104
105    return None
Note: See TracBrowser for help on using the repository browser.