I’m trying to create an XFS filestem on a used disk, and got into a problem. Here is the error message:

# mkfs.xfs /dev/sda1
mkfs.xfs: cannot open /dev/sda1: Device or resource busy

So, let’s see if I can fix it. (Spoiler alert: I think I can, even though it’s not yet done at the time of this writing.)

Let’s check the partitions of /dev/sda:

# parted /dev/sda print
Model: ATA ST1000LM014-1EJ1 (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End    Size   File system  Name  Flags
 1      1049kB  512GB  512GB  ntfs         1

So, there is a partition (/dev/sda1) with 512GB in size, and its filesystem is ntfs. I was pretty sure that I had selected xfs as the filesystem.

Using fdisk tool, it at least reported the partition type as Linux filesystem.

# fdisk -l /dev/sda
Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: E5B6428E-9A65-4956-8F86-274BEC708AE6

Device     Start       End   Sectors   Size Type
/dev/sda1   2048 999999487 999997440 476.9G Linux filesystem

Well, let’s zap the partition table and re-create a new partition again.

# parted /dev/sda -s -a opt mklabel gpt

# parted /dev/sda print
Model: ATA ST1000LM014-1EJ1 (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start  End  Size  File system  Name  Flags
# parted /dev/sda -s -a opt mkpart primary xfs 1 512000

# parted /dev/sda print
Model: ATA ST1000LM014-1EJ1 (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  512GB  512GB  ntfs         primary

There, the File system showed up as ntfs again. Let’s try to create an XFS or ext4 filesystem on /dev/sda1. (I know it’d fail!)

# partprobe
# cat /proc/partitions  | grep sda
   8        0  976762584 sda
   8        1  499998720 sda1

# mkfs.xfs /dev/sda1
mkfs.xfs: cannot open /dev/sda1: Device or resource busy

# mkfs.ext4 /dev/sda1
mke2fs 1.45.6 (20-Mar-2020)
/dev/sda1 contains a ntfs file system
Proceed anyway? (y,N) y
/dev/sda1 is apparently in use by the system; will not make a filesystem here!

At this point, I turned to the internet for help. Fortunately, someone had encountered the same issue and kinda wrote up a blog post. The issue is caused by multipath. It could be a feature, but surely I wasn’t aware of this and definitely did not make use of this.

Let’s list the multipath on /dev/sda:

# multipath -ll /dev/sda
ST1000LM014-1EJ164-SSHD_W773A51Z dm-0 ATA,ST1000LM014-1EJ1
size=932G features='1 queue_if_no_path' hwhandler='0' wp=rw
`-+- policy='service-time 0' prio=1 status=active
  `- 0:0:0:0 sda 8:0 active ready running

Yep, so multipath was dected, and to fix this issue I’m having, I need to remove /dev/sda from the mulitpath. From the same blog post, I should be able to remove /dev/sda with the following command:

# multipath -f
Mar 13 20:58:28 | the -f option requires a map name to remove

Unfortunately, it did not work for me. Remember I wrote at the beginning of this post that I haven’t actually fixed the problem yet? Well, I didn’t make that up. :D

With a bit of more searching on multipath, I found that in order to remove the multipath device, the argument to the command should be the <multipath-device> not the /dev/sda device.

# multipath -f ST1000LM014-1EJ164-SSHD_W773A51Z

No error so far. Let’s proceed to creating an XFS filesystem on /dev/sda1 partition.

# mkfs.xfs /dev/sda1
mkfs.xfs: /dev/sda1 appears to contain an existing filesystem (ntfs).
mkfs.xfs: Use the -f option to force overwrite.

# mkfs.xfs -f /dev/sda1
meta-data=/dev/sda1              isize=512    agcount=4, agsize=31249920 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=124999680, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=61035, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Finally, I’m going to test mount the new filesystem.

# mount /dev/sda1 /mnt
[root@tbataar ~]# lsblk -f | grep sda1
└─sda1                   none               4e3af1ac-7a18-490d-8ffc-3d91e1ab10be   /mnt

Great! I think the fileystem creation on /dev/sda1 partition was a success.

Resources: