1

I have a portable (3,5'') external drive (Seagate, 2TB, USB3) which I would love to use with my Ubuntu 25.10 laptop. But, it has a few bad blocks:

user@user-ubuntu:~$ sudo mkfs.ext4 -c -c -v /dev/sdc1
mke2fs 1.47.2 (1-Jan-2025)
fs_types for mke2fs.conf resolution: 'ext4'
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
122101760 inodes, 488378389 blocks
24418919 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2636120064
14905 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Filesystem UUID: 9d0331a9-5f52-41fa-950e-0f5d4222f528
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
    102400000, 214990848

Running command: badblocks -b 4096 -X -s -w /dev/sdc1 488378388
Testing with pattern 0xaa: done
Reading and comparing: 2.35% done, 13:26:05 elapsed. (310585/0/9192 errors)

So with only 2.35% of the disk surface examined, we already have 9K+ errors. Hmm.

Now, if all I wanted to build was a traditional ext2/3/4 filesystem (which, yes, is what my command above is doing), I would trust mkfs.ext4 to create a filesystem that only populates good blocks and is stable. I realise there is risk in this strategy as whatever led to the already discovered batch of bad blocks could very well result in more corruption.

But say for a moment that I want to take the risk because I don't want to spend money on a new hard drive, or I don't have easy access to a shop with the right hardware.

How can I create a LUKS protected filesystem on such a disk?

The easy/only way I know how to do that is via gnome-disk-utility:

Screenshot of gnome-disk-utility

My concern is that by doing this, any bad block information that was previously discovered and stored in the ext4 filesystem will be erased. The disk utility does not give me a way to instruct it to:

  1. check the disk for bad blocks

  2. do whatever magic it does with LVM and LUKS to create an encrypted volume on the good blocks only

Ignoring the bad blocks and just instructing gnome-disk-utility to do its thing is a guarantee for disaster - unless gnome-disk-utility or the underlying software is clever enough to detect and deal with bad blocks "on the fly" (but I doubt it, otherwise why would badblocks take 10 days to complete?!)

Is there a way to do what I'm after via the GUI? If not, is there a Ubuntu-specific guide to creating a LUKS-protected filesystem on an external disk, or will a generic tutorial like https://shivering-isles.com/2019/09/create-luks-encrypted-devices do the trick?

2
  • 2
    A device that has begun to experience bad block will tend to gain / create / experience more of them, how quick is hard to tell; that is most likely very much dependent of the internal state of media and technicalities. I'd say that it will not allow for a "stable" usable state for very long. Been there, trying to use old worn out equipment. You might wish to try though; enable the Erase feature - if that runs through without faults; good luck. Commented 12 hours ago
  • 1
    BTW; Using LUKS or other encryption / password protection will likely just complicate things - I'd bypass that. Commented 12 hours ago

1 Answer 1

1

Easy solution

Just reanalyze the badblocks list after filesystem creation. e2fsck resembles mkfs.ext4 in having -c. Use the GUI to create the filesystem, unmount it, then e2fsck -cc /dev/mapper/luks-<new partition>. Make sure you select the right partition shown in gnome-disk-utility's "Device" row when the ext4 partition is clicked.

This uses the GUI, but we needed to add a step after the GUI. The "Repair Filesystem" option in the GUI doesn't allow checking for badblocks.

Not using the GUI and using your generic tutorial link is easier. It's just cryptsetup luksFormat /dev/sdc1 && cryptsetup luksOpen /dev/sdc1 openedname && mkfs.ext4 <your options like -c> /dev/mapper/openedname

Hard solution

The LUKS header is always 32768 512-byte sectors. We can offset the old badblocks list (4096-byte sectors) by that. This is much faster on a slow disk but is human-error-prone.

  1. Don't yet erase your old filesystem on /dev/sdc1 with the badblocks info present
  2. Verify dumpe2fs -h /dev/sdc1 | grep 'Block size'. Stop if it's not 4096
  3. Export old badblocks list: dumpe2fs -b /dev/sdc1 > /root/badblocks.old
  4. Use your gnome-disk-utility to erase and format the partition as LUKS-encrypted ext4
  5. Use the GUI to unmount the ext4 partition but don't lock its LUKS container
  6. Note the "Device" row's path after clicking on the new ext4 partition in gnome-disk-utility. Remove the /dev/mapper/ prefix
  7. Verify dmsetup table | grep luks-<new partition>. The output should look like luks-<new parition>: 0 <number> crypt aes-xts-plain64 :64:logon:cryptsetup:<your device>-d0 <ignore but usually 0> <major>:<minor number> 32768 <options...>. Stop if the 0 and 32768 are different
  8. Verify dumpe2fs -h /dev/mapper/luks-<new partition> | grep 'Block size'. Stop if it's not 4096
  9. Offset the badblocks list: </root/badblocks.old awk '{print $0-((32768)*512/4096)}' >/root/badblocks.new
  10. Import the badblocks list: e2fsck /dev/mapper/luks-<new partition> -l /root/badblocks.new

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.