storage - bunnyamin/bunnix GitHub Wiki

Device

  • /dev/nvme0n1 corresponds to the format /dev/sda
  • /dev/nvme0n1p1 corresponds to the format /dev/sda1

Device naming convention: <abbreviated name>_<size>_<year>[_#]

Copy disk

Block-level cloning

sudo dd if=/dev/sdx of=/sdx.img sudo dd if=/sdx.img of=/dev/sdy or cat /sdx.img > /dev/sdy

The data definition program copies all data 1:1. That is, the size of the destination storage device (sdy) has to be the same as the size of the source device (sdx).

The only found method to clone part of a disk requires the creation of a sparse file:

An alternative method is to only clone the partition tables and Rsync the content on the filesystem.

Partition

After partition:

  • Install GRUB EFI and MBR, and grub-mkconfig.
  • Format and add a label to the filesystem for partition:
    • (2) as FAT32 with label "ESP" in uppercase because lowercase labels might not work properly with DOS or Windows,
    • (3) as swap with label of preference, deprecated in favor of swap file,
    • (4) as required by any additional partition.
  • Partition (1) MBR, BIOS should not be formatted. The partition can therefore not be assigned a label because the file system is unformatted.

Format

Make sure mounted devices are unmounted.

Command Example
Fat32 mkfs.msdos -F 32 /dev/<DEVICE>
Fat32 label dosfslabel /dev/<DEVICE> <NAME>
Destroy filesystem wipefs -a /dev/<DEVICE>, then create a new filesystem over the entire device, and if necesary delete the new filesystem.
EXT4 mkfs.ext4 [-L <LABEL>] /dev/<DEVICE>
EXT4 label e2label /dev/<DEVICE> <NAME>
Swap mkswap /dev/<DEVICE>
Swap label swaplabel [-L <LABEL>] /dev/<DEVICE>
Swap "mount" swapon <DEVICE>
Swap "umount" swapoff -a

Multiboot

# Start (sector) End (sector) Size File system (gdisk / parted) Name Flags (parted) Label Comment
1. 2048 4095 1MiB EF02 BIOS boot partition / unformatted bios bios_grub (none) The BIOS boot partition. Used by GRUB to store the boot code. Only switching on the boot flag on the protective MBR partition might not be enough.
2. 4096 1052671 512MiB EF00 EFI System / fat32 esp boot, esp ESP The ESP stores EFI boot loaders.
3. 1052672 9441279 4GiB 8200 Linux swap / linux-swap(v1) swap swap
4. 9441280 ... 2GiB ext4 data data

Fdisk

  1. fdisk /dev/sd? - Enter the program Fdisk interactive session.
  2. o - To remove any existing partitions
  3. p - Conform that all partitions have been deleted by printing out information on current partitions
  4. n - Create a new partition
  5. p - Select it as a primary partition
  6. 1 - Select it as the first partition on the device
  7. 4096 - For the beginning of the first sector and accept the default for the last sector
  8. w - Write the changes to the device and exit

Gdisk

For a Hybrid UEFI GPT + BIOS GPT/MBR boot, partitions 1-3 are required as minimum.

# As root, start the gdisk prompt.
gdisk /dev/<DEVICE>

# Enter "recovery and transformation options (experts only)".
Command (? for help): r

# "make hybrid MBR" to begin an interactive session.
Recovery/transformation command (? for help): h

Type from one to three GPT partition numbers, separated by spaces,
to be added to the hybrid MBR, in sequence: 1 2 3 4 ...

Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? (Y/N): N
Creating entry for GPT partition #1 (MBR partition #1)

# Accept the default.
Enter an MBR hex code (default EF):

Set the bootable flag? (Y/N): N

Creating entry for GPT partition #2 (MBR partition #2)

# Accept the default.
Enter an MBR hex code (default EF):

Set the bootable flag? (Y/N): N

Creating entry for GPT partition #3 (MBR partition #3)

# Accept the default.
Enter an MBR hex code (default 82):
Set the bootable flag? (Y/N): Y

# Enter "extra functionality (experts only)".
Recovery/transformation command (? for help): x

# "recompute CHS values in protective/hybrid MBR".
Expert command (? for help): h

# "write table to disk and exit"
Expert command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!!
Do you want to proceed? (Y/N): Y

# Re-enter
gdisk /dev/<DEVICE>
# "change a partition's name"
# NOTE "name" is not the same as "label".
Command (? for help): c

GNU parted

# As root, start the gdisk prompt.
parted /dev/<DEVICE>

# Use the command "print" to inquire about device information.
(parted) print

# Set the default unit size.
(parted) unit mib

# Create a new disklabel (partition table).
(parted) mklabel gpt

# Create a new partition.
# A value in percent (%) is calculated from the beginning of the device not "start".
(parted) mkpart <primary, extended, logical> <ext2, fat16, fat32, hfs, hfs+, hfsx, linux-swap, NTFS, reiserfs, ufs, btrfs> <0> <0>

# Remove partition.
(parted) rm <PARTITION NUMBER REFERENCE>

# Flag
(parted) set <PARTITION NUMBER REFERENCE> <FLAG> <BOOL>

# Name
# NOTE "name" is not the same as "label".

# Create MBR / BIOS partition.
(parted) mkpart primary 1MiB 2MiB
(parted) name 1 bios
(parted) set 1 bios_grub on
(parted) print

# Expected output of print.
Number  Start    End       Size      File system         Name  Flags
1       1.00MiB  2.00MiB   1.00MiB   <empty; no format>  bios  bios_grub

# Create ESP partition.
# End is 514MiB because the offset is 2MiB from the MBR.
(parted) mkpart primary fat32 2MiB 514MiB
(parted) name 2 esp
(parted) set 2 boot on
(parted) print

# Expected output of print.
Number  Start    End       Size      File system         Name  Flags
1       1.00MiB  2.00MiB   1.00MiB   <empty; no format>  bios  bios_grub
2       2.00MiB  514MiB    512MiB    fat32               esp   boot, esp

# Create swap partition.
# Consider using a 'swap file` instead.
# End is 1538MiB because the offset is 1026MiB ((2 + 512) + 1024).
(parted) mkpart primary linux-swap 514MiB 1538MiB
(parted) name 3 swap
(parted) print

# Expected output of print.
Number  Start    End       Size      File system         Name  Flags
1       1.00MiB  2.00MiB   1.00MiB   <empty; no format>  bios  bios_grub
2       2.00MiB  514MiB    512MiB    fat32               esp   boot, esp
3       514MiB   1538MiB   1024MiB   linux-swap(v1)      swap

# Create the remaining partitions.
# Offset is 1538MiB because (2 + 512 + 1024). End is 100% in order to use the remaining space.
(parted) mkpart primary ext4 1538MiB 100%
(parted) name 4 data
(parted) print

# Expected output of print.
Number  Start    End       Size      File system         Name  Flags
1       1.00MiB  2.00MiB   1.00MiB   <empty; no format>  bios  bios_grub
2       2.00MiB  514MiB    512MiB    fat32               esp   boot, esp
3       514MiB   1538MiB   1024MiB   linux-swap(v1)      swap
4       1538MiB  20479MiB  18941MiB  ext4                data

Verify alignment of partitions

Program Example
blockdev # blockdev --getalignoff /dev/<DEVICE> -> 0
gdisk Command (? for help): v -> No problems found. 2014 free sectors (1007.0 KiB) available in 1 segments, the largest of which is 2014 (1007.0 KiB) in size.
GNU parted parted /dev/<DEVICE>, (parted) align-check minimal 1 -> 1 aligned
GNU parted parted /dev/<DEVICE>,(parted) align-check optimal 1 -> 1 aligned

Calculate

Retrieve all necessary variables.

Query Example
cat /sys/block/sdb/alignment_offset 0
cat /sys/block/sdb/queue/minimum_io_size 512
cat /sys/block/sdb/queue/optimal_io_size 33553920
cat /sys/block/sdb/queue/physical_block_size 512

Calculate the correct alignment using the formula (optimal_io_size + alignment_offset) / physical_block_size). Given the query example (33553920 + 0) / 512 = 65535. The alignment should start at 32MiB or 65535s where the "s" denotes "sector" to be used as units.

Swap

Prefer swap files instead of partition, because more adjustable for portable setups.

Configuration

RAM (GiB) Swap (no hibernation)
1-2 *2
2-8 *1
8-64 *0.5
>64 Min 4 GiB

Swappiness

swappiness This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase aggressiveness, lower values decrease the amount of swap. A value of 0 instructs the kernel not to initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone.

The default value is 60.

  • Get the current value $ cat /proc/sys/vm/swappiness
    • Value 1 Sharply reduce swap inclination.
    • Value 0 Use all available RAM before starting to write to swap.

File

  1. Create a swap file:
    • mkswap -U clear --size 4G --file /swap
  2. swapon /swap
  3. /swap none swap defaults 0 0 > /etc/fstab
  • The path to the swap file in fstab is required, and not UUID or Label.
  • Before deleting the swap file, disable it; # swapoff /swap.

Is swap active?

  • cat /proc/swaps
  • swapon --show

Test

Command Example Comment
Test for bad blocks on device badblocks -s /dev/sdx
⚠️ **GitHub.com Fallback** ⚠️