1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# === This file is part of Calamares - <https://github.com/calamares> ===
#
# Copyright 2019, Collabora Ltd <arnaud.ferraris@collabora.com>
#
# 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 libcalamares
import os
import stat
import subprocess
from time import gmtime, strftime, sleep
from math import gcd
import gettext
_ = gettext.translation("calamares-python",
localedir=libcalamares.utils.gettext_path(),
languages=libcalamares.utils.gettext_languages(),
fallback=True).gettext
def pretty_name():
return _("Installing data.")
def lcm(a, b):
"""
Computes the Least Common Multiple of 2 numbers
"""
return a * b / gcd(a, b)
def get_device_size(device):
"""
Returns a filesystem's total size and block size in bytes.
For block devices, block size is the device's block size.
For other files (fs images), block size is 1 byte.
@param device: str
Absolute path to the device or filesystem image.
@return: tuple(int, int)
The filesystem's size and its block size.
"""
mode = os.stat(device).st_mode
if stat.S_ISBLK(mode):
basedevice = ""
partition = os.path.basename(device)
tmp = partition
while len(tmp) > 0:
tmp = tmp[:-1]
if os.path.exists("/sys/block/" + tmp):
basedevice = tmp
break
# Get device block size
file = open("/sys/block/" + basedevice + "/queue/hw_sector_size")
blocksize = int(file.readline())
file.close()
# Get partition size
file = open("/sys/block/" + basedevice + "/" + partition + "/size")
size = int(file.readline()) * blocksize
file.close()
else:
size = os.path.getsize(device)
blocksize = 1
return size, blocksize
class RawFSLowSpaceError(Exception):
pass
class RawFSItem:
__slots__ = ['source', 'destination', 'filesystem', 'resize']
def copy(self, current=0, total=1):
"""
Copies a raw filesystem on a disk partition, and grow it to the full destination
partition's size if required.
@param current: int
The index of the current item in the filesystems list
(used for progress reporting)
@param total: int
The number of items in the filesystems list
(used for progress reporting)
"""
count = 0
libcalamares.utils.debug("Copying {} to {}".format(self.source, self.destination))
srcsize, srcblksize = get_device_size(self.source)
destsize, destblksize = get_device_size(self.destination)
if destsize < srcsize:
raise RawFSLowSpaceError
return
# Compute transfer block size (100x the LCM of the block sizes seems a good fit)
blksize = int(100 * lcm(srcblksize, destblksize))
# Execute copy
src = open(self.source, "rb")
dest = open(self.destination, "wb")
buffer = src.read(blksize)
while len(buffer) > 0:
dest.write(buffer)
count += len(buffer)
# Compute job progress
progress = ((count / srcsize) + (current)) / total
libcalamares.job.setprogress(progress)
# Read next data block
buffer = src.read(blksize)
src.close()
dest.close()
if self.resize:
if "ext" in self.filesystem:
libcalamares.utils.debug("Resizing filesystem on {}".format(self.destination))
subprocess.run(["e2fsck", "-f", "-y", self.destination])
subprocess.run(["resize2fs", self.destination])
def __init__(self, config, device, fs):
libcalamares.utils.debug("Adding an entry for raw copy of {} to {}".format(
config["source"], device))
self.source = os.path.realpath(config["source"])
# If source is a mount point, look for the actual device mounted on it
if os.path.ismount(self.source):
procmounts = open("/proc/mounts", "r")
for line in procmounts:
if self.source in line.split():
self.source = line.split()[0]
break
self.destination = device
self.filesystem = fs
try:
self.resize = bool(config["resize"])
except KeyError:
self.resize = False
def update_global_storage(item, gs):
for partition in gs:
if partition["device"] == item.destination:
ret = subprocess.run(["blkid", "-s", "UUID", "-o", "value", item.destination],
capture_output=True, text=True)
if ret.returncode == 0:
libcalamares.utils.debug("Setting {} UUID to {}".format(item.destination,
ret.stdout.rstrip()))
gs[gs.index(partition)]["uuid"] = ret.stdout.rstrip()
libcalamares.globalstorage.remove("partitions")
libcalamares.globalstorage.insert("partitions", gs)
def run():
"""Raw filesystem copy module"""
filesystems = list()
partitions = libcalamares.globalstorage.value("partitions")
for partition in partitions:
if partition["mountPoint"]:
for src in libcalamares.job.configuration["targets"]:
if src["mountPoint"] == partition["mountPoint"]:
filesystems.append(RawFSItem(src, partition["device"], partition["fs"]))
for item in filesystems:
try:
item.copy(filesystems.index(item), len(filesystems))
except RawFSLowSpaceError:
return ("Not enough free space",
"{} partition is too small to copy {} on it".format(item.destination, item.source))
update_global_storage(item, partitions)
return None
|