Other gpart alignment: installer vs Handbook (GEOM)

Hi! Ever since I started using FreeBSD (from version 13.0), I've been trying to understand the installer's logic so I can manually reproduce its actions. For example, I was able to reproduce the partitioning for the case "Auto ZFS" with "UEFI (GPT)". Here are the commands I used to do this and 'gpart show' output compared to the installer (installer - nvd0, manually - nvd1):

CASE 1: -a 1M

Code:
root@test:~ # gpart destroy -F nvd1                                                                              
nvd1 destroyed                                                                                                    
root@test:~ # gpart create -s gpt /dev/nvd1                                                                      
nvd1 created                                                                                                      
root@test:~ # gpart add -i 1 -s 260M -t efi /dev/nvd1                                                            
nvd1p1 added                                                                                                      
root@test:~ # gpart add -a 1M -i 2 -s 2G -t freebsd-swap /dev/nvd1                                                
nvd1p2 added                                                                                                      
root@test:~ # gpart add -a 1M -i 3 -t freebsd-zfs /dev/nvd1                                                      
nvd1p3 added                                                                                                      
root@test:~ # gpart show                                                                                          
=>      40  41942960  nvd0  GPT  (20G)                                                                            
        40    532480     1  efi  (260M)                                                                          
    532520      2008        - free -  (1.0M)                                                                      
    534528   4194304     2  freebsd-swap  (2.0G)                                                                  
   4728832  37212160     3  freebsd-zfs  (18G)                                                                    
  41940992      2008        - free -  (1.0M)                                                                      
                                                                                                                 
=>      40  41942960  nvd1  GPT  (20G)                                                                            
        40    532480     1  efi  (260M)                                                                          
    532520      2008        - free -  (1.0M)                                                                      
    534528   4194304     2  freebsd-swap  (2.0G)                                                                  
   4728832  37212160     3  freebsd-zfs  (18G)                                                                    
  41940992      2008        - free -  (1.0M)

As you can see, I used the -a 1M flag for alignment. The result of nvd1 is completely equivalent to nvd0. For automatic installation on nvd0 the setting "Force 4K Sectors? YES" was used while for nvd1 I used -a 1M. But why then are the results the same?

CASE 2: -a 4k

However, if we look at many modern disk partitioning recommendations (Chapter 21: GEOM, for example), we see that -a 4k is usually recommended, which is the more general case (if I understand correctly). If I use -a 4k the result will be different from installer:
Code:
root@test:~ # gpart create -s gpt /dev/nvd1
nvd1 created
root@test:~ # gpart add -i 1 -s 260M -t efi /dev/nvd1
nvd1p1 added
root@test:~ # gpart add -a 4k -i 2 -s 2G -t freebsd-swap /dev/nvd1
nvd1p2 added
root@test:~ # gpart add -a 4k -i 3 -t freebsd-zfs /dev/nvd1
nvd1p3 added
root@test:~ # gpart show
=>      40  41942960  nvd0  GPT  (20G)
        40    532480     1  efi  (260M)
    532520      2008        - free -  (1.0M)
    534528   4194304     2  freebsd-swap  (2.0G)
   4728832  37212160     3  freebsd-zfs  (18G)
  41940992      2008        - free -  (1.0M)

=>      40  41942960  nvd1  GPT  (20G)
        40    532480     1  efi  (260M)
    532520   4194304     2  freebsd-swap  (2.0G)
   4726824  37216176     3  freebsd-zfs  (18G)

In the latter case, we do not observe alignment with "- free - (1.0M)".

Question 1: which partitioning do you consider more correct in the general case: -a 4k (the recommendations in the GEOM chapter) or -a 1M (the way the installer does it)? Or is there no difference at all?

Question 2: I am attaching the graphical installer settings that I tried to replicate. As you can see, the default is "Force 4K Sectors? YES", but why then does the result correspond to -a 1M? And if I set "Force 4K Sectors? NO" the result will be as follows (like when I manually use -a 4k):

