calamares/src/modules/postcfg/main.py

156 lines
5.2 KiB
Python
Raw Normal View History

2016-09-10 13:56:05 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# === This file is part of Calamares - <http://github.com/calamares> ===
#
2021-10-17 16:46:32 +02:00
# Copyright 2014 - 2021, Philip Müller <philm@manjaro.org>
2016-09-10 13:56:05 +02:00
# Copyright 2016, Artoo <artoo@manjaro.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 libcalamares
2018-10-29 18:53:48 +01:00
import subprocess
2021-10-17 16:46:32 +02:00
import os
2016-09-10 13:56:05 +02:00
from shutil import copy2
from distutils.dir_util import copy_tree
2016-09-10 13:56:05 +02:00
from os.path import join, exists
from libcalamares.utils import target_env_call
2016-09-10 13:56:05 +02:00
2017-06-23 11:24:17 +02:00
2016-09-10 13:56:05 +02:00
class ConfigController:
def __init__(self):
self.__root = libcalamares.globalstorage.value("rootMountPoint")
2016-09-10 13:56:05 +02:00
self.__keyrings = libcalamares.job.configuration.get('keyrings', [])
2016-09-10 13:56:05 +02:00
@property
def root(self):
return self.__root
2016-09-10 13:56:05 +02:00
@property
def keyrings(self):
return self.__keyrings
2016-09-10 13:56:05 +02:00
def init_keyring(self):
target_env_call(["pacman-key", "--init"])
2016-09-10 13:56:05 +02:00
def populate_keyring(self):
target_env_call(["pacman-key", "--populate"] + self.keyrings)
2016-09-10 13:56:05 +02:00
def terminate(self, proc):
target_env_call(['killall', '-9', proc])
2021-10-17 16:46:32 +02:00
def remove_symlink(self, target):
for root, dirs, files in os.walk("/" + target):
for filename in files:
path = os.path.join(root, filename)
if os.path.islink(path):
os.unlink(path)
2016-09-10 13:56:05 +02:00
def copy_file(self, file):
if exists("/" + file):
copy2("/" + file, join(self.root, file))
2016-09-10 13:56:05 +02:00
def copy_folder(self, source, target):
if exists("/" + source):
2021-08-09 11:06:58 +02:00
copy_tree("/" + source, join(self.root, target), preserve_symlinks=1)
2016-09-10 13:56:05 +02:00
def remove_pkg(self, pkg, path):
if exists(join(self.root, path)):
target_env_call(['pacman', '-R', '--noconfirm', pkg])
def umount(self, mp):
subprocess.call(["umount", "-l", join(self.root, mp)])
def mount(self, mp):
2019-08-31 11:11:00 +02:00
subprocess.call(["mount", "-B", "/" + mp, join(self.root, mp)])
def rmdir(self, dir):
subprocess.call(["rm", "-Rf", join(self.root, dir)])
def mkdir(self, dir):
2019-09-02 22:42:19 +02:00
subprocess.call(["mkdir", "-p", join(self.root, dir)])
2016-09-10 13:56:05 +02:00
def run(self):
self.init_keyring()
self.populate_keyring()
# Generate mirror list
if exists(join(self.root, "usr/bin/pacman-mirrors")):
if libcalamares.globalstorage.value("hasInternet"):
2018-01-02 09:35:43 +01:00
target_env_call(["pacman-mirrors", "-f3"])
2016-09-10 13:56:05 +02:00
else:
self.copy_file('etc/pacman.d/mirrorlist')
# Initialize package manager databases
if libcalamares.globalstorage.value("hasInternet"):
target_env_call(["pacman", "-Syy"])
2018-08-26 05:06:25 +02:00
# Remove unneeded ucode
2018-10-29 18:53:48 +01:00
cpu_ucode = subprocess.getoutput("hwinfo --cpu | grep Vendor: -m1 | cut -d\'\"\' -f2")
2018-08-26 05:06:25 +02:00
if cpu_ucode == "AuthenticAMD":
self.remove_pkg("intel-ucode", "boot/intel-ucode.img")
elif cpu_ucode == "GenuineIntel":
self.remove_pkg("amd-ucode", "boot/amd-ucode.img")
2021-10-17 16:46:32 +02:00
# Remove symlinks before copying
self.remove_symlink('root')
2016-09-10 13:56:05 +02:00
# Copy skel to root
self.copy_folder('etc/skel', 'root')
2017-06-23 11:24:17 +02:00
# Workaround for pacman-key bug
# FS#45351 https://bugs.archlinux.org/task/45351
# We have to kill gpg-agent because if it stays
# around we can't reliably unmount
2016-09-10 13:56:05 +02:00
# the target partition.
self.terminate('gpg-agent')
2016-09-10 13:56:05 +02:00
# Update grub.cfg
2018-10-29 21:03:13 +01:00
if exists(join(self.root, "usr/bin/update-grub")):
2016-09-10 13:56:05 +02:00
target_env_call(["update-grub"])
2018-09-25 20:15:40 +02:00
# Enable 'menu_auto_hide' when supported in grubenv
2018-09-25 20:16:20 +02:00
if exists(join(self.root, "usr/bin/grub-set-bootflag")):
2018-09-25 20:21:26 +02:00
target_env_call(["grub-editenv", "-", "set", "menu_auto_hide=1", "boot_success=1"])
2018-09-25 20:15:40 +02:00
# Install Office Suite if selected (WIP)
2019-09-02 20:18:17 +02:00
office_package = libcalamares.globalstorage.value("packagechooser_packagechooser")
if not office_package:
libcalamares.utils.warning("no office suite selected, {!s}".format(office_package))
else:
2019-08-31 11:21:11 +02:00
# For PoC we added the Office Packages to mhwd-live overlay in 18.1.0
cmd = ["pacman", "-S", office_package, "--noconfirm", "--config", "/opt/mhwd/pacman-mhwd.conf" ]
self.mkdir("opt/mhwd")
2019-08-31 11:21:11 +02:00
self.mount("opt/mhwd")
self.mount("etc/resolv.conf")
target_env_call(cmd)
2019-08-31 11:21:11 +02:00
self.umount("opt/mhwd")
self.rmdir("opt/mhwd")
self.umount("etc/resolv.conf")
2021-10-17 16:46:32 +02:00
# Remove calamares
self.remove_pkg("calamares", "usr/bin/calamares")
self.remove_pkg("calamares-git", "usr/bin/calamares")
2016-09-10 13:56:05 +02:00
return None
2017-06-23 11:24:17 +02:00
2016-09-10 13:56:05 +02:00
def run():
""" Misc postinstall configurations """
config = ConfigController()
2016-09-10 13:56:05 +02:00
return config.run()