diff mbox series

[isar-cip-core,v3,2/2] initramfs-overlay-hook: Check file system of INITRAMFS_OVERLAY_STORAGE_DEVICE

Message ID 20240708124734.591324-2-clara.kowalsky@siemens.com (mailing list archive)
State Changes Requested
Headers show
Series [isar-cip-core,v3,1/2] initramfs-overlay-hook: Add INITRAMFS_OVERLAY_MOUNT_OPTION | expand

Commit Message

Clara Kowalsky July 8, 2024, 12:47 p.m. UTC
In case of ext*, this detects and fixes file system errors in the
partition device before doing the partition mount.
If the partition turns out to be broken, the factory state is recovered.

Signed-off-by: Clara Kowalsky <clara.kowalsky@siemens.com>
---
 .../initramfs-overlay-hook/files/overlay.hook | 11 +++++++++--
 .../files/overlay.script.tmpl                 | 19 ++++++++++++++++++-
 .../files/overlay_recovery_action.script      | 17 +++++++++++++++++
 .../initramfs-overlay-hook_0.1.bb             |  8 +++++++-
 4 files changed, 51 insertions(+), 4 deletions(-)
 create mode 100644 recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script

Comments

Jan Kiszka July 12, 2024, 7:23 a.m. UTC | #1
On 08.07.24 14:47, Clara Kowalsky wrote:
> In case of ext*, this detects and fixes file system errors in the
> partition device before doing the partition mount.
> If the partition turns out to be broken, the factory state is recovered.
> 
> Signed-off-by: Clara Kowalsky <clara.kowalsky@siemens.com>
> ---
>  .../initramfs-overlay-hook/files/overlay.hook | 11 +++++++++--
>  .../files/overlay.script.tmpl                 | 19 ++++++++++++++++++-
>  .../files/overlay_recovery_action.script      | 17 +++++++++++++++++
>  .../initramfs-overlay-hook_0.1.bb             |  8 +++++++-
>  4 files changed, 51 insertions(+), 4 deletions(-)
>  create mode 100644 recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script
> 
> diff --git a/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook b/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
> index 8b00ecf..6f634c5 100644
> --- a/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
> +++ b/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
> @@ -22,6 +22,13 @@ esac
>  
>  . /usr/share/initramfs-tools/hook-functions
>  
> +hook_error() {
> +    echo "(ERROR): $1" >&2
> +    exit 1
> +}
> +
>  manual_add_modules overlay
> -copy_exec /usr/bin/mountpoint
> -copy_exec /usr/bin/awk
> +copy_exec /usr/bin/mountpoint || hook_error "/usr/bin/mountpoint not found"
> +copy_exec /usr/bin/awk || hook_error "/usr/bin/awk not found"
> +copy_exec /usr/sbin/e2fsck || hook_error "/usr/sbin/e2fsck not found"
> +copy_exec /usr/sbin/mke2fs || hook_error "/usr/sbin/mke2fs not found"
> diff --git a/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl b/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
> index 42eb59c..6120615 100644
> --- a/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
> +++ b/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
> @@ -16,6 +16,11 @@ prereqs()
>  	echo "$PREREQ"
>  }
>  
> +recover_partition_from_fsck_failure()
> +{
> +	true
> +}
> +
>  case $1 in
>  # get pre-requisites
>  prereqs)
> @@ -24,7 +29,13 @@ prereqs)
>  	;;
>  esac
>  
> +ovl_recovery_script="/scripts/${INITRAMFS_OVERLAY_RECOVERY_SCRIPT}"
> +
>  . /scripts/functions
> +# shellcheck source=/scripts/${INITRAMFS_OVERLAY_RECOVERY_SCRIPT}
> +if [ -e "$ovl_recovery_script" ]; then
> +	. "$ovl_recovery_script"
> +fi
>  
>  
>  ovl_partition_device="${INITRAMFS_OVERLAY_STORAGE_DEVICE}"
> @@ -34,9 +45,15 @@ ovl_mount_option="${INITRAMFS_OVERLAY_MOUNT_OPTION}"
>  
>  root_mount_storage=${rootmnt}${ovl_storage_path}
>  storage_mount_point="$(echo "${ovl_storage_path}" | awk -F/ '{print FS$2}' )"
> +partition_fstype=$(get_fstype "${ovl_partition_device}")
>  
>  if ! mountpoint -q "${rootmnt}${storage_mount_point}"; then
> -	if ! mount -t $(get_fstype ${ovl_partition_device}) \
> +	if [ "$partition_fstype" = "ext*" ]; then
> +		if ! e2fsck -p -f "$ovl_partition_device"; then
> +			recover_partition_from_fsck_failure "$ovl_partition_device" "$partition_fstype"
> +		fi
> +	fi
> +	if ! mount -t ${partition_fstype} \
>  		 -o ${ovl_mount_option} \
>  		 ${ovl_partition_device} \
>  		 ${rootmnt}${storage_mount_point}; then
> diff --git a/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script b/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script
> new file mode 100644
> index 0000000..a05e4c3
> --- /dev/null
> +++ b/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script
> @@ -0,0 +1,17 @@
> +#!/bin/sh
> +#
> +# CIP Core, generic profile
> +#
> +# Copyright (c) Siemens AG, 2024
> +#
> +# Authors:
> +#  Clara Kowalsky <clara.kowalsky@siemens.com>
> +#
> +
> +recover_partition_from_fsck_failure()
> +{
> +    ovl_partition_device="$1"
> +    partition_fstype="$2"
> +
> +    mke2fs -t "$partition_fstype" "$ovl_partition_device"
> +}

