# [UFS] sector and alignment explanation



## sakoula (Oct 1, 2013)

Hi,

I have the following disk:


```
# gpart show ada0
=>        34  3907029101  ada0  GPT  (1.8T)
          34        2014        - free -  (1M)
        2048  3907026944     1  freebsd-ufs  (1.8T)
  3907028992         143        - free -  (71k)
```

on a cheap-cheap-cheap 4 port SATA controller:


```
atapci1: <SiI 3114 SATA150 controller> port 0xec00-0xec07,0xe880-0xe883,0xe800-0xe807,0xe480-0xe483,0xe400-0xe40f mem 0xfebffc00-0xfebfffff irq 16 at device 0.0 on pci6
ata3: <ATA channel> at channel 0 on atapci1
ata4: <ATA channel> at channel 1 on atapci1
ata5: <ATA channel> at channel 2 on atapci1
ata6: <ATA channel> at channel 3 on atapci1
....
ada0: 150.000MB/s transfers (SATA 1.x, UDMA5, PIO 8192bytes)
ada0: 1907729MB (3907029168 512 byte sectors: 16H 63S/T 16383C)
ada0: Previously was known as ad14
```

I really cannot find any answer to the following:

 Why does the disk start on the 34 sector? It should not start on 0 sector?
 How can I make the disk 4k aligned? Do I have to consider the 34 as the beginning of the disk? If this shows does this mean that 
	
	



```
2048  3907026944     1  freebsd-ufs  (1.8T)
```
 is NOT 4k aligned?
I have read in many places that a cheap RAID controller uses the last sector of a disk to store the magic data. How can I verify that?

Thanks!


----------



## SirDice (Oct 1, 2013)

sakoula said:
			
		

> - Why does the disk start on the 34 sector? It should not start on 0 sector?


Because it needs to leave a bit of room for partitiontables, bootsectors etc. Even though you may not use them for those disks it's better to leave the area untouched in case some other OS with a braindead fdisk overwrites the data. It's still possible though but so-called "dangerously dedicated disks" aren't supported anymore.



> - how can I make the disk 4k aligned? Do I have to consider the 34 as the beginning of the disk?


With regards to alignment, it will start at 0.


----------



## J65nko (Oct 1, 2013)

sakoula said:
			
		

> ```
> # gpart show ada0
> =>        34  3907029101  ada0  GPT  (1.8T)
> 34        2014        - free -  (1M)
> ...



The start of your freebsd-ufs partition is aligned on a 4K (8 x 512) boundary. Division by eight does not show any remainder after the decimal point:

```
[cmd=$] echo 'scale=4 ; 2048 / 8' | bc[/cmd]
256.0000
```
Another method is to use the modulo operator:

```
[cmd=$]echo '2048 % 8' | bc[/cmd]
0
```
The size of 3907026944 is also a multiple of 8 sectors:

```
[cmd=$]echo '3907026944 % 8' | bc[/cmd]
0
[cmd=$]echo 'scale=4 ;3907026944 % 8' | bc[/cmd]
0.0000
```

Being 4K aligned is a property of the hard disk. Because since many decades the default sector size has been 512 and not 4096, some disks present themselves as having sectors of 512 bytes:


```
[cmd=#] dmesg | grep ada2[/cmd]
ada2 at ata3 bus 0 scbus1 target 1 lun 0
ada2: <ST2000DM001-1CH164 CC24> ATA-8 SATA 3.x device
ada2: 300.000MB/s transfers (SATA 2.x, UDMA5, PIO 8192bytes)
ada2: 1907729MB (3907029168 512 byte sectors: 16H 63S/T 16383C)
ada2: quirks=0x1<4K>
ada2: Previously was known as ad7
```
The kernel reports 512 byte sectors, but also something strange or peculiar, a quirk.

Another method is to use diskinfo(8):

```
[cmd=#] diskinfo -v ada2[/cmd]
ada2
        512             # sectorsize
        2000398934016   # mediasize in bytes (1.8T)
        3907029168      # mediasize in sectors
        4096            # stripesize
        0               # stripeoffset
        3876021         # Cylinders according to firmware.
        16              # Heads according to firmware.
        63              # Sectors according to firmware.
        S1E15YPP        # Disk ident.
```
The stripesize of 4096 indicates a 4K sector disk.


----------



## sakoula (Oct 1, 2013)

Thanks!


----------