Code:
root@test:~ # gpart show
=>      40  41942960  nvd0  GPT  (20G)
        40    532480     1  efi  (260M)
    532520   4194304     2  freebsd-swap  (2.0G)
   4726824  37216176     3  freebsd-zfs  (18G)

=>      40  41942960  nvd1  GPT  (20G)
        40    532480     1  efi  (260M)
    532520   4194304     2  freebsd-swap  (2.0G)
   4726824  37216176     3  freebsd-zfs  (18G)

It seems to me that this result should correspond "Force 4K Sectors? YES". Explain to me please.

P.S.: by the way, all my drives on all machines are Samsung pm9a3 (m.2) and I have always used -a 4k. So it will be an epic fail if I have to repartition disks on all servers, because there are no - free - (1.0M) anywhere.
 

Attachments

  • test.png
    test.png
    43.5 KB · Views: 594
I'm not pretending to have all the answers, but this may help.

Here are the commands I used to do this and 'gpart show' output compared to the installer (installer - nvd0, manually - nvd1):
[...]
The result of nvd1 is completely equivalent to nvd0. For automatic installation on nvd0 the setting "Force 4K Sectors? YES" was used while for nvd1 I used -a 1M. But why then are the results the same?
The installer uses bsdinstall(8). A closer look into bsdinstall shows that for a ZFS install, the alignment, as a result of the user selecting the "4K" option during an automated install, is more finely tuned than you might have expected, zfsboot - L806-L824:
#
# Lay down the desired type of partition scheme
#
local setsize mbrindex align_small align_big
#
# If user has requested 4 K alignment, add these params to the
# gpart add calls. With GPT, we align large partitions to 1 M for
# improved performance on SSDs. MBR does not always play well with gaps
# between partitions, so all alignment is only 4k for that case.
# With MBR, we align the BSD partition that contains the MBR, otherwise
# the system fails to boot.
#
if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
align_small="-a 4k"
align_big="-a 1m"
fi

case "$ZFSBOOT_PARTITION_SCHEME" in
""|GPT*) f_dprintf "$funcname: Creating GPT layout..."
...
Track the usage of align_big, especially for the case of GPT layout at the end of the shown code snippet.

There is also commit bsdinstall.8: Clarify the description of ZFSBOOT_FORCE_4K_SECTORS and related PR 274513. The commit has been merged and appears in 13.3-RELEASE, 14-STABLE, 14.1-RC1, but is not present in 13.2-RELEASE & 14.0-RELEASE, where perhaps you have based your expectations on, in addition to the results shown in your OP. bsdinstall(8) with the better description now shows:
Code:
ENVIRONMENT VARIABLES
 [...]
	ZFSBOOT_FORCE_4K_SECTORS
			    Controls  the minimum sector size of the pool.  If
			    this variable is not  empty,  the  minimum	sector
			    size  is 4K.  If this variable is empty, the mini-
			    mum	sector size is 512.  Note that the sector size
			    of the pool	will always be	at  least  the	sector
			    size of the	backing	disks.	Default: "1"
 
Code:
532520      2008        - free -  (1.0M)
41940992    2008        - free -  (1.0M)
And what are these free spaces created for?
 
The way I understand it, one wants partitions to start and end on physical sector size boundaries, primarily for performance reasons. If done wrong, updating a file system block may require read/modify/write of 2 blocks .

If I recall my powers of 2 math correctly, 1M is evenly divisible by 4K so 1M alignment is effectively 4K alignment.

Is there any difference between 1M and 4K in the real world, other than the loss of some usable space? I don't know.
 
Well, okay. Here are the mathematical conclusions. But what if we use a different logic: run the same software performance test and see a faster result? If the manual option gives a higher speed, then the correct partitioning is manual. If the automatic disk partitioning wins the performance and speed test, then the correct partitioning is automatic. This will be the answer to the first question of the topic starter.
Isn't it? It's not logical?
 
Back
Top