-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_image.sh
executable file
·194 lines (171 loc) · 4.75 KB
/
create_image.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
#!/bin/bash
if [ "$(id -u)" -ne 0 ]; then
echo "You should execute this script as root" >&2
exit 1
fi
if [ ! -d ./work ]; then
echo "Error: directory 'work' does not exists" >&2
exit 1
fi
if [ ! -d /tmp/repo ]; then
echo "Error: directory '/tmp/repo' does not exists" >&2
exit 1
fi
if ! command -v mkfs.ext4 &>/dev/null; then
echo "Error: command 'mkfs.ext4' not found" >&2
exit 1
fi
if ! command -v dd &>/dev/null; then
echo "Error: command 'dd' not found" >&2
exit 1
fi
if ! command -v pacman-conf &>/dev/null; then
echo "Error: command 'pacman-conf' not found" >&2
exit 1
fi
if ! pacman -Q arch-install-scripts &>/dev/null || ! command -v pacstrap &>/dev/null; then
echo "Error: package 'arch-install-scripts' not found" >&2
exit 1
fi
if ! command -v arch-chroot &>/dev/null; then
echo "Error: command 'arch-chroot' not found" >&2
exit 1
fi
if [ ! -r ./.pkgname ]; then
echo "Error: file '.pkgname' does not exists or missing permissions" >&2
exit 1
fi
if [ -z "$(cat ./.pkgname)" ]; then
echo "Error: file '.pkgname' is empty" >&2
exit 1
fi
thispkgname="$(cat ./.pkgname)"
if [ ! -f /tmp/repo/"$thispkgname" ]; then
echo "Error: package does not exists in /tmp/repo" >&2
exit 1
fi
if [ -n "$POST_INSTALL" ]; then
if [ -e "$POST_INSTALL" ]; then
if [ -d "$POST_INSTALL" ]; then
echo "Error: post-install file cannot be a directory" >&2
exit 1
fi
else
echo "Error: post-install file '$POST_INSTALL' not found" >&2
exit 1
fi
fi
case "$1" in
'-s'|'-S'|'--sparse'|'--sparse-file')
SPARSE=1
filetype="sparse"
shift
;;
*)
SPARSE=0
filetype="empty"
;;
esac
if [ -z "$1" ]; then
echo "Error: image size not specified (1 argument)" >&2
exit 1
fi
case "$1" in
''|*[!0-9]*)
echo "size containts something other than number!" >&2
exit 1
;;
0*)
echo "size argument should not start with 0!" >&2
exit 1
;;
*)
:
;;
esac
if [ ! -d /mnt ]; then
echo "Error: directory '/mnt' does not exists" >&2
exit 1
fi
image_size="$1"
shift
set -e
set -x
rm -r -f ./arch.ext4
block_size="8M"
count_times=$((image_size * 1024 / 8))
backtome="$(realpath .)"
##################################################
echo "[] Creating $filetype file"
sleep 1
if [ "$SPARSE" == 1 ]; then
dd if=/dev/zero of=./arch.ext4 bs="1" count="0" seek="${image_size}G" status=progress
else
dd if=/dev/zero of=./arch.ext4 bs="$block_size" count="$count_times" status=progress
fi
#################################################
echo "[] Creating ext4 filesystem"
sleep 1
mkfs.ext4 ./arch.ext4
##################################################
echo "[] Mounting image to /mnt (don't worry if you have something mounted over there)"
sleep 3
mount ./arch.ext4 /mnt
##################################################
echo "[] Modifying /etc/pacman.conf"
sleep 1
cp -a /etc/pacman.conf ./work/pacman.conf
cat <<EOF >> ./work/pacman.conf
[asshole]
SigLevel = Optional
Server = file:///tmp/repo
EOF
##################################################
echo "[] Creating symlink to skuf package in pacman CacheDir"
sleep 1
pacman_cachedir="$(pacman-conf CacheDir)"
if [ -z "$pacman_cachedir" ]; then
pacman_cachedir=/var/cache/pacman/pkg/
fi
case "$pacman_cachedir" in
*/) : ;;
*) pacman_cachedir="${pacman_cachedir}/" ;;
esac
ln -sf -- /tmp/repo/"$thispkgname" "${pacman_cachedir}${thispkgname}"
##################################################
echo "[] Installing packages to image"
sleep 3
pacstrap -c -C ./work/pacman.conf /mnt base sudo linux linux-firmware skuf vim "$@"
rm -f -- "${pacman_cachedir}${thispkgname}"
rm -f ./work/pacman.conf
##################################################
echo "[] Creating temporary script"
sleep 1
if [ -n "$POST_INSTALL" ]; then
cat "$POST_INSTALL" > /mnt/skuf_afterwork
else
cat <<EOF > /mnt/skuf_afterwork
#!/usr/bin/env bash
echo "skuf" > /etc/hostname
echo "127.0.0.1 localhost" >> /etc/hosts
echo "::1 localhost" >> /etc/hosts
echo "root:0000" | chpasswd --crypt-method SHA512
useradd -m -u 1005 -U -G wheel,video,audio -s /bin/bash test
echo "test:0000" | chpasswd --crypt-method SHA512
sed 's/^# %wheel ALL=(ALL:ALL) NOPASSWD: ALL/%wheel ALL=(ALL:ALL) NOPASSWD: ALL/' -i /etc/sudoers
echo "LANG=C.UTF-8" > /etc/locale.conf
EOF
fi
chmod 700 /mnt/skuf_afterwork
##################################################
echo "[] Executing temporary script"
sleep 1
arch-chroot /mnt /skuf_afterwork
rm -f /mnt/skuf_afterwork
##################################################
echo "[] Unmounting /mnt"
umount /mnt
sleep 1
##################################################
echo "Done."
# vim: set ft=sh ts=4 sw=4 et: