2014-07-25 16:49:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2015-02-18 15:06:10 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2017-12-20 14:39:09 +01:00
|
|
|
# === This file is part of Calamares - <https://github.com/calamares> ===
|
2014-07-25 16:49:16 +02:00
|
|
|
#
|
|
|
|
# Copyright 2014, Teo Mrnjavac <teo@kde.org>
|
2014-08-19 11:30:59 +02:00
|
|
|
# Copyright 2014, Daniel Hillenbrand <codeworkx@bbqlinux.org>
|
|
|
|
# Copyright 2014, Philip Müller <philm@manjaro.org>
|
2017-03-29 20:59:35 +02:00
|
|
|
# Copyright 2017, Alf Gaida <agaida@siduction.org>
|
2014-07-25 16:49:16 +02:00
|
|
|
#
|
|
|
|
# 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
|
2014-07-28 17:57:53 +02:00
|
|
|
import re
|
2014-07-28 17:46:56 +02:00
|
|
|
import shutil
|
2014-07-25 16:49:16 +02:00
|
|
|
import subprocess
|
2014-07-28 17:57:53 +02:00
|
|
|
import sys
|
2014-07-28 11:35:24 +02:00
|
|
|
import tempfile
|
2014-07-25 16:49:16 +02:00
|
|
|
|
|
|
|
from libcalamares import *
|
|
|
|
|
2019-01-25 13:26:51 +01:00
|
|
|
import gettext
|
|
|
|
_ = gettext.translation("calamares-python",
|
|
|
|
localedir=utils.gettext_path(),
|
|
|
|
languages=utils.gettext_languages(),
|
|
|
|
fallback=True).gettext
|
2015-02-18 15:47:24 +01:00
|
|
|
|
2019-01-25 13:54:51 +01:00
|
|
|
def pretty_name():
|
|
|
|
return _("Installing filesystems.")
|
|
|
|
|
|
|
|
|
2014-08-01 11:59:44 +02:00
|
|
|
class UnpackEntry:
|
2017-03-29 20:59:35 +02:00
|
|
|
"""
|
|
|
|
Extraction routine using rsync.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param source:
|
|
|
|
:param sourcefs:
|
|
|
|
:param destination:
|
|
|
|
"""
|
2015-02-18 15:47:24 +01:00
|
|
|
__slots__ = ['source', 'sourcefs', 'destination', 'copied', 'total']
|
2014-07-30 15:10:23 +02:00
|
|
|
|
2014-08-19 11:30:59 +02:00
|
|
|
def __init__(self, source, sourcefs, destination):
|
2014-08-01 11:59:44 +02:00
|
|
|
self.source = source
|
2014-08-19 11:30:59 +02:00
|
|
|
self.sourcefs = sourcefs
|
2014-08-01 11:59:44 +02:00
|
|
|
self.destination = destination
|
2014-07-30 15:10:23 +02:00
|
|
|
self.copied = 0
|
|
|
|
self.total = 0
|
2014-07-25 16:49:16 +02:00
|
|
|
|
2015-02-18 15:47:24 +01:00
|
|
|
|
2014-07-28 17:57:53 +02:00
|
|
|
ON_POSIX = 'posix' in sys.builtin_module_names
|
|
|
|
|
|
|
|
|
2014-11-28 18:29:34 +01:00
|
|
|
def list_excludes(destination):
|
2017-03-29 20:59:35 +02:00
|
|
|
"""
|
|
|
|
List excludes for rsync.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param destination:
|
|
|
|
:return:
|
|
|
|
"""
|
2014-12-05 00:27:59 +01:00
|
|
|
lst = []
|
2014-12-05 00:17:33 +01:00
|
|
|
extra_mounts = globalstorage.value("extraMounts")
|
2019-01-25 13:14:23 +01:00
|
|
|
if extra_mounts is None:
|
|
|
|
extra_mounts = []
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-12-05 00:17:33 +01:00
|
|
|
for extra_mount in extra_mounts:
|
|
|
|
mount_point = extra_mount["mountPoint"]
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-12-05 00:17:33 +01:00
|
|
|
if mount_point:
|
|
|
|
lst.extend(['--exclude', mount_point + '/'])
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-11-28 18:29:34 +01:00
|
|
|
return lst
|
|
|
|
|
|
|
|
|
2014-07-29 15:10:18 +02:00
|
|
|
def file_copy(source, dest, progress_cb):
|
2017-03-29 20:59:35 +02:00
|
|
|
"""
|
|
|
|
Extract given image using rsync.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param source:
|
|
|
|
:param dest:
|
|
|
|
:param progress_cb:
|
|
|
|
:return:
|
|
|
|
"""
|
2014-07-28 17:57:53 +02:00
|
|
|
# Environment used for executing rsync properly
|
|
|
|
# Setting locale to C (fix issue with tr_TR locale)
|
|
|
|
at_env = os.environ
|
|
|
|
at_env["LC_ALL"] = "C"
|
|
|
|
|
2014-08-01 11:46:29 +02:00
|
|
|
# `source` *must* end with '/' otherwise a directory named after the source
|
|
|
|
# will be created in `dest`: ie if `source` is "/foo/bar" and `dest` is
|
|
|
|
# "/dest", then files will be copied in "/dest/bar".
|
|
|
|
source += "/"
|
|
|
|
|
2014-11-28 18:29:34 +01:00
|
|
|
args = ['rsync', '-aHAXr']
|
|
|
|
args.extend(list_excludes(dest))
|
|
|
|
args.extend(['--progress', source, dest])
|
2017-03-29 20:59:35 +02:00
|
|
|
process = subprocess.Popen(
|
|
|
|
args, env=at_env, bufsize=1, stdout=subprocess.PIPE, close_fds=ON_POSIX
|
|
|
|
)
|
2014-07-28 17:57:53 +02:00
|
|
|
|
2014-07-29 15:10:18 +02:00
|
|
|
for line in iter(process.stdout.readline, b''):
|
2014-07-28 17:57:53 +02:00
|
|
|
# small comment on this regexp.
|
|
|
|
# rsync outputs three parameters in the progress.
|
|
|
|
# xfer#x => i try to interpret it as 'file copy try no. x'
|
|
|
|
# to-check=x/y, where:
|
|
|
|
# - x = number of files yet to be checked
|
|
|
|
# - y = currently calculated total number of files.
|
2014-07-29 15:10:18 +02:00
|
|
|
# but if you're copying directory with some links in it, the xfer#
|
|
|
|
# might not be a reliable counter (for one increase of xfer, many
|
|
|
|
# files may be created).
|
2014-07-28 17:57:53 +02:00
|
|
|
# In case of manjaro, we pre-compute the total number of files.
|
2014-07-29 15:10:18 +02:00
|
|
|
# therefore we can easily subtract x from y in order to get real files
|
|
|
|
# copied / processed count.
|
|
|
|
m = re.findall(r'xfr#(\d+), ir-chk=(\d+)/(\d+)', line.decode())
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-07-28 17:57:53 +02:00
|
|
|
if m:
|
|
|
|
# we've got a percentage update
|
|
|
|
num_files_remaining = int(m[0][1])
|
|
|
|
num_files_total_local = int(m[0][2])
|
|
|
|
# adjusting the offset so that progressbar can be continuesly drawn
|
|
|
|
num_files_copied = num_files_total_local - num_files_remaining
|
|
|
|
|
|
|
|
# I guess we're updating every 100 files...
|
|
|
|
if num_files_copied % 100 == 0:
|
2014-07-29 15:10:18 +02:00
|
|
|
progress_cb(num_files_copied)
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-08-06 15:10:51 +02:00
|
|
|
process.wait()
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2017-03-29 20:59:35 +02:00
|
|
|
# 23 is the return code rsync returns if it cannot write extended
|
|
|
|
# attributes (with -X) because the target file system does not support it,
|
|
|
|
# e.g., the FAT EFI system partition. We need -X because distributions
|
|
|
|
# using file system capabilities and/or SELinux require the extended
|
|
|
|
# attributes. But distributions using SELinux may also have SELinux labels
|
|
|
|
# set on files under /boot/efi, and rsync complains about those. The only
|
|
|
|
# clean way would be to split the rsync into one with -X and
|
|
|
|
# --exclude /boot/efi and a separate one without -X for /boot/efi, but only
|
|
|
|
# if /boot/efi is actually an EFI system partition. For now, this hack will
|
|
|
|
# have to do. See also:
|
2016-11-04 01:36:17 +01:00
|
|
|
# https://bugzilla.redhat.com/show_bug.cgi?id=868755#c50
|
|
|
|
# for the same issue in Anaconda, which uses a similar workaround.
|
|
|
|
if process.returncode != 0 and process.returncode != 23:
|
2014-08-06 15:10:51 +02:00
|
|
|
return "rsync failed with error code {}.".format(process.returncode)
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-08-06 15:10:51 +02:00
|
|
|
return None
|
2014-07-28 17:57:53 +02:00
|
|
|
|
|
|
|
|
2014-08-19 11:30:59 +02:00
|
|
|
class UnpackOperation:
|
2017-03-29 20:59:35 +02:00
|
|
|
"""
|
|
|
|
Extraction routine using unsquashfs.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param entries:
|
|
|
|
"""
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-08-01 11:59:44 +02:00
|
|
|
def __init__(self, entries):
|
|
|
|
self.entries = entries
|
|
|
|
self.entry_for_source = dict((x.source, x) for x in self.entries)
|
2014-07-25 16:49:16 +02:00
|
|
|
|
2014-07-29 15:10:18 +02:00
|
|
|
def report_progress(self):
|
2017-03-29 20:59:35 +02:00
|
|
|
"""
|
|
|
|
Pass progress to user interface
|
|
|
|
"""
|
2014-07-29 15:10:18 +02:00
|
|
|
progress = float(0)
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-08-01 11:59:44 +02:00
|
|
|
for entry in self.entries:
|
|
|
|
if entry.total == 0:
|
2014-07-25 16:49:16 +02:00
|
|
|
continue
|
|
|
|
|
2014-07-29 15:10:18 +02:00
|
|
|
partialprogress = 0.05 # Having a total !=0 gives 5%
|
2014-07-25 16:49:16 +02:00
|
|
|
|
2015-02-18 15:47:24 +01:00
|
|
|
partialprogress += 0.95 * (entry.copied / float(entry.total))
|
2014-08-01 11:59:44 +02:00
|
|
|
progress += partialprogress / len(self.entries)
|
2014-07-25 16:49:16 +02:00
|
|
|
|
2014-07-29 15:10:18 +02:00
|
|
|
job.setprogress(progress)
|
2014-07-25 16:49:16 +02:00
|
|
|
|
2014-07-29 15:10:18 +02:00
|
|
|
def run(self):
|
2017-03-29 20:59:35 +02:00
|
|
|
"""
|
|
|
|
Extract given image using unsquashfs.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2014-07-29 15:10:18 +02:00
|
|
|
source_mount_path = tempfile.mkdtemp()
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-07-28 11:35:24 +02:00
|
|
|
try:
|
2014-08-01 11:59:44 +02:00
|
|
|
for entry in self.entries:
|
2017-03-29 20:59:35 +02:00
|
|
|
imgbasename = os.path.splitext(
|
|
|
|
os.path.basename(entry.source))[0]
|
2014-07-30 15:06:59 +02:00
|
|
|
imgmountdir = os.path.join(source_mount_path, imgbasename)
|
2014-07-29 15:10:18 +02:00
|
|
|
os.mkdir(imgmountdir)
|
2014-08-19 11:30:59 +02:00
|
|
|
|
|
|
|
self.mount_image(entry, imgmountdir)
|
|
|
|
|
2014-08-19 19:12:48 +02:00
|
|
|
fslist = ""
|
|
|
|
|
2014-08-19 11:30:59 +02:00
|
|
|
if entry.sourcefs == "squashfs":
|
2015-02-18 15:20:02 +01:00
|
|
|
if shutil.which("unsquashfs") is None:
|
2016-04-28 16:36:46 +02:00
|
|
|
msg = ("Failed to find unsquashfs, make sure you have "
|
|
|
|
"the squashfs-tools package installed")
|
|
|
|
print(msg)
|
2015-02-18 15:47:24 +01:00
|
|
|
return ("Failed to unpack image",
|
2016-04-28 16:36:46 +02:00
|
|
|
msg)
|
2014-10-14 15:09:38 +02:00
|
|
|
|
2017-03-29 20:59:35 +02:00
|
|
|
fslist = subprocess.check_output(
|
|
|
|
["unsquashfs", "-l", entry.source]
|
|
|
|
)
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-08-19 11:30:59 +02:00
|
|
|
if entry.sourcefs == "ext4":
|
2017-03-29 20:59:35 +02:00
|
|
|
fslist = subprocess.check_output(
|
|
|
|
["find", imgmountdir, "-type", "f"]
|
|
|
|
)
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-08-19 19:12:48 +02:00
|
|
|
entry.total = len(fslist.splitlines())
|
2014-08-19 11:30:59 +02:00
|
|
|
|
2014-07-29 15:10:18 +02:00
|
|
|
self.report_progress()
|
2014-08-19 11:30:59 +02:00
|
|
|
error_msg = self.unpack_image(entry, imgmountdir)
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-08-06 15:10:51 +02:00
|
|
|
if error_msg:
|
2017-03-29 20:59:35 +02:00
|
|
|
return ("Failed to unpack image {}".format(entry.source),
|
|
|
|
error_msg)
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-08-06 15:10:51 +02:00
|
|
|
return None
|
2014-07-28 11:35:24 +02:00
|
|
|
finally:
|
2017-12-26 22:25:40 +01:00
|
|
|
shutil.rmtree(source_mount_path, ignore_errors=True, onerror=None)
|
2014-07-28 12:17:06 +02:00
|
|
|
|
2014-08-19 11:30:59 +02:00
|
|
|
def mount_image(self, entry, imgmountdir):
|
2017-03-29 20:59:35 +02:00
|
|
|
"""
|
|
|
|
Mount given image as loop device.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param entry:
|
|
|
|
:param imgmountdir:
|
|
|
|
"""
|
2016-06-04 04:15:43 +02:00
|
|
|
if os.path.isdir(entry.source):
|
2017-03-29 20:59:35 +02:00
|
|
|
subprocess.check_call(["mount",
|
|
|
|
"--bind", entry.source,
|
|
|
|
imgmountdir])
|
2016-06-04 04:15:43 +02:00
|
|
|
else:
|
2017-03-29 20:59:35 +02:00
|
|
|
subprocess.check_call(["mount",
|
|
|
|
entry.source,
|
|
|
|
imgmountdir,
|
|
|
|
"-t", entry.sourcefs,
|
|
|
|
"-o", "loop"
|
|
|
|
])
|
2014-08-19 11:30:59 +02:00
|
|
|
|
|
|
|
def unpack_image(self, entry, imgmountdir):
|
2017-03-29 20:59:35 +02:00
|
|
|
"""
|
|
|
|
Unpacks image.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param entry:
|
|
|
|
:param imgmountdir:
|
|
|
|
:return:
|
|
|
|
"""
|
2014-07-30 15:35:51 +02:00
|
|
|
def progress_cb(copied):
|
2015-02-21 11:02:25 +01:00
|
|
|
""" Copies file to given destination target.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param copied:
|
|
|
|
"""
|
2014-08-01 11:59:44 +02:00
|
|
|
entry.copied = copied
|
2014-07-30 15:35:51 +02:00
|
|
|
self.report_progress()
|
|
|
|
|
2014-07-28 11:35:24 +02:00
|
|
|
try:
|
2015-06-14 13:25:37 +02:00
|
|
|
return file_copy(imgmountdir, entry.destination, progress_cb)
|
2014-07-28 11:35:24 +02:00
|
|
|
finally:
|
2014-07-30 15:06:39 +02:00
|
|
|
subprocess.check_call(["umount", "-l", imgmountdir])
|
2014-07-25 16:49:16 +02:00
|
|
|
|
|
|
|
|
2019-01-25 12:01:49 +01:00
|
|
|
def get_supported_filesystems():
|
2017-03-29 20:59:35 +02:00
|
|
|
"""
|
2019-01-25 12:01:49 +01:00
|
|
|
Reads /proc/filesystems (the list of supported filesystems
|
|
|
|
for the current kernel) and returns a list of (names of)
|
|
|
|
those filesystems.
|
2015-02-20 20:54:25 +01:00
|
|
|
"""
|
2014-08-19 19:52:26 +02:00
|
|
|
PATH_PROCFS = '/proc/filesystems'
|
|
|
|
|
2019-01-25 12:01:49 +01:00
|
|
|
if os.path.isfile(PATH_PROCFS) and os.access(PATH_PROCFS, os.R_OK):
|
|
|
|
with open(PATH_PROCFS, 'r') as procfile:
|
|
|
|
filesystems = procfile.read()
|
|
|
|
filesystems = filesystems.replace(
|
|
|
|
"nodev", "").replace("\t", "").splitlines()
|
|
|
|
return filesystems
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
def run():
|
|
|
|
"""
|
|
|
|
Unsquash filesystem.
|
|
|
|
"""
|
2014-07-29 15:10:18 +02:00
|
|
|
root_mount_point = globalstorage.value("rootMountPoint")
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-07-29 15:10:18 +02:00
|
|
|
if not root_mount_point:
|
2019-01-25 13:53:39 +01:00
|
|
|
utils.warning("No mount point for root partition")
|
2019-01-25 13:26:51 +01:00
|
|
|
return (_("No mount point for root partition"),
|
|
|
|
_("globalstorage does not contain a \"rootMountPoint\" key, "
|
|
|
|
"doing nothing"))
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-07-29 15:10:18 +02:00
|
|
|
if not os.path.exists(root_mount_point):
|
2019-01-25 13:53:39 +01:00
|
|
|
utils.warning("Bad root mount point \"{}\"".format(root_mount_point))
|
2019-01-25 13:26:51 +01:00
|
|
|
return (_("Bad mount point for root partition"),
|
|
|
|
_("rootMountPoint is \"{}\", which does not "
|
|
|
|
"exist, doing nothing").format(root_mount_point))
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2019-01-25 12:01:49 +01:00
|
|
|
supported_filesystems = get_supported_filesystems()
|
|
|
|
|
2014-07-25 16:49:16 +02:00
|
|
|
unpack = list()
|
|
|
|
|
2014-07-29 15:10:18 +02:00
|
|
|
for entry in job.configuration["unpack"]:
|
|
|
|
source = os.path.abspath(entry["source"])
|
2014-08-19 11:30:59 +02:00
|
|
|
sourcefs = entry["sourcefs"]
|
2014-08-19 19:52:26 +02:00
|
|
|
|
2019-01-25 12:01:49 +01:00
|
|
|
if sourcefs not in supported_filesystems:
|
2019-01-25 13:53:39 +01:00
|
|
|
utils.warning("The filesystem for \"{}\" ({}) is not supported".format(source, sourcefs))
|
2019-01-25 13:26:51 +01:00
|
|
|
return (_("Bad unsquash configuration"),
|
|
|
|
_("The filesystem for \"{}\" ({}) is not supported").format(source, sourcefs))
|
2014-08-19 12:02:03 +02:00
|
|
|
|
2014-07-30 15:05:19 +02:00
|
|
|
destination = os.path.abspath(root_mount_point + entry["destination"])
|
2014-07-25 16:49:16 +02:00
|
|
|
|
2017-03-29 20:59:35 +02:00
|
|
|
if not os.path.exists(source):
|
2019-01-25 13:53:39 +01:00
|
|
|
utils.warning("The source filesystem \"{}\" does not exist".format(source))
|
2019-01-25 13:26:51 +01:00
|
|
|
return (_("Bad unsquash configuration"),
|
|
|
|
_("The source filesystem \"{}\" does not exist").format(source))
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-07-30 15:05:44 +02:00
|
|
|
if not os.path.isdir(destination):
|
2019-01-25 13:53:39 +01:00
|
|
|
utils.warning(("The destination \"{}\" in the target system is not a directory").format(destination))
|
2019-01-25 13:26:51 +01:00
|
|
|
return (_("Bad unsquash configuration"),
|
|
|
|
_("The destination \"{}\" in the target system is not a directory").format(destination))
|
2014-07-25 16:49:16 +02:00
|
|
|
|
2014-08-19 11:30:59 +02:00
|
|
|
unpack.append(UnpackEntry(source, sourcefs, destination))
|
2014-07-25 16:49:16 +02:00
|
|
|
|
2014-08-19 11:30:59 +02:00
|
|
|
unpackop = UnpackOperation(unpack)
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2014-08-19 11:30:59 +02:00
|
|
|
return unpackop.run()
|