-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysrooter.sh
executable file
·148 lines (122 loc) · 3.62 KB
/
sysrooter.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
143
144
145
146
147
148
#!/usr/bin/env bash
set -euo errexit
usage() {
echo "Usage: $0 -a|--arch <arch> -s|--shell --leap"
echo "Supported architectures: ${!arches[@]}"
exit 1
}
run_zypper() {
sudo ZYPP_CONF=$conf zypper --non-interactive --root $TARGET "$@"
}
if [ $(whoami) = "root" ]; then
echo "Don't run this script as root."
exit -1
fi
ARCH=''
QEMU_SUFFIX=''
CHROOT=0
DISTRO_PATH="tumbleweed"
DISTRO_NAME="tumbleweed"
LEAP_VERSION="15.3"
ROOT="/usr/lib/sysroots"
BASE_URL="http://download.opensuse.org"
declare -A arches=([armv7hl]=1 [aarch64]=1 [ppc64]=1 \
[ppc64le]=1 [riscv64]=1 [s390x]=1)
opts=$(getopt -l "arch:,shell,leap,help" -o "a:s:l:h" -- "$@")
eval set --$opts
while [[ $# -gt 0 ]]; do
case "$1" in
-a|--arch)
ARCH=$2
QEMU_SUFFIX=$ARCH
shift 2
;;
-s|--shell)
CHROOT=1
shift 1
;;
-l|--leap)
DISTRO_PATH="/distribution/leap/$LEAP_VERSION/"
DISTRO_NAME="leap"
shift 1
;;
-h|--help)
usage
exit 0
;;
*)
break
;;
esac
done
[[ -z $ARCH ]] && echo "--arch is a required argument." && usage && exit 1
if [[ -z ${arches[$ARCH]:-} ]]; then
echo "$ARCH is not supported, supported architectures: ${!arches[@]}"
exit 1
fi
TARGET=$ROOT/$ARCH-$DISTRO_NAME
[[ $DISTRO_NAME = "leap" ]] && TARGET=$TARGET-$LEAP_VERSION
cleanup() {
sudo umount -l $TARGET/dev || true
sudo umount -l $TARGET/proc || true
}
trap cleanup EXIT
if [[ $CHROOT -eq 1 ]]; then
[[ ! -d $TARGET ]] && echo "$TARGET does not exist" && exit 1
sudo chroot $TARGET && exit 0
fi
if [[ $DISTRO_NAME = "leap" ]]; then
[[ $ARCH = "riscv64" ]] && echo "Leap doesn't support riscv64." && exit 1
[[ $ARCH = "s390x" ]] && echo "Leap doesn't support s390x." && exit 1
REPOURL="$BASE_URL/distribution/leap/15.3/repo/oss"
else
if [[ "$ARCH" == ppc* ]]; then
REPOURL="$BASE_URL"/ports/ppc/"$DISTRO_PATH"/repo/oss
elif [[ "$ARCH" == riscv64 ]]; then
REPOURL="$BASE_URL"/ports/riscv/"$DISTRO_PATH"/repo/oss
elif [[ "$ARCH" = s390x ]]; then
REPOURL="$BASE_URL"/ports/zsystems/"$DISTRO_PATH"/repo/oss
else
REPOURL="$BASE_URL"/ports/"$ARCH"/"$DISTRO_PATH"/repo/oss
fi
fi
sudo mkdir -p $ROOT
if [ "$ARCH" = "armv7hl" ]; then
QEMU_SUFFIX=arm
fi
# Create a fake zypp.conf
conf=$(mktemp)
cat << EOF > $conf
[main]
arch=$ARCH
EOF
if [ -e $TARGET ]; then
read -p "$TARGET already exists do you want to re-create it? (y/n) "
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Clean up
sudo rm -rf $TARGET/{bin,boot,etc,home,lib,mnt,opt,root,run,sbin,srv,tmp,usr,var}
else
echo "Ok, exiting..."
exit 0
fi
fi
# Add default OSS repo
run_zypper ar -f $REPOURL repo-oss
# Update repos for Leap
if [[ $DISTRO_NAME = "leap" ]]; then
for repo in backports oss sle; do
run_zypper ar -f $BASE_URL/update/leap/$LEAP_VERSION/$repo update-$repo
done
fi
run_zypper --gpg-auto-import-keys ref
# Ubuntu...
[[ -f /usr/bin/qemu-$QEMU_SUFFIX-static ]] && QEMU_SUFFIX="$QEMU_SUFFIX"-static
# Copy emulation binaries
sudo mkdir -p $TARGET/usr/bin
sudo cp /usr/bin/emu /usr/bin/qemu-$QEMU_SUFFIX $TARGET/usr/bin
# Create /dev, /proc for bind mount
sudo mkdir -p $TARGET/dev $TARGET/proc
mount -l | grep "$TARGET/dev" &>/dev/null || sudo mount --bind /dev $TARGET/dev
mount -l | grep "$TARGET/proc" &>/dev/null || sudo mount --bind /proc $TARGET/proc
# Install bash and some other required packages
run_zypper in bash glibc-locale-base terminfo coreutils python3