Saturday, June 11, 2011

The story of storage: hard disk


Hard disk is a kind of storage that uses a concentric stack of disks or "platters" to record data. It is a block device, that says it reads and writes data in fixed-size blocks. Generally, the block size is 512 bytes. So from a software engineer's point of view, a hard disk is just a sequence of continuous blocks of data, and you can visit any of them freely using some kind of address mechanism.


1 MBR

A master boot record (MBR) is the first sector of a hard disk. It serves mainly two functions:
  • Holds a disk's primary partition table.
  • Holds the bootstrapping code. After BIOS initializing the PC, it will load this sector into memory and pass execution to it.
The structure of MBR is as follows:

OffsetDescriptionSize
0x0000Code area440
0x01B8Disk signature4
0x01BCUsually NULL (0x0000)2
0x01BEPrimary partition table (Fore entries, each 16 bytes)64
0x01FEMBR signature (0x55, 0xAA)2

Disk signature is used to uniquely indentify the boot disk by the OS and further by userland processes. But after the introduction of EDDdisk signature can be omitted and code areacan be extended to a length of 446.
By convention, there are exactly four primary partition table entries in the MBR Partition Table scheme. Both the partition length and partition start address are stored as 32-bit quantities. Because the block size is 512 bytes, this implies that neither the maximum size of a partition nor the maximum start address (both in bytes) can exceed 2^32 * 512 bytes, or 2 TiB.
See Partition Table, for more info.


2 CHS

Cylinder-head-sector, also known as CHS, was an early method for giving addresses to each physical block of data on a hard disk drive. Though CHS values no longer have a direct physical relationship to the data stored on disks, pseudo CHS values (which can be translated by disk electronics or software) are still being used by many utility programs.
  • Head: Data is written to or read from a platter of the hard disk by a device called head. Usually, two heads are used to manipulate the data on both surfaces of a platter.
  • Track, Sylinder: A platter surface is composed of concentric circles. They are called tracks. All information stored on a hard disk is recorded in tracks. The tracks are numbered, starting from 0, starting at the outside of the platter and increasing as you go in. All tracks that have the same number and span across each platter surface form a sylinder.
  • Sector: A track is divided into sectors that are the base units managed by a hard disk driver.
So each sector can be addressed by a three-dimensional coordinate system (CHS). The number of sectors a hard disk holds is:
cylinders * heads * sectors
In earlier hard drive designs, the number of sectors per track was fixed and because the outer tracks on a platter have a larger circumference than the inner tracks, space on the outer tracks was wasted. The number of sectors that would fit on the innermost track constrained the number of sectors per track for the entire platter. However, many of today's advanced drives use a formatting technique called Multiple Zone Recording to pack more data onto the surface of the disk. Multiple Zone Recording allows the number of sectors per track to be adjusted so more sectors are stored on the larger, outer tracks. By dividing the outer tracks into more sectors, data can be packed uniformly throughout the surface of a platter, disk surface is used more efficiently, and higher capacities can be achieved with fewer platters. Not only is effective storage capacity increased by as much as 25 percent with Multiple Zone Recording, but the disk-to-buffer transfer rate also is boosted. With more bytes per track data in the outer zones is read at a faster rate.
However, as I metioned before, CHS values no longer have a direct physical relationship to the data stored on disks, the pseudo CHS still uses a uniform schema. The total length of CHS is 24 bits. Below is the detailed limit. See Partition Table.

NameBitsStart FromEnd LimitTotal Number
Cylinder10010231024
Head80254255
Sector616363

So when use the CHS address schema, a hard disk could be no lager than:
(1024 * 255 * 63) * (512) = 8,422,686,720 bytes (about 8.4 GB)


3 LBA

Logical block addressing (LBA) is a common scheme used for specifying the location of blocks of data stored on computer storage devices, generally secondary storage systems such as hard disks. The term LBA can mean either the address or the block to which it refers. Logical blocks in modern computer systems are typically 512 or 1024 bytes each. ISO 9660 CDs (and images of them) use 2048-byte blocks. LBA is a particularly simple addressing scheme; blocks are located by an index, with the first block being LBA=0, the second LBA=1, and so on.
CHS tuples can be converted to LBA addresses using the following formula:
LBA(C,H,S) = ((C * heads_num) + H) * sectors_per_track + S - 1


4 Partition Table

As described before, the partition table in MBR can hold at most four records. Each partion can't exceed 2 TiB. To alleviat this capacity limitation, an new partition schema called GUID Partition Table (GPT) is introduced in industry. See more at UEFI.
Follows is the layout of one 16-byte partition record:

OffsetLengthDescription
0x001status (0x80 = bootable, 0x00 = non-bootable, other = invalid)
0x013CHS address of first sector in partition
0x041partition type
0x053CHS address of last sector in partition
0x084LBA of first sector in the partition
0x0C4number of sectors in partition, in little-endian format

