Skip to contentSkip to conversation

Get rid of cat and grep

PATCH In review Seewer Philippe 7 participants 20 messages Jun 5, 2009 12 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Get rid of cat and grep

cat and grep just bloat the initrd, this patch removes dependencies on them.

---

modules.d/40network/ifup             |    3 ++-
modules.d/40network/install          |    2 +-
modules.d/40network/kill-dhclient.sh |    4 +++-
modules.d/95nbd/nbdroot              |   12 +-----------
modules.d/95nfs/nfsroot              |   12 ++----------
modules.d/95nfs/nfsroot-cleanup.sh   |    2 +-
modules.d/99base/dracut-lib          |   14 ++++++++++++++
7 files changed, 24 insertions(+), 25 deletions(-)
modules.d/40network/ifup +0 -0
index 981a207..e347da5 100755
@@ -100,7 +100,8 @@ if [ -z "$ip" ]; then
    do_dhcp;

else

    # spin through the kernel command line, looking for ip= lines
-    for p in $(cat /proc/cmdline); do
+    [ "$CMDLINE" ] || read CMDLINE </proc/cmdline;
+    for p in $CMDLINE; do
	[ -n "${p%ip=*}" ] && continue
	ip_to_var ${p#ip=}
modules.d/40network/install +0 -0
index d480391..3a06f79 100755
@@ -1,5 +1,5 @@

#!/bin/bash
-dracut_install ip dhclient hostname cat
+dracut_install ip dhclient hostname
# Include wired net drivers, excluding wireless
for modname in find "/lib/modules/$kernel/kernel/drivers" -name '*.ko'; do
if nm -uPA $modname | grep -q eth_type_trans; then

modules.d/40network/kill-dhclient.sh +0 -0
index fcebd32..54b3344 100755
@@ -1,5 +1,7 @@

#!/bin/sh

for f in /tmp/dhclient.*.pid; do

-    [ -e $f ] && kill $(cat $f)
+    [ -e $f ] || continue
+    read PID < $f;
+    kill $PID;

done

modules.d/95nbd/nbdroot +0 -0
index 1130cca..3475239 100755
@@ -71,17 +71,7 @@ getarg ro && nbdrw=ro

getarg rw && nbdrw=rw
fsopts=${fsopts+$fsopts,}${nbdrw}

-if [ ! -e /tmp/nbd.present ]; then
- cat /proc/devices | while read t1 devt; do
- if [ "$devt" = "nbd" ]; then
- >/tmp/nfs.present
- break
- fi
- done
-fi
-if [ ! -e /tmp/nbd.present ]; then
- modprobe nbd || exit 1
-fi
+incol2 /proc/devices nbd || modprobe nbd || exit 1

# XXX better way to wait for the device to be made? i=0

modules.d/95nfs/nfsroot +0 -0
index 2d08b68..da002c5 100755
@@ -46,16 +46,8 @@ getarg rw && nfsrw=rw

nfsflags=${nfsflags+$nfsflags,}${nfsrw}

# Load the modules so the filesystem type is there -if [ ! -e /tmp/nfs.present ]; then
- cat /proc/filesystems | while read t1 fst; do
- case "$fst" in
- nfs|nfs4) >/tmp/nfs.present; break ;;
- esac
- done
-fi
-if [ ! -e /tmp/nfs.present ]; then
- modprobe nfs || exit 1
-fi

+incol2 /proc/filesystems nfs  || modprobe nfs || exit 1
+incol2 /proc/filesystems nfs4 || modprobe nfs || exit 1

# XXX don't forget to move /var/lib/nfs/rpc_pipefs to new /

modules.d/95nfs/nfsroot-cleanup.sh +0 -0
index 4bfdd34..1a5f187 100644
@@ -4,7 +4,7 @@ pid=$(pidof rpc.statd)

pid=$(pidof rpcbind)
[ -n "$pid" ] && kill $pid

-if grep -q rpc_pipefs /proc/mounts; then +if incol2 /proc/mounts /var/lib/nfs/rpc_pipefs; then

    # try to create the destination directory
    [ -d $NEWROOT/var/lib/nfs/rpc_pipefs ] || mkdir -p $NEWROOT/var/lib/nfs/rpc_pipefs 2>/dev/null