Why do we need to put the recover logic into a function? If the script
itself is the interface, we can save a lot of boilerplate and can also
avoid that sourcing with the shellcheck annotation.

> diff --git a/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb b/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb
> index 955748f..7097130 100644
> --- a/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb
> +++ b/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb
> @@ -12,9 +12,12 @@
>  
>  inherit dpkg-raw
>  
> +INITRAMFS_OVERLAY_RECOVERY_SCRIPT ??= "overlay_recovery_action.script"
> +
>  SRC_URI += " \
>      file://overlay.hook \
>      file://overlay.script.tmpl \
> +    file://${INITRAMFS_OVERLAY_RECOVERY_SCRIPT} \
>      "
>  
>  # The variable INITRAMFS_OVERLAY_PATHS contains the directories which are
> @@ -35,7 +38,8 @@ TEMPLATE_FILES = "overlay.script.tmpl"
>  TEMPLATE_VARS += " INITRAMFS_OVERLAY_STORAGE_PATH \
>      INITRAMFS_OVERLAY_PATHS \
>      INITRAMFS_OVERLAY_STORAGE_DEVICE \
> -    INITRAMFS_OVERLAY_MOUNT_OPTION"
> +    INITRAMFS_OVERLAY_MOUNT_OPTION \
> +    INITRAMFS_OVERLAY_RECOVERY_SCRIPT"
>  
>  DEBIAN_DEPENDS = "initramfs-tools, awk, coreutils, util-linux"
>  
> @@ -48,4 +52,6 @@ do_install() {
>          "${D}/usr/share/initramfs-tools/hooks/overlay"
>      install -m 0755 "${WORKDIR}/overlay.script" \
>          "${D}/usr/share/initramfs-tools/scripts/local-bottom/overlay"
> +    install -m 0755 "${WORKDIR}/${INITRAMFS_OVERLAY_RECOVERY_SCRIPT}" \
> +        "${D}/usr/share/initramfs-tools/scripts"
>  }

Jan
Gylstorff Quirin July 12, 2024, 7:39 a.m. UTC | #2
On 7/12/24 9:23 AM, Jan Kiszka wrote:
> On 08.07.24 14:47, Clara Kowalsky wrote:
>> In case of ext*, this detects and fixes file system errors in the
>> partition device before doing the partition mount.
>> If the partition turns out to be broken, the factory state is recovered.
>>
>> Signed-off-by: Clara Kowalsky <clara.kowalsky@siemens.com>
>> ---
>>   .../initramfs-overlay-hook/files/overlay.hook | 11 +++++++++--
>>   .../files/overlay.script.tmpl                 | 19 ++++++++++++++++++-
>>   .../files/overlay_recovery_action.script      | 17 +++++++++++++++++
>>   .../initramfs-overlay-hook_0.1.bb             |  8 +++++++-
>>   4 files changed, 51 insertions(+), 4 deletions(-)
>>   create mode 100644 recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script
>>
>> diff --git a/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook b/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
>> index 8b00ecf..6f634c5 100644
>> --- a/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
>> +++ b/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
>> @@ -22,6 +22,13 @@ esac
>>   
>>   . /usr/share/initramfs-tools/hook-functions
>>   
>> +hook_error() {
>> +    echo "(ERROR): $1" >&2
>> +    exit 1
>> +}
>> +
>>   manual_add_modules overlay
>> -copy_exec /usr/bin/mountpoint
>> -copy_exec /usr/bin/awk
>> +copy_exec /usr/bin/mountpoint || hook_error "/usr/bin/mountpoint not found"
>> +copy_exec /usr/bin/awk || hook_error "/usr/bin/awk not found"
>> +copy_exec /usr/sbin/e2fsck || hook_error "/usr/sbin/e2fsck not found"
>> +copy_exec /usr/sbin/mke2fs || hook_error "/usr/sbin/mke2fs not found"
>> diff --git a/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl b/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
>> index 42eb59c..6120615 100644
>> --- a/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
>> +++ b/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
>> @@ -16,6 +16,11 @@ prereqs()
>>   	echo "$PREREQ"
>>   }
>>   
>> +recover_partition_from_fsck_failure()
>> +{
>> +	true
>> +}
>> +
>>   case $1 in
>>   # get pre-requisites
>>   prereqs)
>> @@ -24,7 +29,13 @@ prereqs)
>>   	;;
>>   esac
>>   
>> +ovl_recovery_script="/scripts/${INITRAMFS_OVERLAY_RECOVERY_SCRIPT}"
>> +
>>   . /scripts/functions
>> +# shellcheck source=/scripts/${INITRAMFS_OVERLAY_RECOVERY_SCRIPT}
>> +if [ -e "$ovl_recovery_script" ]; then
>> +	. "$ovl_recovery_script"
>> +fi
>>   
>>   
>>   ovl_partition_device="${INITRAMFS_OVERLAY_STORAGE_DEVICE}"
>> @@ -34,9 +45,15 @@ ovl_mount_option="${INITRAMFS_OVERLAY_MOUNT_OPTION}"
>>   
>>   root_mount_storage=${rootmnt}${ovl_storage_path}
>>   storage_mount_point="$(echo "${ovl_storage_path}" | awk -F/ '{print FS$2}' )"
>> +partition_fstype=$(get_fstype "${ovl_partition_device}")
>>   
>>   if ! mountpoint -q "${rootmnt}${storage_mount_point}"; then
>> -	if ! mount -t $(get_fstype ${ovl_partition_device}) \
>> +	if [ "$partition_fstype" = "ext*" ]; then
>> +		if ! e2fsck -p -f "$ovl_partition_device"; then
>> +			recover_partition_from_fsck_failure "$ovl_partition_device" "$partition_fstype"
>> +		fi
>> +	fi
>> +	if ! mount -t ${partition_fstype} \
>>   		 -o ${ovl_mount_option} \
>>   		 ${ovl_partition_device} \
>>   		 ${rootmnt}${storage_mount_point}; then
>> diff --git a/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script b/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script
>> new file mode 100644
>> index 0000000..a05e4c3
>> --- /dev/null
>> +++ b/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script
>> @@ -0,0 +1,17 @@
>> +#!/bin/sh
>> +#
>> +# CIP Core, generic profile
>> +#
>> +# Copyright (c) Siemens AG, 2024
>> +#
>> +# Authors:
>> +#  Clara Kowalsky <clara.kowalsky@siemens.com>
>> +#
>> +
>> +recover_partition_from_fsck_failure()
>> +{
>> +    ovl_partition_device="$1"
>> +    partition_fstype="$2"
>> +
>> +    mke2fs -t "$partition_fstype" "$ovl_partition_device"
>> +}
> 
> Why do we need to put the recover logic into a function? If the script
> itself is the interface, we can save a lot of boilerplate and can also
> avoid that sourcing with the shellcheck annotation.

I suggested the function to allow an way to overwrite the recovery logic
in case another behavior is required by downstream.

You would call the script directly?

Quirin

