ChromeOS下Bash脚本if-elif-else逻辑失效,rootfs验证检测异常求助
Let's break down what's wrong with your original script logic and implement a cleaner, reliable solution that properly handles the rootfs verification case.
Problem Analysis: Flawed Original Logic
Your initial if-elif-else structure is mutually exclusive, which creates a critical gap:
- When
/is mounted read-only, the firstifruns themountcommand—but whether that mount succeeds or fails, the script skips theelifbranch entirely (since theifcondition was true). It then proceeds to execute the rest of your code, never hitting the error prompt. - If
/starts as read-write, themount.outfile doesn't exist at all, so theelif'sgrepcommand throws an invisible error and fails to trigger the message.
This is why you saw the script chug along until it hit Read-only file system errors—it never entered the error-handling path when the remount failed due to active rootfs verification.
Elegant Solutions: Check Mount Status Properly
We can ditch the redundant mount.out file and directly verify the remount success using either command exit status or a recheck of the actual mount state.
Solution 1: Use mount Command Exit Status
This is the most straightforward approach—rely on the command's built-in success/failure signal:
# ChromeOS's Specific!!! rootfs="/dev/root" # Ensure this variable is defined earlier in your script # Check if / is mounted read-only if grep -q "${rootfs}.*ro," /proc/mounts; then # Attempt to remount as read-write if ! mount -o remount,rw /; then # Remount failed—rootfs verification is still enabled echo -e "\nRootfs verification for the root partition must be removed to remount / as read-write." echo "To fix this, run the following command and reboot your system:" echo "sudo /usr/share/vboot/bin/make_dev_ssd.sh --remove_rootfs_verification --partitions 4" exit 1 fi fi
grep -qruns silently, avoiding unnecessary output clutter.! mount ...checks if the remount command failed directly, no temp files needed.- The error message is clear and actionable for ChromeOS users.
Solution 2: Recheck Mount State (More Robust)
If you want to be extra sure (in case edge cases affect the mount command's exit status), recheck /proc/mounts after the attempt:
# ChromeOS's Specific!!! rootfs="/dev/root" if grep -q "${rootfs}.*ro," /proc/mounts; then # Attempt remount, discard output mount -o remount,rw / &>/dev/null # Verify if the mount actually switched to read-write if grep -q "${rootfs}.*ro," /proc/mounts; then echo -e "\nRootfs verification is still enabled—cannot remount / as read-write." echo "Run this command and reboot to disable it:" echo "sudo /usr/share/vboot/bin/make_dev_ssd.sh --remove_rootfs_verification --partitions 4" exit 1 fi fi
This approach ignores the mount command's output and only cares about the actual current state of the filesystem, making it resilient to any unexpected command behavior.
Why Your Temporary Nested if Worked
Your quick fix with nested if statements was actually on the right track—it checked the remount result immediately after running the command. The solutions above just refine that logic to be cleaner, more reliable, and avoid relying on temporary files.
Extra Tips for Your Script
- Always quote variables like
"${rootfs}"to avoid issues if the variable ever contains special characters. - Use
echo -eto handle newline characters reliably (single-quoted'\n'doesn't work in all shell environments). - Before writing to
/etc/env.d/30kvmlater in your script, add a quick check to confirm/is read-write—this prevents unexpected "Read-only file system" errors even if something slips through the initial check.
内容的提问来源于stack exchange,提问作者mailman

