July 30, 2012

How to create ultra small Linux machine on Amazon EC2

If you are using amazon aws services, you know that by default Amazon will let you use virtual machine with 8GB root filesystem. But sometimes you are using under 2GB of actual space, so why not to shrink this machine and pay only for 2GB instead of 8GB? This mini-tutorial is for that.

1. Using EC2 console ( create new volume (Elastic block storage->Volumes) with size of 2GB and attach it to your running machine. Dont forget to use same availability zone as current root volume is running.

2. New disk should be seen by OS:

[root@ip-10-202-45-149-vpn1 /]# fdisk -l /dev/xvdf

Disk /dev/xvdf: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/xvdf doesn't contain a valid partition table

3. Check LABEL and UUID of your current root FS.

[root@ip-10-202-45-149-vpn1 ~]# blkid
/dev/xvda1: LABEL="/" UUID="f0b0a1ec-20ae-4ee1-8e11-5fc79aa08f44" TYPE="ext4"

4. Create ext4 filesystem on your new disk with same parameters

[root@ip-10-202-45-149-vpn1 ~]# mkfs.ext4 -L / -U f0b0a1ec-20ae-4ee1-8e11-5fc79aa08f44 /dev/xvdf
mke2fs 1.42 (29-Nov-2011)
Filesystem label=/
OS type: Linux
Block size=4096 (log=2)
Writing superblocks and filesystem accounting information: done

5. Mount disk and copy OS directories to that disk:

[root@ip-10-202-45-149-vpn1 ~]# mkdir /tmp/1
[root@ip-10-202-45-149-vpn1 ~]# mount /dev/xvdf /tmp/1
[root@ip-10-202-45-149-vpn1 /]# cd /
[root@ip-10-202-45-149-vpn1 /]# ls
bin  boot  cgroup  dev  etc  home  lib  lib64  local  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[root@ip-10-202-45-149-vpn1 /]# tar cf - bin boot etc home lib lib64 local media mnt opt root sbin usr var | (cd /tmp/1; tar xf -)
tar: var/run/dbus/system_bus_socket: socket ignored

Ignore that warning :)

6. Create missing directories, on tmp dir set sticky bit

[root@ip-10-202-45-149-vpn1 /]# cd /tmp/1
[root@ip-10-202-45-149-vpn1 1]# ls
bin  boot  etc  home  lib  lib64  local  lost+found  media  mnt  opt  root  sbin  usr  var
[root@ip-10-202-45-149-vpn1 1]# mkdir cgroup dev proc selinux srv sys tmp
[root@ip-10-202-45-149-vpn1 1]# chmod 1777 tmp
[root@ip-10-202-45-149-vpn1 1]# pwd
[root@ip-10-202-45-149-vpn1 1]# ls
bin  boot  cgroup  dev  etc  home  lib  lib64  local  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var

7. Turn off virtual machine and detach both old 8GB root volume and 2GB volume. Attach 2GB volume again, but now with path /dev/sda1. Turn on machine and check if is everything ok (it should be seen as /dev/xvda1 now). You can now freely delete old 8GB volume. You are done :)

[root@ip-10-202-45-149-vpn1 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1            2.0G 1007M  935M  52% /
tmpfs                 298M     0  298M   0% /dev/shm

No comments:

Post a Comment