Most of the time, LBA is used to find a partition. But specification says: if a partition's start block or end block or both are under the 8.4 GB limitation, CHS address should also be correctly record. Otherwise, CHS fields have some kind of default values.
Partition type is used to label the file system used on this partition. For example, the code for linux ext2 is 0x83 and linux swap is 0x82. You can see a list of partition types by sfdisk -T. A hard disk can have at most four primary partitions for there are only four entries in the primary partition table. The following figure gives an example of a hard disk holding two primary partitions.


If you ls /dev/sda* or ls /dev/hda*, you may see the results as follows:
/dev/sda /dev/sda1 /dev/sda2   or


/dev/hda /dev/hda1 /dev/hda2
Please note:
  1. The address mode used in figure is LBA. In CHS dialect, it should be Sector 1 - Sector 63.
  2. The first partition normally starts at sector 63 (LBA), that is just after the first track. The first 63 sectors (first track) can be used for other purpose such as holding bootloader code.
  3. Partition can start and end at any places as soon as there are no overlappings. And may not cover all the space on a hard disk.
To get more partitions, we can subpartition a primary partition into several logical partitions. The primary partition used to house the logical partitions is called an extended partition and it has its own file system type (0x05 extended type). See more at Extended partition.

The story of storage: extended partition



Extended Partition

As we mentioned in Hard Disk, a hard disk can have at most four primary partitions. If we want more partitions, we can change one primary partition into an extended one by subdividing it into logic ones and setting the partition type to 0x5 (extended type).
Like Master Boot Record (MBR) describing a hard disk, a Extended Boot Record (EBR) is used for a logic partition. However, there is one EBR for each logic partition and all the logic partitions in a extended partition is linked one by one using two partition table records in MBR.
EBRs have essentially the same structure as the MBR; except only the first two entries of the partition table are supposed to be used.
The structure of EBR is as follows:

OffsetDescriptionSize
0x0000Generally unused446
0x01BEPartition Table's First entry16
0x1CEPartition Table's Second entry16
0x1DEUnused32
0x1FEMBR signature (0x55, 0xAA)2

Follows is the layout of one 16-byte partition record:

OffsetLengthDescription
0x001status (0x80 = bootable, 0x00 = non-bootable, other = invalid)
0x013CHS address of first sector in partition
0x041partition type
0x053CHS address of last sector in partition
0x084Starting Sector
0x0C4Number of sectors in partition, in little-endian format

The first entry of an EBR partition table points to the logical partition belonging to that EBR:
  • Starting Sector = relative offset between this EBR sector and the first sector of the logical partition This will be the same value for each EBR on the same hard disk; usually 63.
  • Number of Sectors = total count of sectors for this logical partition
The second entry of an EBR partition table will contain zero-bytes if it's the last EBR in the extended partition; otherwise, it points to the next EBR in the EBR chain:
  • Starting Sector = relative address of next EBR within extended partition in other words: Starting Sector = LBA address of next EBR minus LBA address of extended partition's first EBR
  • Number of Sectors = total count of sectors for next logical partition, but count starts from the next EBR sector
The following figure gives an example of a hard disk holding an extended partition and a primary partition. There are two logic partitions in the extended partition.

Cassandra-Setup


       http://github.com/ericflo/twissandra                     
              
        
        
        basic one node cluster
        ======================

Requirements
------------
  * Java >= 1.6 (OpenJDK and Sun have been tested)


  * tar -zxvf apache-cassandra-$VERSION.tar.gz
  * cd apache-cassandra-$VERSION
  * sudo mkdir -p /var/log/cassandra
  * sudo chown -R `whoami` /var/log/cassandra
  * sudo mkdir -p /var/lib/cassandra
  * sudo chown -R `whoami` /var/lib/cassandra

Now that we're ready, let's start it up!

  * bin/cassandra -f


Now let's try to read and write some data using the command line client.

  * bin/cassandra-cli --host localhost --port 9160

To Create Tables
----------------

cassandra> set Keyspace1.Standard2['jsmith']['first'] = 'John'
  Value inserted.
  cassandra> set Keyspace1.Standard2['jsmith']['last'] = 'Smith'
  Value inserted.
  cassandra> set Keyspace1.Standard2['jsmith']['age'] = '42'
  Value inserted.
  cassandra> get Keyspace1.Standard2['jsmith']
    (column=age, value=42; timestamp=1249930062801)
    (column=first, value=John; timestamp=1249930053103)
    (column=last, value=Smith; timestamp=1249930058345)
  Returned 3 rows.
  cassandra>


                Configuring a Cassandra Cluster
                ===============================

Here is what I did to create a running Cassandra Cluster.

