-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebian-img-burner.sh
More file actions
executable file
·290 lines (231 loc) · 7.47 KB
/
Copy pathdebian-img-burner.sh
File metadata and controls
executable file
·290 lines (231 loc) · 7.47 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Image burner program to generate debian images for qemu-kvm
#!/bin/bash -x
SRC_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. $SRC_DIR/config.sh
##############################################################################
DISK_MNT=/mnt/qemu-disk
export http_proxy=$HTTP_PROXY
export https_proxy=$HTTPS_PROXY
export ftp_proxy=$FTP_PROXY
CHROOT_SCRIPT=chroot-run.sh
##############################################################################
function install_prereq {
apt-get install -y debootstrap qemu-system qemu-kvm qemu-utils gdisk
}
# Clean up function to clear out system state.
function cleanup() {
umount $DISK_MNT/proc
umount $DISK_MNT/sys
umount $DISK_MNT/dev
umount $DISK_MNT
umount /dev/nbd0p2
qemu-nbd -d /dev/nbd0
#rm -rf $QEMU_IMG_NAME
}
# Pass the error code, exit-state and error string to the function.
# $1 : return error code.
# $2 : exit state, exit the program if error is critical.
# #3 : error string to print.
#
function exit_on_error() {
ret_code=$1
is_exit=$2
err_str=$3
if [ $ret_code -ne 0 ]; then
echo "** $err_str **"
fi
if [ $ret_code -ne 0 ] && [ $is_exit -ne 0 ]; then
echo "** EXITING THE PROGRAM ERR: $ret_code **"
cleanup
exit $ret_code
fi
}
function fdisk_part {
fdisk /dev/nbd0 << EEOF
I
$gpt
p
w
EEOF
}
# Setting the proxy inside the VM apt.
function set_apt_proxy() {
if [[ -v HTTP_PROXY ]]; then
proxy_setup="Acquire::http::Proxy \"$HTTP_PROXY\";"
echo $proxy_setup > $DISK_MNT/etc/apt/apt.conf
fi
}
# Create a script file to run commands inside the chroot.
function chroot_script() {
set_apt_proxy
APT_INSTALL_APPS="$(echo -e "${IMG_APPS}" | tr -d '[:space:]')"
APT_INSTALL_APPS=${APT_INSTALL_APPS//,/ }
cat >$DISK_MNT/$CHROOT_SCRIPT <<EOL
#!/bin/bash -x
mount -t proc none /proc
mount -t sysfs none /sys
export PATH=$PATH:/usr/bin:/usr/sbin/
export http_proxy=${HTTP_PROXY}
export https_proxy=${HTTPS_PROXY}
export ftp_proxy=${FTP_PROXY}
#Make apt install non interactive
export DEBIAN_FRONTEND=noninteractive
apt-get install -y grub-pc grub-common
apt-get -f install -y
grub-install --force /dev/nbd0 --modules="biosdisk part_msdos"
apt-get install -y sed
# Update the grub command line options.
sed -i '/GRUB_CMDLINE_LINUX_DEFAULT/d' /etc/default/grub
echo 'GRUB_CMDLINE_LINUX_DEFAULT="nomodeset net.ifnames=1 biosdevname=0"' >> /etc/default/grub
update-grub
echo "Updating root password"
echo "${ROOT_PWD}\n${ROOT_PWD}" | passwd root
#Add a new user for the ssh log-in support.
adduser sugesh --gecos "Sugesh Chandran,2017,1234567890,1234567890" --disabled-password
echo "${ROOT_PWD}\n${ROOT_PWD}" | passwd sugesh
#setup the hostname
echo 'sugesh-vm' > /etc/hostname
sed -i '/127.0.0.1/d' /etc/hosts
echo "127.0.0.1\tlocalhost sugesh-vm" >> /etc/hosts
# Install custom image apps.
apt-get install -y ${APT_INSTALL_APPS}
umount /proc/ /sys/ /dev/
EOL
}
function chroot_setup() {
mount --bind /dev/ $DISK_MNT/dev
exit_on_error $? 1 "Failed to mount dev"
chroot_script
chmod a+rwx $DISK_MNT/$CHROOT_SCRIPT
LANG=C chroot $DISK_MNT ./$CHROOT_SCRIPT
umount $DISK_MNT/dev
}
# Function to update VM grub with disk entry.
function update_vm_grubcfg() {
sed -i -e 's/nbd0p2/sda2/g' $DISK_MNT/boot/grub/grub.cfg
exit_on_error $? 1 "ERR: Failed to update grub,"
}
# Function to update fstab with right disk entries.
function set_fstab() {
cat >$DISK_MNT/etc/fstab <<EOL
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during installation
/dev/sda2 / ext4 errors=remount-ro 0 1
# swap was on /dev/sda1 during installation
/dev/sda1 none swap sw 0 0
EOL
}
#Function to create apt source list.
function setup_apt_src() {
rm -rf $DISK_MNT/etc/apt/sources.list.d/*
cat >$DISK_MNT/etc/apt/sources.list <<EOL
deb http://archive.ubuntu.com/ubuntu ${IMG_SUITE} main restricted
deb http://archive.ubuntu.com/ubuntu ${IMG_SUITE}-updates main restricted
deb http://archive.ubuntu.com/ubuntu ${IMG_SUITE} universe
deb http://archive.ubuntu.com/ubuntu ${IMG_SUITE}-updates universe
deb http://archive.ubuntu.com/ubuntu ${IMG_SUITE} multiverse
deb http://archive.ubuntu.com/ubuntu ${IMG_SUITE}-updates multiverse
deb http://archive.ubuntu.com/ubuntu ${IMG_SUITE}-backports main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu ${IMG_SUITE}-security main restricted
deb http://archive.ubuntu.com/ubuntu ${IMG_SUITE}-security universe
deb http://archive.ubuntu.com/ubuntu ${IMG_SUITE}-security multiverse
## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
deb http://archive.canonical.com/ubuntu ${IMG_SUITE} partner
# deb-src http://archive.canonical.com/ubuntu ${IMG_SUITE} partne
EOL
}
# Create the static network configuration file.
function set_network() {
cat >$DISK_MNT/etc/network/interfaces <<EOL
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto ens3
iface ens3 inet dhcp
#address 20.0.0.2
#netmask 255.255.254.0
EOL
#Bionic beaver is now using systemd, netplan for networking so set it up
mkdir -p $DISK_MNT/etc/netplan
cat >$DISK_MNT/etc/netplan/config.yaml <<EOL
network:
version: 2
renderer: NetworkManager
EOL
}
# function to attach the image to nbd and format it.
function nbd_attach_format_install {
modprobe nbd max_part=32
qemu-nbd -c /dev/nbd0 $QEMU_IMG_NAME
exit_on_error $? 1 "ERR: Failed to init nbd, cannot create image"
fdisk_part
exit_on_error $? 1 "ERR: Failed to do partition"
mkswap /dev/nbd0p1
exit_on_error $? 1 "ERR: Failed to create swap partition"
mkfs.ext3 /dev/nbd0p2
exit_on_error $? 1 "ERR: Failed to format partition"
mkdir -p $DISK_MNT
mount /dev/nbd0p2 $DISK_MNT
exit_on_error $? 1 "ERR: Failed to mount disk,"
debootstrap --include=$IMG_KERNEL_APPS,$DEFAULT_IMG_APPS \
$IMG_SUITE $DISK_MNT $IMG_MIRROR_URL
}
function create_qcow_disk {
# XXX :: validate the input before using the file name.
case $QCOW_SIZE in
10)
QCOW_SIZE='10G'
gpt='10G-ubuntu-gpt-disk'
;;
15)
QCOW_SIZE='15G'
gpt='15G-ubuntu-gpt-disk'
;;
20)
QCOW_SIZE='20G'
gpt='20G-ubuntu-gpt-disk'
;;
200)
QCOW_SIZE='200G'
gpt='200G-ubuntu-gpt-disk'
;;
500)
QCOW_SIZE='500G'
gpt='500G-ubuntu-gpt-disk'
;;
*)
QCOW_SIZE=10G
gpt='10G-ubuntu-gpt-disk'
;;
esac
echo "Creating the disk of size $QCOW_SIZE and diskfile $gpt"
rm -rf $QEMU_IMG_NAME
qemu-img create -f qcow2 $QEMU_IMG_NAME $QCOW_SIZE
exit_on_error $? 1 "ERR:Failed to create qemu-img"
}
function main {
install_prereq
cleanup
create_qcow_disk
nbd_attach_format_install
chroot_setup
update_vm_grubcfg
set_fstab
set_network
setup_apt_src
cleanup
}
main