-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_appliance.sh
executable file
·138 lines (125 loc) · 3.6 KB
/
create_appliance.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
#!/bin/bash
# ============================================================================
# Script for creating appliances exported from SUSE Studio
# (http://susestudio.com) on your local system.
#
# Requires kiwi (http://kiwi.berlios.de/).
#
# Author: James Tan <[email protected]>
# Contact: [email protected]
# ============================================================================
# Recover backup files
function recover_backup {
for dir in 'source'; do
if [ -f $dir/config.xml.bak ]; then
cp $dir/config.xml.bak $dir/config.xml
echo " -> Recovered Backup $dir/config.xml.bak to $dir/config.xml"
fi
done
}
# Prints and runs the given command. Aborts if the command fails.
function run_cmd {
command=$1
echo $command
$command
if [ $? -ne 0 ]; then
echo
echo "** Appliance creation failed!"
while true; do
read -p "Recover config.xml files? [y/n] " yn
case $yn in
[Nn]* ) break;;
[Yy]* ) recover_backup;break;;
esac
done
exit 1
fi
}
# Display usage.
function usage {
echo >&2 "Usage:"
echo >&2 " create_appliance.sh"
}
# Check that we're root.
if [ `whoami` != 'root' ]; then
echo "Please run this script as root."
exit 1
fi
# Check that kiwi is installed.
kiwi=`which kiwi 2> /dev/null`
if [ $? -ne 0 ]; then
echo "Kiwi is required but not found on your system."
echo "Run the following command to install kiwi:"
echo
echo " zypper install kiwi kiwi-tools kiwi-desc-* kiwi-doc"
echo
exit 1
fi
# Check kiwi version.
kiwi_ver='4.98.15'
installed_kiwi_ver=`rpm -q --whatprovides kiwi`
if [ "$installed_kiwi_ver" != "$kiwi_ver" ]; then
echo "'$kiwi_ver' expected, but '$installed_kiwi_ver' found."
while true; do
read -p "Continue? [y/n] " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
esac
done
fi
# Check architecture (i686, x86_64).
image_arch='i686'
sys_arch=`uname -m`
linux32=`which linux32 2>/dev/null`
if [ "$image_arch" = 'i686' ] && [ "$sys_arch" = 'x86_64' ]; then
if [ "$linux32" = '' ]; then
echo "'linux32' is required but not found."
exit 1
else
kiwi="$linux32 $kiwi"
fi
elif [ "$image_arch" = 'x86_64' ] && [ "$sys_arch" = 'i686' ]; then
echo "Cannot build $image_arch image on a $sys_arch machine."
exit 1
fi
# Replace internal repositories in config.xml.
echo "** Checking for internal repositories..."
for repo in ; do
# check if the repos are already replaced
update_repo=0
for dir in 'source'; do
grep -q "{$repo}" $dir/config.xml && update_repo=1
done
if [ $update_repo -eq 1 ]; then
# prompt for repo url
read -p "Enter repository URL for '$repo': " url
escaped_url=`echo "$url" | sed -e 's/\//\\\\\//g'`
for dir in 'source'; do
# backup config.xml first
if [ ! -f $dir/config.xml.bak ]; then
cp $dir/config.xml $dir/config.xml.bak
echo " -> Backed up $dir/config.xml to $dir/config.xml.bak"
fi
sed -i "s/{$repo}/$escaped_url/g" $dir/config.xml
done
fi
done
# Create appliance.
echo
echo "** Creating appliance..."
run_cmd "rm -rf build/root"
run_cmd "mkdir -p build image"
log="$(pwd)/prepare.log"
run_cmd "$kiwi --prepare source --root build/root --logfile $log" $log
log="$(pwd)/create.log"
run_cmd "$kiwi --create build/root -d image --logfile $log" $log
# And we're done!
image_file="image/mint_appliance_32bit.i686-0.0.26.vmdk"
qemu_options='-snapshot'
[[ "$image_file" =~ \.iso$ ]] && qemu_options='-cdrom'
echo
echo "** Appliance created successfully! ($image_file)"
echo "To boot the image using qemu-kvm, run the following command:"
echo " qemu-kvm -m 512 $qemu_options $image_file &"
echo