1

I've been reading about using a 4096Byte logical block size if my hard drive uses a physical 4096Byte block size for its sectors. So I just tried to create one on a spare hard drive, and I don't understand the warning at the final prompt. See below.

~$ cat /sys/class/block/sda/queue/physical_block_size
4096
~$ cat /sys/class/block/sda/queue/logical_block_size
512
server@Server:~$ sudo parted /dev/sda
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: ATA WDC WD30EFRX-68A (scsi)
Disk /dev/sda: 3001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt

Number  Start  End  Size  File system  Name  Flags

(parted) mklabel gpt
Warning: The existing disk label on /dev/sda will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes                                                               
(parted) mkpart primary 4096B 3001GB
Warning: You requested a partition from 4096B to 3001GB.                  
The closest location we can manage is 17.4kB to 3001GB.
Is this still acceptable to you?
Yes/No? n

Why can't I just use a 4096 block size?

thomasrutter
  • 36,068
  • 10
  • 86
  • 105
john smith
  • 2,963
  • 10
  • 37
  • 53
  • Carefully reread: It's the start of the partition not the size block. – solsTiCe Nov 23 '15 at 22:05
  • Oh. How do I set the block size? Also where can I find info about where to start my drive from? Surely it should just default to 0? – john smith Nov 23 '15 at 22:16
  • You normally have to have space for gpt partitioning at beginning of drive. All new systems start now at sector 2048. Better to use gdisk or gparted as they automatically set everything correctly if you are not 100% sure of what you are doing. http://askubuntu.com/questions/201164/proper-alignment-of-partitions-on-an-advanced-format-hdd-using-parted And: http://www.rodsbooks.com/gdisk/ – oldfred Nov 23 '15 at 22:38
  • Well I don't want to just "rely" on that. I want to understand how it works, and why my config is wrong. – john smith Nov 23 '15 at 22:43

4 Answers4

4

The physical and logical block (sector) sizes are both determined by your disk hardware and cannot be changed. The vast majority of hard disks today use 512-byte logical sectors, although I've heard of some high-end disks that now use 4096-byte logical sectors. Some external enclosures also re-map the logical sector size to be 4096 bytes.

The combination of 512-byte logical sectors with 4096-byte physical sectors can result in performance problems if partitions are not aligned on multiples of 8 sectors. Most partitioning tools today handle this automatically -- in fact, they typically align on multiples of 2048 sectors (1 MiB), which of course is a multiple of 8 sectors and so works well. (Using 2048-sector alignment also works with many RAID and SSD products that require alignment on power-of-2 values higher than 8, which is why 2048 is the default.) Older tools aligned on "cylinder" boundaries. In the distant past (1980s-ish), cylinder alignment produced performance benefits; but then "cylinders" became a convenient fiction, then an inconvenient fiction, then a serious problem. Linux tools adjusted to these changes rather slowly, but today all the major programs ignore cylinders and use 2048-sector alignment by default.

You can adjust the alignment value to anything you like with gdisk -- you must type x to get to the experts' menu, then type d to change the alignment value. Note that this option is on the experts' menu for a reason: If you don't fully understand what you're doing, you're more likely to cause problems than solve them by using this feature. Even if you're an expert, the space savings from changing from 2048- to 8-sector alignment will be trivial (under 1MiB), so even I almost never do this. (I'm gdisk's author.)

