Optimized and solved bugs
This commit is contained in:
59
main.py
59
main.py
@@ -92,23 +92,28 @@ def update_grub(uuid, offset):
|
|||||||
new_lines = lines[:]
|
new_lines = lines[:]
|
||||||
for i, line in enumerate(new_lines):
|
for i, line in enumerate(new_lines):
|
||||||
if line.startswith("GRUB_CMDLINE_LINUX_DEFAULT"):
|
if line.startswith("GRUB_CMDLINE_LINUX_DEFAULT"):
|
||||||
# Remove existing resume and resume_offset entries
|
# Extract current quoted content
|
||||||
line = re.sub(r'resume=UUID=\S+', '', line)
|
match = re.match(r'^(GRUB_CMDLINE_LINUX_DEFAULT=)(["\'])(.*)(\2)$', line.strip())
|
||||||
line = re.sub(r'resume_offset=\S+', '', line)
|
if not match:
|
||||||
line = re.sub(r'\s+', ' ', line) # Clean up extra spaces
|
print("[!] Unexpected format in GRUB_CMDLINE_LINUX_DEFAULT, skipping safe modification.")
|
||||||
|
continue
|
||||||
|
|
||||||
# Safely inject new resume info inside quotes
|
prefix, quote, content, _ = match.groups()
|
||||||
match = re.match(r'^(GRUB_CMDLINE_LINUX_DEFAULT=)(["\'])(.*?)(["\'])$', line.strip())
|
|
||||||
if match:
|
# Remove existing resume/resume_offset if present
|
||||||
prefix, quote, content, _ = match.groups()
|
content = re.sub(r'\s*resume=UUID=\S+', '', content)
|
||||||
content = content.strip()
|
content = re.sub(r'\s*resume_offset=\S+', '', content)
|
||||||
if not content.endswith(' '):
|
content = content.strip()
|
||||||
content += ' '
|
|
||||||
content += f'resume=UUID={uuid} resume_offset={offset}'
|
# Append new values
|
||||||
new_lines[i] = f'{prefix}{quote}{content}{quote}\n'
|
resume_args = f"resume=UUID={uuid} resume_offset={offset}"
|
||||||
|
if content:
|
||||||
|
content += f" {resume_args}"
|
||||||
else:
|
else:
|
||||||
# If the line format is unexpected, append the resume parameters at the end
|
content = resume_args
|
||||||
new_lines[i] = line.strip() + f' resume=UUID={uuid} resume_offset={offset}\n'
|
|
||||||
|
# Write the modified line
|
||||||
|
new_lines[i] = f"{prefix}{quote}{content}{quote}\n"
|
||||||
break
|
break
|
||||||
|
|
||||||
if confirm_file_change(GRUB_CONF, new_lines):
|
if confirm_file_change(GRUB_CONF, new_lines):
|
||||||
@@ -130,8 +135,28 @@ def update_mkinitcpio():
|
|||||||
|
|
||||||
new_lines = lines[:]
|
new_lines = lines[:]
|
||||||
for i, line in enumerate(new_lines):
|
for i, line in enumerate(new_lines):
|
||||||
if line.startswith("HOOKS=") and "resume" not in line:
|
if line.startswith("HOOKS="):
|
||||||
new_lines[i] = line.strip().rstrip(")") + " resume)\n"
|
# Extract hook list
|
||||||
|
match = re.search(r'\((.*?)\)', line)
|
||||||
|
if not match:
|
||||||
|
continue
|
||||||
|
|
||||||
|
hooks = match.group(1).split()
|
||||||
|
if "resume" in hooks:
|
||||||
|
print("[-] 'resume' hook already present.")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Insert resume after encrypt (or just before filesystems as fallback)
|
||||||
|
if "encrypt" in hooks:
|
||||||
|
index = hooks.index("encrypt") + 1
|
||||||
|
elif "filesystems" in hooks:
|
||||||
|
index = hooks.index("filesystems")
|
||||||
|
else:
|
||||||
|
index = len(hooks)
|
||||||
|
|
||||||
|
hooks.insert(index, "resume")
|
||||||
|
new_line = f'HOOKS=({" ".join(hooks)})\n'
|
||||||
|
new_lines[i] = new_line
|
||||||
break
|
break
|
||||||
|
|
||||||
if confirm_file_change(MKINITCPIO, new_lines):
|
if confirm_file_change(MKINITCPIO, new_lines):
|
||||||
|
|||||||
Reference in New Issue
Block a user