Archive

Posts Tagged ‘arch’

Archlinux EC2 Public AMI

April 5th, 2009

I made 2 public Archlinux EC2 AMI.

Important Notes:

  • Most instructions on this page are outdated. All necessary packages have been included inside latest AMI
  • If you want to build your own AMI, I released this build script on gitub and aur packages.

Update 2011/1/25

Update kernel to 2.6.37 and fix account creation.

Update 2010/8/30

Change static ip to kernel dhcp and remove initrd

Update 2010/8/28

The network configure will be saved when image first time booted. If you want to revert to dhcp in case you need to rebuild or stop. You should run this

sudo /etc/rc.d/ec2 stop

I also changed default cflags, so if you want to recompile packages, you can use srcpac. For example

sudo abs extra/python
sudo srcpac -Sb python

Update 2010/8/21

Add a user arch with the same ssh key as root.

The hostname is now static, if you want to rebundle, make sure change HOSTNAME in rc.conf to myhost and remove last line of /etc/hosts.

Here is the new build script.

Update 2010/7/23:

Updated to BTRFS as root.

Update 2010/7/20:

Updated to pvgrub and EBS.
Here is the updated script to generate an EC2 EBS.
I also made an aur package for kernel26 with patch from gentoo and opensuse.

There is a simple patch for mainline kernel from amazon.

Arch AMI ID
i386 ami-5ae11133
x86_64 ami-84e111ed

Updates:
10/21/2009: Updated all packages and use ubuntu kernels. Here is the new AMI making script. Those kernels will load some unnecessary modules, you will need to unload them manually. I will update again if I can found more stable kernel.

They are very basic installation with just ssh. If you need tools like ec2-ami-tools or ec2-api-tools, you can find my aur packages here. Or you can add my private repo to your pacman.conf.

[iphash]
Server = http://static.iphash.net/public/i686/

or

[iphash]
Server = http://static.iphash.net/public/x86_64/

Then

pacman -Sy ec2-ami-tools ec2-api-tools

If you want to roll your own image. Here (outdated - see beginning of this post) is the script I used to make these AMIs.

If you wish to set hostname and domainname you can pass following script as instance user-data.

MYHOST=yourhost    #set your real hostname here
MYDOMAIN=yourdomain  #set your domainname here
 
sed -i s/myhost/$MYHOST/ /etc/rc.conf
hostname $MYHOST
 
echo "NISDOMAINNAME=\"$MYDOMAIN\"" >/etc/conf.d/nisdomainname
nisdomainname $MYDOMAIN
 
/etc/rc.d/syslog-ng restart
 
x=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
if [ $(echo $x|grep 404|wc -l) -eq 0 ]; then
cat <<EOF>/etc/hosts
#<ip-address>   <hostname.domain.org>   <hostname>
127.0.0.1               localhost.localdomain   localhost
$x  $MYHOST.$MYDOMAIN  $MYHOST
# End of file
EOF
fi
cp /etc/skel/.bash* /root/
Bookmark and Share  
 

Yejun Linux , , , ,

Deploy Archlinux Chroot onto VPS

March 31st, 2009

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

./miniarch

or

./miniarch i686

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

Bookmark and Share  
 

Yejun Linux , , , , ,