Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Hvornum <anton.feeds@gmail.com>2021-03-08 17:13:21 +0100
committerAnton Hvornum <anton.feeds@gmail.com>2021-03-08 17:13:21 +0100
commitfb55e318e5dc87d05da4c636722d24ab9e9cb5b1 (patch)
tree58668a19e299c83642dda2c4e76d1ff18f87577b
parent6306de4bfee2d44ab1f362078a47d6d9a05835ef (diff)
Added two new functions. partition.safe_to_format() and partition.has_content(). The first does some sanity checks to verify if we can format the partition or not. The second temporarly mounts the parition and checks if there's content inside and returns accordingly.
-rw-r--r--archinstall/lib/disk.py20
-rw-r--r--examples/guided.py4
2 files changed, 21 insertions, 3 deletions
diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py
index 2f3d8233..16d6f704 100644
--- a/archinstall/lib/disk.py
+++ b/archinstall/lib/disk.py
@@ -1,4 +1,4 @@
-import glob, re, os, json, time # Time is only used to gracefully wait for new paritions to come online
+import glob, re, os, json, time, hashlib
from collections import OrderedDict
from .exceptions import DiskError
from .general import *
@@ -173,6 +173,22 @@ class Partition():
else:
return f'Partition(path={self.path}, fs={self.filesystem}{mount_repr})'
+ def has_content(self):
+ temporary_mountpoint = '/tmp/'+hashlib.md5(bytes(f"{time.time()}", 'UTF-8')+os.urandom(12)).hexdigest()
+ if (handle := sys_command(f'/usr/bin/mount {self.path} {temporary_mountpoint}')).exit_code != 0:
+ raise DiskError(f'Could not mount and check for content on {self.path} because: {b"".join(handle)}')
+
+ files = len(glob.glob(f"{temporary_mountpoint}/*"))
+ sys_command(f'/usr/bin/umount {temporary_mountpoint}')
+
+ return True if files > 0 else False
+
+ def safe_to_format(self):
+ if self.target_mountpoint == '/boot' and self.has_content():
+ return False
+
+ return True
+
def format(self, filesystem=None, path=None, allow_formatting=None, log_formating=True):
"""
Format can be given an overriding path, for instance /dev/null to test
@@ -223,7 +239,7 @@ class Partition():
encrypted_partition = luks2(self, None, None)
encrypted_partition.format(path)
self.filesystem = 'crypto_LUKS'
-
+
else:
raise UnknownFilesystemFormat(f"Fileformat '{filesystem}' is not yet implemented.")
return True
diff --git a/examples/guided.py b/examples/guided.py
index b289016b..fc1fe88d 100644
--- a/examples/guided.py
+++ b/examples/guided.py
@@ -281,8 +281,10 @@ with archinstall.Filesystem(archinstall.arguments['harddrive'], archinstall.GPT)
fs.use_entire_disk(archinstall.arguments.get('filesystem', 'ext4'))
else:
for partition in archinstall.arguments['harddrive']:
- if partition.allow_formatting:
+ if partition.allow_formatting and partition.safe_to_format():
partition.format()
+ else:
+ archinstall.log(f"Did not format {partition} because .safe_to_format() returned False or .allow_formatting was False", level=archinstall.LOG_LEVELS.Debug)
if archinstall.arguments.get('!encryption-password', None):
# First encrypt and unlock, then format the desired partition inside the encrypted part.