-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmkvyos_with_zsn.sh
executable file
·142 lines (115 loc) · 3.81 KB
/
mkvyos_with_zsn.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
#!/bin/bash
export LIBGUESTFS_BACKEND=direct
which guestfish > /dev/null
if [ $? -ne 0 ]; then
echo "guestfish is not installed"
exit 1
fi
which qemu-img > /dev/null
if [ $? -ne 0 ]; then
echo "qemu-img is not installed"
exit 1
fi
usage() {
echo "
USAGE:
$0 path_to_image path_to_zvr_tar path_to_zsn_vyos_bin vyos_version"
}
if [ -z $1 ]; then
echo "missing parameter path_to_image"
usage
exit 1
fi
if [ ! -f $1 ]; then
echo "cannot find the image"
exit 1
fi
if [ -z $2 ]; then
echo "missing parameter path_to_zvr_tar"
usage
exit 1
fi
if [ ! -f $2 ]; then
echo "cannot find the zvr.tar.gz"
exit 1
fi
if [ -z $3 ]; then
echo "missing parameter path_to_zsn_vyos_bin"
usage
exit 1
fi
if [ ! -f $3 ]; then
echo "cannot find the zsn-agent.bin"
exit 1
fi
vyosVersion="1.1.7"
if [ ! -z $4 ]; then
vyosVersion=$4
echo "vyos version $vyosVersion"
if [ $vyosVersion != "1.1.7" ] && [ $vyosVersion != "1.2.0" ]; then
echo "vyos version must be 1.1.7 or 1.2.0"
usage
exit 1
fi
fi
imgfile=$1
isVmdk=0
if echo $1 | grep -q -i '\.vmdk$'; then
isVmdk=1
imgfile=${1%%.vmdk}.qcow2
qemu-img convert -f vmdk -O qcow2 "$1" "$imgfile"
fi
set -e
if [ $vyosVersion = "1.1.7" ]; then
ROOTPATH="/"
VyosPostScript="/opt/vyatta/etc/config/scripts/vyatta-postconfig-bootup.script"
else
ROOTPATH="/boot/zs_vyos/rw/"
VyosPostScript="/boot/zs_vyos/rw/config/scripts/vyos-postconfig-bootup.script"
fi
tmpdir=$(mktemp -d)
atexit() {
/bin/rm -fr $tmpdir
[ $isVmdk -eq 1 ] && /bin/rm -f $imgfile || true
}
trap atexit EXIT SIGHUP SIGINT SIGTERM
tar xzf $2 -C $tmpdir
DATA=$tmpdir/zvr-data.tar.gz
BOOTSCRIPT=$tmpdir/vyos-postconfig-bootup.script
VERSION=`date +%Y%m%d`
bash -c "$3"
ZSN_DIR=/usr/local/zstack/zsn-agent/bin
cp $ZSN_DIR/zsn-agent $tmpdir/zsn-agent
cp $ZSN_DIR/zstack-network-agent $tmpdir/zstack-network-agent
guestfish <<_EOF_
add $imgfile
run
mount /dev/sda1 /
write $ROOTPATH/etc/version $VERSION
mkdir-p $ROOTPATH/home/vyos/zvr/data/
mkdir-p $ROOTPATH/home/vyos/zvr/keepalived/script
mkdir-p $ROOTPATH/home/vyos/zvr/ssh
mkdir-p $ROOTPATH/etc/conntrackd
mkdir-p $ROOTPATH/opt/vyatta/etc/config/scripts/
mkdir-p $ROOTPATH/usr/local/zstack/zsn-agent/bin
upload $tmpdir/zsn-agent $ROOTPATH$ZSN_DIR/zsn-agent
upload $tmpdir/zstack-network-agent $ROOTPATH/etc/init.d/zstack-network-agent
upload $BOOTSCRIPT $VyosPostScript
tar-in $DATA $ROOTPATH/home/vyos/zvr/data/ compress:gzip
download /boot/grub/grub.cfg /tmp/grub.cfg
! sed -e 's/^set[[:space:]]\+timeout[[:space:]]*=[[:space:]]*[[:digit:]]\+/set timeout=0/g' -e '/^echo.*Grub menu/,/^fi$/d' /tmp/grub.cfg > /tmp/grub.cfg.new
upload /tmp/grub.cfg.new /boot/grub/grub.cfg
download $ROOTPATH/etc/security/limits.conf /tmp/limits.conf
! grep -w "vyos" /tmp/limits.conf | grep nofile | grep soft && sed -i 's/vyos soft nofile [0-9]*/vyos soft nofile 20971520/' /tmp/limits.conf || echo "vyos soft nofile 20971520" >> /tmp/limits.conf
! grep -w "vyos" /tmp/limits.conf | grep nofile | grep hard && sed -i 's/vyos hard nofile [0-9]*/vyos hard nofile 20971520/' /tmp/limits.conf || echo "vyos hard nofile 20971520" >> /tmp/limits.conf
#! grep -w "root" /tmp/limits.conf | grep nofile | grep soft && sed -i 's/root soft nofile [0-9]*/root soft nofile 20971520/' /tmp/limits.conf || echo "root soft nofile 20971520" >> /tmp/limits.conf
! grep -w "root" /tmp/limits.conf | grep nofile | grep hard && sed -i 's/root hard nofile [0-9]*/root hard nofile 20971520/' /tmp/limits.conf || echo "root hard nofile 20971520" >> /tmp/limits.conf
upload /tmp/limits.conf $ROOTPATH/etc/security/limits.conf
_EOF_
/bin/rm -rf $tmpdir
/bin/rm -rf /tmp/grub.cfg /tmp/limits.conf /tmp/sysctl.conf
if [ $isVmdk -eq 1 ]; then
/bin/rm -f "$1"
qemu-img convert -f qcow2 -O vmdk "$imgfile" "$1"
fi
echo "successfully installed $2,$3 to vyos image $1"