> 
>> diff --git a/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb b/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb
>> index 955748f..7097130 100644
>> --- a/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb
>> +++ b/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb
>> @@ -12,9 +12,12 @@
>>   
>>   inherit dpkg-raw
>>   
>> +INITRAMFS_OVERLAY_RECOVERY_SCRIPT ??= "overlay_recovery_action.script"
>> +
>>   SRC_URI += " \
>>       file://overlay.hook \
>>       file://overlay.script.tmpl \
>> +    file://${INITRAMFS_OVERLAY_RECOVERY_SCRIPT} \
>>       "
>>   
>>   # The variable INITRAMFS_OVERLAY_PATHS contains the directories which are
>> @@ -35,7 +38,8 @@ TEMPLATE_FILES = "overlay.script.tmpl"
>>   TEMPLATE_VARS += " INITRAMFS_OVERLAY_STORAGE_PATH \
>>       INITRAMFS_OVERLAY_PATHS \
>>       INITRAMFS_OVERLAY_STORAGE_DEVICE \
>> -    INITRAMFS_OVERLAY_MOUNT_OPTION"
>> +    INITRAMFS_OVERLAY_MOUNT_OPTION \
>> +    INITRAMFS_OVERLAY_RECOVERY_SCRIPT"
>>   
>>   DEBIAN_DEPENDS = "initramfs-tools, awk, coreutils, util-linux"
>>   
>> @@ -48,4 +52,6 @@ do_install() {
>>           "${D}/usr/share/initramfs-tools/hooks/overlay"
>>       install -m 0755 "${WORKDIR}/overlay.script" \
>>           "${D}/usr/share/initramfs-tools/scripts/local-bottom/overlay"
>> +    install -m 0755 "${WORKDIR}/${INITRAMFS_OVERLAY_RECOVERY_SCRIPT}" \
>> +        "${D}/usr/share/initramfs-tools/scripts"
>>   }
> 
> Jan
>
Jan Kiszka July 12, 2024, 7:47 a.m. UTC | #3
On 12.07.24 09:39, Gylstorff Quirin wrote:
> 
> 
> On 7/12/24 9:23 AM, Jan Kiszka wrote:
>> On 08.07.24 14:47, Clara Kowalsky wrote:
>>> In case of ext*, this detects and fixes file system errors in the
>>> partition device before doing the partition mount.
>>> If the partition turns out to be broken, the factory state is recovered.
>>>
>>> Signed-off-by: Clara Kowalsky <clara.kowalsky@siemens.com>
>>> ---
>>>   .../initramfs-overlay-hook/files/overlay.hook | 11 +++++++++--
>>>   .../files/overlay.script.tmpl                 | 19 ++++++++++++++++++-
>>>   .../files/overlay_recovery_action.script      | 17 +++++++++++++++++
>>>   .../initramfs-overlay-hook_0.1.bb             |  8 +++++++-
>>>   4 files changed, 51 insertions(+), 4 deletions(-)
>>>   create mode 100644
>>> recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script
>>>
>>> diff --git
>>> a/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
>>> b/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
>>> index 8b00ecf..6f634c5 100644
>>> --- a/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
>>> +++ b/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
>>> @@ -22,6 +22,13 @@ esac
>>>     . /usr/share/initramfs-tools/hook-functions
>>>   +hook_error() {
>>> +    echo "(ERROR): $1" >&2
>>> +    exit 1
>>> +}
>>> +
>>>   manual_add_modules overlay
>>> -copy_exec /usr/bin/mountpoint
>>> -copy_exec /usr/bin/awk
>>> +copy_exec /usr/bin/mountpoint || hook_error "/usr/bin/mountpoint not
>>> found"
>>> +copy_exec /usr/bin/awk || hook_error "/usr/bin/awk not found"
>>> +copy_exec /usr/sbin/e2fsck || hook_error "/usr/sbin/e2fsck not found"
>>> +copy_exec /usr/sbin/mke2fs || hook_error "/usr/sbin/mke2fs not found"
>>> diff --git
>>> a/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
>>> b/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
>>> index 42eb59c..6120615 100644
>>> --- a/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
>>> +++ b/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
>>> @@ -16,6 +16,11 @@ prereqs()
>>>       echo "$PREREQ"
>>>   }
>>>   +recover_partition_from_fsck_failure()
>>> +{
>>> +    true
>>> +}
>>> +
>>>   case $1 in
>>>   # get pre-requisites
>>>   prereqs)
>>> @@ -24,7 +29,13 @@ prereqs)
>>>       ;;
>>>   esac
>>>   +ovl_recovery_script="/scripts/${INITRAMFS_OVERLAY_RECOVERY_SCRIPT}"
>>> +
>>>   . /scripts/functions
>>> +# shellcheck source=/scripts/${INITRAMFS_OVERLAY_RECOVERY_SCRIPT}
>>> +if [ -e "$ovl_recovery_script" ]; then
>>> +    . "$ovl_recovery_script"
>>> +fi
>>>       ovl_partition_device="${INITRAMFS_OVERLAY_STORAGE_DEVICE}"
>>> @@ -34,9 +45,15 @@ ovl_mount_option="${INITRAMFS_OVERLAY_MOUNT_OPTION}"
>>>     root_mount_storage=${rootmnt}${ovl_storage_path}
>>>   storage_mount_point="$(echo "${ovl_storage_path}" | awk -F/ '{print
>>> FS$2}' )"
>>> +partition_fstype=$(get_fstype "${ovl_partition_device}")
>>>     if ! mountpoint -q "${rootmnt}${storage_mount_point}"; then
>>> -    if ! mount -t $(get_fstype ${ovl_partition_device}) \
>>> +    if [ "$partition_fstype" = "ext*" ]; then
>>> +        if ! e2fsck -p -f "$ovl_partition_device"; then
>>> +            recover_partition_from_fsck_failure
>>> "$ovl_partition_device" "$partition_fstype"
>>> +        fi
>>> +    fi
>>> +    if ! mount -t ${partition_fstype} \
>>>            -o ${ovl_mount_option} \
>>>            ${ovl_partition_device} \
>>>            ${rootmnt}${storage_mount_point}; then
>>> diff --git
>>> a/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script b/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script
>>> new file mode 100644
>>> index 0000000..a05e4c3
>>> --- /dev/null
>>> +++
>>> b/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script
>>> @@ -0,0 +1,17 @@
>>> +#!/bin/sh
>>> +#
>>> +# CIP Core, generic profile
>>> +#
>>> +# Copyright (c) Siemens AG, 2024
>>> +#
>>> +# Authors:
>>> +#  Clara Kowalsky <clara.kowalsky@siemens.com>
>>> +#
>>> +
>>> +recover_partition_from_fsck_failure()
>>> +{
>>> +    ovl_partition_device="$1"
>>> +    partition_fstype="$2"
>>> +
>>> +    mke2fs -t "$partition_fstype" "$ovl_partition_device"
>>> +}
>>
>> Why do we need to put the recover logic into a function? If the script
>> itself is the interface, we can save a lot of boilerplate and can also
>> avoid that sourcing with the shellcheck annotation.
> 
> I suggested the function to allow an way to overwrite the recovery logic
> in case another behavior is required by downstream.
> 
> You would call the script directly?

