Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why fdisk can't open root disk with MBR for writing?

6 views
Skip to first unread message

Yuri

unread,
Sep 14, 2012, 8:19:45 PM9/14/12
to
I am trying to change the active partition in MBR.
This should be a matter of changing only two bits (clearing one in one
byte and setting another one in another byte).

However, fdisk complains:
fdisk: Failed to write MBR. Try to use gpart(8).

truss reveals that fdisk failed to open the root disk for writing:
open("/dev/ad4",O_RDWR,00) ERR#1 'Operation not
permitted'
open("/dev/ad4",O_RDONLY,00) = 3 (0x3)
...
...
pwrite(0x3,0x34048200,0x200,0x0,0x0,0x0) ERR#9 'Bad file descriptor'
Failed to write MBR. Try to use gpart(8)

The question is: why the disk can't be open RW under root?
The secondary issue is the bug in fdisk: once -a option is supplied and
it can't open it RW it should just say so, and not suggest using gpart
because gpart will probably have the same issue.

Yuri
_______________________________________________
freebsd...@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hacke...@freebsd.org"

Garrett Cooper

unread,
Sep 14, 2012, 8:29:22 PM9/14/12
to
On Fri, Sep 14, 2012 at 5:19 PM, Yuri <yu...@rawbw.com> wrote:
> I am trying to change the active partition in MBR.
> This should be a matter of changing only two bits (clearing one in one byte
> and setting another one in another byte).
>
> However, fdisk complains:
> fdisk: Failed to write MBR. Try to use gpart(8).
>
> truss reveals that fdisk failed to open the root disk for writing:
> open("/dev/ad4",O_RDWR,00) ERR#1 'Operation not
> permitted'
> open("/dev/ad4",O_RDONLY,00) = 3 (0x3)
> ...
> ...
> pwrite(0x3,0x34048200,0x200,0x0,0x0,0x0) ERR#9 'Bad file descriptor'
> Failed to write MBR. Try to use gpart(8)
>
> The question is: why the disk can't be open RW under root?
> The secondary issue is the bug in fdisk: once -a option is supplied and it
> can't open it RW it should just say so, and not suggest using gpart because
> gpart will probably have the same issue.

1. What version of FreeBSD?
2. What do you have set for kern.geom.debug_flags?
3. Is /dev/ad4 (or any partitions / slices under it) currently in use?

Thanks!
-Garrett

Warren Block

unread,
Sep 14, 2012, 10:23:51 PM9/14/12
to
On Fri, 14 Sep 2012, Yuri wrote:

> I am trying to change the active partition in MBR.
> This should be a matter of changing only two bits (clearing one in one byte
> and setting another one in another byte).
>
> However, fdisk complains:
> fdisk: Failed to write MBR. Try to use gpart(8).
>
> truss reveals that fdisk failed to open the root disk for writing:
> open("/dev/ad4",O_RDWR,00) ERR#1 'Operation not
> permitted'
> open("/dev/ad4",O_RDONLY,00) = 3 (0x3)
> ...
> ...
> pwrite(0x3,0x34048200,0x200,0x0,0x0,0x0) ERR#9 'Bad file descriptor'
> Failed to write MBR. Try to use gpart(8)
>
> The question is: why the disk can't be open RW under root?
> The secondary issue is the bug in fdisk: once -a option is supplied and it
> can't open it RW it should just say so, and not suggest using gpart because
> gpart will probably have the same issue.

Did you actually try gpart? GEOM prevents writes to providers that are
in use, but gpart should handle it correctly if the problem is just that
fdisk doesn't understand GEOM.

# gpart set -a active -i 1 ada0

If that fails, the provider is in use, usually mounted. The safety can
be disabled, but it's better to figure out what has it in use.

Yuri

unread,
Sep 15, 2012, 1:24:19 AM9/15/12
to
On 09/14/2012 19:23, Warren Block wrote:
> Did you actually try gpart? GEOM prevents writes to providers that
> are in use, but gpart should handle it correctly if the problem is
> just that fdisk doesn't understand GEOM.
>
> # gpart set -a active -i 1 ada0
>
> If that fails, the provider is in use, usually mounted. The safety
> can be disabled, but it's better to figure out what has it in use.

Yes it is the hard drive with mounted /. But changing the active slice
should be safe in any case.
gpart worked.
This safety feature is protecting people from themselves. Sounds very
familiar.

Thanks!
Yuri

Warren Block

unread,
Sep 15, 2012, 8:24:32 AM9/15/12
to
On Fri, 14 Sep 2012, Yuri wrote:

> On 09/14/2012 19:23, Warren Block wrote:
>> Did you actually try gpart? GEOM prevents writes to providers that are in
>> use, but gpart should handle it correctly if the problem is just that fdisk
>> doesn't understand GEOM.
>>
>> # gpart set -a active -i 1 ada0
>>
>> If that fails, the provider is in use, usually mounted. The safety can be
>> disabled, but it's better to figure out what has it in use.
>
> Yes it is the hard drive with mounted /. But changing the active slice should
> be safe in any case.
> gpart worked.
> This safety feature is protecting people from themselves. Sounds very
> familiar.

gpart allowed it, so it's not the safety feature but merely that fdisk
is aging and can't deal well with GEOM.

Julian H. Stacey

unread,
Sep 15, 2012, 9:22:50 AM9/15/12
to
Hi Yuri & hackers,

Garrett Cooper wrote:
> On Fri, Sep 14, 2012 at 5:19 PM, Yuri <yu...@rawbw.com> wrote:
> > I am trying to change the active partition in MBR.
> > This should be a matter of changing only two bits (clearing one in one byte
> > and setting another one in another byte).
> >
> > However, fdisk complains:
> > fdisk: Failed to write MBR. Try to use gpart(8).
> >
> > truss reveals that fdisk failed to open the root disk for writing:
> > open("/dev/ad4",O_RDWR,00) ERR#1 'Operation not
> > permitted'
> > open("/dev/ad4",O_RDONLY,00) = 3 (0x3)
> > ...
> > ...
> > pwrite(0x3,0x34048200,0x200,0x0,0x0,0x0) ERR#9 'Bad file descriptor'
> > Failed to write MBR. Try to use gpart(8)
> >
> > The question is: why the disk can't be open RW under root?
> > The secondary issue is the bug in fdisk: once -a option is supplied and it
> > can't open it RW it should just say so, and not suggest using gpart because
> > gpart will probably have the same issue.
>
> 1. What version of FreeBSD?
> 2. What do you have set for kern.geom.debug_flags?
> 3. Is /dev/ad4 (or any partitions / slices under it) currently in use?
>
> Thanks!
> -Garrett

I have been applying this diff to my man fdisk:

http://berklix.com/~jhs/src/bsd/fixes/FreeBSD/src/gen/sbin/fdisk/

*** 8.0-RELEASE/src/sbin/fdisk/fdisk.8 Sat Mar 14 22:32:16 2009
--- new-generic/src/sbin/fdisk/fdisk.8 Sat Mar 14 22:35:10 2009
***************
*** 462,464 ****
--- 462,468 ----
The
.Xr bsdlabel 8
command must be used for this.
+ .Pp
+ When running multi user, you cannot write unless you first run this:
+ .br
+ sysctl kern.geom.debugflags=16

I never submitted it as a send-pr,
anyone think I should submit it to help save people ?

Cheers,
Julian
--
Julian Stacey, BSD Unix Linux C Sys Eng Consultant, Munich http://berklix.com
Reply below not above, like a play script. Indent old text with "> ".
Send plain text. Not: HTML, multipart/alternative, base64, quoted-printable.

Benjamin Kaduk

unread,
Sep 15, 2012, 2:49:41 PM9/15/12
to
On Sat, 15 Sep 2012, Julian H. Stacey wrote:

> I have been applying this diff to my man fdisk:
>
> http://berklix.com/~jhs/src/bsd/fixes/FreeBSD/src/gen/sbin/fdisk/
>
> *** 8.0-RELEASE/src/sbin/fdisk/fdisk.8 Sat Mar 14 22:32:16 2009
> --- new-generic/src/sbin/fdisk/fdisk.8 Sat Mar 14 22:35:10 2009
> ***************
> *** 462,464 ****
> --- 462,468 ----
> The
> .Xr bsdlabel 8
> command must be used for this.
> + .Pp
> + When running multi user, you cannot write unless you first run this:
> + .br
> + sysctl kern.geom.debugflags=16
>
> I never submitted it as a send-pr,
> anyone think I should submit it to help save people ?

We have had a long discussion about kern.geom.debugflags starting here:
http://lists.freebsd.org/pipermail/freebsd-current/2011-October/028090.html
My understanding from that discussion is that your patch should not be
accepted.

-Ben Kaduk

Garrett Cooper

