| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# |
| 4 |
# lxc: linux Container library |
| 5 |
|
| 6 |
# Authors: |
| 7 |
# Daniel Lezcano <[email protected]> |
| 8 |
# Re4son <[email protected]> |
| 9 |
# Greg Olsen <[email protected]> (lxc-devuan) |
| 10 |
# Tanya <[email protected]> |
| 11 |
|
| 12 |
# This library is free software; you can redistribute it and/or |
| 13 |
# modify it under the terms of the GNU Lesser General Public |
| 14 |
# License as published by the Free Software Foundation; either |
| 15 |
# version 2.1 of the License, or (at your option) any later version. |
| 16 |
|
| 17 |
# This library is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
# Lesser General Public License for more details. |
| 21 |
|
| 22 |
# You should have received a copy of the GNU Lesser General Public |
| 23 |
# License along with this library; if not, write to the Free Software |
| 24 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
# Detect use under userns (unsupported) |
| 27 |
for arg in "$@"; do |
| 28 |
[ "$arg" = "--" ] && break |
| 29 |
if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then |
| 30 |
echo "This template can't be used for unprivileged containers." 1>&2 |
| 31 |
echo "You may want to try the \"download\" template instead." 1>&2 |
| 32 |
exit 1 |
| 33 |
fi |
| 34 |
done |
| 35 |
|
| 36 |
# Make sure the usual locations are in PATH |
| 37 |
export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin |
| 38 |
export GREP_OPTIONS="" |
| 39 |
|
| 40 |
MIRROR=${MIRROR:-http://http.kali.org/kali} |
| 41 |
## SECURITY_MIRROR=${SECURITY_MIRROR:-http://http.kali.org/} |
| 42 |
LOCALSTATEDIR="@LOCALSTATEDIR@" |
| 43 |
LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@" |
| 44 |
# Allows the lxc-cache directory to be set by environment variable |
| 45 |
LXC_CACHE_PATH=${LXC_CACHE_PATH:-"$LOCALSTATEDIR/cache/lxc"} |
| 46 |
DOWNLOAD_KEYRING=${DOWNLOAD_KEYRING:-1} |
| 47 |
|
| 48 |
find_interpreter() |
| 49 |
{ |
| 50 |
given_interpreter=$(basename "$1") |
| 51 |
|
| 52 |
if [ ! -d /proc/sys/fs/binfmt_misc/ ] ; then |
| 53 |
return 1 |
| 54 |
fi |
| 55 |
for file in /proc/sys/fs/binfmt_misc/* ; do |
| 56 |
if [ "$file" = "/proc/sys/fs/binfmt_misc/register" -o \ |
| 57 |
"$file" = "/proc/sys/fs/binfmt_misc/status" ] ; then |
| 58 |
continue |
| 59 |
fi |
| 60 |
interpreter_path=$(sed -n "/^interpreter/s/interpreter \([^[:space:]]*\)/\1/p" "$file") |
| 61 |
interpreter=$(basename "$interpreter_path") |
| 62 |
|
| 63 |
if [ "$given_interpreter" = "$interpreter" ] ; then |
| 64 |
echo "$interpreter_path" |
| 65 |
return 0 |
| 66 |
fi |
| 67 |
done |
| 68 |
return 1 |
| 69 |
} |
| 70 |
|
| 71 |
configure_kali() |
| 72 |
{ |
| 73 |
rootfs=$1 |
| 74 |
hostname=$2 |
| 75 |
num_tty=$3 |
| 76 |
|
| 77 |
# squeeze only has /dev/tty and /dev/tty0 by default, |
| 78 |
# therefore creating missing device nodes for tty1-4. |
| 79 |
for tty in $(seq 1 "$num_tty"); do |
| 80 |
if [ ! -e "$rootfs/dev/tty$tty" ]; then |
| 81 |
mknod "$rootfs/dev/tty$tty" c 4 "$tty" |
| 82 |
fi |
| 83 |
done |
| 84 |
|
| 85 |
# configure the inittab |
| 86 |
cat <<EOF > $rootfs/etc/inittab |
| 87 |
id:3:initdefault: |
| 88 |
si::sysinit:/etc/init.d/rcS |
| 89 |
l0:0:wait:/etc/init.d/rc 0 |
| 90 |
l1:1:wait:/etc/init.d/rc 1 |
| 91 |
l2:2:wait:/etc/init.d/rc 2 |
| 92 |
l3:3:wait:/etc/init.d/rc 3 |
| 93 |
l4:4:wait:/etc/init.d/rc 4 |
| 94 |
l5:5:wait:/etc/init.d/rc 5 |
| 95 |
l6:6:wait:/etc/init.d/rc 6 |
| 96 |
# Normally not reached, but fallthrough in case of emergency. |
| 97 |
z6:6:respawn:/sbin/sulogin |
| 98 |
1:2345:respawn:/sbin/getty 38400 console |
| 99 |
$(for tty in $(seq 1 "$num_tty"); do echo "c${tty}:12345:respawn:/sbin/getty 38400 tty${tty} linux" ; done;) |
| 100 |
p6::ctrlaltdel:/sbin/init 6 |
| 101 |
p0::powerfail:/sbin/init 0 |
| 102 |
EOF |
| 103 |
|
| 104 |
# symlink mtab |
| 105 |
[ -e "$rootfs/etc/mtab" ] && rm "$rootfs/etc/mtab" |
| 106 |
ln -s /proc/self/mounts "$rootfs/etc/mtab" |
| 107 |
|
| 108 |
# disable selinux in kali |
| 109 |
mkdir -p "$rootfs/selinux" |
| 110 |
echo 0 > "$rootfs/selinux/enforce" |
| 111 |
|
| 112 |
# configure the network using the dhcp |
| 113 |
cat <<EOF > $rootfs/etc/network/interfaces |
| 114 |
auto lo |
| 115 |
iface lo inet loopback |
| 116 |
|
| 117 |
auto eth0 |
| 118 |
iface eth0 inet dhcp |
| 119 |
EOF |
| 120 |
|
| 121 |
# set the hostname |
| 122 |
cat <<EOF > $rootfs/etc/hostname |
| 123 |
$hostname |
| 124 |
EOF |
| 125 |
|
| 126 |
# set minimal hosts |
| 127 |
cat <<EOF > $rootfs/etc/hosts |
| 128 |
127.0.0.1 localhost |
| 129 |
127.0.1.1 $hostname |
| 130 |
|
| 131 |
# The following lines are desirable for IPv6 capable hosts |
| 132 |
::1 localhost ip6-localhost ip6-loopback |
| 133 |
ff02::1 ip6-allnodes |
| 134 |
ff02::2 ip6-allrouters |
| 135 |
EOF |
| 136 |
|
| 137 |
# reconfigure some services |
| 138 |
|
| 139 |
# but first reconfigure locales - so we get no noisy perl-warnings |
| 140 |
if [ -z "$LANG" ] || echo $LANG | grep -E -q "^C(\..+)*$"; then |
| 141 |
cat >> "$rootfs/etc/locale.gen" << EOF |
| 142 |
en_US.UTF-8 UTF-8 |
| 143 |
EOF |
| 144 |
chroot "$rootfs" locale-gen en_US.UTF-8 UTF-8 |
| 145 |
chroot "$rootfs" update-locale LANG=en_US.UTF-8 |
| 146 |
else |
| 147 |
encoding=$(locale charmap) |
| 148 |
[ -z "${encoding}" ] && encoding="UTF-8" |
| 149 |
chroot "$rootfs" sed -e "s/^# \(${LANG} ${encoding}\)/\1/" \ |
| 150 |
-i /etc/locale.gen 2> /dev/null |
| 151 |
cat >> "$rootfs/etc/locale.gen" << EOF |
| 152 |
$LANG $encoding |
| 153 |
EOF |
| 154 |
chroot "$rootfs" locale-gen "$LANG" "$encoding" |
| 155 |
chroot "$rootfs" update-locale LANG="$LANG" |
| 156 |
fi |
| 157 |
|
| 158 |
# remove pointless services in a container |
| 159 |
chroot "$rootfs" /usr/sbin/update-rc.d -f checkroot.sh disable |
| 160 |
chroot "$rootfs" /usr/sbin/update-rc.d -f umountfs disable |
| 161 |
chroot "$rootfs" /usr/sbin/update-rc.d -f hwclock.sh disable |
| 162 |
chroot "$rootfs" /usr/sbin/update-rc.d -f hwclockfirst.sh disable |
| 163 |
|
| 164 |
# generate new SSH keys |
| 165 |
if [ -x "$rootfs/var/lib/dpkg/info/openssh-server.postinst" ]; then |
| 166 |
cat > "$rootfs/usr/sbin/policy-rc.d" << EOF |
| 167 |
#!/bin/sh |
| 168 |
exit 101 |
| 169 |
EOF |
| 170 |
chmod +x "$rootfs/usr/sbin/policy-rc.d" |
| 171 |
|
| 172 |
if [ -f "$rootfs/etc/init/ssh.conf" ]; then |
| 173 |
mv "$rootfs/etc/init/ssh.conf" "$rootfs/etc/init/ssh.conf.disabled" |
| 174 |
fi |
| 175 |
|
| 176 |
rm -f "$rootfs/etc/ssh/"ssh_host_*key* |
| 177 |
|
| 178 |
DPKG_MAINTSCRIPT_PACKAGE=openssh DPKG_MAINTSCRIPT_NAME=postinst chroot "$rootfs" /var/lib/dpkg/info/openssh-server.postinst configure |
| 179 |
sed -i "s/root@$(hostname)/root@$hostname/g" "$rootfs/etc/ssh/"ssh_host_*.pub |
| 180 |
|
| 181 |
if [ -f "$rootfs/etc/init/ssh.conf.disabled" ]; then |
| 182 |
mv "$rootfs/etc/init/ssh.conf.disabled" "$rootfs/etc/init/ssh.conf" |
| 183 |
fi |
| 184 |
|
| 185 |
rm -f "$rootfs/usr/sbin/policy-rc.d" |
| 186 |
fi |
| 187 |
|
| 188 |
# set initial timezone as on host |
| 189 |
if [ -f /etc/timezone ]; then |
| 190 |
cat /etc/timezone > "$rootfs/etc/timezone" |
| 191 |
rm -f "$rootfs/etc/localtime" |
| 192 |
chroot "$rootfs" dpkg-reconfigure -f noninteractive tzdata |
| 193 |
elif [ -f /etc/sysconfig/clock ]; then |
| 194 |
. /etc/sysconfig/clock |
| 195 |
echo "$ZONE" > "$rootfs/etc/timezone" |
| 196 |
rm -f "$rootfs/etc/localtime" |
| 197 |
chroot "$rootfs" dpkg-reconfigure -f noninteractive tzdata |
| 198 |
else |
| 199 |
echo "Timezone in container is not configured. Adjust it manually." |
| 200 |
fi |
| 201 |
|
| 202 |
if [ -n "$authkey" ]; then |
| 203 |
local ssh_dir_path="${rootfs}/root/.ssh" |
| 204 |
mkdir -p "$ssh_dir_path" |
| 205 |
cp "$authkey" "${ssh_dir_path}/authorized_keys" |
| 206 |
chmod 700 "$ssh_dir_path" |
| 207 |
echo "Inserted SSH public key from '$authkey' into /root/.ssh/authorized_keys" |
| 208 |
fi |
| 209 |
|
| 210 |
return 0 |
| 211 |
} |
| 212 |
|
| 213 |
write_sourceslist() |
| 214 |
{ |
| 215 |
local rootfs="$1"; shift |
| 216 |
local release="$1"; shift |
| 217 |
local arch="$1"; shift |
| 218 |
|
| 219 |
local prefix="deb" |
| 220 |
if [ -n "${arch}" ]; then |
| 221 |
prefix="deb [arch=${arch}]" |
| 222 |
fi |
| 223 |
|
| 224 |
if [ "$mainonly" = 1 ]; then |
| 225 |
non_main='' |
| 226 |
else |
| 227 |
non_main=' contrib non-free non-free-firmware' |
| 228 |
fi |
| 229 |
|
| 230 |
cat >> "${rootfs}/etc/apt/sources.list" << EOF |
| 231 |
${prefix} $MIRROR ${release} main${non_main} |
| 232 |
EOF |
| 233 |
} |
| 234 |
|
| 235 |
apt_update_packages(){ |
| 236 |
local apt_update_args="update" |
| 237 |
chroot "${rootfs}" apt-get $apt_update_args |
| 238 |
return $? |
| 239 |
} |
| 240 |
|
| 241 |
install_packages() |
| 242 |
{ |
| 243 |
local rootfs="$1"; shift |
| 244 |
local packages="$*" |
| 245 |
|
| 246 |
if [ -n "${packages}" ] && apt_update_packages; then |
| 247 |
chroot "${rootfs}" apt-get install -y --no-install-recommends ${packages} |
| 248 |
elif [ -n "${packages}" ]; then |
| 249 |
echo "Apt update failed, not downloading packages" |
| 250 |
fi |
| 251 |
} |
| 252 |
|
| 253 |
configure_kali_systemd() |
| 254 |
{ |
| 255 |
path=$1 |
| 256 |
rootfs=$2 |
| 257 |
config=$3 |
| 258 |
num_tty=$4 |
| 259 |
|
| 260 |
# just in case systemd is not installed |
| 261 |
mkdir -p "${rootfs}/lib/systemd/system" |
| 262 |
mkdir -p "${rootfs}/etc/systemd/system/getty.target.wants" |
| 263 |
|
| 264 |
# Fix getty-static-service as debootstrap does not install dbus |
| 265 |
if [ -e "$rootfs//lib/systemd/system/getty-static.service" ] ; then |
| 266 |
local tty_services |
| 267 |
tty_services=$(for i in $(seq 2 "$num_tty"); do echo -n "getty@tty${i}.service "; done; ) |
| 268 |
sed 's/ getty@tty.*/'" $tty_services "'/g' \ |
| 269 |
"$rootfs/lib/systemd/system/getty-static.service" | \ |
| 270 |
sed 's/\(tty2-tty\)[5-9]/\1'"${num_tty}"'/g' > "$rootfs/etc/systemd/system/getty-static.service" |
| 271 |
fi |
| 272 |
|
| 273 |
# This function has been copied and adapted from lxc-fedora |
| 274 |
rm -f "${rootfs}/etc/systemd/system/default.target" |
| 275 |
chroot "${rootfs}" ln -s /dev/null /etc/systemd/system/udev.service |
| 276 |
chroot "${rootfs}" ln -s /dev/null /etc/systemd/system/systemd-udevd.service |
| 277 |
chroot "${rootfs}" ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target |
| 278 |
# Setup getty service on the ttys we are going to allow in the |
| 279 |
# default config. Number should match lxc.tty.max |
| 280 |
( cd "${rootfs}/etc/systemd/system/getty.target.wants" |
| 281 |
for i in $(seq 1 "$num_tty") ; do ln -sf ../getty\@.service getty@tty"${i}".service; done ) |
| 282 |
|
| 283 |
# Since we use static-getty.target; we need to mask [email protected] generated by |
| 284 |
# container-getty-generator, so we don't get multiple instances of agetty running. |
| 285 |
# See https://github.com/lxc/lxc/issues/520 and https://github.com/lxc/lxc/issues/484 |
| 286 |
( cd "${rootfs}/etc/systemd/system/getty.target.wants" |
| 287 |
for i in $(seq 0 "$num_tty"); do ln -sf /dev/null container-getty\@"${i}".service; done ) |
| 288 |
|
| 289 |
return 0 |
| 290 |
} |
| 291 |
|
| 292 |
# Check if given path is in a btrfs partition |
| 293 |
is_btrfs() |
| 294 |
{ |
| 295 |
[ -e "$1" -a "$(stat -f -c '%T' "$1")" = "btrfs" ] |
| 296 |
} |
| 297 |
|
| 298 |
# Check if given path is the root of a btrfs subvolume |
| 299 |
is_btrfs_subvolume() |
| 300 |
{ |
| 301 |
[ -d "$1" -a "$(stat -f -c '%T' "$1")" = "btrfs" -a "$(stat -c '%i' "$1")" -eq 256 ] |
| 302 |
} |
| 303 |
|
| 304 |
try_mksubvolume() |
| 305 |
{ |
| 306 |
path=$1 |
| 307 |
[ -d "$path" ] && return 0 |
| 308 |
mkdir -p "$(dirname "$path")" |
| 309 |
if which btrfs >/dev/null 2>&1 && is_btrfs "$(dirname "$path")"; then |
| 310 |
btrfs subvolume create "$path" |
| 311 |
else |
| 312 |
mkdir -p "$path" |
| 313 |
fi |
| 314 |
} |
| 315 |
|
| 316 |
try_rmsubvolume() |
| 317 |
{ |
| 318 |
path=$1 |
| 319 |
[ -d "$path" ] || return 0 |
| 320 |
if which btrfs >/dev/null 2>&1 && is_btrfs_subvolume "$path"; then |
| 321 |
btrfs subvolume delete "$path" |
| 322 |
else |
| 323 |
rm -rf "$path" |
| 324 |
fi |
| 325 |
} |
| 326 |
|
| 327 |
cleanup() |
| 328 |
{ |
| 329 |
try_rmsubvolume "$cache/partial-$release-$arch" |
| 330 |
try_rmsubvolume "$cache/rootfs-$release-$arch" |
| 331 |
} |
| 332 |
|
| 333 |
download_kali() |
| 334 |
{ |
| 335 |
init=init |
| 336 |
iproute=iproute2 |
| 337 |
dhcpclient=dhcpcd-base |
| 338 |
packages=\ |
| 339 |
$init,\ |
| 340 |
ifupdown,\ |
| 341 |
locales,\ |
| 342 |
ca-certificates,\ |
| 343 |
dialog,\ |
| 344 |
$dhcpclient,\ |
| 345 |
netbase,\ |
| 346 |
net-tools,\ |
| 347 |
$iproute,\ |
| 348 |
openssh-server,\ |
| 349 |
kali-archive-keyring,\ |
| 350 |
apt-transport-https |
| 351 |
|
| 352 |
cache=$1 |
| 353 |
arch=$2 |
| 354 |
release=$3 |
| 355 |
interpreter="$4" |
| 356 |
interpreter_path="$5" |
| 357 |
|
| 358 |
trap cleanup EXIT SIGHUP SIGINT SIGTERM |
| 359 |
|
| 360 |
# Create the cache |
| 361 |
mkdir -p "$cache" |
| 362 |
|
| 363 |
# If kali-archive-keyring isn't installed, fetch GPG keys directly |
| 364 |
releasekeyring=/usr/share/keyrings/kali-archive-keyring.gpg |
| 365 |
lreleasekeyring=/etc/apt/trusted.gpg.d/kali-archive-keyring.gpg |
| 366 |
if [ -f "$releasekeyring" ]; then |
| 367 |
apt_gpg_opt="--keyring=${releasekeyring}" |
| 368 |
elif [ -f "$lreleasekeyring" ]; then |
| 369 |
apt_gpg_opt="--keyring=${lreleasekeyring}" |
| 370 |
elif [ "$DOWNLOAD_KEYRING" = 1 ]; then |
| 371 |
[ ! -d "/etc/apt/trusted.gpg.d" ] && lreleasekeyring="$cache/archive-key.gpg" |
| 372 |
if [[ "$(id -u)" == "0" ]]; then |
| 373 |
gpgkeyname="archive-key" |
| 374 |
wget https://archive.kali.org/${gpgkeyname}.asc -O - --quiet \ |
| 375 |
| gpg --import --no-default-keyring --keyring="${lreleasekeyring}" |
| 376 |
apt_gpg_opt="--keyring=${lreleasekeyring}" |
| 377 |
else |
| 378 |
echo "Must be root (sudo) to save $lreleasekeyring" |
| 379 |
fi |
| 380 |
fi |
| 381 |
if [ -z "$apt_gpg_opt" ]; then |
| 382 |
echo "WARNING: No GPG check" |
| 383 |
apt_gpg_opt='--no-check-gpg' |
| 384 |
fi |
| 385 |
# check the mini kali was not already downloaded |
| 386 |
try_mksubvolume "$cache/partial-$release-$arch" |
| 387 |
if [ $? -ne 0 ]; then |
| 388 |
echo "Failed to create '$cache/partial-$release-$arch' directory" |
| 389 |
return 1 |
| 390 |
fi |
| 391 |
|
| 392 |
# download a mini kali into a cache |
| 393 |
echo "Downloading kali minimal ..." |
| 394 |
if [ "$interpreter" = "" ] ; then |
| 395 |
debootstrap --verbose --variant=minbase --arch="$arch" \ |
| 396 |
--include=$packages "${apt_gpg_opt}" \ |
| 397 |
"$release" "$cache/partial-$release-$arch" "$MIRROR" |
| 398 |
if [ $? -ne 0 ]; then |
| 399 |
echo "Failed to download the rootfs, aborting." |
| 400 |
return 1 |
| 401 |
fi |
| 402 |
else |
| 403 |
debootstrap --foreign --verbose --variant=minbase --arch="$arch" \ |
| 404 |
--include=$packages "${apt_gpg_opt}" \ |
| 405 |
"$release" "$cache/partial-$release-$arch" "$MIRROR" |
| 406 |
if [ $? -ne 0 ]; then |
| 407 |
echo "Failed to download the rootfs, aborting." |
| 408 |
return 1 |
| 409 |
fi |
| 410 |
mkdir -p "$(dirname "$cache/partial-$release-$arch/$interpreter_path")" |
| 411 |
cp "$interpreter" "$cache/partial-$release-$arch/$interpreter_path" |
| 412 |
if [ $? -ne 0 ]; then |
| 413 |
echo "failed to copy $interpreter to $cache/partial-$release-$arch/$interpreter_path" |
| 414 |
return 1 |
| 415 |
fi |
| 416 |
chroot "$cache/partial-$release-$arch" debootstrap/debootstrap --second-stage |
| 417 |
if [ $? -ne 0 ]; then |
| 418 |
echo "failed to update the rootfs, aborting" |
| 419 |
return 1 |
| 420 |
fi |
| 421 |
fi |
| 422 |
|
| 423 |
mv "$1/partial-$release-$arch" "$1/rootfs-$release-$arch" |
| 424 |
echo "Download complete." |
| 425 |
trap EXIT |
| 426 |
trap SIGINT |
| 427 |
trap SIGTERM |
| 428 |
trap SIGHUP |
| 429 |
|
| 430 |
return 0 |
| 431 |
} |
| 432 |
|
| 433 |
copy_kali() |
| 434 |
{ |
| 435 |
cache=$1 |
| 436 |
arch=$2 |
| 437 |
rootfs=$3 |
| 438 |
release=$4 |
| 439 |
|
| 440 |
# make a local copy of the minikali |
| 441 |
echo -n "Copying rootfs to $rootfs..." |
| 442 |
try_mksubvolume "$rootfs" |
| 443 |
if which btrfs >/dev/null 2>&1 && \ |
| 444 |
is_btrfs_subvolume "$cache/rootfs-$release-$arch" && \ |
| 445 |
is_btrfs_subvolume "$rootfs"; then |
| 446 |
realrootfs="$(dirname "$config")"/rootfs |
| 447 |
[ "$rootfs" = "$realrootfs" ] || umount "$rootfs" || return 1 |
| 448 |
btrfs subvolume delete "$realrootfs" || return 1 |
| 449 |
btrfs subvolume snapshot "$cache/rootfs-$release-$arch" "$realrootfs" || return 1 |
| 450 |
[ "$rootfs" = "$realrootfs" ] || mount --bind "$realrootfs" "$rootfs" || return 1 |
| 451 |
else |
| 452 |
rsync -SHaAX "$cache/rootfs-$release-$arch"/ $rootfs/ || return 1 |
| 453 |
fi |
| 454 |
return 0 |
| 455 |
} |
| 456 |
|
| 457 |
install_kali() |
| 458 |
{ |
| 459 |
rootfs=$1 |
| 460 |
release=$2 |
| 461 |
arch=$3 |
| 462 |
cache="$4/kali" |
| 463 |
interpreter="$5" |
| 464 |
interpreter_path="$6" |
| 465 |
flushcache=$7 |
| 466 |
mkdir -p $LOCALSTATEDIR/lock/subsys/ |
| 467 |
( |
| 468 |
flock -x 9 |
| 469 |
if [ $? -ne 0 ]; then |
| 470 |
echo "Cache repository is busy." |
| 471 |
return 1 |
| 472 |
fi |
| 473 |
|
| 474 |
if [ "$flushcache" -eq 1 ]; then |
| 475 |
echo "Flushing cache..." |
| 476 |
cleanup |
| 477 |
fi |
| 478 |
|
| 479 |
echo "Checking cache download in $cache/rootfs-$release-$arch ... " |
| 480 |
if [ ! -e "$cache/rootfs-$release-$arch" ]; then |
| 481 |
download_kali "$cache" "$arch" "$release" "$interpreter" "$interpreter_path" |
| 482 |
if [ $? -ne 0 ]; then |
| 483 |
echo "Failed to download 'kali base'" |
| 484 |
return 1 |
| 485 |
fi |
| 486 |
fi |
| 487 |
|
| 488 |
copy_kali "$cache" "$arch" "$rootfs" "$release" |
| 489 |
if [ $? -ne 0 ]; then |
| 490 |
echo "Failed to copy rootfs" |
| 491 |
return 1 |
| 492 |
fi |
| 493 |
|
| 494 |
return 0 |
| 495 |
|
| 496 |
) 9>$LOCALSTATEDIR/lock/subsys/lxc-kali |
| 497 |
|
| 498 |
return $? |
| 499 |
} |
| 500 |
|
| 501 |
copy_configuration() |
| 502 |
{ |
| 503 |
path=$1 |
| 504 |
rootfs=$2 |
| 505 |
hostname=$3 |
| 506 |
arch=$4 |
| 507 |
num_tty=$5 |
| 508 |
|
| 509 |
# Generate the configuration file |
| 510 |
# if there is exactly one veth network entry, make sure it has an |
| 511 |
# associated hwaddr. |
| 512 |
nics=$(grep -ce '^lxc\.net\.0\.type[ \t]*=[ \t]*veth' "$path/config") |
| 513 |
if [ "$nics" -eq 1 ]; then |
| 514 |
grep -q "^lxc.net.0.hwaddr" "$path/config" || sed -i -e "/^lxc\.net\.0\.type[ \t]*=[ \t]*veth/a lxc.net.0.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" "$path/config" |
| 515 |
fi |
| 516 |
|
| 517 |
## Add all the includes |
| 518 |
echo "" >> "$path/config" |
| 519 |
echo "# Common configuration" >> "$path/config" |
| 520 |
if [ -e "${LXC_TEMPLATE_CONFIG}/kali.common.conf" ]; then |
| 521 |
echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/kali.common.conf" >> "$path/config" |
| 522 |
fi |
| 523 |
if [ -e "${LXC_TEMPLATE_CONFIG}/kali.${release}.conf" ]; then |
| 524 |
echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/kali.${release}.conf" >> "$path/config" |
| 525 |
fi |
| 526 |
|
| 527 |
## Add the container-specific config |
| 528 |
echo "" >> "$path/config" |
| 529 |
echo "# Container specific configuration" >> "$path/config" |
| 530 |
grep -q "^lxc.rootfs.path" "$path/config" 2> /dev/null || echo "lxc.rootfs.path = $rootfs" >> "$path/config" |
| 531 |
|
| 532 |
cat <<EOF >> $path/config |
| 533 |
lxc.tty.max = $num_tty |
| 534 |
lxc.uts.name = $hostname |
| 535 |
lxc.arch = $arch |
| 536 |
lxc.pty.max = 1024 |
| 537 |
EOF |
| 538 |
|
| 539 |
if [ $? -ne 0 ]; then |
| 540 |
echo "Failed to add configuration" |
| 541 |
return 1 |
| 542 |
fi |
| 543 |
|
| 544 |
return 0 |
| 545 |
} |
| 546 |
|
| 547 |
post_process() |
| 548 |
{ |
| 549 |
local rootfs="$1"; shift |
| 550 |
local release="$1"; shift |
| 551 |
local arch="$1"; shift |
| 552 |
local hostarch="$1"; shift |
| 553 |
local interpreter="$1"; shift |
| 554 |
local packages="$*" |
| 555 |
|
| 556 |
# Disable service startup |
| 557 |
cat > "${rootfs}/usr/sbin/policy-rc.d" << EOF |
| 558 |
#!/bin/sh |
| 559 |
exit 101 |
| 560 |
EOF |
| 561 |
chmod +x "${rootfs}/usr/sbin/policy-rc.d" |
| 562 |
|
| 563 |
# If the container isn't running a native architecture, setup multiarch |
| 564 |
if [ "$interpreter" = "" -a "${arch}" != "${hostarch}" ]; then |
| 565 |
# Test if dpkg supports multiarch |
| 566 |
if ! chroot "$rootfs" dpkg --print-foreign-architectures 2>&1; then |
| 567 |
chroot "$rootfs" dpkg --add-architecture "${hostarch}" |
| 568 |
fi |
| 569 |
fi |
| 570 |
|
| 571 |
# Write a new sources.list containing both native and multiarch entries |
| 572 |
> "${rootfs}/etc/apt/sources.list" |
| 573 |
if [ "$interpreter" != "" -a "${arch}" = "${hostarch}" ]; then |
| 574 |
write_sourceslist "${rootfs}" "${release}" "${arch}" |
| 575 |
else |
| 576 |
write_sourceslist "${rootfs}" "${release}" |
| 577 |
fi |
| 578 |
|
| 579 |
# Install Packages in container |
| 580 |
if [ -n "${packages}" ]; then |
| 581 |
local pack_list |
| 582 |
pack_list="${packages//,/ }" |
| 583 |
echo "Installing packages: ${pack_list}" |
| 584 |
install_packages "${rootfs}" "${pack_list}" |
| 585 |
fi |
| 586 |
|
| 587 |
# Re-enable service startup |
| 588 |
rm "${rootfs}/usr/sbin/policy-rc.d" |
| 589 |
|
| 590 |
# Fix by Greg Olsen, avoid warning 'stdin not a tty' in some cases |
| 591 |
if [[ -f "${rootfs}/root/.profile" ]]; then |
| 592 |
sed -i -e 's/^mesg n/test -t 0 \&\& mesg n/g' ${rootfs}/root/.profile |
| 593 |
fi |
| 594 |
|
| 595 |
# end |
| 596 |
} |
| 597 |
|
| 598 |
clean() |
| 599 |
{ |
| 600 |
cache=${LXC_CACHE_PATH:-"$LOCALSTATEDIR/cache/lxc/kali"} |
| 601 |
|
| 602 |
if [ ! -e "$cache" ]; then |
| 603 |
exit 0 |
| 604 |
fi |
| 605 |
|
| 606 |
# lock, so we won't purge while someone is creating a repository |
| 607 |
( |
| 608 |
flock -x 9 |
| 609 |
if [ $? != 0 ]; then |
| 610 |
echo "Cache repository is busy." |
| 611 |
exit 1 |
| 612 |
fi |
| 613 |
|
| 614 |
echo -n "Purging the download cache..." |
| 615 |
rm --preserve-root --one-file-system -rf "$cache" && echo "Done." || exit 1 |
| 616 |
exit 0 |
| 617 |
|
| 618 |
) 9>$LOCALSTATEDIR/lock/subsys/lxc-kali |
| 619 |
} |
| 620 |
|
| 621 |
usage() |
| 622 |
{ |
| 623 |
cat <<EOF |
| 624 |
Template specific options can be passed to lxc-create after a '--' like this: |
| 625 |
|
| 626 |
lxc-create --name=NAME [-lxc-create-options] -- [-template-options] |
| 627 |
|
| 628 |
Usage: $1 -h|--help -p|--path=<path> [-c|--clean] [-a|--arch=<arch>] [-r|--release=<release>] |
| 629 |
[--mirror=<mirror>] |
| 630 |
[--package=<package_name1,package_name2,...>] |
| 631 |
[-I|--interpreter-path=<interpreter path>] |
| 632 |
[-F | --flush-cache] [-S|--auth-key=<keyfile>] |
| 633 |
|
| 634 |
Options : |
| 635 |
|
| 636 |
-h, --help print this help text |
| 637 |
-p, --path=PATH directory where config and rootfs of this VM will be kept |
| 638 |
-S, --auth-key=KEYFILE SSH public key to inject into the container as the root user. |
| 639 |
-a, --arch=ARCH The container architecture. Can be one of: i686, x86_64, |
| 640 |
amd64, armhf, armel. Defaults to host arch. |
| 641 |
-r, --release=RELEASE Kali release. Can be one of: kali-dev, kali-rolling. |
| 642 |
Defaults to kali-rolling. |
| 643 |
--mirror=MIRROR Kali mirror to use during installation. Overrides the MIRROR |
| 644 |
environment variable (see below). |
| 645 |
--packages=PACKAGE_NAME1,PACKAGE_NAME2,... |
| 646 |
List of additional packages to install. Comma separated, without space. |
| 647 |
-c, --clean only clean up the cache and terminate |
| 648 |
--enable-non-free include also Kali's contrib, non-free and non-free-firmware repositories. |
| 649 |
-I|--interpreter-path=INTERPRETER-PATH |
| 650 |
Path of the binfmt interpreter to copy to the rootfs |
| 651 |
-F | --flush-cache Flush the kali release cache |
| 652 |
|
| 653 |
Environment variables: |
| 654 |
|
| 655 |
MIRROR The Kali package mirror to use. See also the --mirror switch above. |
| 656 |
Defaults to '$MIRROR' |
| 657 |
DOWNLOAD_KEYRING Sets whether to download keyring when missing or ignore keyring checks |
| 658 |
Defaults to 1 |
| 659 |
|
| 660 |
EOF |
| 661 |
return 0 |
| 662 |
} |
| 663 |
|
| 664 |
options=$(getopt -o hp:n:a:r:cI:FS: -l arch:,auth-key:,clean,help,enable-non-free,mirror:,name:,packages:,path:,release:,rootfs:,interpreter-path:,flush-cache -- "$@") |
| 665 |
if [ $? -ne 0 ]; then |
| 666 |
usage "$(basename "$0")" |
| 667 |
exit 1 |
| 668 |
fi |
| 669 |
eval set -- "$options" |
| 670 |
|
| 671 |
littleendian=$(lscpu | grep '^Byte Order' | grep -q Little && echo yes) |
| 672 |
|
| 673 |
arch=$(uname -m) |
| 674 |
if [ "$arch" = "i686" ]; then |
| 675 |
arch="i386" |
| 676 |
elif [ "$arch" = "x86_64" ]; then |
| 677 |
arch="amd64" |
| 678 |
elif [ "$arch" = "armv7l" ]; then |
| 679 |
arch="armhf" |
| 680 |
elif [ "$arch" = "aarch64" ]; then |
| 681 |
arch="arm64" |
| 682 |
fi |
| 683 |
hostarch=$arch |
| 684 |
packages= |
| 685 |
mainonly=1 |
| 686 |
flushcache=0 |
| 687 |
|
| 688 |
while true |
| 689 |
do |
| 690 |
case "$1" in |
| 691 |
-h|--help) usage "$0" && exit 1;; |
| 692 |
--) shift 1; break ;; |
| 693 |
|
| 694 |
-a|--arch) arch=$2; shift 2;; |
| 695 |
-S|--auth-key) authkey=$2; shift 2;; |
| 696 |
-I|--interpreter-path) |
| 697 |
interpreter="$2"; shift 2;; |
| 698 |
-c|--clean) clean=1; shift 1;; |
| 699 |
--enable-non-free) mainonly=0; shift 1;; |
| 700 |
--mirror) MIRROR=$2; shift 2;; |
| 701 |
-n|--name) name=$2; shift 2;; |
| 702 |
--packages) packages=$2; shift 2;; |
| 703 |
-p|--path) path=$2; shift 2;; |
| 704 |
-r|--release) release=$2; shift 2;; |
| 705 |
--rootfs) rootfs=$2; shift 2;; |
| 706 |
-F|--flush-cache) flushcache=1; shift 1;; |
| 707 |
*) break ;; |
| 708 |
esac |
| 709 |
done |
| 710 |
|
| 711 |
if [ ! -z "$clean" -a -z "$path" ]; then |
| 712 |
clean || exit 1 |
| 713 |
exit 0 |
| 714 |
fi |
| 715 |
|
| 716 |
if [ "$arch" = "i686" ]; then |
| 717 |
arch=i386 |
| 718 |
fi |
| 719 |
|
| 720 |
if [ "$arch" = "x86_64" ]; then |
| 721 |
arch=amd64 |
| 722 |
fi |
| 723 |
|
| 724 |
if [ "$interpreter" = "" ] ; then |
| 725 |
if [ $hostarch = "i386" -a $arch = "amd64" ]; then |
| 726 |
echo "can't create $arch container on $hostarch" |
| 727 |
exit 1 |
| 728 |
fi |
| 729 |
|
| 730 |
if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \ |
| 731 |
[ $arch != "armhf" -a $arch != "armel" ]; then |
| 732 |
echo "can't create $arch container on $hostarch" |
| 733 |
exit 1 |
| 734 |
fi |
| 735 |
else |
| 736 |
if ! file -b "${interpreter}" |grep -q "statically linked" ; then |
| 737 |
echo "'${interpreter}' must be statically linked" 1>&2 |
| 738 |
exit 1 |
| 739 |
fi |
| 740 |
interpreter_path=$(find_interpreter "$interpreter") |
| 741 |
if [ $? -ne 0 ] ; then |
| 742 |
echo "no binfmt interpreter using $(basename "$interpreter")" 1>&2 |
| 743 |
exit 1 |
| 744 |
fi |
| 745 |
fi |
| 746 |
|
| 747 |
type debootstrap |
| 748 |
if [ $? -ne 0 ]; then |
| 749 |
echo "'debootstrap' command is missing" |
| 750 |
exit 1 |
| 751 |
fi |
| 752 |
|
| 753 |
if [ -z "$path" ]; then |
| 754 |
echo "'path' parameter is required" |
| 755 |
exit 1 |
| 756 |
fi |
| 757 |
|
| 758 |
if [ "$(id -u)" != "0" ]; then |
| 759 |
echo "This script should be run as 'root'" |
| 760 |
exit 1 |
| 761 |
fi |
| 762 |
|
| 763 |
if [ -n "$authkey" ]; then |
| 764 |
if [ ! -f "$authkey" ]; then |
| 765 |
echo "SSH keyfile '$authkey' not found" |
| 766 |
exit 1 |
| 767 |
fi |
| 768 |
# This is mostly to prevent accidental uage of the private key instead |
| 769 |
# of the public key. |
| 770 |
if [ "${authkey: -4}" != ".pub" ]; then |
| 771 |
echo "SSH keyfile '$authkey' does not end with '.pub'" |
| 772 |
exit 1 |
| 773 |
fi |
| 774 |
fi |
| 775 |
|
| 776 |
release=${release:-kali-rolling} |
| 777 |
|
| 778 |
# detect rootfs |
| 779 |
config="$path/config" |
| 780 |
if [ -z "$rootfs" ]; then |
| 781 |
if grep -q '^lxc.rootfs.path' "$config" 2> /dev/null ; then |
| 782 |
rootfs=$(awk -F= '/^lxc.rootfs.path[ \t]+=/{ print $2 }' "$config") |
| 783 |
else |
| 784 |
rootfs=$path/rootfs |
| 785 |
fi |
| 786 |
fi |
| 787 |
|
| 788 |
# determine the number of ttys - default is 4 |
| 789 |
if grep -q '^lxc.tty.max' "$config" 2> /dev/null ; then |
| 790 |
num_tty=$(awk -F= '/^lxc.tty.max[ \t]+=/{ print $2 }' "$config") |
| 791 |
else |
| 792 |
num_tty=4 |
| 793 |
fi |
| 794 |
|
| 795 |
install_kali "$rootfs" "$release" "$arch" "$LXC_CACHE_PATH" "$interpreter" "$interpreter_path" "$flushcache" |
| 796 |
if [ $? -ne 0 ]; then |
| 797 |
echo "failed to install kali" |
| 798 |
exit 1 |
| 799 |
fi |
| 800 |
|
| 801 |
configure_kali "$rootfs" "$name" $num_tty |
| 802 |
if [ $? -ne 0 ]; then |
| 803 |
echo "failed to configure kali for a container" |
| 804 |
exit 1 |
| 805 |
fi |
| 806 |
|
| 807 |
copy_configuration "$path" "$rootfs" "$name" $arch $num_tty |
| 808 |
if [ $? -ne 0 ]; then |
| 809 |
echo "failed write configuration file" |
| 810 |
exit 1 |
| 811 |
fi |
| 812 |
|
| 813 |
configure_kali_systemd "$path" "$rootfs" "$config" $num_tty |
| 814 |
|
| 815 |
post_process "${rootfs}" "${release}" ${arch} ${hostarch} "${interpreter}" "${packages}" |
| 816 |
|
| 817 |
if [ ! -z "$clean" ]; then |
| 818 |
clean || exit 1 |
| 819 |
exit 0 |
| 820 |
fi |