-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiZy_builder_vm_generate
executable file
·610 lines (470 loc) · 11.2 KB
/
miZy_builder_vm_generate
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#!/bin/bash
## hyphop ##
NAME=miZy_builder_vm_generate
DST="wheezy|jessie|stable|xenial"
usage() { echo "
$NAME - make vm image for next miZy system building )
ABOUT:
generate stable debian/ubuntu minimal image with pre-installed
necessary packages, with configured user access and network up
USAGE:
$NAME yes # debian stable (stretch)
$NAME [$DST] # select any
$NAME xenial # ubuntu xenial
$NAME wheezy # debian wheezy
$NAME jessie # debian jessie
ok! now u can start vm ./miZy_builder_vm
CONFIG:
* miZy_builder_vm.net.conf - config for network ip + nat
DEBOOTSTRAP FIX:
cd /usr/share/debootstrap/scripts
ln -s gutsy xenial
ln -s sid jessie
#ln -s sid stretch
NOTE:
read about ** Bootstrap a basic Debian system **
man debootstrap
";}
set -e
#[ "$1" = "yes" ] || { usage; exit 0; }
case $1 in
yes)
;;
wheezy|jessie|stable|xenial)
VER=$1
;;
*)
usage
exit 0
esac
#
#cd /tmp
BLNME=miZyBldr
BLDIR=$BLNME
[ -d $BLDIR ] || mkdir $BLDIR
#echo $PWD | grep /tmp/ || { echo "wrong work dir $PWD"; exit 1; }
[ "$BLDIR" = "" ] && { echo "wrong $BLDIR"; exit 1; }
echo "make miZy builder in $BLDIR"
#mkdir $BLDIR
#exit 0
## debian
#VER=wheezy
##VER=jessie
##VER=stretch
## ubuntu
#VER=xenial
[ "$VER" ] || VER=stable
FROM=http://http.debian.net/debian/
DEB=1
UBU=
[ "$XENIAL" ] && VER=xenial
[ "$VER" = "xenial" ] && {
FROM=http://archive.ubuntu.com/ubuntu/
DEB=
UBU=1
XENIAL=1
}
BLIMG2=$BLDIR.squashfs
BLIMG=$BLDIR.$VER.squashfs
#./miZy_builder_vm_generate: line 85: debootstrap: command not found
which debootstrap >/dev/null || {
echo "install debootstrap package and try again!!!
apt-get install debootstrap" 1>&2
exit 1
}
which mksquashfs >/dev/null || {
echo "install squashfs-tools package and try again!!!
apt-get install squashfs-tools" 1>&2
exit 1
}
debootstrap="debootstrap --arch=amd64 $VER $BLDIR $FROM"
echo "[i] $debootstrap => $BLIMG" 1>&2
MODULES_INIT="
virtio
virtio_blk
virtio_net
virtio_pci
aufs
ext4
squashfs
loop
"
modules_all_remove=
PACKAGES_LINUX="linux-image-amd64"
## development
PACKAGES="\
bison make flex time bc python gettext
ncurses-dev u-boot-tools squashfs-tools zlib1g-dev
bzip2 lzma gawk xz-utils
subversion git
openssh-server bash-completion tcpdump lsof
"
#PACKAGES="$PACKAGES
#ash lzop aufs-tools libzip2 mercurial mc bwm-ng
#"
PACKAGES="$PACKAGES
nano mc ash lzop aufs-tools bwm-ng mercurial
lbzip2
"
#libzip2
[ "$UBU" ] && {
PACKAGES_LINUX="linux-image-generic"
PACKAGES="$PACKAGES
"
}
[ "$DEB" ] && {
PACKAGES="$PACKAGES
busybox
net-tools
"
}
# add
#lbzip2
#pkg-config xsltproc
#build-essential libncurses5-dev make pkg-config unzip zlib1g-dev libssl-dev
[ -f $0.conf ] && {
echo "[i] override or add conf from $0.conf"
. $0.conf
}
echo "$debootstrap + $PACKAGES_LINUX + $PACKAGES"
#exit 0
echo "DEBOOTSTRAP RUN"
$debootstrap
echo OK
#[ "$XENIAL" ] &&
mount -t proc procfs $BLDIR/proc
[ "$UBU" ] && {
echo "deb http://archive.ubuntu.com/ubuntu $VER main restricted universe
" > $BLDIR/etc/apt/sources.list
chroot $BLDIR env LC_ALL=C apt-get update
}
echo "INSTALL PKG 1"
chroot $BLDIR env LC_ALL=C apt-get install -y --force-yes --no-install-recommends $PACKAGES_LINUX
echo OK
echo "INSTALL PKG 2"
chroot $BLDIR env LC_ALL=C apt-get install -y --force-yes $PACKAGES
echo OK
echo $BLNME > $BLDIR/etc/hostname
## USERS ACCESS
echo 'T0:23:respawn:/sbin/getty -L ttyS0 115200 vt100' >>$BLDIR/etc/inittab
chroot $BLDIR env LC_ALL=C passwd -d root
## SSH BLOCK
mkdir $BLDIR/root/.ssh
cat << END >> $BLDIR/root/.ssh/authorized_keys
## put fixed authorized_keys there
## yes is empty there
END
## get public ssh keys for current user
[ "$get_my_ssh_keys" = "" ] || {
echo "[i] add authorized_keys from ~/.ssh/*.pub"
echo "## get from ~/.ssh/*.pub" >> $BLDIR/root/.ssh/authorized_keys
cat ~/.ssh/*.pub >> $BLDIR/root/.ssh/authorized_keys
}
## get public ssh keys for $0.pub
[ -s $0.pub ] && {
echo "[i] add authorized_keys from $0.pub"
echo "## get from $0.pub" >> $BLDIR/root/.ssh/authorized_keys
cat $0.pub >> $BLDIR/root/.ssh/authorized_keys
}
## set some prefs
#chroot $BLDIR update-alternatives --set editor /usr/bin/mcedit
cat << END > $BLDIR/root/.selected_editor
SELECTED_EDITOR="/usr/bin/mcedit"
END
cat << END > $BLDIR/etc/profile.d/locale.sh
export LANG=en_US
export LANGUAGE=en_US
export LC_CTYPE=C
export LC_ALL=C
END
## INITRD SETUP
cat << END > $BLDIR/etc/initramfs-tools/initramfs.conf
MODULES=list
BUSYBOX=y
KEYMAP=n
COMPRESS=gzip
DEVICE=
NFSROOT=auto
END
for MOD in $MODULES_INIT; do
echo "init mod $MOD"
echo $MOD >> $BLDIR/etc/initramfs-tools/modules
done
MODULES_BLACK="
"
[ -f $0.nomod ] && MODULES_BLACK="$MODULES_BLACK$(cat $0.nomod)"
for MOD in $MODULES_BLACK; do
echo "blacklist $MOD"
echo "blacklist $MOD" >> $BLDIR/etc/modprobe.d/vm.conf
done
#cp aufs/aufs $BLDIR/usr/share/initramfs-tools/hooks
cat << END >> $BLDIR/usr/share/initramfs-tools/hooks/aufs
#!/bin/sh
if [ "$1" = "prereqs" ]; then exit 0; fi
. /usr/share/initramfs-tools/hook-functions
copy_exec /bin/grep
copy_exec /bin/sed
copy_exec /usr/bin/cut
copy_exec /bin/chmod
END
chmod 0755 $BLDIR/usr/share/initramfs-tools/hooks/aufs
cat << "END" > $BLDIR/etc/initramfs-tools/scripts/init-bottom/rootaufs
#!/bin/sh
## hyphop ##
## fixed & adapted
# Copyright 2008 Nicholas A. Schembri State College PA USA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
case $1 in
prereqs)
exit 0
;;
esac
export aufs
for x in $(cat /proc/cmdline); do
case $x in
root=*)
ROOTNAME=${x#root=}
;;
aufs=*)
aufs=${x#aufs=}
#~ case $aufs in
#~ tmpfs-debug)
#~ aufs=tmpfs
#~ aufsdebug=1
#~ ;;
#~ esac
;;
esac
done
echo "root-aufs: Setting up aufs on ${rootmnt} as root file system "
modprobe aufs || exit 0
mkdir /aufs
mkdir /rw
mkdir /ro
rw_ok=
case $aufs in
vd*)
mount -t ext4 /dev/$aufs /rw && rw_ok=1
;;
#tmp*)
*)
mount -t tmpfs aufs-tmpfs /rw && rw_ok=1
;;
esac
[ "$rw_ok" = "1" ] || mount -t tmpfs aufs-tmpfs /rw
mount -n -o move ${rootmnt} /ro || {
echo "root-aufs error: ${rootmnt} failed to move to /ro"
exit 0
}
mount -t aufs -o dirs=/rw:/ro=ro aufs /aufs || {
echo "root-aufs error: Failed to mount /aufs files system"
exit 0
}
#test for mount points on aufs file system
[ -d /aufs/ro ] || mkdir /aufs/ro
[ -d /aufs/rw ] || mkdir /aufs/rw
# the real root file system is hidden on /ro of the init file system. move it to /ro
mount -n -o move /ro /aufs/ro || {
echo "root-aufs error: Failed to move /ro /aufs/ro"
exit 0
}
# tmpfs file system is hidden on /rw
mount -n -o move /rw /aufs/rw || {
echo "root-aufs error: Failed to move /rw /aufs/rw"
exit 0
}
mount -n -o move /aufs ${rootmnt}
exit 0
END
# cp aufs/rootaufs $BLDIR/etc/initramfs-tools/scripts/init-bottom
chmod 0755 $BLDIR/etc/initramfs-tools/scripts/init-bottom/rootaufs
##
cat << END >> $BLDIR/etc/fstab
tmpfs /tmp tmpfs defaults,noatime,nosuid 0 0
END
echo "UPDATE INITRAMFS"
chroot $BLDIR env LC_ALL=C update-initramfs -v -u
echo OK
## NET
. ./miZy_builder_vm.net.conf
## resolv config
rm $BLDIR/etc/resolv.conf
cat << END > $BLDIR/etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 2001:4860:4860::8888
nameserver 2001:4860:4860::8844
END
cat << END >> $BLDIR/etc/hosts
##
$ip0 $BLNME
$ip0h hst
$ip1 vrt
$ip1h vhst
END
## ubuntu
## eth0 eth1 > ens3 ens4
cat << END >> $BLDIR/etc/network/interfaces
##
#auto eth0
#iface eth0 inet static
# netmask 255.255.255.0
# address $ip0
# gateway $ip0g
auto eth1
iface eth1 inet static
netmask 255.255.255.0
address $ip1
# gateway $ip1g
##
END
cat << "END" > $BLDIR/etc/rc.local
#!/bin/sh
echo "[i] rc.local"
## get config vm net from kernel args
for v in `cat /proc/cmdline`; do
case $v in
hostname=*)
echo ${v#hostname=} > /proc/sys/kernel/hostname
;;
run=*)
run=${v#run=}
case $run in
http*)
s=/tmp/init.script
wget $run -O $s
chmod 0755 $s
cd /tmp
$s
;;
vbd*)
s=/tmp/vdb/init.script
mkdir /tmp/vdb
mount /dev/vdb /tmp/vdb
$s
;;
# *tar*)
*)
p=$PWD
d=/tmp/scripts
mkdir $d && cd $d
for disk in vdb vdc vdd vde; do
[ -b /dev/$disk ] && {
tar -zx < /dev/$disk
for s in *init* ; do
[ -f $s ] && {
echo "RUN: $d/$s"
./$s
}
done
}
done
cd $p
;;
# *)
# echo "IGNORE RUN $run"
esac
;;
password=*)
echo "PASSWORD: root:${v#password=}"
echo root:${v#password=} | chpasswd
echo "SSH reconfig"
echo "Match User root" >> /etc/ssh/sshd_config
echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
;;
ip0=*)
ip=${v#ip0=}
br=
eth=eth0
[ "$ip" ] && {
echo "ifconfig $eth $ip netmask 255.255.255.0"
#/sbin/ifconfig $eth $ip netmask 255.255.255.0
br=`echo $ip | egrep -o \\.+\\\\.`
br="${br}255"
echo "ip address add $ip/24 broadcast $br dev $eth"
ip address add $ip/24 broadcast $br dev $eth
ip link set $eth up
}
;;
ip0g=*)
gw=${v#ip0g=}
[ "$gw" ] && {
echo "route add default gw $gw"
#/sbin/route add default gw ${v#ip0g=}
ip route add default via $gw
}
;;
ip1=*)
ip=${v#ip1=}
eth=eth1
[ "$ip" ] && {
echo "ifconfig $eth $ip netmask 255.255.255.0"
#/sbin/ifconfig $eth $ip netmask 255.255.255.0
br=`echo $ip | egrep -o \\.+\\\\.`
br="${br}255"
echo "ip address add $ip/24 broadcast $br dev $eth"
ip address add $ip/24 broadcast $br dev $eth
ip link set $eth up
}
;;
ip1g=*)
gw=${v#ip1g=}
[ "$gw" ] && {
echo "route add default gw $gw"
#/sbin/route add default gw ${v#ip0g=}
ip route add default via $gw
}
;;
esac
done
END
## last debian need it
chmod 0777 $BLDIR/etc/rc.local
## remove some files
[ "1" ] && {
rm -rf $BLDIR/usr/share/doc/*
rm -rf $BLDIR/usr/share/man/??
rm -rf $BLDIR/usr/share/man/??_*
for l in `ls $BLDIR/usr/share/locale/`; do
case $l in
en|en_US)
echo "leave locale $l"
;;
*)
#echo "remove locale $l"
rm -r $BLDIR/usr/share/locale/$l
esac
done
}
rm -rf $BLDIR/var/cache/apt/*
[ "$modules_all_remove" = "" ] || {
echo "[i] remove all modules"
rm -rf $BLDIR/lib/modules
}
## pack
#( cd $BLDIR && find . -print0 | cpio --null -o --format=newc | lzop -9 > ../$BLDIR.lzo )
#( cd $BLDIR && find . -print0 | cpio --null -o --format=newc > $BLDIR.cpio )
#[ "$XENIAL" ] &&
umount $BLDIR/proc
rm -rf $BLDIR/lib/firmware/
rm -rf $BLDIR/debootstrap
mksquashfs $BLDIR $BLIMG -comp xz
ln -sf $BLIMG $BLIMG2
echo "[i] $BLIMG - is ready, now u can run ./miZy_builder_vm"
###
### END
###