-
Notifications
You must be signed in to change notification settings - Fork 45
/
0-check-and-set.sh
executable file
·129 lines (110 loc) · 3.54 KB
/
0-check-and-set.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
#!/bin/bash
build_packages=( multistrap binfmt-support qemu-user-static )
sshd_packages=( ssh openssh-server )
conf_default=multistrap.conf
conf_powerpcspe=multistrap_debian-ports.conf
conf_s390x=multistrap_no-sshd.conf
rootfs_suffix=rootfs
unset check_and_set
# Available architectures with their associated qemu
declare -A qemu_static
#qemu_static[amd64]=qemu-x86_64-static => see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=703825
qemu_static[arm64]=qemu-aarch64-static
qemu_static[armel]=qemu-arm-static
qemu_static[armhf]=qemu-arm-static
qemu_static[i386]=qemu-i386-static
qemu_static[mips]=qemu-mips-static
qemu_static[mipsel]=qemu-mipsel-static
qemu_static[powerpc]=qemu-ppc-static
qemu_static[powerpcspe]=qemu-ppc-static
qemu_static[ppc64el]=qemu-ppc64le-static
qemu_static[s390x]=qemu-s390x-static
print_archs() {
echo " - $host_arch"
for i in ${!qemu_static[@]}; do
if [[ $i != $host_arch ]]; then
echo " - $i"
fi
done
}
# Ckeck script is sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "`basename $0` needs to be sourced"
exit 1
fi
# Get caller script
caller_script=`basename $(caller | awk '{print $2}')`
exit_or_return=`[[ $caller_script != NULL ]] && echo exit|| echo return`
# Make sure only root can run our script
if [[ $(id -u) != 0 ]]; then
echo "This script must be run as root"
$exit_or_return 1
fi
# Required packages to build rootfs
for i in ${build_packages[@]}; do
if ! dpkg -s $i 2>/dev/null | grep -q "Status: install ok installed"; then
echo "$i package is required, please install it"
$exit_or_return 1
fi
done
# Get host architecture
host_arch=`dpkg --print-architecture`
# Print usage
if [[ ! $1 || $1 == "-h" || $1 == "--help" ]]; then
running_script=`[[ $caller_script != NULL ]] && echo $caller_script ||\
echo "source \`basename ${BASH_SOURCE[0]}\`"`
echo "usage: $running_script ARCHITECTURE [MULTISTRAP_CONF]"
echo " ARCHITECTURE can be:"
print_archs
echo " MULTISTRAP_CONF is a multistrap configuration file"
echo " defaults to $conf_default"
echo " defaults to $conf_s390x for s390x"
echo " defaults to $conf_powerpcspe for powerpcspe"
$exit_or_return 1
fi
arch=$1
# Default multistrap configuration file
case $arch in
powerpcspe) conf_file=$conf_powerpcspe ;;
s390x) conf_file=$conf_s390x ;;
*) conf_file=$conf_default
esac
# User defined multistrap configuration file
if [[ $2 ]]; then
conf_file=$2
if [[ ! -f $conf_file ]]; then
echo "$conf_file file does not exist"
$exit_or_return 1
fi
fi
# Check architecture is suppported
if [[ $arch != $host_arch ]]; then
if [[ ! ${qemu_static[$arch]} ]]; then
echo "$arch not valid, architectures supported are:"
print_archs
$exit_or_return 1
fi
# Find qemu binary
qemu_path=`which ${qemu_static[$arch]}`
case $arch in
powerpcspe)
# Set qemu-ppc-static to support powerpcspe
export QEMU_CPU=e500v2
;;
s390x)
# qemu-s390x-static cannot install openssh-server
for i in ${sshd_packages[@]}; do
if grep -q "\b$i\b" $conf_file; then
echo "$i package in $conf_file cannot be installed for s390x"
$exit_or_return 1
fi
done
;;
esac
fi
# Create build directory
build_dir=build/$arch
mkdir -p $build_dir 2>/dev/null
# Set rootfs directory
rootfs_dir=$arch\-$rootfs_suffix
check_and_set=1