calamares/src/modules/umount/main.py

72 lines
2.2 KiB
Python
Raw Normal View History

2014-07-24 13:51:14 +02:00
#!/usr/bin/env python3
2015-02-18 15:06:10 +01:00
# -*- coding: utf-8 -*-
#
2014-07-24 13:51:14 +02:00
# === This file is part of Calamares - <http://github.com/calamares> ===
#
# Copyright 2014, Aurélien Gâteau <agateau@kde.org>
#
# Calamares 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, either version 3 of the License, or
# (at your option) any later version.
#
# Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
import os
import subprocess
import libcalamares
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
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
2014-07-29 14:53:07 +02:00
if mount_point.startswith(root_mount_point):
lst.append((device, mount_point))
2015-06-14 13:25:37 +02:00
2014-07-24 13:51:14 +02:00
return lst
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")
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:
subprocess.check_call(["umount", "-lv", mount_point])
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
return None