-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathubuntu-virtinst-preseed.sh
executable file
·275 lines (244 loc) · 7.33 KB
/
ubuntu-virtinst-preseed.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/bash -e
WORKDIR=`dirname $0`
CONFIG_FILE=$WORKDIR/config.sh
[ -f $CONFIG_FILE ] && source $CONFIG_FILE
SUPPORTED="(focal|bionic|xenial|trusty)"
ARCH=amd64
SITE=${UBUNTU_SITE:-http://ftp.riken.go.jp/Linux/ubuntu}
PROXY=${UBUNTU_PROXY:-${PROXY:-$http_proxy}}
DISKIMG_DIR=${DISKIMG_DIR:-$HOME/images}
ISO_DIR=${UBUNTU_ISO_DIR:-$HOME/iso/ubuntu}
NTPSERVER=${NTPSERVER:-ntp.nict.jp}
USERNAME=ubuntu
# Unless password is specified NAME is used for password by default
#PASSWORD=
NUM_CPU=4
MEMORY=8192
DISKSIZE=50G
DISKFORMAT=qcow2
# You can use the following keyword
# %ISO_DIR%
# %ARCH%
# %RELEASE_NAME% : precise, quantal, ....
# %RELEASE_VERSION% : 12.04, 12.10, ....
# %RELEASE_FULLVER% (including minor version for LTS) : 12.04.3, 10.04.4
ISO_LOCATION_FORMAT_DEFAULT=%ISO_DIR%/ubuntu-%RELEASE_FULLVER%-server-%ARCH%.iso
ISO_LOCATION_FORMAT=${UBUNTU_ISO_LOCATION_FORMAT:-$ISO_LOCATION_FORMAT_DEFAULT}
function usage() {
cat <<EOF
Usage: $0 [options] NAME RELEASE
Options:
-a ARCH : VM architecture: x86_64, i386 (default: $ARCH)
-c NUM_CPU : VM number of CPU (default: $NUM_CPU)
-m MEMORY : VM memory size [MB] (default: $MEMORY)
-f DISKFORMAT : QEMU image format: qcow2, raw (default: $DISKFORMAT)
-s DISKSIZE : QEMU image size, e.g., 50G (default: $DISKSIZE)
-u USERNAME : Username of the default user (default: $USERNAME)
-p PASSWORD : Password for the default user (default: $PASSWORD)
-P : Do not use preseed.cfg
Configurations:
DISKIMG_DIR=$DISKIMG_DIR
UBUNTU_SITE=$SITE
UBUNTU_PROXY=$PROXY
UBUNTU_ISO_DIR=$ISO_DIR
UBUNTU_ISO_LOCATION_FORMAT=$ISO_LOCATION_FORMAT
EOF
exit 1
}
while getopts "a:c:m:f:s:u:p:Ph" OPT; do
case $OPT in
a) ARCH=$OPTARG
if [ "$ARCH" != "i386" -a "$ARCH" != "amd64" ]; then
echo "Arch must be either amd64 or i386."
exit 1
fi
;;
c) NUM_CPU=$OPTARG; ;;
m) MEMORY=$OPTARG; ;;
f) DISKFORMAT=$OPTARG
if [ "$DISKFORMAT" != "qcow2" -a "$DISKFORMAT" != "raw" ]; then
echo "Disk format must be either qcow2 or raw."
exit 1
fi
;;
s) DISKSIZE=$OPTARG; ;;
u) USERNAME=$OPTARG; ;;
p) PASSWORD=$OPTARG; ;;
P) NO_PRESEED=true; ;;
?) usage
;;
esac
done
shift `expr $OPTIND - 1`
if [ -z "$1" ]; then
echo "Name must be specified!"
usage
exit 1
fi
if [ -z "$2" ]; then
echo "release must be specified! $SUPPORTED"
echo "Usage: $0 [options] NAME RELEASE"
exit 1
fi
NAME=$1
DISK=$DISKIMG_DIR/$NAME.img
if [ -z "$PASSWORD" ]; then
PASSWORD=$NAME
fi
RELEASE_NAME=$2
if [[ ! "$RELEASE_NAME" =~ $SUPPORTED ]]; then
echo "Release '$RELEASE_NAME' is not supported."
echo "$SUPPORTED must be specified"
exit 2
fi
if [ "$ARCH" == "amd64" ]; then
VIRT_ARCH=x86_64
else
VIRT_ARCH=i386
fi
case "$RELEASE_NAME" in
focal)
RELEASE_FULLVER=20.04
;;
bionic)
RELEASE_FULLVER=18.04
;;
xenial)
RELEASE_FULLVER=16.04
;;
trusty)
RELEASE_FULLVER=14.04
;;
esac
if [ -z "$OS_VARIANT" ]; then
OS_VARIANT=auto
fi
LOCATION=$SITE/dists/$RELEASE_NAME/main/installer-$ARCH/
if [ -n "$RELEASE_FULLVER" ]; then
RELEASE_VERSION=`echo $RELEASE_FULLVER | cut -d . -f 1-2`
ISO_LOCATION=`echo $ISO_LOCATION_FORMAT | sed \
-e "s|%ISO_DIR%|$ISO_DIR|g" \
-e "s|%ARCH%|$ARCH|g" \
-e "s|%RELEASE_NAME%|$RELEASE_NAME|g" \
-e "s|%RELEASE_VERSION%|$RELEASE_VERSION|g" \
-e "s|%RELEASE_FULLVER%|$RELEASE_FULLVER|g" \
`
if [ -f $ISO_LOCATION ]; then
LOCATION=$ISO_LOCATION
fi
fi
function generate_preseed_cfg() {
PRESEED_DIR=/tmp/preseed$$
PRESEED_BASENAME=preseed.cfg
PRESEED_FILE=$PRESEED_DIR/$PRESEED_BASENAME
mkdir -p $PRESEED_DIR
cat > $PRESEED_FILE <<EOF
d-i debian-installer/language string en
d-i debian-installer/locale string en_US.UTF-8
d-i debian-installer/country string JP
d-i console-setup/ask_detect boolean false
d-i console-setup/layoutcode string jp
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string $NAME
d-i netcfg/get_domain string localdomain
d-i netcfg/wireless_wep string
d-i mirror/country string JP
d-i mirror/http/hostname string jp.archive.ubuntu.com
d-i mirror/http/directory string /ubuntu
d-i mirror/http/proxy string $PROXY
d-i mirror/http/mirror select jp.archive.ubuntu.com
d-i clock-setup/utc boolean true
d-i time/zone string Asia/Tokyo
d-i clock-setup/ntp boolean true
d-i clock-setup/ntp-server string $NTPSERVER
d-i partman-auto/method string lvm
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select Finish partitioning and write changes to disk
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i passwd/user-fullname string $USERNAME
d-i passwd/username string $USERNAME
d-i passwd/user-password password $PASSWORD
d-i passwd/user-password-again password $PASSWORD
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
#tasksel tasksel/first multiselect
tasksel tasksel/first multiselect standard
d-i pkgsel/include string openssh-server build-essential acpid git-core ntp
d-i pkgsel/upgrade select none
d-i pkgsel/update-policy select none
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i finish-install/reboot_in_progress note
EOF
}
function cleanup_preseed_cfg() {
rm -v -f $PRESEED_FILE
rmdir -v $PRESEED_DIR
}
function create_disk() {
if [ ! -f $DISK ]; then
qemu-img create -f $DISKFORMAT $DISK $DISKSIZE
fi
}
function virtinst_with_preseed() {
sudo virt-install \
--name $NAME \
--os-type linux \
--os-variant $OS_VARIANT \
--virt-type kvm \
--connect=qemu:///system \
--vcpus $NUM_CPU \
--ram $MEMORY \
--arch $VIRT_ARCH \
--serial pty \
--console pty \
--disk=$DISK,format=$DISKFORMAT,bus=virtio \
--nographics \
--location $LOCATION \
--initrd-inject $PRESEED_FILE \
--extra-args "
console=ttyS0,115200
file=/$PRESEED_BASENAME
auto=true
priority=critical
interface=auto
language=en
country=JP
locale=en_US.UTF-8
console-setup/layoutcode=jp
console-setup/ask_detect=false
" \
--network network=default,model=virtio
}
function virtinst_without_preseed() {
sudo virt-install \
--name $NAME \
--os-type linux \
--os-variant ubuntu${RELEASE_NAME} \
--virt-type kvm \
--connect=qemu:///system \
--vcpus $NUM_CPU \
--ram $MEMORY \
--arch $VIRT_ARCH \
--serial pty \
--console pty \
--disk=$DISK,format=qcow2,bus=virtio \
--nographics \
--location $LOCATION \
--extra-args "console=ttyS0,115200" \
--network network=default,model=virtio
}
create_disk
if [ "$NO_PRESEED" != "true" ]; then
generate_preseed_cfg
virtinst_with_preseed
cleanup_preseed_cfg
else
virtinst_without_preseed
fi