Then I'm not yet getting the idea completely yet: You are already
testing for the existence of that script and only source it when
available, and the default function is "do nothing". So, what is the
benefit of that compared to testing for the script directly where the
function is called now?

Jan
diff mbox series

Patch

diff --git a/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook b/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
index 8b00ecf..6f634c5 100644
--- a/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
+++ b/recipes-initramfs/initramfs-overlay-hook/files/overlay.hook
@@ -22,6 +22,13 @@  esac
 
 . /usr/share/initramfs-tools/hook-functions
 
+hook_error() {
+    echo "(ERROR): $1" >&2
+    exit 1
+}
+
 manual_add_modules overlay
-copy_exec /usr/bin/mountpoint
-copy_exec /usr/bin/awk
+copy_exec /usr/bin/mountpoint || hook_error "/usr/bin/mountpoint not found"
+copy_exec /usr/bin/awk || hook_error "/usr/bin/awk not found"
+copy_exec /usr/sbin/e2fsck || hook_error "/usr/sbin/e2fsck not found"
+copy_exec /usr/sbin/mke2fs || hook_error "/usr/sbin/mke2fs not found"
diff --git a/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl b/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
index 42eb59c..6120615 100644
--- a/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
+++ b/recipes-initramfs/initramfs-overlay-hook/files/overlay.script.tmpl
@@ -16,6 +16,11 @@  prereqs()
 	echo "$PREREQ"
 }
 