Rod Smith
  • 43,599
  • 7
  • 62
  • 102
  • Wow, so glad you could share your help Rod. Given your understanding I'd like to elaborate on a few areas around this topic. Why does partitioning software measure computer units in mebibytes? Also, why do partitions align on multiples of 2048. I'm not sure what you mean by 2048 either, what is this referring to? The way I see it, is that surely if you have a 4096byte phsyical sector, you want your partitions to align at 4096 bytes? – john smith Nov 24 '15 at 07:48
  • 1
    Nobody is saying 2048 bytes. On a drive with 4096 byte sectors, you want to align the starts of partitions to multiples of 4096 bytes, so that clusters in the partition begin on sector boundaries on the actual drive. You are getting confused by the part about 2048 sectors, so ignore that - the important part is that partitions begin on a multiple of 4096 bytes. You should consider using gparted so you don't need to worry about any of this. – thomasrutter Nov 24 '15 at 11:15
  • 1
    The mention of 2048 *sectors* was a passing reference to the fact that higher level partitioning tools tend to align partitions at 1MiB boundaries. Assuming the classic sector size of 512 bytes, that's 2048 sectors. You don't need to align at 1MiB boundaries in most cases; for old fashioned flash drives and SSDs without sector-level wear-levelling or dynamic reallocation it can help a little. – thomasrutter Nov 24 '15 at 11:33
  • John, disk storage is allocated in chunks of one sector (sometimes called a "block"), which has traditionally been 512 bytes in size. Thus, partition table data structures are defined in terms of sectors/blocks. To get sizes in bytes, you must multiply the number of sectors by the sector size (512 bytes). If you forget to do this, you'll become hopelessly confused. Advanced Format disks increase the physical sector size to 4096 bytes, but (to date) have maintained a 512-byte *logical* sector size for backwards compatibility purposes. – Rod Smith Nov 24 '15 at 13:50
  • Concerning mebibyte units, see [this page](http://stopabusingsiprefixes.org/stopabusing.html) for information on SI vs. IEEE-1541 units. In brief, IEEE-1541 units are slightly larger than their closest SI counterparts, which leads to increasing confusion as disk sizes have grown. – Rod Smith Nov 24 '15 at 13:52
  • thomasrutter, I don't understand your statement, "partitioning tools tend to align partitions at 1MiB boundaries. Assuming the classic sector size of 512 bytes, that's 2048 sectors." How do you do the maths here? How did you arrive at the conclusion that 1MiB (whatever a mebibyte is) = 2048 sectors if the logical sector is 512bytes. How!? – john smith Nov 27 '15 at 21:22
  • Rod I am confused by your statement of "To get sizes in bytes, you must multiply the number of sectors by the sector size (512 bytes)". I don't know how many sectors I have? Also, are these supposed to be the logical sectors of the physical sectors? This is so confusing. Also, why is the alignment every 2048 sectors? Surely you want it to be aligned every 8 sectors to match the 512byte logical sector to the 4096byte physical sector; thus aligned every 4096 bytes. – john smith Nov 27 '15 at 21:25
  • Typically, one logical sector is 512 bytes. Thus, the conversion factor is to multiply sectors by 512 to get bytes or to divide bytes by 512 to get sectors. Since 2048 is a multiple of 8 aligning on 2048-sector boundaries also aligns on 8-sector boundaries. As noted in my answer, some other technologies (RAID and SSD) have their own alignment requirements that are usually on power-of-2 multiples of sectors well above 8 sectors (typically between 256 and 1024 sectors, but sometimes as high as 2048 sectors). That's why 2048 is the new standard -- it works for (almost) everything. – Rod Smith Nov 29 '15 at 02:17
1
Warning: You requested a partition from 4096B to 3001GB.                  
The closest location we can manage is 17.4kB to 3001GB.
Is this still acceptable to you?

What this is saying is that due to other things at the beginning of the drive (metadata about the partitioning scheme or similar) the closest it can get to the start of the disk is 17.4kB in. You don't want this; you want it to start on a 4KiB boundary. So you should cancel and try again, this time requesting to start the partition on the first 4096B boundary beyond that point (try 20480B).

The reason you have to go through this manually is you're using a low level partitioning tool. A higher level tool like gparted will automatically align the start of a partition to a round number for you. These days many such tools will align partitions to a multiple of 1MiB (1024x1024 bytes) as this neatly solves both the 512byte, 4096 byte boundaries as well as aligning with blocks on things like USB flash drives.

thomasrutter
  • 36,068
  • 10
  • 86
  • 105
  • I don't understand this at all. Why would I want my partition to align at 2048bytes if I have a physical block size of 4096 bytes? – john smith Nov 24 '15 at 07:50
  • I didn't say 2048 bytes. You want the start of your partition to be on a multiple of 4096 bytes. The first multiple of 4096 bytes you are allowed to use is 20480 bytes, since the first 17.4kB is reserved. That rules out using 0, 4096, 8192, 12288 or 16384, and so the next one is 20480. – thomasrutter Nov 24 '15 at 11:09
0

Simply leave the calculation to parted using percents as units:

mkpart primary 0% 100%

Which should result in aligned partition:

(parted) print                                                            
Model: ATA SAMSUNG MZ7LM3T8 (scsi)
Disk /dev/sdb: 3841GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  3841GB  3841GB               primary
Tombart
  • 889
  • 11
  • 25
0

GPT needs the first 34 sectors of your disk to store

  • the protective MBR in the first sector (LBA 0),
  • the primary GPT-header in the second sector (LBA 1),
  • and up to 128 partition table entries in the following 32 sectors (LBA 2 - LBA 33)

These sectors are reserved, no other data can be stored in these sectors, a partition can not start in any of them.

34 sectors with a size of 512B = 17408B = 17.4kB

That's why you get the warning The closest location we can manage is 17.4kB to 3001GB.

You should not proceed here, it would lead to a bad alignment of the partition, this is already covered in other answers you received.

Note that also the last 33 sectors of the disk are reserved to keep a copy of the primary GPT-header and the partition table entries.

https://en.wikipedia.org/wiki/GUID_Partition_Table

mook765
  • 14,911
  • 5
  • 35
  • 67
  • And what should you do instead? – Iulian Onofrei Mar 14 '20 at 17:59
  • 1
    @IulianOnofrei Use better tools (like `Gparted`) to manage your partitions. You need proper background knowledge if you use command-line tools like `parted`, also you need to know the used commands well. `Gparted` takes care of proper alignment and takes care of filesystems inside the partitions. If you, for example, use `parted` to resize a partition, the filesystem would remain unchanged and you'd have to manage the filesystem size in an extra manual step. – mook765 Mar 14 '20 at 20:29