OSDN Git Service

[update] : Added postcfg
[alterlinux/alterlinux-calamares.git] / src / modules / postcfg / main.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 # === This file is part of Calamares - <http://github.com/calamares> ===
5 #
6 #   Copyright 2014 - 2019, Philip Müller <philm@manjaro.org>
7 #   Copyright 2016, Artoo <artoo@manjaro.org>
8 #
9 #   Calamares is free software: you can redistribute it and/or modify
10 #   it under the terms of the GNU General Public License as published by
11 #   the Free Software Foundation, either version 3 of the License, or
12 #   (at your option) any later version.
13 #
14 #   Calamares is distributed in the hope that it will be useful,
15 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #   GNU General Public License for more details.
18 #
19 #   You should have received a copy of the GNU General Public License
20 #   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
21
22 import libcalamares
23 import subprocess
24
25 from shutil import copy2
26 from distutils.dir_util import copy_tree
27 from os.path import join, exists
28 from libcalamares.utils import target_env_call
29
30
31 class ConfigController:
32     def __init__(self):
33         self.__root = libcalamares.globalstorage.value("rootMountPoint")
34         self.__keyrings = libcalamares.job.configuration.get('keyrings', [])
35
36     @property
37     def root(self):
38         return self.__root
39
40     @property
41     def keyrings(self):
42         return self.__keyrings
43
44     def init_keyring(self):
45         target_env_call(["pacman-key", "--init"])
46
47     def populate_keyring(self):
48         target_env_call(["pacman-key", "--populate"] + self.keyrings)
49
50     def terminate(self, proc):
51         target_env_call(['killall', '-9', proc])
52
53     def copy_file(self, file):
54         if exists("/" + file):
55             copy2("/" + file, join(self.root, file))
56
57     def copy_folder(self, source, target):
58         if exists("/" + source):
59             copy_tree("/" + source, join(self.root, target))
60
61     def remove_pkg(self, pkg, path):
62         if exists(join(self.root, path)):
63             target_env_call(['pacman', '-R', '--noconfirm', pkg])
64
65     def umount(self, mp):
66         subprocess.call(["umount", "-l", join(self.root, mp)])
67
68     def mount(self, mp):
69         subprocess.call(["mount", "-B", "/" + mp, join(self.root, mp)])
70
71     def rmdir(self, dir):
72         subprocess.call(["rm", "-Rf", join(self.root, dir)])
73
74     def mkdir(self, dir):
75         subprocess.call(["mkdir", "-p", join(self.root, dir)])
76
77     def run(self):
78         self.init_keyring()
79         self.populate_keyring()
80
81         # Generate mirror list
82         if exists(join(self.root, "usr/bin/pacman-mirrors")):
83             if libcalamares.globalstorage.value("hasInternet"):
84                 target_env_call(["pacman-mirrors", "-f3"])
85         else:
86             self.copy_file('etc/pacman.d/mirrorlist')
87
88         # Initialize package manager databases
89         if libcalamares.globalstorage.value("hasInternet"):
90             target_env_call(["pacman", "-Syy"])
91
92         # Remove unneeded ucode
93         cpu_ucode = subprocess.getoutput("hwinfo --cpu | grep Vendor: -m1 | cut -d\'\"\' -f2")
94         if cpu_ucode == "AuthenticAMD":
95             self.remove_pkg("intel-ucode", "boot/intel-ucode.img")
96         elif cpu_ucode == "GenuineIntel":
97             self.remove_pkg("amd-ucode", "boot/amd-ucode.img")
98
99         # Remove calamares
100         self.remove_pkg("calamares", "usr/bin/calamares")
101
102         # Copy skel to root
103         self.copy_folder('etc/skel', 'root')
104
105         # Workaround for pacman-key bug
106         # FS#45351 https://bugs.archlinux.org/task/45351
107         # We have to kill gpg-agent because if it stays
108         # around we can't reliably unmount
109         # the target partition.
110         self.terminate('gpg-agent')
111
112         # Update grub.cfg
113         if exists(join(self.root, "usr/bin/update-grub")):
114             target_env_call(["update-grub"])
115
116         # Enable 'menu_auto_hide' when supported in grubenv
117         if exists(join(self.root, "usr/bin/grub-set-bootflag")):
118             target_env_call(["grub-editenv", "-", "set", "menu_auto_hide=1", "boot_success=1"])
119
120         # Install Office Suite if selected (WIP)
121         office_package = libcalamares.globalstorage.value("packagechooser_packagechooser")
122         if not office_package:
123             libcalamares.utils.warning("no office suite selected, {!s}".format(office_package))
124         else:
125             # For PoC we added the Office Packages to mhwd-live overlay in 18.1.0
126             cmd = ["pacman", "-S", office_package, "--noconfirm", "--config", "/opt/mhwd/pacman-mhwd.conf" ]
127             self.mkdir("opt/mhwd")
128             self.mount("opt/mhwd")
129             self.mount("etc/resolv.conf")
130             target_env_call(cmd)
131             self.umount("opt/mhwd")
132             self.rmdir("opt/mhwd")
133             self.umount("etc/resolv.conf")
134
135         return None
136
137
138 def run():
139     """ Misc postinstall configurations """
140
141     config = ConfigController()
142
143     return config.run()