Potin Lai | 01bd61a | 2022-09-28 12:00:05 +0800 | [diff] [blame] | 1 | #!/bin/bash -e |
| 2 | |
| 3 | DEVICE_NODE="/dev/mmcblk0" |
| 4 | DEFAULT_FS_TYPE="btrfs" |
| 5 | |
| 6 | # |
| 7 | # check device node exist |
| 8 | # |
| 9 | if [[ ! -b "${DEVICE_NODE}" ]]; then |
| 10 | echo "${DEVICE_NODE} is not available" |
| 11 | exit 0 |
| 12 | fi |
| 13 | |
| 14 | # |
| 15 | # check if device mounted correctly |
| 16 | # |
| 17 | if ! (mount || true) | grep "${DEVICE_NODE}"> /dev/null; then |
| 18 | echo "${DEVICE_NODE} is not mounted" |
| 19 | else |
| 20 | echo "${DEVICE_NODE} is mounted" |
| 21 | exit 0 |
| 22 | fi |
| 23 | |
| 24 | # |
| 25 | # check filesystem |
| 26 | # |
| 27 | FS_TYPE_CHECK="$(blkid "${DEVICE_NODE}" | sed 's/.*TYPE="\([^"]*\).*/\1/' || true)" |
| 28 | |
| 29 | if [[ "${FS_TYPE_CHECK}" = "" ]]; then |
| 30 | FS_TYPE="unknown" |
| 31 | else |
| 32 | FS_TYPE="${FS_TYPE_CHECK}" |
| 33 | fi |
| 34 | |
| 35 | case "${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 | ;; |
| 48 | esac |