Last active
January 4, 2023 01:56
-
-
Save YutaroHayakawa/ee33b93f29ce3c4047a15732828e383c to your computer and use it in GitHub Desktop.
Bash script to setup simple Fedora chroot environment from container layer image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# You can find the container image for each release from here. Please rewrite below url to your nearest one. | |
# https://admin.fedoraproject.org/mirrormanager/ | |
CONTAINER_IMAGE="http://mirror.crucial.com.au/fedora/linux/releases/30/Container/x86_64/images/Fedora-Container-Base-30-1.2.x86_64.tar.xz" | |
function setup() { | |
local CHROOT=$1 | |
if [ ! -e $CHROOT ]; then | |
mkdir $CHROOT | |
else | |
echo "$CHROOT already exists" | |
exit 1 | |
fi | |
cd $CHROOT | |
# Extract layer file and untar it. Cleanup unneccesary files as well. | |
wget $CONTAINER_IMAGE | |
tar xvf `ls | grep "Fedora-Container"` | |
layer=$(find . -name layer.tar) | |
uuid=$(echo $layer | cut -d "/" -f 2) | |
cp $layer . | |
rm -r $uuid *.json repositories | |
tar xvf layer.tar | |
chmod 744 . | |
rm *.tar.xz *.tar | |
mkdir dev/pts | |
cp /etc/resolv.conf etc | |
# Bind mount some special filesystems from host | |
mount --bind /proc proc | |
mount --bind /sys sys | |
mount --bind /dev dev | |
cd .. | |
chroot $CHROOT mount -t devpts none /dev/pts/ | |
chroot $CHROOT dnf -y install iproute git vim zsh | |
chroot $CHROOT bash -c "echo \"export PATH=$PATH:/usr/local/bin:/sbin\" >> /root/.zshrc" | |
} | |
function cleanup() { | |
local CHROOT=$1 | |
if [ ! -e $CHROOT ]; then | |
echo "$CHROOT doesn't exist" | |
exit 1 | |
fi | |
umount $CHROOT/proc | |
umount $CHROOT/sys | |
umount $CHROOT/dev/pts | |
umount $CHROOT/dev | |
rm -rf $CHROOT | |
} | |
if [ -z $1 ] || [ -z $2 ]; then | |
echo "Usage: $0 <chroot directory> <setup|cleanup>" | |
exit 1 | |
fi | |
$2 $1 |
well.. other than that i do need to remove your chroot container archive thinger...
but other than that, well done :)
also likely won't need to install all the packages.
what was your application for this that so much stuff was missing ?
is this image you're extracting like a plugin to container environment that has all this located under a different tree ?
TBH, I don't even remember how I was using this..., but I grad to see this useful for you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I will love you forever :)
Thank you for not making me do this over again by hand :)