Thursday, November 11, 2010

EFI Partition

How to format EFI partition on Mac OS X (use it for boot partition)?
- This is useful if the hard disk is formatted with GUID/GPT partition table.
- On Mac OS X Leopard (10.5 to 10.5.X)
diskutil eraseVolume "HFS+" "EFI" /dev/diskXs1
- On Mac OS X Snow Leopard (10.6 to 10.6.X)
newfs_hfs -v EFI /dev/diskXs1
- Note: change the "X" with hard disk number (e.g. 0, 1, etc). Check with diskutil list.

How to restore or recover EFI partition?
- This is useful if your EFI partition auto-mounted (it is not supposed to) whenever boot into Mac OS X.
- This happen when you use "diskutil eraseVolume "HFS+" "EFI" /dev/diskXs1" on Snow Leopard to format the EFI partition.
- Steps are:-
1) Boot with Windows Vista or 7 installation DVD if Windows is not installed or corrupted.
2) Follow on-screen instruction, just like you're going to install Windows.
3) Select Custom.
4) You will see the hard disk partitions. Delete or remove the first 200MB partition. Sometime there are more than one 200MB partition at the beginning of the hard disk partitions, delete all of it (at the beginning only). Reboot.
5) Boot into OSX.
6) Execute these commands (assuming it only one hard disk - /dev/rdisk0. Press enter for each commands):-
fdisk -e /dev/rdisk0
Enter 'help' for information
fdisk: 1> p
Disk: /dev/rdisk0    geometry: 19457/255/63 [312581808 sectors]
Offset: 0    Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
 1: EE 1023 254  63 - 1023 254  63 [         1 -     409639]
 2: AF 1023 254  63 - 1023 254  63 [    409640 -  156290904] HFS+       
*3: 07 1023 254  63 - 1023 254  63 [ 156962688 -  155356936] HPFS/QNX/AUX
 4: 00    0   0   0 -    0   0   0 [         0 -          0] unused     
fdisk: 1> e 1
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
 1: EE 1023 254  63 - 1023 254  63 [         1 -     409639]
Partition id ('0' to disable)  [0 - FF]: [EE] (? for help) EE
Do you wish to edit in CHS mode? [n] n
Partition offset [0 - 312581808]: [63] 1
Partition size [1 - 312581807]: [312581807]
fdisk:*1>  w

(just enter these)
7) Reboot
- Now the EFI partition should be look like this (Type = EFI):-
diskutil list/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *160.0 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS OSX                     80.0 GB    disk0s2
   3:       Microsoft Basic Data                         79.5 GB    disk0s3

- Use this command to format the EFI partition:-
newfs_hfs -v EFI /dev/disk0s1

Good luck! :D

Monday, January 25, 2010

Simulation Cisco ASA with QEMU and GNS3

My howto is based on this HOWTO:

Credit to Jeremy Grossmann.

List of tools/software use in this simulation
  • VMware Worksatation
  • GNS3 0.7 RC1
  • QEMU v 0.9.0 
  • Cisco ASA image: asa802-k8.bin

Referring to the above diagram, VMachine1_HOST will be the host of  ASA which we're going to simulate. I chose CentOS-5.4-x86_64 for the VMachine1_HOST. Any other linux OS should be ok. VMachine1_HOST and VMachine2 (optional) actually guest OS, installed in VMware Worksation. Any other virtual machine software, e.g. VirtualBox, should be ok too.

To inter-connect between VMware guest OS with GNS3 virtual network, we need to:
  • Configure VMachine1_HOST to use "Host-only" network interface (vmnet1-7 & vmnet9).
  • Both VMachine1_HOST VMware guest OS and "Cloud" need to configured to use same vmnet network interface. Same rule apply to VMachine2 VMware guest OS and it "Cloud".
 MaulanaSS "Cloud" is to enable ASA to communicate with physical network.

However, this simulation is not perfect because only "one" side is connected to ASA. It should be two side connected to ASA which are "inside" & "outside" network. But, until ASA ver 8 can be fully simulated using GNS3, this is the closest simulation we can get.

The next step is to get an initrd and a Linux kernel from your ASA image to use them with Qemu and also fix the initrd for our needs. The initrd is zipped and archived in the ASA image, asa802-k8.bin, we have to extract it.

These steps need to be done at VMachine1_HOST:
1) Create an hexadecimal dump of your image:
hexdump -C asa802-k8.bin > asa802-k8.hex

2) Search for the ZIP header:
grep "1f 8b 08 00 1d" asa802-k8.hex
001228b0  1f 8b 08 00 1d 3d 73 46  00 03 ec 3a 6d 54 14 57  |…..=sF…:mT.W|
We can see that the ZIP file starts at offset 1228b0.

3) Let’s find the image size:
ls -la asa802-k8.bin
-rwxr-xr-x  1 root  staff  14524416 26 Nov 20:14 asa802-k8.bin
14524416 bytes.

4) Now we need to find out where in the file we can start extracting the ZIP part.
echo "14524416 ; ibase=16 ; last - 1228B0" | bc | tail -n 1

