-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmkimage-yum-chroot.sh
executable file
·261 lines (227 loc) · 7.3 KB
/
mkimage-yum-chroot.sh
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env bash
#
# Create a base image work folder.
#
# Does it all. Pulls install repos from archive,
# updates to latest packages,
# cleans up temp apt files to reduce image size,
#
# image file is not created
#
# chroot can be used to edit work folder
clear
set +x
echo "Centos or Oracle Linux 7.x base image work folder"
echo
echo "edit bin_folder variable for where you placed the list files"
echo "add-packages.txt, remove-packages.txt"
echo
echo "------------------------------------------------------------"
echo "Default is to work with Centos repo. Edit WHICH REPO section to"
echo "use Oracle Linux repo"
echo
echo "Enable epel repo section to install packages from epel repo"
echo ""
echo "To see possible group names run: sudo yum group list"
echo "------------------------------------------------------------"
echo
echo
usage() {
cat <<EOOPTS
$(basename $0) [OPTIONS] <work_folder>
OPTIONS:
-e "<env group>" Environment Group to install in the container.
The default is "Minimal Install". ONLY USE ONCE.
-g "<groups>" The groups of packages to install in the container.
The default is blank. May Use mutiple times.
-y <yumconf> The path to the yum config to install packages from. The
default is /etc/yum.conf for Centos/RHEL
and /etc/dnf/dnf.conf for Fedora
EOOPTS
exit 1
}
# option defaults
yum_config=/etc/yum.conf
if [ -f /etc/dnf/dnf.conf ] && command -v dnf &> /dev/null; then
yum_config=/etc/dnf/dnf.conf
alias yum=dnf
fi
#set bin folder
bin_folder=/home/kenny/mkimage
# for names with spaces, use double quotes (") as install_env_group=('Core' '"Compute Node"')
install_env_group=()
install_packages=()
install_other_groups=()
remove_packages=()
while getopts ":y:g:h:e:" opt; do
case $opt in
y)
yum_config=$OPTARG
;;
h)
usage
;;
e)
install_env_group="$OPTARG"
;;
g)
install_other_groups+=("$OPTARG")
;;
\?)
echo "Invalid option: -$OPTARG"
usage
;;
esac
done
shift $((OPTIND - 1))
work_folder=$1
if [[ -z $work_folder ]]; then
usage
fi
# default to Core group if not specified otherwise
if [ ${#install_env_group[*]} -eq 0 ]; then
install_env_group=('Minimal Install')
fi
target=$(mktemp -d --tmpdir $(basename $0).XXXXXX)
set -x
mkdir -m 755 "$target"/dev
mknod -m 600 "$target"/dev/console c 5 1
mknod -m 600 "$target"/dev/initctl p
mknod -m 666 "$target"/dev/full c 1 7
mknod -m 666 "$target"/dev/null c 1 3
mknod -m 666 "$target"/dev/ptmx c 5 2
mknod -m 666 "$target"/dev/random c 1 8
mknod -m 666 "$target"/dev/tty c 5 0
mknod -m 666 "$target"/dev/tty0 c 4 0
mknod -m 666 "$target"/dev/urandom c 1 9
mknod -m 666 "$target"/dev/zero c 1 5
# amazon linux yum will fail without vars set
if [ -d /etc/yum/vars ]; then
mkdir -p -m 755 "$target"/etc/yum
cp -a /etc/yum/vars "$target"/etc/yum/
fi
# install environment group
if [[ -n "$install_env_group" ]];
then
yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
--setopt=group_package_types=mandatory -y groupinstall "${install_env_group[*]}"
fi
#Which repo - choose only one below
#For Centos use:
cp /etc/yum.repos.d/CentOS-Base.repo "$target"/etc/yum.repos.d/
#For Oracle Linux use:
#cp /etc/yum.repos.d/public-yum-ol7.repo "$target"/etc/yum.repos.d/
#Enable epel repo
#cp /etc/yum.repos.d/epel.repo "$target"/etc/yum.repos.d/
#Create docker-image-info file
info_file="$target"/etc/docker-image-info
echo "Date/Time Created" >> $info_file
echo "-----------------------------" >> $info_file
date >> $info_file
echo >> $info_file
echo "Environment Group Installed" >> $info_file
echo "-----------------------------" >> $info_file
echo $install_env_group >> $info_file
echo >> $info_file
echo "Packages Removed" >> $info_file
echo "-----------------------------" >> $info_file
# remove packages
rm -f "$target"/etc/yum/protected.d/systemd.conf
#note that the above file should also be removed on the host running this script
in_folder=/home/kenny/mkimage
package_removal_list=$bin_folder/remove-packages.txt
while read package_removal;
do
yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
--setopt=group_package_types=mandatory -y remove "$package_removal"
echo $package_removal >> $info_file
done < $package_removal_list
# install other groups
echo "" >> $info_file
echo "Other Groups Installed" >> $info_file
echo "-----------------------------" >> $info_file
if [[ -n "$install_other_groups" ]];
then
for group_name in "${install_other_groups[@]}"; do
yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
--setopt=group_package_types=mandatory -y groupinstall "$group_name"
echo $group_name >> $info_file
done
fi
# install additional packages
echo "" >> $info_file
echo "Additional Packages Installed" >> $info_file
echo "-----------------------------" >> $info_file
package_addition_list=$bin_folder/add-packages.txt
while read package_name;
do
yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
--setopt=group_package_types=mandatory -y install "$package_name"
echo $package_name >> $info_file
done < $package_addition_list
yum -c "$yum_config" --installroot="$target" -y clean all
cat > "$target"/etc/sysconfig/network <<EOF
NETWORKING=yes
HOSTNAME=localhost.localdomain
EOF
# effectively: febootstrap-minimize --keep-zoneinfo --keep-rpmdb --keep-services "$target".
# locales
rm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
# docs and man pages
rm -rf "$target"/usr/share/{man,doc,info,gnome/help}
# cracklib
rm -rf "$target"/usr/share/cracklib
# i18n
rm -rf "$target"/usr/share/i18n
# yum cache
rm -rf "$target"/var/cache/yum
mkdir -p --mode=0755 "$target"/var/cache/yum
# sln
rm -rf "$target"/sbin/sln
# ldconfig
rm -rf "$target"/etc/ld.so.cache "$target"/var/cache/ldconfig
mkdir -p --mode=0755 "$target"/var/cache/ldconfig
if [ -z "$version" ]; then
for file in "$target"/etc/{redhat,system}-release
do
if [ -r "$file" ]; then
version="$(sed 's/^[^0-9\]*\([0-9.]\+\).*$/\1/' "$file")"
break
fi
done
fi
#copy clean-image.sh
cp $bin_folder/clean-pre-image.sh $target/root/
chroot $target /bin/bash -c "chmod 755 /root/clean-pre-image.sh"
#move tmp folder to work folder
mv $target $work_folder
#preview with chroot
set +x
clear
echo "WORK FOLDER IMAGE INFO"
echo "---------------------------------------------------------------------"
echo
chroot $work_folder /bin/bash -c "cat /etc/docker-image-info"
#closing comments
echo
echo
echo "CLOSING HELP"
echo "---------------------------------------------------------------------"
echo
echo "use chroot to further test and edit contents of your work folder:"
echo
echo "sudo chroot $work_folder /bin/bash"
echo
echo "when ready create your docker image manually by running:"
echo
echo "sudo tar -C $work_folder -c . | docker import - <name>:<tag>"
echo
echo "To minimize the size of the image file after using yum"
echo "and prior to running the above tar/import command"
echo "run:"
echo
echo "sudo yum clean all"
echo
echo
echo
#I can only imagine