public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
* [PATCH] expand-on-first-boot: Fix early exit in helper script
@ 2022-06-10  3:19 Uladzimir Bely
  2022-06-10  5:14 ` Jan Kiszka
  0 siblings, 1 reply; 5+ messages in thread
From: Uladzimir Bely @ 2022-06-10  3:19 UTC (permalink / raw)
  To: isar-users

Changes in 342a751e introduced a regression when error from 'find'
makes helper script early exit (with -1 error code) just after an
attempt to assign ROOT_DEV_SLAVE variable. So, the last partition
remained not resized.

Passing 'find' output to 'head -1' masks possible error from 'find'
without breaking the logic and continues the script execution.

Signed-off-by: Uladzimir Bely <ubely@ilbers.de>
---
 .../expand-on-first-boot/files/expand-last-partition.sh         | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
index f3e29a1c..934a014f 100755
--- a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
+++ b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
@@ -11,7 +11,7 @@ set -e
 
 ROOT_DEV="$(findmnt / -o source -n)"
 ROOT_DEV_NAME=${ROOT_DEV##*/}
-ROOT_DEV_SLAVE=$(find /sys/block/"${ROOT_DEV_NAME}"/slaves -mindepth 1 -print -quit 2>/dev/null)
+ROOT_DEV_SLAVE=$(find /sys/block/"${ROOT_DEV_NAME}"/slaves -mindepth 1 -print -quit 2>/dev/null | head -1)
 if [ -n "${ROOT_DEV_SLAVE}" ]; then
 	ROOT_DEV=/dev/${ROOT_DEV_SLAVE##*/}
 fi
-- 
2.20.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] expand-on-first-boot: Fix early exit in helper script
  2022-06-10  3:19 [PATCH] expand-on-first-boot: Fix early exit in helper script Uladzimir Bely
@ 2022-06-10  5:14 ` Jan Kiszka
  2022-06-10  7:13   ` Uladzimir Bely
  0 siblings, 1 reply; 5+ messages in thread
From: Jan Kiszka @ 2022-06-10  5:14 UTC (permalink / raw)
  To: Uladzimir Bely, isar-users

On 10.06.22 05:19, Uladzimir Bely wrote:
> Changes in 342a751e introduced a regression when error from 'find'
> makes helper script early exit (with -1 error code) just after an
> attempt to assign ROOT_DEV_SLAVE variable. So, the last partition
> remained not resized.
> 
> Passing 'find' output to 'head -1' masks possible error from 'find'
> without breaking the logic and continues the script execution.
> 
> Signed-off-by: Uladzimir Bely <ubely@ilbers.de>
> ---
>  .../expand-on-first-boot/files/expand-last-partition.sh         | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
> index f3e29a1c..934a014f 100755
> --- a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
> +++ b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
> @@ -11,7 +11,7 @@ set -e
>  
>  ROOT_DEV="$(findmnt / -o source -n)"
>  ROOT_DEV_NAME=${ROOT_DEV##*/}
> -ROOT_DEV_SLAVE=$(find /sys/block/"${ROOT_DEV_NAME}"/slaves -mindepth 1 -print -quit 2>/dev/null)
> +ROOT_DEV_SLAVE=$(find /sys/block/"${ROOT_DEV_NAME}"/slaves -mindepth 1 -print -quit 2>/dev/null | head -1)
>  if [ -n "${ROOT_DEV_SLAVE}" ]; then
>  	ROOT_DEV=/dev/${ROOT_DEV_SLAVE##*/}
>  fi

"-quit" is supposed to obsolete "head -1". Feel free to prove me wrong,
but that was the reason for dropping it.

Jan

-- 
Siemens AG, Technology
Competence Center Embedded Linux

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] expand-on-first-boot: Fix early exit in helper script
  2022-06-10  5:14 ` Jan Kiszka
@ 2022-06-10  7:13   ` Uladzimir Bely
  2022-06-10  9:32     ` Henning Schild
  0 siblings, 1 reply; 5+ messages in thread
From: Uladzimir Bely @ 2022-06-10  7:13 UTC (permalink / raw)
  To: isar-users, Jan Kiszka