modules.d/99base/dracut-lib +0 -0
index e3f4794..07c1bc7 100644
@@ -36,3 +36,17 @@ check_occurances() {
 
    [ $count -eq $expected ]

}
+
+incol2() {

+    local dummy check;
+    local file="$1";
+    local str="$2";

+
+ [ -z "$file" ] && return;
+ [ -z "$str" ] && return;
+
+ while read dummy check ; do
+ [ "$check" = "$str" ] && return 0
+ done < $file
+ return 1
+}

--
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

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

#2 lore raw

On 06/05/2009 01:56 PM, Seewer Philippe wrote:
...

Review

Quoted 13 lines
(quoted diff) +13 -0
+incol2() {
+ local dummy check;
+ local file="$1";
+ local str="$2";
+
+ [ -z "$file" ] && return;
+ [ -z "$str" ] && return;
+
+ while read dummy check ; do
+ [ "$check" = "$str" ] && return 0
+ done < $file
+ return 1
+}

This better has to be:

- while read dummy check ; do
+ while read dummy check restofline; do
--
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

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

#4 lore raw
Quoted 19 lines · Harald Hoyer

On 06/05/2009 01:56 PM, Seewer Philippe wrote:
...

Review

Quoted 13 lines
(quoted diff) +13 -0
+incol2() {
+ local dummy check;
+ local file="$1";
+ local str="$2";
+
+ [ -z "$file" ] && return;
+ [ -z "$str" ] && return;
+
+ while read dummy check ; do
+ [ "$check" = "$str" ] && return 0
+ done < $file
+ return 1
+}

This better has to be:

Quoted 2 lines

- while read dummy check ; do
+ while read dummy check restofline; do

Aye. copy/paste mistake. sorry about that

--
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

#5 lore raw
In reply to a message not in the archive

Review

Quoted 4 lines · Seewer Philippe

cat and grep just bloat the initrd, this patch removes dependencies on them.

(quoted diff) +2 -0
+incol2 /proc/filesystems nfs  || modprobe nfs || exit 1
+incol2 /proc/filesystems nfs4 || modprobe nfs || exit 1

While I prefer the conditional list syntax, there has been push back on IRC about using it. Consider if xxx then, ugly as it may be. --
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

A message that is not in the archive (sent off-list or not yet received): 1244213032.6963.1.camel-FqX9LgGZnHWDB2HL1qBt2PIbXMQ5te18@public.gmane.org

#7 lore raw
Quoted 8 lines · David Dillow

Review

Quoted 4 lines · Seewer Philippe

cat and grep just bloat the initrd, this patch removes dependencies on them.

(quoted diff) +2 -0
+incol2 /proc/filesystems nfs  || modprobe nfs || exit 1
+incol2 /proc/filesystems nfs4 || modprobe nfs || exit 1

While I prefer the conditional list syntax, there has been push back on IRC about using it. Consider if xxx then, ugly as it may be.

I would accept that style, but not:

cmd1 && { a; b; c; } || { d; e; f && { g; h; i;}}
--
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

#8 lore raw
In reply to a message not in the archive
Quoted 1 line · Seewer Philippe

cat and grep just bloat the initrd, this patch removes dependencies on them.

Are we really at a place where we're that worried about 100k? I'd rather not have to implement all of coreutils in shell where needed.

Bill
--
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

A message that is not in the archive (sent off-list or not yet received): 20090605155832.GH5107-Zdt1ptygihhQcNjhGXsBABcY2uh10dtjAL8bYrjMMd8@public.gmane.org

#10 lore raw
Quoted 6 lines · Bill Nottingham
Quoted 1 line · Seewer Philippe

cat and grep just bloat the initrd, this patch removes dependencies on them.

Are we really at a place where we're that worried about 100k? I'd rather not have to implement all of coreutils in shell where needed.

I'm not too happy about this, additionally because it made the code less readable. But he already pushed it to git.

Warren
--
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

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

#12 lore raw
Quoted 9 lines · Warren Togami
Quoted 6 lines · Bill Nottingham
Quoted 1 line · Seewer Philippe

cat and grep just bloat the initrd, this patch removes dependencies on them.

Are we really at a place where we're that worried about 100k? I'd rather not have to implement all of coreutils in shell where needed.