5) Extract the zipped part of the ASA image:
tail -c 13334352 asa802-k8.bin > asa802-k8.gz

6) Decompress it with gzip:
gzip -d asa802-k8
gzip: asa802-k8.gz: decompression OK, trailing garbage ignored

7) Make a temp directory and go into it so we can extract the files contained in the uncompressed archive file (the initrd):
mkdir tmp ; cd tmp

8) Now extract the archive with cpio (you must have the administrator rights to successfully extract device files).
cpio -i --no-absolute-filenames --make-directories < ../asa802-k8

9) Copy the Linux kernel to your previous directory:
cp vmlinuz ../asa802-k8.kernel

10) Before compressing back the initrd, create the following script in asa/scripts/
(Modified by me for better execution)

if test ! -e /mnt/disk0/lina_monitor
# fdisk /dev/hda << EOF
# mkdosfs -F 16 /dev/hda1

# Create based on starting cylinder parameter, etc
echo "d" >  /asa/scripts/
echo "o" >> /asa/scripts/
echo "n" >> /asa/scripts/
echo "p" >> /asa/scripts/
echo "1" >> /asa/scripts/
echo $2  >> /asa/scripts/
echo " " >> /asa/scripts/
echo "t" >> /asa/scripts/
echo "4" >> /asa/scripts/
echo "w" >> /asa/scripts/

/bin/umount $MNT > /dev/null 2> /dev/null
echo -n "Initializing partition - "
/sbin/fdisk $DEV < /asa/scripts/ > /dev/null 2> /dev/null
echo "done!"
/sbin/mkdosfs -F 16 /dev/hda1
echo ""
echo "System tables written to disk"
 mount -t vfat -o umask=0000,noatime,check=s,shortname=mixed /dev/hda1 /mnt/disk0
 cp /asa/bin/lina /mnt/disk0/lina
 cp /asa/bin/lina_monitor /mnt/disk0/lina_monitor
modprobe e100
modprobe e1000
ifconfig eth0 up
ifconfig eth1 up
ifconfig eth2 up
ifconfig eth3 up
ifconfig eth4 up
ifconfig eth5 up
if test $FIRST_START = yes
 echo ""
 echo ""
 echo "This is your first boot, please wait about 1 min and then type the following commands:"
 echo "cd /mnt/disk0"
 echo "/mnt/disk0/lina_monitor"
 echo ""
 echo "Please note to use the following command under ASA to save your configs:"
 echo "copy run disk0:/.private/startup-config"
 echo ""
# exit
cd /mnt/disk0

11) Set asa/scripts/ file executable:
chmod +x asa/script/
12) In order for the script to be loaded at startup, edit etc/init.d/rcS and change /asa/bin/lina_monitor with /asa/scripts/

13) Now you can compress all the file and have the initrd ready to use in Qemu:
find . | cpio -o -H newc | gzip -9 > ../asa802-k8.initrd.gz

Starting ASA with QEMU
Make sure these command executed in the same directory that contain ASA initrd, asa802-k8.initrd.gz,
& kernel, asa802-k8.kernel.
1) Create FLASH file (QEMU virtual hard disk):
qemu-img create FLASH 256M

2) Create custom script, called, to automatically bring up "tap0" interface and enable TCP/IP forwarding at VMachine1_HOST:
ifconfig tap0
echo 1 > /proc/sys/net/ipv4/ip_forward

3) Start ASA using QEMU command:
(add parameter to support communication with host computer)
qemu -m 256 -hda FLASH -kernel asa802-k8.kernel -initrd asa802-k8.initrd.gz -nographic -append "auto nousb console=ttyS0,9600 bigphysarea=16384 ide1=noprobe hda=980,16,32" -net "nic,vlan=0,macaddr=00:00:ab:cd:ef:00,model=pcnet" -net "nic,vlan=1,macaddr=00:00:ab:cd:ef:01,model=pcnet" -net "nic,vlan=2,macaddr=00:00:ab:cd:ef:02,model=pcnet" -net "nic,vlan=3,macaddr=00:00:ab:cd:ef:03,model=pcnet" -net "nic,vlan=4,macaddr=00:00:ab:cd:ef:04,model=pcnet" -net "nic,vlan=5,macaddr=00:00:ab:cd:ef:05,model=pcnet" -net "tap,vlan=0,ifname=tap0," -serial "telnet::3000,server,nowait" -hdachs "980,16,32"
or you can create script file, e.g. called

As you can see, the first ASA NIC port (interface Ethernet 0/0) and VMachine1_HOST tap0 interface is in the same VLAN (vlan=0). So, ASA interface Ethernet 0/0 IP address should be in the same subnet with VMachine1_HOST tap0 IP address. This way ASA can communicate with VMachine1_HOST computer and physical network.

If you decided to simulate ASA with another appliance, e.g. Cisco router 3640, you can use diagram at the beginning of the HOWTO as an example.

How to save configuration on ASA
For unknown reason, we cannot use write command to save configuration. However, there is workaround to overcome this issue. copy command will be use instead to save configuration:
copy run disk0:/.private/startup-config

Good luck!!!