Stop individual Cassandra instances
Re-created data and log directories (I did this just to ensure a clean slate)
I added to my local hosts file two aliases for my servers (cass01 and cass02). This helped in the following step.
Three changes are needed to the default conf/storage-conf.xml file on my first server.
Change from localhost to cass01
Change from localhost to cass01
Change from 127.0.0.1 to cass01
On my second server I changed the and accordingly to cass02 and made cass01
Started Cassandra servers and tested successfully using the set …/get Keyspace1.Standard1['jsmith'] example. I was able to connect to both hosts via cassandra-cli and see the results created on just one node. I was able to create data on the second node and view on the first node.
A new command is available to describe your cluster.

$ bin/nodeprobe -host cass01 ring
Address       Status     Load          Range                                      Ring
                                       148029780173059661585165369000220362256
192.168.100.4 Up         0 bytes       59303445267720348277007645348152900920     |<--|
192.168.100.5 Up         0 bytes       148029780173059661585165369000220362256    |-->|
Now with my first introduction successful, time to start using and seeing the true power of using Cassandra.


Realtime Example
================

On nk114 [Master]
-----------------
vi  conf/storage-conf.xml

Test Cluster

      nk114
      nk75

nk114ListenAddress>

nk114ThriftAddress>


Steps to run
-------------
 bin/cassandra -f
 bin/cassandra-cli --host nk114 --port 9160

to check the cluster status
 ---------------------------
 bin/nodeprobe -host nk114 ring

On nk75 [Slave]
---------------
vi  conf/storage-conf.xml

Test Cluster

      nk75

nk75ListenAddress>

nk75ThriftAddress>


Steps to run
------------

 bin/cassandra -f
 bin/cassandra-cli --host nk114 --port 9160
                
 to check the cluster status
 ---------------------------
 bin/nodeprobe -host nk114 ring

"clocksource tsc unstable" solution


cat /sys/devices/system/clocksource/clocksource0/available_clocksource

Try these options with grub command line 
clocksource=acpi_pm
clocksource=hpet
acpi=off
noapic
nolapic
notsc

Configure TCP/IP from the Command Prompt Windows

1. To  view your TCP/IP settings

netsh interface ip show config

2. Configure your computer's IP address and other TCP/IP related settings
netsh interface ip set address name="Local Area Connection" static 192.168.0.100 255.255.255.0 192.168.0.1 1

3. Export your current IP settings

netsh -c interface dump > c:'location1.txt

4. Import your IP settings

netsh -f c:'location1.txt

5. Automatically obtain an IP address from a DHCP server:

netsh interface ip set address "Local Area Connection" dhcp

6. Configure DNS and WINS addresses.

netsh interface ip set dns "Local Area Connection" static 192.168.0.200
netsh interface ip set wins "Local Area Connection" static 192.168.0.200

Or, if you want, you can configure your NIC to dynamically obtain it's DNS settings:

netsh interface ip set dns "Local Area Connection" dhcp


7. To setup Static IP Address:

From the command prompt:
1. Type 
C:\Users\Administrator> netsh interface ipv4 show interfaces
Idx  Met   MTU   State        Name
—  —  —–  ———–  ——————-
  1   50 4294967295  connected    Loopback Pseudo-Interface 1
 10   20   1500  connected    Local Area Connection
This should show the Network Connections. We are looking for the name here. On mine, I have one LAN interface and is named as “Local Area Connection”
2. To set a static IP Address type the following command
C:\Users\Administrator>netsh interface ipv4 set address name=”Local Area Connect
ion” source=static address=192.168.0.5 mask=255.255.255.0 gateway=192.168.0.1
The syntax is
netsh interface ipv4 set address name=”” source=static address= mask= gateway=
Where:
ID is the name of the LAN Connection
StaticIP is the static IP address that you are setting
SubnetMask is the subnet mask for the IP address
DefaultGateway is the default gateway
3. Now set the DNS Servers one at a time with the followind command. For each DNS server, increase the index number.
C:\Users\Administrator>netsh interface ipv4 add dnsserver name=”Local Area Conne
ction” address=192.168.0.1 index=1
C:\Users\Administrator>netsh interface ipv4 add dnsserver name=”Local Area Conne
ction” address=192.168.0.10 index=2
The syntax is
netsh interface ipv4 add dnsserver name=”” address=index=1
Where:
ID is the name of the Network Connection
DNSIP is the IP address of your DNS server
This should do. To confirm, do an “ipconfig”
Ethernet adapter Local Area Connection:
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Broadcom 440x 10/100 Integrated Controller
   Physical Address. . . . . . . . . : 00-1D-09-D4-2C-8F
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   IPv4 Address. . . . . . . . . . . : 192.168.0.5(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1
   DNS Servers . . . . . . . . . . . : 192.168.0.1
                                       192.168.0.10
                                       127.0.0.1
   NetBIOS over Tcpip. . . . . . . . : Enabled
Set IP through DHCP Server
To set the DHCP Server, from the command line
C:\Users\Administrator> netsh interface ipv4 set address name=”Local Area Connection” source=dhcp
Syntax is
netsh interface ipv4 set address name=”ID” source=dhcp
where ID is the name of the Network Connection