How to find disk is in use? - Linux

This is a discussion on How to find disk is in use? - Linux ; I am looking for a system call or something similar to check whether given disk (node /dev/sda, /dev/sdh etc) is in use. This will be implemented in C language on Linux, so please suggest from C language point. For example, ...

+ Reply to Thread
Results 1 to 16 of 16

Thread: How to find disk is in use?

  1. How to find disk is in use?

    I am looking for a system call or something similar to check whether
    given disk (node /dev/sda, /dev/sdh etc) is in use. This will be
    implemented in C language on Linux, so please suggest from C language
    point.

    For example,

    #check-disk-in-use /dev/sda
    Yes, in use
    #check-disk-in-use /dev/sdh
    Not in use

    Please understand that user can provide only block device node, not a
    partition.

    Disk is use means:
    A running application is born from this disk
    A running application is reading/writing to this disk
    Disk or disk partition is mounted and used
    RAW read/write is happening on this disk
    etc.
    Basically if disk is used by any means from system/Linux this program
    should be able to identifiy it. My system has 16 disks, and Linux OS
    would be born from one of this disk and remaining 15 disk may be used
    for data storage and for some application program.

    Thanks,
    -Adar


  2. Re: How to find disk is in use?


    adar wrote:
    > I am looking for a system call or something similar to check whether
    > given disk (node /dev/sda, /dev/sdh etc) is in use. This will be
    > implemented in C language on Linux, so please suggest from C language
    > point.
    >
    > For example,
    >
    > #check-disk-in-use /dev/sda
    > Yes, in use
    > #check-disk-in-use /dev/sdh
    > Not in use
    >
    > Please understand that user can provide only block device node, not a
    > partition.
    >
    > Disk is use means:
    > A running application is born from this disk
    > A running application is reading/writing to this disk
    > Disk or disk partition is mounted and used
    > RAW read/write is happening on this disk
    > etc.
    > Basically if disk is used by any means from system/Linux this program
    > should be able to identifiy it. My system has 16 disks, and Linux OS
    > would be born from one of this disk and remaining 15 disk may be used
    > for data storage and for some application program.


    The command 'fuser -m ' will do this. If you get no output at
    all, then the block device is not in use. If you get output, it is a
    list of process IDs of processes using that device along with a code
    for how they are using it if appropriate.

    If you need to write your own code to do it, obtain the source code to
    'fuser' and borrow what you need.

    DS


  3. Re: How to find disk is in use?

    Thank you David. Unfortunately it does not work on block device like
    /dev/sda, it works provided a partition where a process is running.

    For example
    Ran fdisk /dev/sdc, without quiting fdisk, I ran fuser -m /dev/sdc, it
    did not show me as anybody using it. Actually fdisk was in the process
    of making partition.
    Later mkfs /dev/sdc1 and ran fuser -m /dev/sdc, it still did not show
    me as mkfs using it.
    It works on partition though.

    Welcome any more suggestions,

    -Adar




    David Schwartz wrote:
    > adar wrote:
    > > I am looking for a system call or something similar to check whether
    > > given disk (node /dev/sda, /dev/sdh etc) is in use. This will be
    > > implemented in C language on Linux, so please suggest from C language
    > > point.
    > >
    > > For example,
    > >
    > > #check-disk-in-use /dev/sda
    > > Yes, in use
    > > #check-disk-in-use /dev/sdh
    > > Not in use
    > >
    > > Please understand that user can provide only block device node, not a
    > > partition.
    > >
    > > Disk is use means:
    > > A running application is born from this disk
    > > A running application is reading/writing to this disk
    > > Disk or disk partition is mounted and used
    > > RAW read/write is happening on this disk
    > > etc.
    > > Basically if disk is used by any means from system/Linux this program
    > > should be able to identifiy it. My system has 16 disks, and Linux OS
    > > would be born from one of this disk and remaining 15 disk may be used
    > > for data storage and for some application program.

    >
    > The command 'fuser -m ' will do this. If you get no output at
    > all, then the block device is not in use. If you get output, it is a
    > list of process IDs of processes using that device along with a code
    > for how they are using it if appropriate.
    >
    > If you need to write your own code to do it, obtain the source code to
    > 'fuser' and borrow what you need.
    >
    > DS



  4. Re: How to find disk is in use?

    adar wrote:
    > Disk is use means:
    > A running application is born from this disk
    > A running application is reading/writing to this disk


    By the time you obtain the information "a running application is
    reading or writing to this disk", that information is quite likely
    obsolete, and so non-diagnostic uses of it are likely to lead to race
    condition.

    Moreover, thanks to the buffer cache, process can open the device,
    write data, close the device, and terminate. The data might not be
    flushed out to the device until after the process has already
    terminated.

    It's more customary to consider the device to be in use by a process
    while it has an open file descriptor on it.


  5. Re: How to find disk is in use?

    adar wrote:
    > Thank you David. Unfortunately it does not work on block device like
    > /dev/sda, it works provided a partition where a process is running.


    fuser certainly does work on a block device.

    > For example
    > Ran fdisk /dev/sdc, without quiting fdisk, I ran fuser -m /dev/sdc, it


    For a block device, ``fuser -m'' means list all processes which have
    open files in a filesystem that is mounted on that device.

    The fdisk program is using /dev/sdc itself. The only way fdisk will
    show up under ``fuser -m'' is if its executable happens to be be on the
    filesystem mounted on /dev/sdc.

    Is /dev/sdc even mounted at all?

    > did not show me as anybody using it. Actually fdisk was in the process
    > of making partition.
    > Later mkfs /dev/sdc1 and ran fuser -m /dev/sdc, it still did not show
    > me as mkfs using it.


    If you are running mkfs on a device, and "fuser -m" shows anything for
    that device, your system is in deep trouble.

    > Welcome any more suggestions,


    Read man pages carefully to understand the options you are using?


  6. Re: How to find disk is in use?

    On 17 Oct 2006 23:02:16 -0700 Kaz Kylheku wrote:

    | adar wrote:
    |> Thank you David. Unfortunately it does not work on block device like
    |> /dev/sda, it works provided a partition where a process is running.
    |
    | fuser certainly does work on a block device.

    I just tested it myself and it does not.

    ================================================== ===========================
    root@canopus:/root 136> fuser -m /dev/hda
    root@canopus:/root 137> fuser -m /dev/hda3
    /dev/hda3: 1e 848e 1172e 1229e 1232e 1235e 1238e 1242e 1255e 1256e 1266e 1276e 1286e 1296e
    [...truncated...]
    root@canopus:/root 138> fuser -m /dev/hda
    root@canopus:/root 139>
    ================================================== ===========================


    |> For example
    |> Ran fdisk /dev/sdc, without quiting fdisk, I ran fuser -m /dev/sdc, it
    |
    | For a block device, ``fuser -m'' means list all processes which have
    | open files in a filesystem that is mounted on that device.
    |
    | The fdisk program is using /dev/sdc itself. The only way fdisk will
    | show up under ``fuser -m'' is if its executable happens to be be on the
    | filesystem mounted on /dev/sdc.
    |
    | Is /dev/sdc even mounted at all?

    What he wants to know is: is the _whole_ /dev/sdc in use in any way?
    The answer needs to be yes if _any_ partition within /dev/sdc is in use.
    So for my above example, I should get "yes" if I ask if /dev/hda is in
    use.


    |> did not show me as anybody using it. Actually fdisk was in the process
    |> of making partition.
    |> Later mkfs /dev/sdc1 and ran fuser -m /dev/sdc, it still did not show
    |> me as mkfs using it.
    |
    | If you are running mkfs on a device, and "fuser -m" shows anything for
    | that device, your system is in deep trouble.

    What if I ran mkfs on /dev/hda when /dev/hda3 is mounted?


    |> Welcome any more suggestions,
    |
    | Read man pages carefully to understand the options you are using?

    His problem is a different perspective.

    I have found that when a device is mounted already, and attempt to format
    the device will fail with a message about it being busy. I have never
    played with this enough to see how far that would go; I merely ran into
    it once when I entered the wrong partition. But what would have happened
    had I left the partition off entirely.

    I have also seen a case where updating the partition table when a partition
    within that device as pointed to by that table, even though the particular
    entry that is mounted is not changed, causes the kernel to refuse to reload
    that new table from the disk I just wrote it to. So I was unable to access
    the changes I made in other partitions until I reboot. IMHO, that is a
    misfeature; it should test to see if the new table would impact the existing
    mounted partitions, and refuse to active any "new" partitions that in any
    way overlap any part of the mounted partitions. But any new partitions that
    are separate from active ones should become available. Only root can do any
    of this, so there needs to be some level of assuming they know what they are
    doing. Protecting actively mounted partitions is good, but blocking the
    whole disk is bad. Still, this might be a mechanism for the OP to make use
    of somehow.

    --
    |---------------------------------------/----------------------------------|
    | Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
    | first name lower case at ipal.net / spamtrap-2006-10-18-0838@ipal.net |
    |------------------------------------/-------------------------------------|

  7. Re: How to find disk is in use?

    On 17 Oct 2006 15:23:28 -0700 adar wrote:

    | I am looking for a system call or something similar to check whether
    | given disk (node /dev/sda, /dev/sdh etc) is in use. This will be
    | implemented in C language on Linux, so please suggest from C language
    | point.
    |
    | For example,
    |
    | #check-disk-in-use /dev/sda
    | Yes, in use
    | #check-disk-in-use /dev/sdh
    | Not in use
    |
    | Please understand that user can provide only block device node, not a
    | partition.
    |
    | Disk is use means:
    | A running application is born from this disk
    | A running application is reading/writing to this disk
    | Disk or disk partition is mounted and used
    | RAW read/write is happening on this disk
    | etc.
    | Basically if disk is used by any means from system/Linux this program
    | should be able to identifiy it. My system has 16 disks, and Linux OS
    | would be born from one of this disk and remaining 15 disk may be used
    | for data storage and for some application program.

    I don't know of a specific feature that would let the kernel tell you this
    directly. So I suspect what you will need to do is codify into your program
    what the relationship between /dev/sda3 and /dev/sda is. In other words,
    for a test of a full device, the program needs to know _all_ the possible
    partition device names and run the test against them all to see if any of
    them is busy. I also suggest that you do NOT read the partition table to
    get a shortcut of a subset of partitions to test if this is an application
    that you may be distributing to others (maybe there are conditions under
    which it could be used where the on-disk partition table does not reflect
    that actual set of busy partitions known to the active kernel). You can
    get what the kernel thinks are "the partitions" in /proc/partitions.

    If you are at a kernel level and have the "sysfs" feature active, you can
    derive a set of common names for the whole-to-partition device relationship
    within /sys/block for each respective device.

    Since I can write over a partition table even with a partition on that
    disk mounted, I can say that attempting to open the whole device for
    writing is not going to be prevented by a busy partition. Maybe you have
    already discovered this and thus seek a better way.

    Don't forget that in DOS style partition tables, which are still the main
    style used on PCs, partitions numbered 5 and up, referred to usually as
    "logical partitions" are actually inside a "primary partition" known as
    the "extended partition". If any of the logical partitions is busy, I
    presume you would want to be told that the extended partition is also busy.

    --
    |---------------------------------------/----------------------------------|
    | Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
    | first name lower case at ipal.net / spamtrap-2006-10-18-0856@ipal.net |
    |------------------------------------/-------------------------------------|

  8. Re: How to find disk is in use?

    phil-news-nospam@ipal.net wrote:
    > What he wants to know is: is the _whole_ /dev/sdc in use in any way?


    To do that, you have to run fuser both ways on the disk and all of the
    partitions, e.g.:

    if /sbin/fuser -s /dev/sdc* || /sbin/fuser -sm /dev/sdc* ; then
    echo /dev/sdc is in use in some way
    fi


  9. Re: How to find disk is in use?

    adar wrote:

    > Thank you David. Unfortunately it does not work on block device like
    > /dev/sda, it works provided a partition where a process is running.
    >
    > For example
    > Ran fdisk /dev/sdc, without quiting fdisk, I ran fuser -m /dev/sdc, it
    > did not show me as anybody using it. Actually fdisk was in the process
    > of making partition.


    fdisk does not memmap the drive. Try "fuser /dev/sdc" and it
    should give you fdisk's PID.

    Kind regards,

    Iwo




  10. Re: How to find disk is in use?

    On Wed, 18 Oct 2006 17:25:43 +0100 Iwo Mergler wrote:
    | adar wrote:
    |
    |> Thank you David. Unfortunately it does not work on block device like
    |> /dev/sda, it works provided a partition where a process is running.
    |>
    |> For example
    |> Ran fdisk /dev/sdc, without quiting fdisk, I ran fuser -m /dev/sdc, it
    |> did not show me as anybody using it. Actually fdisk was in the process
    |> of making partition.
    |
    | fdisk does not memmap the drive. Try "fuser /dev/sdc" and it
    | should give you fdisk's PID.

    fuser's -m option refers to mounted filesystems, not memory mapping.

    -m name specifies a file on a mounted file system or a block device
    that is mounted. All processes accessing files on that file sys-
    tem are listed. If a directory file is specified, it is auto-
    matically changed to name/. to use any file system that might be
    mounted on that directory.

    --
    |---------------------------------------/----------------------------------|
    | Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
    | first name lower case at ipal.net / spamtrap-2006-10-18-1831@ipal.net |
    |------------------------------------/-------------------------------------|

  11. Re: How to find disk is in use?

    Phil,

    Your suggestions like walking through /sys/block/sda[1-n], and
    verifying for open file descriptors could be one step of the whole
    process.
    Second would be to walk through mount tables.
    I am sure as you said one need somehow find a relationship between
    /dev/sda and /dev/sda[1..n].

    Sure, fuser and lsof commands are ffirst few steps, but they are not
    the complete solution. However, fuser and lsof user source code might
    defintely would provide some hint, I yet to see the source code.

    In my system, these disks are hotpluggable, and am developing a tool
    to make sure before removing a disk user can be informed about the
    disk status as it is in use and it is safe to remove the disk without
    any loss of data. . There are many scenarios, but tool must inform the
    user, it is not safe to remove. For example, when you unmount a
    filesystem, it is smart to tell user that when in use i5 is in use by a
    process. Similarly tool must find for a complete disk, not just the
    partition.

    Kindly send your suggestions, more is better.

    -Adar







    phil-news-nospam@ipal.net wrote:
    > On Wed, 18 Oct 2006 17:25:43 +0100 Iwo Mergler wrote:
    > | adar wrote:
    > |
    > |> Thank you David. Unfortunately it does not work on block device like
    > |> /dev/sda, it works provided a partition where a process is running.
    > |>
    > |> For example
    > |> Ran fdisk /dev/sdc, without quiting fdisk, I ran fuser -m /dev/sdc, it
    > |> did not show me as anybody using it. Actually fdisk was in the process
    > |> of making partition.
    > |
    > | fdisk does not memmap the drive. Try "fuser /dev/sdc" and it
    > | should give you fdisk's PID.
    >
    > fuser's -m option refers to mounted filesystems, not memory mapping.
    >
    > -m name specifies a file on a mounted file system or a block device
    > that is mounted. All processes accessing files on that file sys-
    > tem are listed. If a directory file is specified, it is auto-
    > matically changed to name/. to use any file system that might be
    > mounted on that directory.
    >
    > --
    > |---------------------------------------/----------------------------------|
    > | Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
    > | first name lower case at ipal.net / spamtrap-2006-10-18-1831@ipal.net |
    > |------------------------------------/-------------------------------------|



  12. Re: How to find disk is in use?

    phil-news-nospam@ipal.net wrote:

    > On Wed, 18 Oct 2006 17:25:43 +0100 Iwo Mergler
    > wrote:
    > | fdisk does not memmap the drive. Try "fuser /dev/sdc" and it
    > | should give you fdisk's PID.
    >
    > fuser's -m option refers to mounted filesystems, not memory mapping.
    >
    > -m name specifies a file on a mounted file system or a block device
    > that is mounted. All processes accessing files on that file sys-
    > tem are listed. If a directory file is specified, it is auto-
    > matically changed to name/. to use any file system that might be
    > mounted on that directory.
    >


    Yes, you are right. I looked at the access
    type section. Sorry.

    Using "fuser /dev/sdc" works anyway, even for
    the wrong reasons. Read "fdisk doesn't *mount* the drive..." :-)

    Kind regards,

    Iwo


  13. Re: How to find disk is in use?

    On 18 Oct 2006 23:02:00 -0700 adar wrote:

    | Your suggestions like walking through /sys/block/sda[1-n], and
    | verifying for open file descriptors could be one step of the whole
    | process.
    | Second would be to walk through mount tables.
    | I am sure as you said one need somehow find a relationship between
    | /dev/sda and /dev/sda[1..n].
    |
    | Sure, fuser and lsof commands are ffirst few steps, but they are not
    | the complete solution. However, fuser and lsof user source code might
    | defintely would provide some hint, I yet to see the source code.
    |
    | In my system, these disks are hotpluggable, and am developing a tool
    | to make sure before removing a disk user can be informed about the
    | disk status as it is in use and it is safe to remove the disk without
    | any loss of data. . There are many scenarios, but tool must inform the
    | user, it is not safe to remove. For example, when you unmount a
    | filesystem, it is smart to tell user that when in use i5 is in use by a
    | process. Similarly tool must find for a complete disk, not just the
    | partition.

    And the tool must check all the partitions as I do not believe there is
    any single kernel facility to do so. It could read /proc/mounts to see
    if any partition or the whole device is mounted. It should also check
    to see if any is open by a program (for example being exported via NBD
    or emulation by a virtualizer).

    I don't know such a program exists. I do believe such a program would
    be a good tool to have. And it would be better if it can detail to the
    user a list of all the programs and processes involved in keeping the
    disk busy.

    --
    |---------------------------------------/----------------------------------|
    | Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
    | first name lower case at ipal.net / spamtrap-2006-10-19-1427@ipal.net |
    |------------------------------------/-------------------------------------|

  14. Re: How to find disk is in use?




    phil-news-nospam@ipal.net wrote:
    > On 17 Oct 2006 15:23:28 -0700 adar wrote:
    >
    > | I am looking for a system call or something similar to check whether
    > | given disk (node /dev/sda, /dev/sdh etc) is in use. This will be
    > | implemented in C language on Linux, so please suggest from C language
    > | point.
    > |
    > | For example,
    > |
    > | #check-disk-in-use /dev/sda
    > | Yes, in use
    > | #check-disk-in-use /dev/sdh
    > | Not in use
    > |
    > | Please understand that user can provide only block device node, not a
    > | partition.
    > |
    > | Disk is use means:
    > | A running application is born from this disk
    > | A running application is reading/writing to this disk
    > | Disk or disk partition is mounted and used
    > | RAW read/write is happening on this disk
    > | etc.
    > | Basically if disk is used by any means from system/Linux this program
    > | should be able to identifiy it. My system has 16 disks, and Linux OS
    > | would be born from one of this disk and remaining 15 disk may be used
    > | for data storage and for some application program.
    >
    > I don't know of a specific feature that would let the kernel tell you this
    > directly. So I suspect what you will need to do is codify into your program
    > what the relationship between /dev/sda3 and /dev/sda is. In other words,
    > for a test of a full device, the program needs to know _all_ the possible
    > partition device names and run the test against them all to see if any of
    > them is busy. I also suggest that you do NOT read the partition table to
    > get a shortcut of a subset of partitions to test if this is an application
    > that you may be distributing to others (maybe there are conditions under
    > which it could be used where the on-disk partition table does not reflect
    > that actual set of busy partitions known to the active kernel). You can
    > get what the kernel thinks are "the partitions" in /proc/partitions.
    >


    > If you are at a kernel level and have the "sysfs" feature active, you can
    > derive a set of common names for the whole-to-partition device relationship
    > within /sys/block for each respective device.
    >
    > Since I can write over a partition table even with a partition on that
    > disk mounted, I can say that attempting to open the whole device for
    > writing is not going to be prevented by a busy partition. Maybe you have
    > already discovered this and thus seek a better way.
    >

    No I have not discoverd this, of course, I am looking for better way. I
    am yet very early stage, just preparing. However i am thinking in the
    same angle. For example:
    1) Do what ever "# fdisk -l /dev/sdc" command would do, basically
    reading the partition table
    Or this can be done through sys/block also... what is the better
    method...
    2) Do what ever # umount /dev/sdc[1-..N] , if some process is running
    on that parition.
    3) Do what ever lsof and fuser does

    By the way I keeping distance from /proc filesystem as it can get
    obsloute any time


  15. Re: How to find disk is in use?

    phil-news-nospam@ipal.net wrote:

    >
    >
    >
    > I don't know of a specific feature that would let the kernel tell you this
    > directly. So I suspect what you will need to do is codify into your program
    > what the relationship between /dev/sda3 and /dev/sda is. In other words,
    > for a test of a full device, the program needs to know _all_ the possible
    > partition device names and run the test against them all to see if any of
    > them is busy. I also suggest that you do NOT read the partition table to
    > get a shortcut of a subset of partitions to test if this is an application
    > that you may be distributing to others (maybe there are conditions under
    > which it could be used where the on-disk partition table does not reflect
    > that actual set of busy partitions known to the active kernel). You can
    > get what the kernel thinks are "the partitions" in /proc/partitions.
    >


    > If you are at a kernel level and have the "sysfs" feature active, you can
    > derive a set of common names for the whole-to-partition device relationship
    > within /sys/block for each respective device.
    >
    > Since I can write over a partition table even with a partition on that
    > disk mounted, I can say that attempting to open the whole device for
    > writing is not going to be prevented by a busy partition. Maybe you have
    > already discovered this and thus seek a better way.
    >

    No I have not discoverd this, of course, I am looking for better way. I
    have not started writing code for this feature yet, just preparing.
    However I am thinking in the same angle. For example:
    1) Do what ever "# fdisk -l /dev/sdc" command would do, basically
    reading the partition table
    Or this can be done through sys/block also... what is the better
    method...???
    2) Do what ever # umount /dev/sdc[1-..N] , if some process is running
    on that parition.
    3) Do what ever lsof and fuser does

    By the way I keeping distance from /proc filesystem as it can get
    obsloute any time


  16. Re: How to find disk is in use?

    phil-news-nospam@ipal.net wrote:

    >
    >
    >
    > I don't know of a specific feature that would let the kernel tell you this
    > directly. So I suspect what you will need to do is codify into your program
    > what the relationship between /dev/sda3 and /dev/sda is. In other words,
    > for a test of a full device, the program needs to know _all_ the possible
    > partition device names and run the test against them all to see if any of
    > them is busy. I also suggest that you do NOT read the partition table to
    > get a shortcut of a subset of partitions to test if this is an application
    > that you may be distributing to others (maybe there are conditions under
    > which it could be used where the on-disk partition table does not reflect
    > that actual set of busy partitions known to the active kernel). You can
    > get what the kernel thinks are "the partitions" in /proc/partitions.
    >


    > If you are at a kernel level and have the "sysfs" feature active, you can
    > derive a set of common names for the whole-to-partition device relationship
    > within /sys/block for each respective device.
    >
    > Since I can write over a partition table even with a partition on that
    > disk mounted, I can say that attempting to open the whole device for
    > writing is not going to be prevented by a busy partition. Maybe you have
    > already discovered this and thus seek a better way.
    >

    No I have not discoverd this, of course, I am looking for better way. I
    have not started writing code for this feature yet, just preparing.
    However I am thinking in the same angle. For example:
    1) Do what ever "# fdisk -l /dev/sdc" command would do, basically
    reading the partition table
    Or this can be done through sys/block also... what is the better
    method...???
    2) Do what ever # umount /dev/sdc[1-..N] , if some process is running
    on that parition.
    3) Do what ever lsof and fuser does

    By the way I keeping distance from /proc filesystem as it can get
    obsloute any time


+ Reply to Thread