Skip to content

ddrescue


Example Scanario

In this scanario I have a 120GB drive that is 17% wearing level left that is "hanging" sometimes and its time to write to a brand new

Copy a 120GB drive to a 240GB ssd drive

ddrescue -f -n -v -b 4096 /dev/sdb /dev/sdc /root/clone.log

Fix the GPT Header on the new drive

sgdisk -e /dev/sdc

Once we have booted the drive we want to rize the lvm partition from 120GB to 240GB

  • NOTE: Double check so we working on the right drive

    ```bash parted /dev/sdb -s print Model: KINGSTON SA400S37240G (scsi) Disk /dev/sdb: 240GB Sector size (logical/physical): 512B/4096B Partition Table: gpt Disk Flags:

    Number Start End Size File system Name Flags 1 1049kB 2097kB 1049kB 1 bios_grub 2 2097kB 1000MB 998MB fat32 2 boot, esp 3 1000MB 128GB 127GB 3

```

Resize Part

parted /dev/sdb resizepart 3 100%
Information: You may need to update /etc/fstab.

(03:24:13)-[root@localhost] /home/wuseman $ parted /dev/sdb -s print
Model: KINGSTON  SA400S37240G (scsi)
Disk /dev/sdb: 240GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  2097kB  1049kB               1     bios_grub
 2      2097kB  1000MB  998MB   fat32        2     boot, esp
 3      1000MB  240GB   239GB                3

Update LVM and expand to 95% of the drive

pvresize /dev/gpt-auto-root

Force LVM to see the reality

pvscan --cache
  pvscan[16700] PV /dev/gpt-auto-root online.

The Alignment Trap (LUKS2 + 4K Sectors)

When resizing a LUKS2 partition on a drive with 4K physical sectors, using parted with 100% can lead to a boot failure with the error: device-mapper: reload ioctl failed: Invalid argument. This is because the partition must end on a sector that is a multiple of 8 (512B * 8 = 4096B).

Correct Resize Method (Manual Alignment)

Instead of parted, use fdisk to ensure the partition ends on a mathematically "clean" sector.

  1. Open the drive:
    fdisk /dev/sdb
    
  2. Delete the LUKS partition (usually 3): Enter d, then 3.
  3. Create a new partition with the EXACT same start sector: Enter n, then 3. Use the same start sector as before (e.g., 1953792).
  4. Calculate a safe end sector (Total sectors on disk - 2048 or a number divisible by 8): For a 240GB Kingston, the magic number is often 468862087.
  5. CRITICAL: When asked to remove the signature, answer N (No).
  6. Change type to Linux LVM: Enter t, then 30 (or the alias lvm).
  7. Write and exit: Enter w.

Expand the Decrypted Stack

Once the partition table is corrected and the device is opened, expand the layers from the inside out.

# 1. Open the LUKS container
cryptsetup luksOpen /dev/sdb3 rootfs

# 2. Resize the LUKS layer to match the new partition size
cryptsetup resize rootfs

# 3. Inform LVM that the Physical Volume has grown
pvresize /dev/mapper/rootfs

# 4. Expand the Logical Volume and the Filesystem (ext4) in one go
lvextend -l +100%FREE -r /dev/elitedesk/rootfs

Troubleshooting

Error Cause Solution
Invalid argument Partition end is not 4K aligned Delete and recreate partition ending on a sector divisible by 8.
Device busy Stale device-mapper entries Run dmsetup remove_all --force or reboot the Live-CD.
Sector size mismatch LUKS header expects 4096B Use cryptsetup luksDump to verify and align partition accordingly.