I'm not too happy about this, additionally because it made the code less readable. But he already pushed it to git.

Ah well... I guess I'll label it an rfc patch next time I post something like that.

When I was going through the code this morning I just noticed that cat was used only 4 times and grep just once. Personally I prefer to keep the number of pulled in utilities as small as possible. Hence the replacement. If you don't like the way I did it, please suggest something else.

Regards,
Philippe
--
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

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

#14 lore raw
Quoted 15 lines · Seewer Philippe
Quoted 11 lines · Warren Togami
Quoted 7 lines · Bill Nottingham
Quoted 2 lines · Seewer Philippe

cat and grep just bloat the initrd, this patch removes dependencies on them.

Are we really at a place where we're that worried about 100k? I'd rather not have to implement all of coreutils in shell where needed.

I'm not too happy about this, additionally because it made the code less readable. But he already pushed it to git.

Quoted 8 lines

Ah well... I guess I'll label it an rfc patch next time I post something like that.

When I was going through the code this morning I just noticed that cat was used only 4 times and grep just once. Personally I prefer to keep the number of pulled in utilities as small as possible. Hence the replacement. If you don't like the way I did it, please suggest something else.

Just because the scripts don't use it, doesn't mean it doesn't have value in the initramfs. Being dropped to a rescue shell and not being able to cat files for diagnostics is sort of annoying.

Dave

--
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

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

#16 lore raw
Quoted 3 lines · Dave Jones

Just because the scripts don't use it, doesn't mean it doesn't have value in the initramfs. Being dropped to a rescue shell and not being able to cat files for diagnostics is sort of annoying.

Agreed.

Warren
--
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

#17 lore raw
In reply to a message not in the archive
Quoted 3 lines · Dave Jones

Just because the scripts don't use it, doesn't mean it doesn't have value in the initramfs. Being dropped to a rescue shell and not being able to cat files for diagnostics is sort of annoying.

So build cat as a shell function :)

More seriously, I agree it is helpful to have some tools available when trying to debug, or develop dracut. But when I move this into production in a cluster here, I'm going to want my image as lean as I can get it.

What do you think of making a module, say 99useful-tools, what installs various tools that would be helpful, and have it installed by default. That way, it is easy to have the best of both worlds.
--
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

A message that is not in the archive (sent off-list or not yet received): 1244227068.6963.37.camel-FqX9LgGZnHWDB2HL1qBt2PIbXMQ5te18@public.gmane.org

#19 lore raw
Quoted 7 lines · David Dillow
Quoted 3 lines · Dave Jones

Just because the scripts don't use it, doesn't mean it doesn't have value in the initramfs. Being dropped to a rescue shell and not being able to cat files for diagnostics is sort of annoying.

So build cat as a shell function :)

Quoted 7 lines

More seriously, I agree it is helpful to have some tools available when trying to debug, or develop dracut. But when I move this into production in a cluster here, I'm going to want my image as lean as I can get it.

What do you think of making a module, say 99useful-tools, what installs various tools that would be helpful, and have it installed by default. That way, it is easy to have the best of both worlds.

That is what the much neglected 95debug module is for.

Quoted 5 lines

--
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

--
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

A message that is not in the archive (sent off-list or not yet received): 759A4D42-1B71-438B-A361-4CFCB6E289E7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#21 lore raw
Quoted 7 lines · Victor Lowther
Quoted 4 lines · David Dillow

What do you think of making a module, say 99useful-tools, what installs
various tools that would be helpful, and have it installed by default. That way, it is easy to have the best of both worlds.

That is what the much neglected 95debug module is for.

Now if people would just use it. :)
--
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

#22 lore raw
In reply to a message not in the archive
Quoted 7 lines · David Dillow
Quoted 3 lines · Dave Jones

Just because the scripts don't use it, doesn't mean it doesn't have value in the initramfs. Being dropped to a rescue shell and not being able to cat files for diagnostics is sort of annoying.

So build cat as a shell function :)

Quoted 7 lines

More seriously, I agree it is helpful to have some tools available when trying to debug, or develop dracut. But when I move this into production in a cluster here, I'm going to want my image as lean as I can get it.

What do you think of making a module, say 99useful-tools, what installs various tools that would be helpful, and have it installed by default. That way, it is easy to have the best of both worlds.

