OSDN Git Service

2020.05.10 update
[rebornos/cnchi-gnome-osdn.git] / Cnchi / systemd_boot.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # systemd_boot.py
5 #
6 # Copyright © 2013-2017 Antergos
7 #
8 # This file is part of Cnchi.
9 #
10 # Cnchi is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Cnchi is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # The following additional terms are in effect as per Section 7 of the license:
21 #
22 # The preservation of all legal notices and author attributions in
23 # the material or in the Appropriate Legal Notices displayed
24 # by works containing it is required.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with Cnchi; If not, see <http://www.gnu.org/licenses/>.
28
29
30 """ Systemd-boot (gummiboot) installation """
31
32 import logging
33 import os
34
35 import parted3.fs_module as fs
36
37 from misc.run_cmd import chroot_call
38
39
40 class SystemdBoot(object):
41     """ Class to perform boot loader installation """
42
43     def __init__(self, dest_dir, settings, uuids):
44         self.dest_dir = dest_dir
45         self.settings = settings
46         self.uuids = uuids
47
48     def install(self):
49         """ Install Systemd-boot bootloader to the EFI System Partition """
50         logging.debug("Cnchi will install the Systemd-boot (Gummiboot) loader")
51
52         # Setup bootloader menu
53         menu_dir = os.path.join(self.dest_dir, "boot/loader")
54         os.makedirs(menu_dir, mode=0o755, exist_ok=True)
55         menu_path = os.path.join(menu_dir, "loader.conf")
56         with open(menu_path, 'w') as menu_file:
57             menu_file.write("default antergos\n")
58             menu_file.write("timeout 3\n")
59
60         # Setup boot entries
61         conf = {}
62         options = ""
63
64         if not self.settings.get('use_luks'):
65             options = "root=UUID={0} rw quiet".format(self.uuids["/"])
66         else:
67             luks_root_volume = self.settings.get('luks_root_volume')
68             logging.debug("Luks Root Volume: %s", luks_root_volume)
69             mapper = "/dev/mapper/{0}".format(luks_root_volume)
70             luks_root_volume_uuid = fs.get_uuid(mapper)
71
72             if (self.settings.get("partition_mode") == "advanced" and
73                     self.settings.get('use_luks_in_root')):
74                 # In advanced, if using luks in root device,
75                 # we store root device it in luks_root_device var
76                 root_device = self.settings.get('luks_root_device')
77                 self.uuids["/"] = fs.get_uuid(root_device)
78
79             key = ""
80             if not self.settings.get("luks_root_password"):
81                 key = "cryptkey=UUID={0}:ext2:/.keyfile-root"
82                 key = key.format(self.uuids["/boot"])
83
84             if not self.settings.get('use_lvm'):
85                 options = "cryptdevice=UUID={0}:{1} {2} root=UUID={3} rw quiet"
86                 options = options.format(
87                     self.uuids["/"],
88                     luks_root_volume,
89                     key,
90                     luks_root_volume_uuid)
91             else:
92                 # Quick fix for issue #595 (lvm+luks)
93                 options = "cryptdevice=UUID={0}:{1} {2} root=/dev/dm-1 rw quiet"
94                 options = options.format(
95                     self.uuids["/"],
96                     luks_root_volume,
97                     key)
98
99         if self.settings.get("zfs"):
100             zfs_pool_name = self.settings.get("zfs_pool_name")
101             options += ' zfs={0}'.format(zfs_pool_name)
102
103         conf['default'] = []
104         conf['default'].append("title\tRebornOS\n")
105         conf['default'].append("linux\t/vmlinuz-linux\n")
106         conf['default'].append("initrd\t/intel-ucode.img\n")
107         conf['default'].append("initrd\t/initramfs-linux.img\n")
108         conf['default'].append("options\t{0}\n\n".format(options))
109
110         conf['fallback'] = []
111         conf['fallback'].append("title\tRebornOS (fallback)\n")
112         conf['fallback'].append("linux\t/vmlinuz-linux\n")
113         conf['fallback'].append("initrd\t/intel-ucode.img\n")
114         conf['fallback'].append("initrd\t/initramfs-linux-fallback.img\n")
115         conf['fallback'].append("options\t{0}\n\n".format(options))
116
117         if self.settings.get('feature_lts'):
118             conf['lts'] = []
119             conf['lts'].append("title\tRebornOS LTS\n")
120             conf['lts'].append("linux\t/vmlinuz-linux-lts\n")
121             conf['lts'].append("initrd\t/intel-ucode.img\n")
122             conf['lts'].append("initrd\t/initramfs-linux-lts.img\n")
123             conf['lts'].append("options\t{0}\n\n".format(options))
124
125             conf['lts_fallback'] = []
126             conf['lts_fallback'].append("title\tRebornOS LTS (fallback)\n")
127             conf['lts_fallback'].append("linux\t/vmlinuz-linux-lts\n")
128             conf['lts_fallback'].append("initrd\t/intel-ucode.img\n")
129             conf['lts_fallback'].append("initrd\t/initramfs-linux-lts-fallback.img\n")
130             conf['lts_fallback'].append("options\t{0}\n\n".format(options))
131
132         # Write boot entries
133         entries_dir = os.path.join(self.dest_dir, "boot/loader/entries")
134         os.makedirs(entries_dir, mode=0o755, exist_ok=True)
135
136         entry_path = os.path.join(entries_dir, "antergos.conf")
137         with open(entry_path, 'w') as entry_file:
138             for line in conf['default']:
139                 entry_file.write(line)
140
141         entry_path = os.path.join(entries_dir, "antergos-fallback.conf")
142         with open(entry_path, 'w') as entry_file:
143             for line in conf['fallback']:
144                 entry_file.write(line)
145
146         if self.settings.get('feature_lts'):
147             entry_path = os.path.join(entries_dir, "antergos-lts.conf")
148             with open(entry_path, 'w') as entry_file:
149                 for line in conf['lts']:
150                     entry_file.write(line)
151
152             entry_path = os.path.join(
153                 entries_dir, "antergos-lts-fallback.conf")
154             with open(entry_path, 'w') as entry_file:
155                 for line in conf['lts_fallback']:
156                     entry_file.write(line)
157
158         # Install bootloader
159         logging.debug("Installing systemd-boot bootloader...")
160         cmd = ['bootctl', '--path=/boot', 'install']
161         if chroot_call(cmd, self.dest_dir, 300) is False:
162             self.settings.set('bootloader_installation_successful', False)
163         else:
164             self.settings.set('bootloader_installation_successful', True)