+recover_partition_from_fsck_failure()
+{
+	true
+}
+
 case $1 in
 # get pre-requisites
 prereqs)
@@ -24,7 +29,13 @@  prereqs)
 	;;
 esac
 
+ovl_recovery_script="/scripts/${INITRAMFS_OVERLAY_RECOVERY_SCRIPT}"
+
 . /scripts/functions
+# shellcheck source=/scripts/${INITRAMFS_OVERLAY_RECOVERY_SCRIPT}
+if [ -e "$ovl_recovery_script" ]; then
+	. "$ovl_recovery_script"
+fi
 
 
 ovl_partition_device="${INITRAMFS_OVERLAY_STORAGE_DEVICE}"
@@ -34,9 +45,15 @@  ovl_mount_option="${INITRAMFS_OVERLAY_MOUNT_OPTION}"
 
 root_mount_storage=${rootmnt}${ovl_storage_path}
 storage_mount_point="$(echo "${ovl_storage_path}" | awk -F/ '{print FS$2}' )"
+partition_fstype=$(get_fstype "${ovl_partition_device}")
 
 if ! mountpoint -q "${rootmnt}${storage_mount_point}"; then
-	if ! mount -t $(get_fstype ${ovl_partition_device}) \
+	if [ "$partition_fstype" = "ext*" ]; then
+		if ! e2fsck -p -f "$ovl_partition_device"; then
+			recover_partition_from_fsck_failure "$ovl_partition_device" "$partition_fstype"
+		fi
+	fi
+	if ! mount -t ${partition_fstype} \
 		 -o ${ovl_mount_option} \
 		 ${ovl_partition_device} \
 		 ${rootmnt}${storage_mount_point}; then
diff --git a/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script b/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script
new file mode 100644
index 0000000..a05e4c3
--- /dev/null
+++ b/recipes-initramfs/initramfs-overlay-hook/files/overlay_recovery_action.script
@@ -0,0 +1,17 @@ 
+#!/bin/sh
+#
+# CIP Core, generic profile
+#
+# Copyright (c) Siemens AG, 2024
+#
+# Authors:
+#  Clara Kowalsky <clara.kowalsky@siemens.com>
+#
+
+recover_partition_from_fsck_failure()
+{
+    ovl_partition_device="$1"
+    partition_fstype="$2"
+
+    mke2fs -t "$partition_fstype" "$ovl_partition_device"
+}
diff --git a/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb b/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb
index 955748f..7097130 100644
--- a/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb
+++ b/recipes-initramfs/initramfs-overlay-hook/initramfs-overlay-hook_0.1.bb
@@ -12,9 +12,12 @@ 
 
 inherit dpkg-raw
 
+INITRAMFS_OVERLAY_RECOVERY_SCRIPT ??= "overlay_recovery_action.script"
+
 SRC_URI += " \
     file://overlay.hook \
     file://overlay.script.tmpl \
+    file://${INITRAMFS_OVERLAY_RECOVERY_SCRIPT} \
     "
 
 # The variable INITRAMFS_OVERLAY_PATHS contains the directories which are
@@ -35,7 +38,8 @@  TEMPLATE_FILES = "overlay.script.tmpl"
 TEMPLATE_VARS += " INITRAMFS_OVERLAY_STORAGE_PATH \
     INITRAMFS_OVERLAY_PATHS \
     INITRAMFS_OVERLAY_STORAGE_DEVICE \
-    INITRAMFS_OVERLAY_MOUNT_OPTION"
+    INITRAMFS_OVERLAY_MOUNT_OPTION \
+    INITRAMFS_OVERLAY_RECOVERY_SCRIPT"
 
 DEBIAN_DEPENDS = "initramfs-tools, awk, coreutils, util-linux"
 
@@ -48,4 +52,6 @@  do_install() {
         "${D}/usr/share/initramfs-tools/hooks/overlay"
     install -m 0755 "${WORKDIR}/overlay.script" \
         "${D}/usr/share/initramfs-tools/scripts/local-bottom/overlay"
+    install -m 0755 "${WORKDIR}/${INITRAMFS_OVERLAY_RECOVERY_SCRIPT}" \
+        "${D}/usr/share/initramfs-tools/scripts"
 }