Update 7/20/2010: I updated this script to be more Lxc friendly. And I also made small patch to modify inittab rc.sysinit rc.shutdown for lxc. If you are not use using dhcpd, you will still need to modify /etc/rc.conf to setup default route.
Download the new script here, and Lxc patch.
Most VPS providers do not have archlinux image or allow changing root device like Linode does. Even though I am comfortable dealing with debian or ubuntu, but tiny difference between them are still annoying over the time. So I decide to install a mini chroot enviroment onto all of them to normalize linux enviroment.
If you want to use a ubuntu or debian chroot, you probably should read DebootstrapChroot. My method here only applys to Archlinux.
These scripts are only for Linux newbies like myself, who are lazy to type all that many commands every time. If you are a Linux guru or sysadmin, you may find this method trivial, insecure or laughable.
Prepare your local system
I assume you already have at least one working Archlinux system installed. First you need to install some necessary tools. If you do not have an archlinux installed, you may skip to last section of post and test the one I built.
pacman -Sy devtools lzma cpio
Devtools includes mkarchroot which is a script bootstrip a mini root similar to debootstrap. If you just run “mkarchroot miniroot base”, it can make you a working mini archlinux. But the default installation is huge about 500MB. You probably do not want all of them inside a VPS enviroment.
Lzma, Cpio are my choice of packaging, you can also use zip, tar, gzip or bzip2, and modify other parts of my script accordingly.
Make a working chroot
The first script is to make a compact mini root and compress it to a single file.
You can either download (outdated) or copy/paste following lines to a file name miniarch
#!/bin/bash
# 2009 Copyright Yejun Yang (yejunx AT gmail DOT com)
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
# http://creativecommons.org/licenses/by-nc-sa/3.0/us/
PACKS="sed gawk coreutils filesystem texinfo grep pacman \
module-init-tools wget curl net-tools procps nano tar cpio zip \
gzip bzip2 lzma psmisc initscripts iputils dnsutils iproute2 \
less dash which"
if [[ $1 == i686 ]]; then
ARCH=i686
else
ARCH=x86_64
fi
ROOT=mini_$ARCH
cat <<EOF > pacman.conf
[options]
HoldPkg = pacman glibc
SyncFirst = pacman
[core]
Server = ftp://mirror.cs.vt.edu/pub/ArchLinux/\$repo/os/$ARCH
Server = http://archlinux.mirrors.uk2.net/\$repo/os/$ARCH
Include = /etc/pacman.d/mirrorlist
[extra]
Server = ftp://mirror.cs.vt.edu/pub/ArchLinux/\$repo/os/$ARCH
Server = http://archlinux.mirrors.uk2.net/\$repo/os/$ARCH
Include = /etc/pacman.d/mirrorlist
[community]
Server = ftp://mirror.cs.vt.edu/pub/ArchLinux/\$repo/os/$ARCH
Server = http://archlinux.mirrors.uk2.net/\$repo/os/$ARCH
Include = /etc/pacman.d/mirrorlist
EOF
mkarchroot -C pacman.conf $ROOT $PACKS
chmod 666 $ROOT/dev/null
mknod -m 666 $ROOT/dev/random c 1 8
mknod -m 666 $ROOT/dev/urandom c 1 9
mknod -m 600 $ROOT/dev/console c 5 1
mkdir -m 755 $ROOT/dev/pts
mkdir -m 1777 $ROOT/dev/shm
echo nameserver 4.2.2.1 > $ROOT/etc/resolv.conf
echo nameserver 4.2.2.2 >> $ROOT/etc/resolv.conf
find $ROOT -depth -print | cpio -ov | lzma -5 > $ROOT.cpio.lzma
Modify PACKS= to packages you want to be installed.
You should also modify the Server= to whichever fast for you. I used rankmirrors to find out the fastest server.
Run this script
or
or both.
It will make a minimal working chroot for Archlinux under currect directory and pack them into a single file mini_x86_64.cpio.lzma or mini_i686.cpio.lzma. These two file should be around 40MB if everything worked correctly.
Copy these .lzma file to your webserver root. Now you can safely delete the working directory
Deploy to VPS
You can download the files you just made to your vps and unpack them. But I made simple script to do that.
You can download or copy/paste following line to a file name deploy
#!/bin/bash
# 2009 Copyright Yejun Yang (yejunx AT gmail DOT com)
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
# http://creativecommons.org/licenses/by-nc-sa/3.0/us/
ARCH=$(uname -m)
if [[ $ARCH != x86_64 ]]; then
ARCH=i686
fi
URL=http://YOURWEBSERVER/
if [ -e /var/chroot/mini_${ARCH} ]; then
echo "**** /var/chroot/mini_${ARCH} already exists. "
echo "**** You have to remove previous deployment."
exit 1
fi
mkdir -p /var/chroot
cd /var/chroot
echo "Start downloading ${URL}mini_${ARCH}.cpio.lzma , be patient ..."
wget -q -O - ${URL}mini_${ARCH}.cpio.lzma | lzma -d | cpio -idv
deploy_success () {
cat <<EOF
# Please add following lines to /etc/fstab
# Mount for chroot
proc /var/chroot/mini_${ARCH}/proc proc defaults 0 0
/dev /var/chroot/mini_${ARCH}/dev none rw,bind 0 0
/dev/pts /var/chroot/mini_${ARCH}/dev/pts none rw,bind 0 0
tmpfs /var/chroot/mini_${ARCH}/dev/shm tmpfs defaults 0 0
/lib/modules /var/chroot/mini_${ARCH}/lib/modules none ro,bind 0 0
/tmp /var/chroot/mini_${ARCH}/tmp none rw,bind 0 0
# Mini chroot has been deployed to /var/chroot/mini_${ARCH} , please try
# sudo cp /etc/resolv.conf /var/chroot/mini_${ARCH}/etc/resolv.conf
# sudo mount -a
# sudo chroot /var/chroot/mini_${ARCH}
EOF
}
deploy_fail () {
echo "**** Mini chroot deployment failed"
echo -n "**** Clean up /var/chroot/mini_${ARCH} ...."
rm -rf /var/chroot/mini_${ARCH}
echo "done"
}
if [[ -f /var/chroot/mini_${ARCH}/.arch-chroot ]]; then
deploy_success
else
deploy_fail
fi
Change URL= to your own webserver.
Before you running this script on your target machine. Make sure lzma, wget and cpio are installed. If you are using ubuntu, you can run
sudo aptitude update
sudo aptitude install lzma wget cpio
Running this script will deploy a mini chrootable archlinux in to /var/chroot/mini_i686 or /var/chroot/mini_x86_64. The unpacked size will be around 200MB.
To simplify this process, you can copy this file to webserver as well.
wget -q -O - http://yourwebsite/deploy |sudo bash
done.
For lazy people or testing only
If you are so lazy to make your own archlinux mini root or you don’t have a working archlinux, you may test my prebuilt mini root by running following line, you will still need lzma, cpio and wget on your target machine.
wget -q -O - http://bit.ly/iZzq |sudo bash
Disclaimer
I DO NOT guarantee the correctness of these script and my prebuilt chroot. Be caution running any command with sudo. You may not hold me responsible for anything happened to your system.
Updates:
April 5, 2009: changed /bin/sh to /bin/bash
Yejun Linux arch, archlinux, chroot, cloud, mkarchroot, vps