Bug 196102 - "gpart: device busy" even though it's not in use
Summary: "gpart: device busy" even though it's not in use
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-12-18 17:59 UTC by david
Modified: 2017-09-19 15:16 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description david 2014-12-18 17:59:54 UTC
i bought a new hard drive, and am trying to setup geli with ZFS on it.
geli refuses to write the metadata, when the first partition is too big.
so i am trying to delete the first partition, to recreate a smaller one.
yet gpart claims my drive is "busy". here's the relevant output:

root@mango:/etc/ > gpart delete -i0 da0
gpart: index '0': No such file or directory
root@mango:/etc/ > gpart delete -i1 da0
gpart: Device busy
root@mango:/etc/ > gpart destroy -F da0
gpart: Device busy
root@mango:/etc/ > sysctl kern.geom.debugflags=16
kern.geom.debugflags: 16 -> 16
root@mango:/etc/ > gpart show da0
=>        40  7814037088  da0  GPT  (3.6T)
          40  7814037080    1  freebsd-zfs  (3.6T)
  7814037120           8       - free -  (4.0K)

root@mango:/etc/ > dd if=/dev/zero of=/dev/da0 bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes transferred in 2.630170 secs (39867229 bytes/sec)
root@mango:/etc/ > gpart show da0                             
=>        40  7814037088  da0  GPT  (3.6T)
          40  7814037080    1  freebsd-zfs  (3.6T)
  7814037120           8       - free -  (4.0K)

root@mango:/etc/ > gpart destroy -F da0                       
gpart: Device busy
root@mango:/etc/ > mount
sys/ROOT/default on / (zfs, local, noatime, nfsv4acls)
devfs on /dev (devfs, local, multilabel)
data on /data (zfs, local, noatime, nfsv4acls)
backup/default on /mnt (zfs, local, noatime, nfsv4acls)
sys/ports on /usr/ports (zfs, local, noatime, nfsv4acls)
sys/src on /usr/src (zfs, local, noatime, nfsv4acls)
root@mango:/etc/ > umount /dev/da0
umount: /dev/da0: unknown file system
root@mango:/etc/ > fuser /dev/da0 
/dev/da0:
root@mango:/etc/ > uname -a
FreeBSD mango 11.0-CURRENT FreeBSD 11.0-CURRENT #0 r275830: Wed Dec 17 13:33:45 CET 2014     root@mango:/usr/obj/usr/src/sys/GENERIC-NODEBUG  amd64
Comment 1 david 2014-12-19 17:02:50 UTC
FYI: after i rebooted the device is working just fine.
Comment 2 Stefan 2016-08-14 14:16:22 UTC
Just installed FreeBSD 11 RC1 on da1, da2, da3 (mirrored zpool) without swap, intending to add swap space on da0 after installation.
At the end of the installer I went to the console to add swap space, starting with "gpart destroy da0" (it contained an NTFS partition). gpart says "Device busy".
So I rebooted and after the first bootup I retried. Still "Device busy". Rebooted a third time. Device still "busy".
Then I zeroed the first gigabyte of da0 using dd. After this, "gpart show" did not list da0 anymore.
Now "gpart destroy da0" resulted in "gpart: arg0 'da0': Invalid argument".
However, "gpart create -s GPT da0" now succeeded "da0 created".

I guess this bug only occurs if the system is being installed not on da0 but on da1 or higher. So it could probably be reproduced by installing FreeBSD onto da1+ and then attempting to manually repartition da0.

Hope this helps.
Comment 3 Andrey V. Elsukov freebsd_committer freebsd_triage 2016-08-14 14:23:35 UTC
(In reply to Stefan from comment #2)
> Just installed FreeBSD 11 RC1 on da1, da2, da3 (mirrored zpool) without
> swap, intending to add swap space on da0 after installation.
> At the end of the installer I went to the console to add swap space,
> starting with "gpart destroy da0" (it contained an NTFS partition). gpart
> says "Device busy".

It is busy, because before destroying a partition table you must to delete all its partitions or use -F flag. 

> I guess this bug only occurs if the system is being installed not on da0 but
> on da1 or higher. So it could probably be reproduced by installing FreeBSD
> onto da1+ and then attempting to manually repartition da0.

This isn't bug. It works as expected.
Comment 4 Stefan 2016-08-14 14:49:54 UTC
Thank you for the quick reply, Andrey.

Sounds plausible, but then the message "Device busy" is very misleading imho. 

Suggestion: 
Could the message maybe changed to something more informative and less confusing like "Please delete the partitions first or force destroying the partition table using the -F flag"?
Comment 5 Alexey Dokuchaev freebsd_committer freebsd_triage 2017-09-12 10:18:21 UTC
I agree, this is pretty annoying.  I've filed a DR (https://reviews.freebsd.org/D12336) to include more elaborate example of how to properly delete partitions and partition table in the gpart(8) manual page.
Comment 6 Alexey Dokuchaev freebsd_committer freebsd_triage 2017-09-19 15:16:38 UTC
gpart(8) manual page was updated to include example of how to properly delete partitions and partitioning scheme as of base r323764, hence I'm closing this PR.