Skip to contentSkip to conversation

ismounted fix

PATCH Superseded Dave Young 2 participants 3 messages Sep 17, 2012 2 messages not in the archive

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Dave Young

author
#0 lore raw

PATCH 1/3 ismounted fix

ismounted handles both find-by-dev and find-by-mnt, but there's two issues:
1. for find-by-dev, it use readlink to get the canonical dev name, but lvm is different with other devices, lvm use the symlinks in /proc/mounts without converting to canonical name.
2. for nfs mounting, just use [ -b $dev ] is not enough, it need being handled seperately.

grep all current ismounted() user, they are all ismounted <mntdir>, so change to split the ismounted <dev> to another function dev_ismounted. Afterwards ismounted() should be converted to dir_ismounted.

in dev_ismounted, fix the dev name issue by using maj:min to comparing with items in /proc/self/mountinfo. OTOH for nfs share just compare with the 1st field of /proc/mounts.

---
 modules.d/99base/dracut-lib.sh |   55 ++++++++++++++++++++++++++++++++---------
 1 file changed, 43 insertions(+), 12 deletions(-)
modules.d/99base/dracut-lib.sh +43 -12
@@ -452,26 +452,57 @@ udevproperty() {
     fi
 }
 
-find_mount() {
-    local dev mnt etc wanted_dev
-    wanted_dev="$(readlink -e -q $1)"
+# get_maj_min <device>
+# Prints the major and minor of a device node.
+# Example:
+# $ get_maj_min /dev/sda2
+# 8:2
+get_maj_min() {
+    local _dev
+    _dev=$(stat -L -c '$((0x%t)):$((0x%T))' "$1" 2>/dev/null)
+    _dev=$(eval "echo $_dev")
+    echo $_dev
+}
+
+find_mount_point() {
+    local _x majmin mnt wanted_majmin
+    wanted_majmin="$(get_maj_min $1)"
+    while read _x _x majmin _x mnt _x; do
+        [ "$majmin" = "$wanted_majmin" ] && echo "$mnt" && return 0
+    done < /proc/self/mountinfo
+    return 1
+}
+
+nfs_find_mount_point() {
+    local dev mnt etc
     while read dev mnt etc; do
-        [ "$dev" = "$wanted_dev" ] && echo "$dev" && return 0
+        [ "$dev" = "$1" ] && echo "$mnt" && return 0
     done < /proc/mounts
     return 1
 }
 
-# usage: ismounted <mountpoint>
-# usage: ismounted /dev/<device>
-ismounted() {
+find_mount_dev() {
+    local dev mnt etc
+    while read dev mnt etc; do
+        [ "$mnt" = "$1" ] && echo "$dev" && return 0
+    done < /proc/mounts
+    return 1
+}
+
+# usage: dev_ismounted /dev/<device>
+# usage: dev_ismounted nfs-share
+dev_ismounted() {
     if [ -b "$1" ]; then
-        find_mount "$1" > /dev/null && return 0
-        return 1
+        find_mount_point "$1" > /dev/null && return 0
+    else
+        nfs_find_mount_point "$1" > /dev/null && return 0
     fi
+    return 1
+}
 
-    while read a m a; do
-        [ "$m" = "$1" ] && return 0
-    done < /proc/mounts
+# usage: ismounted <mountpoint>
+ismounted() {
+    find_mount_dev "$1" > /dev/null && return 0
     return 1
 }
 
modules.d/99base/module-setup.sh +1 -1
@@ -13,7 +13,7 @@ depends() {
 
 install() {
     local _d
-    dracut_install mount mknod mkdir pidof sleep chroot \
+    dracut_install mount mknod mkdir pidof sleep chroot stat \
         sed ls flock cp mv dmesg rm ln rmmod mkfifo umount readlink setsid
     inst $(command -v modprobe) /sbin/modprobe
 

A message that is not in the archive (sent off-list or not yet received): 20120917080034.GA15753-4/[email protected]

#2 lore raw
Quoted 5 lines · Dave Young

ismounted handles both find-by-dev and find-by-mnt, but there's two issues:
1. for find-by-dev, it use readlink to get the canonical dev name, but lvm is different with other devices, lvm use the symlinks in /proc/mounts without converting to canonical name.

Well, dm-N are private DM names and should not be used anywhere in userspace. The official device name is /dev/mapper/<name> where the <name> is exported by /sys/block/dm-<N>/dm/name. You need something like:

cn_device=$(cat /sys/block/${device##/dev/}/dm/name)

Quoted 10 lines

2. for nfs mounting, just use [ -b $dev ] is not enough, it need being handled seperately.

grep all current ismounted() user, they are all ismounted <mntdir>, so change to split the ismounted <dev> to another function dev_ismounted. Afterwards ismounted() should be converted to dir_ismounted.

in dev_ismounted, fix the dev name issue by using maj:min to comparing with items in /proc/self/mountinfo. OTOH for nfs share just compare with the 1st field of /proc/mounts.

Do you know about findmnt(1)? It's smaller than stat(1) and provides all necessary functionality ;-)

Quoted 10 lines
modules.d/99base/dracut-lib.sh +7 -3
-find_mount() {
-    local dev mnt etc wanted_dev
-    wanted_dev="$(readlink -e -q $1)"
+# get_maj_min <device>
+# Prints the major and minor of a device node.
+# Example:
+# $ get_maj_min /dev/sda2
+# 8:2
+get_maj_min() {
+    local _dev

modules.d/99base/dracut-lib.sh udevproperty() { · line 471

Quoted 10 lines
modules.d/99base/dracut-lib.sh +10 -0
+    _dev=$(stat -L -c '$((0x%t)):$((0x%T))' "$1" 2>/dev/null)
+    _dev=$(eval "echo $_dev")
+    echo $_dev
+}
+
+find_mount_point() {
+    local _x majmin mnt wanted_majmin
+    wanted_majmin="$(get_maj_min $1)"
+    while read _x _x majmin _x mnt _x; do
+        [ "$majmin" = "$wanted_majmin" ] && echo "$mnt" && return 0

You should not expect that devno returned by stat(1) matches with devno in /proc/self/mountinfo. For example btrfs uses some pseudo-devnos (don't forget that btrfs is not strictly per-device filesystem):

  # ls -la /dev/sdb
  brw-rw---- 1 root disk 8, 16 Sep 17 10:24 /dev/sdb
                         ^^^^^
  # grep /dev/sdb /proc/self/mountinfo 
  56 21 0:44 / /mnt/test rw,relatime - btrfs /dev/sdb rw,ssd,nospace_cache
        ^^^^

It's probably better to use a canonical device name.

  # findmnt /dev/sdb 
  TARGET    SOURCE   FSTYPE OPTIONS
  /mnt/test /dev/sdb btrfs  rw,relatime,ssd,nospace_cache

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com

A message that is not in the archive (sent off-list or not yet received): 20120917084322.GF13016-s5vVilr7EKH/[email protected]

author
#4 lore raw

Hi,

Thanks for the comment

Quoted 7 lines · Karel Zak
Quoted 5 lines · Dave Young

ismounted handles both find-by-dev and find-by-mnt, but there's two issues:
1. for find-by-dev, it use readlink to get the canonical dev name, but lvm is different with other devices, lvm use the symlinks in /proc/mounts without converting to canonical name.

Quoted 6 lines

Well, dm-N are private DM names and should not be used anywhere in userspace. The official device name is /dev/mapper/<name> where the <name> is exported by /sys/block/dm-<N>/dm/name. You need something like:

cn_device=$(cat /sys/block/${device##/dev/}/dm/name)

Good to know.

Quoted 8 lines
Quoted 8 lines

2. for nfs mounting, just use [ -b $dev ] is not enough, it need being handled seperately.

grep all current ismounted() user, they are all ismounted <mntdir>, so change to split the ismounted <dev> to another function dev_ismounted. Afterwards ismounted() should be converted to dir_ismounted.

Quoted 6 lines
Quoted 3 lines

in dev_ismounted, fix the dev name issue by using maj:min to comparing with items in /proc/self/mountinfo. OTOH for nfs share just compare with the 1st field of /proc/mounts.

Do you know about findmnt(1)? It's smaller than stat(1) and provides all necessary functionality ;-)

great! I never know this findmnt..

findmnt works well with both nfs and lvm, replace these logic with findmnt looks more elegant. Will update this patch with findmnt.

Quoted 26 lines
Quoted 11 lines
modules.d/99base/dracut-lib.sh +7 -3
-find_mount() {
-    local dev mnt etc wanted_dev
-    wanted_dev="$(readlink -e -q $1)"
+# get_maj_min <device>
+# Prints the major and minor of a device node.
+# Example:
+# $ get_maj_min /dev/sda2
+# 8:2
+get_maj_min() {
+    local _dev

modules.d/99base/dracut-lib.sh udevproperty() { · line 471

Quoted 10 lines
modules.d/99base/dracut-lib.sh +10 -0
+    _dev=$(stat -L -c '$((0x%t)):$((0x%T))' "$1" 2>/dev/null)
+    _dev=$(eval "echo $_dev")
+    echo $_dev
+}
+
+find_mount_point() {
+    local _x majmin mnt wanted_majmin
+    wanted_majmin="$(get_maj_min $1)"
+    while read _x _x majmin _x mnt _x; do
+        [ "$majmin" = "$wanted_majmin" ] && echo "$mnt" && return 0

You should not expect that devno returned by stat(1) matches with devno in /proc/self/mountinfo. For example btrfs uses some pseudo-devnos (don't forget that btrfs is not strictly per-device filesystem):

Quoted 7 lines
  # ls -la /dev/sdb
  brw-rw---- 1 root disk 8, 16 Sep 17 10:24 /dev/sdb
                         ^^^^^
  # grep /dev/sdb /proc/self/mountinfo 
  56 21 0:44 / /mnt/test rw,relatime - btrfs /dev/sdb rw,ssd,nospace_cache
        ^^^^

Thanks for reminding, btrfs is special especially with multi devices. I remember this devnos confuse lilo as well in my slackware so I have to use a boot partition.

Quoted 18 lines

It's probably better to use a canonical device name.

  # findmnt /dev/sdb 
  TARGET    SOURCE   FSTYPE OPTIONS
  /mnt/test /dev/sdb btrfs  rw,relatime,ssd,nospace_cache

Karel

Signature · 6 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
--
To unsubscribe from this list: send the line "unsubscribe initramfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html