unread,
Sep 15, 2012, 3:03:38 PM9/15/12
to
On Sat, Sep 15, 2012 at 11:49 AM, Benjamin Kaduk <ka...@mit.edu> wrote:
> On Sat, 15 Sep 2012, Julian H. Stacey wrote:
>
>> I have been applying this diff to my man fdisk:
>>
>> http://berklix.com/~jhs/src/bsd/fixes/FreeBSD/src/gen/sbin/fdisk/
>>
>> *** 8.0-RELEASE/src/sbin/fdisk/fdisk.8 Sat Mar 14 22:32:16 2009
>> --- new-generic/src/sbin/fdisk/fdisk.8 Sat Mar 14 22:35:10 2009
>> ***************
>> *** 462,464 ****
>> --- 462,468 ----
>> The
>> .Xr bsdlabel 8
>> command must be used for this.
>> + .Pp
>> + When running multi user, you cannot write unless you first run this:
>> + .br
>> + sysctl kern.geom.debugflags=16
>>
>> I never submitted it as a send-pr,
>> anyone think I should submit it to help save people ?
>
> We have had a long discussion about kern.geom.debugflags starting here:
> http://lists.freebsd.org/pipermail/freebsd-current/2011-October/028090.html
> My understanding from that discussion is that your patch should not be
> accepted.

Correct. Either fdisk should be fixed or updated to point out that
gpart should be used. There is some useful utility for fdisk still,
but it's waning.
Thanks,
-Garrett

Julian H. Stacey

unread,
Sep 16, 2012, 5:23:03 AM9/16/12
to
This is a resend as Benjamin Kaduk <ka...@mit.edu>
dropped the
Yuri <yu...@rawbw.com>
from CC line, & Yuri was the original first poster in thread who
my patch would presumably have helped.

Reference:
> From: Benjamin Kaduk <ka...@mit.edu>
> Date: Sat, 15 Sep 2012 14:49:41 -0400 (EDT)
> Message-id: <alpine.GSO.1.10....@multics.mit.edu>

Benjamin Kaduk wrote:
> On Sat, 15 Sep 2012, Julian H. Stacey wrote:
>
> > I have been applying this diff to my man fdisk:
> >
> > http://berklix.com/~jhs/src/bsd/fixes/FreeBSD/src/gen/sbin/fdisk/
> >
> > *** 8.0-RELEASE/src/sbin/fdisk/fdisk.8 Sat Mar 14 22:32:16 2009
> > --- new-generic/src/sbin/fdisk/fdisk.8 Sat Mar 14 22:35:10 2009
> > ***************
> > *** 462,464 ****
> > --- 462,468 ----
> > The
> > .Xr bsdlabel 8
> > command must be used for this.
> > + .Pp
> > + When running multi user, you cannot write unless you first run this:
> > + .br
> > + sysctl kern.geom.debugflags=16
> >
> > I never submitted it as a send-pr,
> > anyone think I should submit it to help save people ?
>
> We have had a long discussion about kern.geom.debugflags starting here:
> http://lists.freebsd.org/pipermail/freebsd-current/2011-October/028090.html
> My understanding from that discussion is that your patch should not be
> accepted.

Got to travel now, will read that thread later,

A shame if we would leave fdisk crippled undocumented, when so easy
to doc. the solution. Fdisk is known across many OSs Unix & beyond,
our BSD tools may be better nicer, but fdisk it what many will first
reach for,
a shame not to help newer people & visitors used to fdisk.
We inside FreeBSD crippled fdisk by adding these flags. Outsiders
wont expect the weirdness & get impression FreeBSD has a bug.

Cheers,
Julian
--
Julian Stacey, BSD Unix Linux C Sys Eng Consultant, Munich http://berklix.com
Reply below not above, like a play script. Indent old text with "> ".
Send plain text. Not: HTML, multipart/alternative, base64, quoted-printable.

Chris Rees

unread,
Sep 16, 2012, 5:41:49 AM9/16/12
to
I think you have a point, but at the moment fdisk really doesn't work
properly at all; I find so many people complaining on IRC about "Why
doesn't fdisk work?"

To be honest, I'd be happy with replacing fdisk with a huge warning
"USE GPART!!"; fdisk isn't really standard anyway.

The alternative of course is to fix fdisk... *properly*.

Chris

Andrey V. Elsukov

unread,
Sep 16, 2012, 5:42:50 AM9/16/12
to
On 15.09.2012 17:22, Julian H. Stacey wrote:
> + When running multi user, you cannot write unless you first run this:
> + .br
> + sysctl kern.geom.debugflags=16
>
> I never submitted it as a send-pr,
> anyone think I should submit it to help save people ?

I don't think this is good idea. Setting this flag is more dangerous
than helpful. And i already removed this suggestion from several places.
All utilities that know how to work with GEOM, do their job without
such hack.

--
WBR, Andrey V. Elsukov

signature.asc

Lokadamus