[-- Attachment #1: Type: text/plain, Size: 2615 bytes --]

In the email from Friday, 10 June 2022 08:14:19 +03 user Jan Kiszka wrote:
> On 10.06.22 05:19, Uladzimir Bely wrote:
> > Changes in 342a751e introduced a regression when error from 'find'
> > makes helper script early exit (with -1 error code) just after an
> > attempt to assign ROOT_DEV_SLAVE variable. So, the last partition
> > remained not resized.
> > 
> > Passing 'find' output to 'head -1' masks possible error from 'find'
> > without breaking the logic and continues the script execution.
> > 
> > Signed-off-by: Uladzimir Bely <ubely@ilbers.de>
> > ---
> > 
> >  .../expand-on-first-boot/files/expand-last-partition.sh         | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git
> > a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.s
> > h
> > b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.s
> > h index f3e29a1c..934a014f 100755
> > ---
> > a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.s
> > h +++
> > b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.s
> > h @@ -11,7 +11,7 @@ set -e
> > 
> >  ROOT_DEV="$(findmnt / -o source -n)"
> >  ROOT_DEV_NAME=${ROOT_DEV##*/}
> > 
> > -ROOT_DEV_SLAVE=$(find /sys/block/"${ROOT_DEV_NAME}"/slaves -mindepth 1
> > -print -quit 2>/dev/null) +ROOT_DEV_SLAVE=$(find
> > /sys/block/"${ROOT_DEV_NAME}"/slaves -mindepth 1 -print -quit 2>/dev/null
> > | head -1)> 
> >  if [ -n "${ROOT_DEV_SLAVE}" ]; then
> >  
> >  	ROOT_DEV=/dev/${ROOT_DEV_SLAVE##*/}
> >  
> >  fi
> 
> "-quit" is supposed to obsolete "head -1". Feel free to prove me wrong,
> but that was the reason for dropping it.
> 
> Jan

Yes, "-quit" makes 'find' to exit after first match. But if nothing found, the return code of 'find' is 
"-1" and due to script's "set -e" it just exits.

An example that demonstrates the behaviour (on raspberry):

root@isar:~# cat /boot/test.sh  
#!/bin/sh 

set -e 

echo "1: start" 
ROOT_DEV="$(findmnt / -o source -n)" 
echo "2: ROOT_DEV=${ROOT_DEV}" 
ROOT_DEV_NAME=${ROOT_DEV##*/} 
echo "3: ROOT_DEV_NAME=${ROOT_DEV_NAME}" 
ROOT_DEV_SLAVE=$(find /sys/block/"${ROOT_DEV_NAME}"/slaves -mindepth 1 -print -quit 
2>/dev/null) 
echo "4: ROOT_DEV_SLAVE=${ROOT_DEV_SLAVE}" 
root@isar:~# sh /boot/test.sh  
1: start 
2: ROOT_DEV=/dev/mmcblk0p2 
3: ROOT_DEV_NAME=mmcblk0p2 
root@isar:~# echo $? 
1 
root@isar:~# ls /sys/block/ | grep mmc 
mmcblk0 
root@isar:~# 
So, the easiest way to mask error returned by 'find' is to use something like 'head -1' or 'tee /
dev/null'. In this case script just continues with empty ROOT_DEV_SLAVE.

-- 
Uladzimir Bely


[-- Attachment #2: Type: text/html, Size: 7241 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] expand-on-first-boot: Fix early exit in helper script
  2022-06-10  7:13   ` Uladzimir Bely
@ 2022-06-10  9:32     ` Henning Schild
  2022-06-10 10:03       ` Uladzimir Bely
  0 siblings, 1 reply; 5+ messages in thread
From: Henning Schild @ 2022-06-10  9:32 UTC (permalink / raw)
  To: Uladzimir Bely; +Cc: isar-users, Jan Kiszka

Am Fri, 10 Jun 2022 10:13:54 +0300
schrieb Uladzimir Bely <ubely@ilbers.de>:

> In the email from Friday, 10 June 2022 08:14:19 +03 user Jan Kiszka
> wrote:
> > On 10.06.22 05:19, Uladzimir Bely wrote:  
> > > Changes in 342a751e introduced a regression when error from 'find'
> > > makes helper script early exit (with -1 error code) just after an
> > > attempt to assign ROOT_DEV_SLAVE variable. So, the last partition
> > > remained not resized.
> > > 
> > > Passing 'find' output to 'head -1' masks possible error from
> > > 'find' without breaking the logic and continues the script
> > > execution.
> > > 
> > > Signed-off-by: Uladzimir Bely <ubely@ilbers.de>
> > > ---
> > > 
> > >  .../expand-on-first-boot/files/expand-last-partition.sh
> > > | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git
> > > a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.s
> > > h
> > > b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.s
> > > h index f3e29a1c..934a014f 100755
> > > ---
> > > a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.s
> > > h +++
> > > b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.s
> > > h @@ -11,7 +11,7 @@ set -e
> > > 
> > >  ROOT_DEV="$(findmnt / -o source -n)"
> > >  ROOT_DEV_NAME=${ROOT_DEV##*/}
> > > 
> > > -ROOT_DEV_SLAVE=$(find /sys/block/"${ROOT_DEV_NAME}"/slaves
> > > -mindepth 1 -print -quit 2>/dev/null) +ROOT_DEV_SLAVE=$(find
> > > /sys/block/"${ROOT_DEV_NAME}"/slaves -mindepth 1 -print -quit
> > > 2>/dev/null | head -1)> 
> > >  if [ -n "${ROOT_DEV_SLAVE}" ]; then
> > >  
> > >  	ROOT_DEV=/dev/${ROOT_DEV_SLAVE##*/}
> > >  
> > >  fi  
> > 
> > "-quit" is supposed to obsolete "head -1". Feel free to prove me
> > wrong, but that was the reason for dropping it.
> > 
> > Jan  
> 
> Yes, "-quit" makes 'find' to exit after first match. But if nothing
> found, the return code of 'find' is "-1" and due to script's "set -e"
> it just exits.
> 
> An example that demonstrates the behaviour (on raspberry):
> 
> root@isar:~# cat /boot/test.sh  
> #!/bin/sh 
> 
> set -e 
> 
> echo "1: start" 
> ROOT_DEV="$(findmnt / -o source -n)" 
> echo "2: ROOT_DEV=${ROOT_DEV}" 
> ROOT_DEV_NAME=${ROOT_DEV##*/} 
> echo "3: ROOT_DEV_NAME=${ROOT_DEV_NAME}" 
> ROOT_DEV_SLAVE=$(find /sys/block/"${ROOT_DEV_NAME}"/slaves -mindepth
> 1 -print -quit 
> 2>/dev/null)   
> echo "4: ROOT_DEV_SLAVE=${ROOT_DEV_SLAVE}" 
> root@isar:~# sh /boot/test.sh  
> 1: start 
> 2: ROOT_DEV=/dev/mmcblk0p2 
> 3: ROOT_DEV_NAME=mmcblk0p2 
> root@isar:~# echo $? 
> 1 
> root@isar:~# ls /sys/block/ | grep mmc 
> mmcblk0 
> root@isar:~# 
> So, the easiest way to mask error returned by 'find' is to use
> something like 'head -1' or 'tee / dev/null'. In this case script
> just continues with empty ROOT_DEV_SLAVE.

i think something other than head would be better to catch the case
where find does not find anything.

i.e.

rc=0
find || rc=$?
if [ $rc -eq 0 -a -n "${ROOT_DEV_SLAVE}" ]; then

or simply use a "find || true"

Henning


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] expand-on-first-boot: Fix early exit in helper script
  2022-06-10  9:32     ` Henning Schild
@ 2022-06-10 10:03       ` Uladzimir Bely
  0 siblings, 0 replies; 5+ messages in thread
From: Uladzimir Bely @ 2022-06-10 10:03 UTC (permalink / raw)
  To: Henning Schild; +Cc: isar-users, Jan Kiszka

In the email from Friday, 10 June 2022 12:32:53 +03 user Henning Schild wrote:
> Am Fri, 10 Jun 2022 10:13:54 +0300
> 
> schrieb Uladzimir Bely <ubely@ilbers.de>:
> > In the email from Friday, 10 June 2022 08:14:19 +03 user Jan Kiszka
> > 
> > wrote:
> > > On 10.06.22 05:19, Uladzimir Bely wrote:
> > > > Changes in 342a751e introduced a regression when error from 'find'
> > > > makes helper script early exit (with -1 error code) just after an
> > > > attempt to assign ROOT_DEV_SLAVE variable. So, the last partition
> > > > remained not resized.
> > > > 
> > > > Passing 'find' output to 'head -1' masks possible error from
> > > > 'find' without breaking the logic and continues the script
> > > > execution.
> > > > 
> > > > Signed-off-by: Uladzimir Bely <ubely@ilbers.de>
> > > > ---
> > > > 
> > > >  .../expand-on-first-boot/files/expand-last-partition.sh
> > > >  
> > > > | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
> > > > 
> > > > diff --git
> > > > a/meta/recipes-support/expand-on-first-boot/files/expand-last-partitio
> > > > n.s
> > > > h
> > > > b/meta/recipes-support/expand-on-first-boot/files/expand-last-partitio
> > > > n.s
> > > > h index f3e29a1c..934a014f 100755
> > > > ---
> > > > a/meta/recipes-support/expand-on-first-boot/files/expand-last-partitio
> > > > n.s
> > > > h +++
> > > > b/meta/recipes-support/expand-on-first-boot/files/expand-last-partitio
> > > > n.s
> > > > h @@ -11,7 +11,7 @@ set -e
> > > > 
> > > >  ROOT_DEV="$(findmnt / -o source -n)"
> > > >  ROOT_DEV_NAME=${ROOT_DEV##*/}
> > > > 
> > > > -ROOT_DEV_SLAVE=$(find /sys/block/"${ROOT_DEV_NAME}"/slaves
> > > > -mindepth 1 -print -quit 2>/dev/null) +ROOT_DEV_SLAVE=$(find
> > > > /sys/block/"${ROOT_DEV_NAME}"/slaves -mindepth 1 -print -quit
> > > > 2>/dev/null | head -1)>
> > > > 
> > > >  if [ -n "${ROOT_DEV_SLAVE}" ]; then
> > > >  
> > > >  	ROOT_DEV=/dev/${ROOT_DEV_SLAVE##*/}
> > > >  
> > > >  fi
> > > 
> > > "-quit" is supposed to obsolete "head -1". Feel free to prove me
> > > wrong, but that was the reason for dropping it.
> > > 
> > > Jan
> > 
> > Yes, "-quit" makes 'find' to exit after first match. But if nothing
> > found, the return code of 'find' is "-1" and due to script's "set -e"
> > it just exits.
> > 
> > An example that demonstrates the behaviour (on raspberry):
> > 
> > root@isar:~# cat /boot/test.sh
> > #!/bin/sh
> > 
> > set -e
> > 
> > echo "1: start"
> > ROOT_DEV="$(findmnt / -o source -n)"
> > echo "2: ROOT_DEV=${ROOT_DEV}"
> > ROOT_DEV_NAME=${ROOT_DEV##*/}
> > echo "3: ROOT_DEV_NAME=${ROOT_DEV_NAME}"
> > ROOT_DEV_SLAVE=$(find /sys/block/"${ROOT_DEV_NAME}"/slaves -mindepth
> > 1 -print -quit
> > 2>/dev/null)
> > echo "4: ROOT_DEV_SLAVE=${ROOT_DEV_SLAVE}"
> > root@isar:~# sh /boot/test.sh
> > 1: start
> > 2: ROOT_DEV=/dev/mmcblk0p2
> > 3: ROOT_DEV_NAME=mmcblk0p2
> > root@isar:~# echo $?
> > 1
> > root@isar:~# ls /sys/block/ | grep mmc
> > mmcblk0
> > root@isar:~#
> > So, the easiest way to mask error returned by 'find' is to use
> > something like 'head -1' or 'tee / dev/null'. In this case script
> > just continues with empty ROOT_DEV_SLAVE.
> 
> i think something other than head would be better to catch the case
> where find does not find anything.
> 
> i.e.
> 
> rc=0
> find || rc=$?
> if [ $rc -eq 0 -a -n "${ROOT_DEV_SLAVE}" ]; then
> 
> or simply use a "find || true"
> 
> Henning

Yes, '|| true' also works. And it looks better then 'tee' or 'head'.

I'd also like to make this more clear for me... On my machine I have 
ROOT_DEV_NAME=nvme0n1p3 (partition #3), but in
"/sys/block/" I have only "nvme0n1" (whole device) directory with empty 
"slaves" inside.

Similar thing I see on Raspberry - ROOT_DEV_NAME is a partition on sdcard, 
while only whole (mmcblk0) device is mentioned in "/sys/block/"

In which cases nodes for the partition could exist in "/sys/block", so that 
'find' would not produce errors?

-- 
Uladzimir Bely




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-06-10 10:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-10  3:19 [PATCH] expand-on-first-boot: Fix early exit in helper script Uladzimir Bely
2022-06-10  5:14 ` Jan Kiszka
2022-06-10  7:13   ` Uladzimir Bely
2022-06-10  9:32     ` Henning Schild
2022-06-10 10:03       ` Uladzimir Bely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox