Resolving “Bad magic number in super-block” Error When Using e2label on Linux Partitions


2 views

When working with Linux filesystems, the e2label command typically throws the "Bad magic number in super-block" error in these specific scenarios:

$ e2label /dev/sda2
e2label: Bad magic number in super-block while trying to open /dev/sda2
Couldn't find valid filesystem superblock.

The primary reason for this error is attempting to manipulate a partition that either:

  • Doesn't contain an ext2/ext3/ext4 filesystem
  • Has corrupted superblock data

From the fdisk output shown:

/dev/sda2              14         268     2048287+  82  Linux swap / Solaris

This partition has ID 82, indicating it's a swap partition - not an ext filesystem that e2label expects.

To properly identify filesystem types before using e2label, use:

$ lsblk -f
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sda                                                      
├─sda1 ext4   /boot 5c7c...                              /boot
├─sda2 swap         f890...                              [SWAP]
└─sda3 ext4   /     2a3f...                              /

The error when running e2label on /dev/sda (the raw device) occurs because:

$ file -s /dev/sda
/dev/sda: DOS/MBR boot sector

The raw disk contains a partition table, not a filesystem superblock.

For swap partitions, use swaplabel instead:

$ swaplabel /dev/sda2
LABEL: 
UUID:  f890c...

When you suspect superblock corruption on actual ext filesystems:

$ fsck -n /dev/sda3
fsck from util-linux 2.31.1
e2fsck 1.44.1 (24-Mar-2018)
Warning!  /dev/sda3 is mounted.
Warning: skipping journal recovery because doing a read-only filesystem check.
/: clean, 129023/1179648 files, 1212412/4712960 blocks

Here's how to properly check and label an ext4 partition:

# First verify it's actually ext*
$ sudo file -sL /dev/sda1
/dev/sda1: Linux rev 1.0 ext4 filesystem data...

# Then safely apply label
$ sudo e2label /dev/sda1 "new_boot_label"

# Confirm changes
$ lsblk -o name,label /dev/sda1
NAME  LABEL
sda1  new_boot_label

Let's break down what we're seeing in your fdisk output:

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14         268     2048287+  82  Linux swap / Solaris
/dev/sda3             269        2611    18818810   83  Linux

The key observations from your e2label attempts:

e2label /dev/sda1 → /boot (works)
e2label /dev/sda2 → fails
e2label /dev/sda3 → / (works)

The "Bad magic number" error occurs when:

  • Attempting to read a raw disk device (/dev/sda) instead of a partition
  • Trying to access a swap partition (/dev/sda2) which doesn't contain an ext filesystem
  • The superblock is corrupted (not your case since other partitions work)

An ext filesystem superblock starts with these magic bytes:

#define EXT2_SUPER_MAGIC 0xEF53

struct ext2_super_block {
    __u32 s_inodes_count;
    __u32 s_blocks_count;
    // ...
    __u16 s_magic;  // This must match EXT2_SUPER_MAGIC
};

Here's how to safely check and set labels:

# Check if a partition is ext2/3/4 before labeling
blkid /dev/sda1
# Sample output: /dev/sda1: UUID="..." TYPE="ext4"

# For ext filesystems only:
e2label /dev/sda1 "new_boot_label"

# For swap partitions, use swaplabel instead:
swaplabel -L "swap_partition" /dev/sda2

If you ever encounter real superblock corruption:

# Find backup superblocks
mkfs.ext4 -n /dev/sdX1

# Use alternative superblock
fsck.ext4 -b 32768 /dev/sdX1

For different filesystem types:

  • XFS: xfs_admin -L
  • Btrfs: btrfs filesystem label
  • NTFS: ntfslabel