unread,
Sep 16, 2012, 3:08:01 PM9/16/12
to
Am 16.09.2012 11:41, schrieb Chris Rees:
> On 16 September 2012 10:23, Julian H. Stacey <j...@berklix.com> wrote:
>> This is a resend as Benjamin Kaduk <ka...@mit.edu>
>> dropped the
>> Yuri <yu...@rawbw.com>
>> from CC line, & Yuri was the original first poster in thread who
>> my patch would presumably have helped.
>>
>> Reference:
>>> From: Benjamin Kaduk <ka...@mit.edu>
>>> Date: Sat, 15 Sep 2012 14:49:41 -0400 (EDT)
>>> Message-id: <alpine.GSO.1.10....@multics.mit.edu>
>> Benjamin Kaduk wrote:
>>> On Sat, 15 Sep 2012, Julian H. Stacey wrote:
>>>
>>>> I have been applying this diff to my man fdisk:
>>>>
>>>> http://berklix.com/~jhs/src/bsd/fixes/FreeBSD/src/gen/sbin/fdisk/
>>>>
>>>> *** 8.0-RELEASE/src/sbin/fdisk/fdisk.8 Sat Mar 14 22:32:16 2009
>>>> --- new-generic/src/sbin/fdisk/fdisk.8 Sat Mar 14 22:35:10 2009
>>>> ***************
>>>> *** 462,464 ****
>>>> --- 462,468 ----
>>>> The
>>>> .Xr bsdlabel 8
>>>> command must be used for this.
>>>> + .Pp
>>>> + When running multi user, you cannot write unless you first run this:
>>>> + .br
>>>> + sysctl kern.geom.debugflags=16
>>>>
>>>> I never submitted it as a send-pr,
>>>> anyone think I should submit it to help save people ?
>>> We have had a long discussion about kern.geom.debugflags starting here:
>>> http://lists.freebsd.org/pipermail/freebsd-current/2011-October/028090.html
>>> My understanding from that discussion is that your patch should not be
>>> accepted.
>> Got to travel now, will read that thread later,
>>
>> A shame if we would leave fdisk crippled undocumented, when so easy
>> to doc. the solution. Fdisk is known across many OSs Unix & beyond,
>> our BSD tools may be better nicer, but fdisk it what many will first
>> reach for,
>> a shame not to help newer people & visitors used to fdisk.
>> We inside FreeBSD crippled fdisk by adding these flags. Outsiders
>> wont expect the weirdness & get impression FreeBSD has a bug.
> I think you have a point, but at the moment fdisk really doesn't work
> properly at all; I find so many people complaining on IRC about "Why
> doesn't fdisk work?"
>
> To be honest, I'd be happy with replacing fdisk with a huge warning
> "USE GPART!!"; fdisk isn't really standard anyway.
>
> The alternative of course is to fix fdisk... *properly*.
>
> Chris
> _______________________________________________
> freebsd...@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hacke...@freebsd.org"
>
FreeBSD 9.1RC1 have a live-cd option and with this, fdisk work fine.

Julian H. Stacey

unread,
Sep 17, 2012, 6:32:31 AM9/17/12
to
Long ago I used to have a lot of failures from fdisk, till some kind soul
on a mail list rescued me with
sysctl kern.geom.debugflags=16
One can but speculate how many others could be saved if we add a note to man
fdisk (or as below per Chris, better fix the source).
We should also add to 'man fdisk' SEE ALSO gpart(1)
Adding both would rescue maximal people & point the way ahead.


> To be honest, I'd be happy with replacing fdisk with a huge warning
> "USE GPART!!"; fdisk isn't really standard anyway.

For visitors from other Unixes, they'll look for fdisk.
(I've never bothered moving to geom / gpart myself, fdisk works fine for me
with debugflags=16)


> The alternative of course is to fix fdisk... *properly*.

Yes, better than adding to manual to document the bug, would be to fix src/
(IMO should have been done way back by those who introduced the
bit in kern.geom.debugflags that broke fdisk). I'd be tempted
to do it myself but must travel soon. Maybe someone fancies
hacking the src/ ?

Till src is patched, a tiny patch to manual would serve as a
better than nothing rescue / reminder to rescue those stuck.


>From Lokadamus <loka...@gmx.de>

> FreeBSD 9.1RC1 have a live-cd option and with this, fdisk work fine.

Ah yes, that reminds me Ive also seen Fdisk work OK single user.
(Single user awkward though when running a server or ssh/rlogin to a
headless host)

Cheers,
Julian
--
Julian Stacey, BSD Unix Linux C Sys Eng Consultant, Munich http://berklix.com
Reply below not above, like a play script. Indent old text with "> ".
Send plain text. Not: HTML, multipart/alternative, base64, quoted-printable.
0 new messages