-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkchroot
executable file
·145 lines (134 loc) · 3.08 KB
/
mkchroot
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
#!/bin/sh
# Build an OpenMandriva chroot (possibly including qemu setup)
# Usage:
# mkchroot [-v VERSION] [-a ARCH] Directory
# Default:
# VERSION=cooker
# ARCH=host arch
if [ "$(id -u)" != '0' ]; then
exec sudo $0 "$@"
exit 1
fi
usage() {
cat <<EOF
$0 - Build an OpenMandriva chroot (possibly including qemu setup)
mkchroot [-v VERSION] [-a ARCH] Directory
Default:
VERSION=cooker
ARCH=host arch
EOF
exit 1
}
OMV_VERSION=cooker
ARCH=$(uname -m)
[ "$ARCH" = "x86_64" ] && grep -q Ryzen /proc/cpuinfo && ARCH="znver1"
ROOTDIR=""
while [ "$#" -gt 0 ]; do
case "$1" in
-v|--version)
shift
OMV_VERSION="$1"
;;
-a|--arch)
shift
ARCH="$1"
;;
-h|--help)
usage
;;
-*)
usage
;;
*)
if [ -z "$ROOTDIR" ]; then
ROOTDIR="$1"
else
usage
fi
;;
esac
shift
done
[ -z "$ROOTDIR" ] && usage
TEMPDIR="$(mktemp -d /tmp/mkchrootXXXXXX)"
cd "$TEMPDIR"
# Poor man's version of "dnf install --installroot=" --
# need to get the repository configs from rpm packages
# without having the repositories configured...
PKGS=http://abf-downloads.openmandriva.org/$OMV_VERSION/repository/$ARCH/main/release/
curl -s -L $PKGS |grep '^<a' |cut -d'"' -f2 >PACKAGES
PRE_PACKAGES="distro-release-repos distro-release-repos-keys"
for i in $PRE_PACKAGES; do
P=$(grep "^$i-[0-9].*" PACKAGES)
if [ "$?" != "0" ]; then
echo "Can't find $i package for target" >&2
exit 1
fi
curl -O $PKGS/$P
rpm -r "$ROOTDIR" -Uvh --ignorearch --nodeps $P
done
# Set up qemu if necessary
case "$ARCH" in
znver1)
QEMUARCH=x86_64
;;
i?86)
QEMUARCH=i386
;;
arm*)
QEMUARCH=arm
;;
*)
QEMUARCH="$ARCH"
;;
esac
case "$(uname -m)" in
znver1)
HOSTARCH=x86_64
;;
i?86)
HOSTARCH=i386
;;
arm*)
HOSTARCH=arm
;;
*)
HOSTARCH="$(uname -m)"
;;
esac
CHROOT_COMMAND="chroot"
if [ "$HOSTARCH" = "$QEMUARCH" ]; then
# No qemu needed...
unset QEMUARCH
elif [ "$HOSTARCH" = "x86_64" ] && [ "$QEMUARCH" = "i386" ]; then
# Also no need for qemu -- setarch and friends are enough
unset QEMUARCH
CHROOT_COMMAND="i386 chroot"
elif [ "$HOSTARCH" = "aarch64" ] && [ "$QEMUARCH" = "arm" ]; then
# FIXME is there any way to tell a 64-bit-only CPU apart from
# a regular aarch64 CPU that can handle 32bit code???
# For now, let's assume we're on a regular CPU...
unset QEMUARCH
CHROOT_COMMAND="linux32 chroot"
fi
if [ -n "$QEMUARCH" ]; then
dnf -y install qemu-$QEMUARCH-static || :
mkdir -p $ROOTDIR/usr/bin
cp /usr/bin/qemu-$QEMUARCH-static $ROOTDIR/usr/bin/
fi
# FIXME allow better package selection
PACKAGES="basesystem task-devel rpm-build dnf bash"
if ! dnf -y install --installroot=$ROOTDIR --releasever=$OMV_VERSION --forcearch=$ARCH --refresh $PACKAGES; then
echo "Failed to install packages, probably there are dependency problems."
rm -rf $ROOTDIR
exit 1
fi
# This should be there already, but better safe than sorry
mkdir $ROOTDIR/home &>/dev/null || :
cp -f /etc/passwd /etc/shadow /etc/group $ROOTDIR/etc
for i in /dev /dev/shm /dev/pts /proc /sys /home; do
echo "$i $ROOTDIR$i none bind 0 0" >>/etc/fstab
mount "$ROOTDIR$i"
done
echo "Done -- you can enter the chroot environment using"
echo " $CHROOT_COMMAND $ROOTDIR"