Not quite. Since 99useful-tools (or 95debug come to that) would have to be pulled in as a dependency if your module-scripts use tools from there.

debugging on the console: yes, I agree completely. Inside scripts: Please use only if there's no other (readable) way. --
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

#23 lore raw
In reply to a message not in the archive
Quoted 5 lines · Bill Nottingham
Quoted 1 line · Seewer Philippe

cat and grep just bloat the initrd, this patch removes dependencies on them.

Are we really at a place where we're that worried about 100k? I'd rather not have to implement all of coreutils in shell where needed.

People have this crazy fetish with booting fast.

Quoted 6 lines

Bill
--
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

--
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

A message that is not in the archive (sent off-list or not yet received): E0B0F7E0-7880-4219-AE91-B7305665424D-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#25 lore raw
Quoted 4 lines · Victor Lowther
Quoted 2 lines

Are we really at a place where we're that worried about 100k? I'd rather not have to implement all of coreutils in shell where needed.

People have this crazy fetish with booting fast.

And implementing grep in shell makes it faster? It may be, but that's certainly not a given. Moreover, I wouldn't be surprised if eventually the udev rules or the initramfs will need something that's not practical to do with 'while read foo'.

Bill
--
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

A message that is not in the archive (sent off-list or not yet received): 20090605163828.GA7041-Zdt1ptygihhQcNjhGXsBABcY2uh10dtjAL8bYrjMMd8@public.gmane.org

#27 lore raw
Quoted 11 lines · Bill Nottingham
Quoted 3 lines · Victor Lowther
Quoted 2 lines

Are we really at a place where we're that worried about 100k? I'd rather not have to implement all of coreutils in shell where needed.

People have this crazy fetish with booting fast.

And implementing grep in shell makes it faster? It may be, but that's certainly not a given. Moreover, I wouldn't be surprised if eventually the udev rules or the initramfs will need something that's not practical to do with 'while read foo'.

Bill

Please stop the discussion, this patch was not making things _that_ unreadable and is not _that_ bad.

Sure, if we have to do things in shell which really look ugly and we have a tool for it, better copy over the tool.

Now back to real work and real problems :)
--
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

#28 lore raw
In reply to a message not in the archive
Quoted 10 lines · Bill Nottingham
Quoted 4 lines · Victor Lowther
Quoted 2 lines

Are we really at a place where we're that worried about 100k? I'd rather not have to implement all of coreutils in shell where needed.

People have this crazy fetish with booting fast.

And implementing grep in shell makes it faster? It may be, but that's certainly not a given. Moreover, I wouldn't be surprised if eventually the udev rules or the initramfs will need something that's not practical to do with 'while read foo'.

Implementing it in shell isn't a huge speed change either way I'll wager -- well, maybe with fork + exec costs -- but when you have 200+ clients boot and pull the initramfs at the same time, smaller is better.

Granted, 100k savings here isn't much, but small savings add up. The big gains will be from fixing up all the extra modules we load into the image...
--
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

#29 lore raw
In reply to a message not in the archive

Why not just reimplement cat in sed?

cat() {
    for x in "$@"; do
        sed s/,/,/ < "$1"
    done
}
Quoted 124 lines · Seewer Philippe

cat and grep just bloat the initrd, this patch removes dependencies on them.

---

modules.d/40network/ifup             |    3 ++-
modules.d/40network/install          |    2 +-
modules.d/40network/kill-dhclient.sh |    4 +++-
modules.d/95nbd/nbdroot              |   12 +-----------
modules.d/95nfs/nfsroot              |   12 ++----------
modules.d/95nfs/nfsroot-cleanup.sh   |    2 +-
modules.d/99base/dracut-lib          |   14 ++++++++++++++
7 files changed, 24 insertions(+), 25 deletions(-)
modules.d/40network/ifup +0 -0
index 981a207..e347da5 100755
@@ -100,7 +100,8 @@ if [ -z "$ip" ]; then
   do_dhcp;

else

   # spin through the kernel command line, looking for ip= lines
