forked from opensciencegrid/tarball-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage1.py
executable file
·166 lines (132 loc) · 5.81 KB
/
stage1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
Make a "stage 1" directory for the non-root client.
This directory contains an RPM database with entries for the prerequisites of
the non-root client that are *not* going to ship with the final non-root
tarball. This is software that the user is expected to have on their machine
already, such as 'bash'.
The database is needed for installing the software we will put in the final
tarballs, but will not be included in the tarballs.
"""
from __future__ import absolute_import
from __future__ import print_function
import glob
import grp
import os
from os.path import join as opj
import pipes
import shlex
import shutil
import stat
import subprocess
import sys
import yumconf
import common
from common import statusmsg, errormsg, safe_makedirs, Error
# Character devices to put in /dev in the chroot. Fields are:
# name, major, minor, group, perms
DEVICES = [('core', 1, 6, 'root', 0o600),
('mem', 1, 1, 'kmem', 0o640),
('null', 1, 3, 'root', 0o666),
('port', 1, 4, 'kmem', 0o640),
('zero', 1, 5, 'root', 0o666),
('urandom', 1, 9, 'root', 0o666)]
def make_stage1_root_dir(stage1_root):
"""Make or empty a directory to be used for building the stage1.
Prompt the user before removing anything (if the dir already exists).
"""
if stage1_root == "/":
raise Error("You may not use '/' as the output directory")
try:
if os.path.isdir(stage1_root):
print("Stage 1 directory (%r) already exists. Reuse it? Note that the contents will be emptied! " % stage1_root)
user_choice = input("[y/n] ? ").strip().lower()
if not user_choice.startswith('y'):
raise Error("Not overwriting %r. Remove it or pass a different directory" % stage1_root)
shutil.rmtree(stage1_root)
os.makedirs(stage1_root)
except OSError as err:
raise Error("Could not create stage 1 root dir %s: %s" % (stage1_root, err))
def init_stage1_rpmdb(stage1_root):
"""Create an rpmdb"""
err = subprocess.call(["rpm", "--initdb", "--root", stage1_root])
if err:
raise Error("Could not initialize rpmdb into %r (rpm process returned %d)" % (stage1_root, err))
def init_stage1_devices(stage1_root):
"""Make /dev file system in the chroot"""
devdir = os.path.join(stage1_root, 'dev')
os.makedirs(devdir)
for name, major, minor, group, perms in DEVICES:
path = opj(devdir, name)
try:
os.mknod(path, perms | stat.S_IFCHR, os.makedev(major, minor))
os.chown(path, -1, grp.getgrnam(group).gr_gid)
except EnvironmentError as err:
raise Error("Could not create /dev/%s in the chroot: %s" % (name, err))
def get_stage1_packages(pkglist_file):
with open(pkglist_file, 'rt') as filehandle:
return list(filter(None, shlex.split(filehandle.read(), comments=True)))
def _install_stage1_packages(yum, dver, stage1_root, stage1_packages):
def yuminstall(packages):
yum.install(installroot=stage1_root, packages=packages)
def yumforceinstall(packages, **kwargs):
yum.force_install(installroot=stage1_root, packages=packages, **kwargs)
def yumforceerase(packages):
yum.force_erase(installroot=stage1_root, packages=packages)
yum.yum_clean()
yumforceinstall(['filesystem'], noscripts=True)
yumforceinstall(['bash', 'grep', 'info', 'findutils', 'libacl', 'libattr'], noscripts=True, resolve=True)
yumforceinstall(['coreutils'], noscripts=True)
if dver == 'el6':
yumforceinstall(['coreutils-libs', 'pam', 'ncurses', 'gmp'], resolve=True)
if dver in ['el6', 'el7']:
yuminstall(['yum-plugin-priorities'])
subprocess.call(['touch', opj(stage1_root, 'etc/fstab')])
yuminstall(stage1_packages)
# Hack: we don't want libpsl in stage 1, we need it in the tarball
if dver in ['el8', 'el9']:
yumforceerase(["libpsl"])
def install_stage1_packages(stage1_root, repofile, dver, basearch, pkglist_file):
stage1_packages = get_stage1_packages(pkglist_file)
with common.MountProcFS(stage1_root):
with yumconf.YumInstaller(repofile, dver, basearch) as yum:
_install_stage1_packages(yum, dver, stage1_root, stage1_packages)
def make_stage1_filelist(stage_dir, pkglist_file):
oldwd = os.getcwd()
includes_file = pkglist_file.replace('.lst','-include.lst')
if not os.path.exists(includes_file):
includes_file = '/dev/null'
try:
os.chdir(stage_dir)
os.system('find . -not -type d | sort > stage1_filelist_tmp')
os.system('comm -2 -3 stage1_filelist_tmp ' + includes_file + ' > stage1_filelist')
finally:
os.chdir(oldwd)
def make_stage1_rpmlist(stage_dir, stage1_root):
oldwd = os.getcwd()
try:
os.chdir(stage_dir)
os.system('rpm -qa --root=' + pipes.quote(stage1_root) + ' > stage1_rpmlist')
finally:
os.chdir(oldwd)
def make_stage1_dir(stage_dir, repofile, dver, basearch, pkglist_file):
def _statusmsg(msg):
statusmsg("[%r,%r]: %s" % (dver, basearch, msg))
_statusmsg("Using %r for stage 1 directory" % stage_dir)
stage1_root = os.path.realpath(stage_dir)
try:
_statusmsg("Making stage 1 root directory")
make_stage1_root_dir(stage1_root)
_statusmsg("Initializing stage 1 rpm db")
init_stage1_rpmdb(stage1_root)
_statusmsg("Initializing /dev in root dir")
init_stage1_devices(stage1_root)
_statusmsg("Installing stage 1 packages from " + pkglist_file)
install_stage1_packages(stage1_root, repofile, dver, basearch, pkglist_file)
_statusmsg("Making file list")
make_stage1_filelist(stage_dir, pkglist_file)
_statusmsg("Making rpm list")
make_stage1_rpmlist(stage_dir, stage1_root)
return True
except Error as err:
errormsg(str(err))
return False