blob: 0ee90078bdfb99b0e08a8c5f35e6cff78dcde182 [file] [log] [blame]
Potin Lai01bd61a2022-09-28 12:00:05 +08001#!/bin/bash -e
2
3DEVICE_NODE="/dev/mmcblk0"
4DEFAULT_FS_TYPE="btrfs"
5
6#
7# check device node exist
8#
9if [[ ! -b "${DEVICE_NODE}" ]]; then
10 echo "${DEVICE_NODE} is not available"
11 exit 0
12fi
13
14#
15# check if device mounted correctly
16#
17if ! (mount || true) | grep "${DEVICE_NODE}"> /dev/null; then
18 echo "${DEVICE_NODE} is not mounted"
19else
20 echo "${DEVICE_NODE} is mounted"
21 exit 0
22fi
23
24#
25# check filesystem
26#
27FS_TYPE_CHECK="$(blkid "${DEVICE_NODE}" | sed 's/.*TYPE="\([^"]*\).*/\1/' || true)"
28
29if [[ "${FS_TYPE_CHECK}" = "" ]]; then
30 FS_TYPE="unknown"
31else
32 FS_TYPE="${FS_TYPE_CHECK}"
33fi
34
35case "${FS_TYPE}" in
36 "${DEFAULT_FS_TYPE}")
37 echo "Filesystem (${FS_TYPE}) found on ${DEVICE_NODE}"
38 ;;
39
40 *)
41 echo "No or unexpected filesystem (${FS_TYPE}) found on ${DEVICE_NODE}"
42 if ! output=$(mkfs."${DEFAULT_FS_TYPE}" --force "${DEVICE_NODE}" 2>&1); then
43 echo "failed to create ${DEFAULT_FS_TYPE} on ${DEVICE_NODE}:"
44 echo "${output}"
45 exit 1
46 fi
47 ;;
48esac