modules.d/40network/ifup +2 -1
-    for p in $(cat /proc/cmdline); do
+    [ "$CMDLINE" ] || read CMDLINE </proc/cmdline;
+    for p in $CMDLINE; do
   [ -n "${p%ip=*}" ] && continue
   ip_to_var ${p#ip=}
modules.d/40network/install +1 -1
index d480391..3a06f79 100755
@@ -1,5 +1,5 @@
 #!/bin/bash
-dracut_install ip dhclient hostname cat
+dracut_install ip dhclient hostname
 # Include wired net drivers, excluding wireless

for modname in find "/lib/modules/$kernel/kernel/drivers" -name '*.ko'; do
if nm -uPA $modname | grep -q eth_type_trans; then

modules.d/40network/kill-dhclient.sh +0 -0
index fcebd32..54b3344 100755
@@ -1,5 +1,7 @@
 #!/bin/sh

for f in /tmp/dhclient.*.pid; do

modules.d/40network/kill-dhclient.sh +3 -1
-    [ -e $f ] && kill $(cat $f)
+    [ -e $f ] || continue
+    read PID < $f;
+    kill $PID;

done

modules.d/95nbd/nbdroot +0 -0
index 1130cca..3475239 100755
@@ -71,17 +71,7 @@ getarg ro && nbdrw=ro

getarg rw && nbdrw=rw
fsopts=${fsopts+$fsopts,}${nbdrw}

modules.d/95nbd/nbdroot +1 -11
-if [ ! -e /tmp/nbd.present ]; then
-    cat /proc/devices | while read t1 devt; do
-    if [ "$devt" = "nbd" ]; then
-        >/tmp/nfs.present
-        break
-    fi
-    done
-fi
-if [ ! -e /tmp/nbd.present ]; then
-    modprobe nbd || exit 1
-fi
+incol2 /proc/devices nbd || modprobe nbd || exit 1

# XXX better way to wait for the device to be made? i=0

modules.d/95nfs/nfsroot +2 -10
index 2d08b68..da002c5 100755
@@ -46,16 +46,8 @@ getarg rw && nfsrw=rw
 nfsflags=${nfsflags+$nfsflags,}${nfsrw}
 # Load the modules so the filesystem type is there
-if [ ! -e /tmp/nfs.present ]; then
-    cat /proc/filesystems | while read t1 fst; do
-        case "$fst" in
-        nfs|nfs4) >/tmp/nfs.present; break ;;
-    esac
-    done
-fi
-if [ ! -e /tmp/nfs.present ]; then
-    modprobe nfs || exit 1
-fi
+incol2 /proc/filesystems nfs  || modprobe nfs || exit 1
+incol2 /proc/filesystems nfs4 || modprobe nfs || exit 1
 # XXX don't forget to move /var/lib/nfs/rpc_pipefs to new /
modules.d/95nfs/nfsroot-cleanup.sh +0 -0
index 4bfdd34..1a5f187 100644
@@ -4,7 +4,7 @@ pid=$(pidof rpc.statd)
 pid=$(pidof rpcbind)

[ -n "$pid" ] && kill $pid
-if grep -q rpc_pipefs /proc/mounts; then +if incol2 /proc/mounts /
var/lib/nfs/rpc_pipefs; then # try to create the destination directory
[ -d $NEWROOT/var/lib/nfs/rpc_pipefs ] || mkdir -p $NEWROOT/var/
lib/nfs/rpc_pipefs 2>/dev/null

modules.d/99base/dracut-lib +14 -1
index e3f4794..07c1bc7 100644
@@ -36,3 +36,17 @@ check_occurances() {
   [ $count -eq $expected ]
 }
+
+incol2() {
+    local dummy check;
+    local file="$1";
+    local str="$2";
+
+    [ -z "$file" ] && return;
+    [ -z "$str"  ] && return;
+
+    while read dummy check ; do
+    [ "$check" = "$str" ] && return 0
+    done < $file
+    return 1
+}
 
--
Quoted 3 lines

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

--
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

A message that is not in the archive (sent off-list or not yet received): 8E821C8B-E7E8-4F71-ACCF-872B463AE3FF-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#31 lore raw
Quoted 7 lines · Victor Lowther

Why not just reimplement cat in sed?

cat() {
     for x in "$@"; do
         sed s/,/,/ < "$1"
     done
}

or shell:

cat() {
	local line
	while [ -n "$1" ]; do
		while read line; do
			echo "$line"
		done < "$1"
		shift
	done
}

--
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