SSD alignment on x64

after DD the x64 combined ext4 image to a new SSD I used parted to check partition alignment on the SSD and parted reported the partitions are not aligned.

  1. I know how to re-align the partitions by booting up GPARTED but is there a way I can do it from inside openwrt ?
  2. Is it possible that future x64 openwrt images align themselves to 2048 instead of 512 in future?
GNU Parted 3.6
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
  align-check TYPE N                       check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all]            display the partition table, or available devices, or free space, or all
        found partitions
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  type NUMBER TYPE-ID or TYPE-UUID         type set TYPE-ID or TYPE-UUID of partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted) print all
Model: ATA NT-256 2242 (scsi)
Disk /dev/sda: 256GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      262kB   17.0MB  16.8MB  primary  ext2         boot
 2      17.3MB  126MB   109MB   primary  ext4


(parted) align-check opt 1
1 not aligned: 512s % 2048s != 0s
(parted) align-check opt 2
2 not aligned: 33792s % 2048s != 0s
(parted)

just discovered that FDISK aligns the next primary partition automatically without any further actions. This means I can just extroot into partion 3 and have root properly aligned.

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3):
First sector (33280-500118191, default 247808): 247808
Last sector, +/-sectors or +/-size{K,M,G,T,P} (247808-500118191, default 500118191):

Created a new partition 3 of type 'Linux' and of size 238.4 GiB.

Command (m for help): p
Disk /dev/sda: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Disk model: NT-256 2242
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xe9bad6fa

Device     Boot  Start       End   Sectors   Size Id Type
/dev/sda1  *       512     33279     32768    16M 83 Linux
/dev/sda2        33792    246783    212992   104M 83 Linux
/dev/sda3       247808 500118191 499870384 238.4G 83 Linux

Command (m for help): w
The partition table has been altered.
Syncing disks.

You can see that FDISK started partition 3 at 247808 ( i.e. instead of naturally starting directly after partition 2 at 246783). To confirm this I opened up a spreadsheet and typed in the formula:

cell A1 type in "=246783 + 2" which results in 246785 which would be the natural starting sector of partition 3
cell B1 type in "=A1/1024" which results in a misaligned sector of 241.0009766
in cell C1 type in "=mod(B1,1)"

highlight cells A1 and B1 and C1 then use the cell drag handle to copy the formulas down until you get to see a modulus = "0" in column C. The corresponding value in column A will be the aligned sector which in this case 247808 which is what FDISK returns.

You can modify target/linux/x86/image/Makefile as follows but it renders the system unbootable. When I inspect the output of fdisk -l /dev/nvme0n1 it does not contain a BIOS boot partition.

diff --git a/target/linux/x86/image/Makefile                            
b/target/linux/x86/image/Makefile index 29bebeb74890..db679002c5b5      
100644 --- a/target/linux/x86/image/Makefile +++                        
b/target/linux/x86/image/Makefile @@ -33,6 +33,7 @@ GRUB_TIMEOUT:=$(call
qstrip,$(CONFIG_GRUB_TIMEOUT)) GRUB_TITLE:=$(call                       
qstrip,$(CONFIG_GRUB_TITLE))                                            
                                                                        
 BOOTOPTS:=$(call qstrip,$(CONFIG_GRUB_BOOTOPTS))                       
+PARTITION_OFFSET := 1M                                                 
                                                                        
 define Build/combined                                                  
        $(CP) $(KDIR)/$(KERNEL_NAME) $@.boot/boot/vmlinuz               
@@ -44,7 +45,7 @@ define Build/combined                                 
                $(INSTALL_DIR) $@.boot/efi/boot                         
                $(CP) $(STAGING_DIR_IMAGE)/grub2/boot$(if $(CONFIG_x86_64),x64,ia32).efi $@.boot/efi/boot/
        )                                                               
-       PADDING="1" SIGNATURE="$(IMG_PART_SIGNATURE)" \                 
+       PARTOFFSET="$(PARTITION_OFFSET)" PADDING="1" SIGNATURE="$(IMG_PART_SIGNATURE)" \
                $(if $(filter $(1),efi),GUID="$(IMG_PART_DISKGUID)") $(SCRIPT_DIR)/gen_image_generic.sh \
                $@ \                                                    
                $(CONFIG_TARGET_KERNEL_PARTSIZE) $@.boot \     

that modification is correct for grub to boot correctly but I believe but will also need to specify DD to begin writing at 1M or the IMG file itself will need to specify this so that GRUB can locate the boot partition. I am not sure if DD has this option in which case it must be specified inside the IMG file.

If you build the image with ImageBuilder and modify
scripts/gen_image_generic.sh

OUTPUT="$1"
KERNELSIZE="$2"
KERNELDIR="$3"
KERNELPARTTYPE=${KERNELPARTTYPE:-83}
ROOTFSSIZE="$4"
ROOTFSIMAGE="$5"
ROOTFSPARTTYPE=${ROOTFSPARTTYPE:-83}
ALIGN="$5"

To

ALIGN="1024"

It wil produce 2048 sectors aligned images

root@fuj-s920:~# gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/sda: 31277232 sectors, 14.9 GiB
Model: Innodisk DEMSR- 
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): E9BAD6FA-7D1C-4DD0-C46B-4B84E666DF00
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 31277198
Partitions will be aligned on 2-sector boundaries
Total free space is 22362255 sectors (10.7 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          526335   256.0 MiB   8300  Linux Filesystem
   2          526336         8914943   4.0 GiB     8300  Linux Filesystem
 128              34            2047   1007.0 KiB  EF02  EFI Legacy BIOS Boot

Command (? for help): 

Non-efi images add an extra 256 (I think) sector gap between partitions, but efi images are supposedly legacy bootable.

I tried that but it did not give a bootable system. Just stuck at the BIOS on my machine. Did yours actually boot (I assume so given the gdisk output you generated).

EDIT: I take that back, it booted fine.

Next question is how to get the BIOS boot partition to start at 2024 and then the remaining ones after that.

The bios boot partition does not need to be aligned

I opened a PR adding this tunable

1 Like