Skip to content

Instantly share code, notes, and snippets.

@inku25253
Last active August 6, 2018 10:31
Show Gist options
  • Save inku25253/c61ea6e98977e3729d3788d2f289f0bb to your computer and use it in GitHub Desktop.
Save inku25253/c61ea6e98977e3729d3788d2f289f0bb to your computer and use it in GitHub Desktop.
chrootで開発環境を切り替えするやつ。
#!/bin/bash
#devenv [template] [dir]
templateDir=/devenv/
usage(){
echo "devenv [template] [dir]"
echo
}
if [ $EUID != 0 ]; then
echo "Please run as root user"
exit 1
fi
if [ $# -ne 2 ]; then
echo "invalid arguments"
usage
exit
fi
for dir in $templateDir*; do
if [ -d "$dir" ]; then
if [ ! -f "$dir/config" ]; then
echo "missing machine config file [$dir]"
continue
fi
Rootdir=$dir/root
Script=/bin/bash
source "$dir/config"
if [ ! -d "$Rootdir" ]; then
echo "missing machine root directory [$Rootdir]"
continue
fi
if [ ! "$Name" == "$1" ]; then
continue
fi
declare -a machine=("$Rootdir" "$Script")
break
fi
done
if [ ! "$machine" ]; then
echo "argument error: missing templates [$1]"
exit
fi
machinePath=${machine[0]}
mountpoint -q "$machinePath/proc" || mount -t proc none "$machinePath/proc"
mountpoint -q "$machinePath/sys" || mount -o bind /sys "$machinePath/sys"
mountpoint -q "$machinePath/dev" || mount -o bind /dev "$machinePath/dev"
mountpoint -q "$machinePath/devsrc" || mount -o bind "$2" "$machinePath/devsrc"
chroot "${machinePath}" ${machine[1]}
umount "$machinePath/proc"
umount "$machinePath/sys"
umount "$machinePath/dev"
umount "$machinePath/devsrc"
echo EXIT.
#!/bin/bash
#devenv [template] [dir]
templateBaseDir=/devenv/
mirror=http://dl-cdn.alpinelinux.org/alpine/
usage(){
echo "devnewenv [templateName]"
echo
}
if [ $EUID != 0 ]; then
echo "Please run as root user"
exit 1
fi
if [ $# -ne 1 ]; then
echo "invalid arguments"
usage
exit
fi
templateName=$1
templateDir=${templateBaseDir}${templateName}
chroot_dir=${templateDir}/root
mkdir -p ${chroot_dir}/devsrc
cat << _EOF_ > ${templateDir}/config
Name=${templateName}
Script="/bin/ash -l"
_EOF_
apk.static \
-X ${mirror}/latest-stable/main \
-U --allow-untrusted \
--root ${chroot_dir} \
--initdb add alpine-base
pushd ${chroot_dir}
mknod -m 666 ${chroot_dir}/dev/full c 1 7
mknod -m 666 ${chroot_dir}/dev/ptmx c 5 2
mknod -m 644 ${chroot_dir}/dev/random c 1 8
mknod -m 644 ${chroot_dir}/dev/urandom c 1 9
mknod -m 666 ${chroot_dir}/dev/zero c 1 5
mknod -m 666 ${chroot_dir}/dev/tty c 5 0
cp /etc/resolv.conf ${chroot_dir}/etc/
mkdir -p ${chroot_dir}/root
mkdir -p ${chroot_dir}/etc/apk
echo "${mirror}/v3.8/main" > ${chroot_dir}/etc/apk/repositories
mount -t proc none ${chroot_dir}/proc
mount -o bind /sys ${chroot_dir}/sys
mount -o bind /dev ${chroot_dir}/dev
chroot "${chroot_dir}" /bin/sh -c "\
rc-update add devfs sysinit &&\
rc-update add dmesg sysinit &&\
rc-update add mdev sysinit &&\
rc-update add hwclock boot &&\
rc-update add modules boot &&\
rc-update add sysctl boot &&\
rc-update add hostname boot &&\
rc-update add bootmisc boot &&\
rc-update add syslog boot &&\
rc-update add mount-ro shutdown &&\
rc-update add killprocs shutdown &&\
rc-update add savecache shutdown &&\
echo rc-update done. &&\
exit;\
"
umount "${chroot_dir}/proc"
umount "${chroot_dir}/sys"
umount "${chroot_dir}/dev"
echo Done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment