sudo umount /mntはシェルスクリプトでは実行されませんが、コマンドラインで成功するのはなぜですか?

sudo umount /mntはシェルスクリプトでは実行されませんが、コマンドラインで成功するのはなぜですか?

環境で次のスクリプトを実行すると、Debian 12 liveこのスクリプトでは実行できないことがわかりましたumount /mnt。プロンプトはです/mnt : target is busy。ただし、スクリプトが実行された後は、sudo umount /mntコマンドラインから正常に実行できます。このスクリプトに777権限を追加しました。問題は何ですか?

#!/usr/bin/bash
set +x
current_path=$(pwd)
echo $current_path
hdstr=/dev/mmcblk0
hdstr1=${hdstr}p1
hdstr2=${hdstr}p2
hdstr3=${hdstr}p3

root_path=/mnt
boot_path=${root_path}/boot
grub_path=${boot_path}/grub
efi_path=${boot_path}/efi

lib64_path=${root_path}/lib64
bin_path=${root_path}/bin
lib_path=${root_path}/lib

echo "debug:function definition"
    
execute_command() {
    local command=$1
    eval "$command"
    local status=$?
    if [ $status -eq 0 ]; then
        echo "run the cmd:$command success"
    else
        echo "failed to run the cmd:$command" 
        exit 1
    fi
}

add_env_for_chroot() {
    echo "add env for chroot"
    execute_command "sudo mkdir -p ${lib64_path}"
    execute_command "sudo ln -s ${lib_path} ${lib64_path}"
    execute_command "sudo cp ${lib_path}/* ${lib64_path}/ -nra"
}


change_root() {
    echo "change root"
    #execute_command "sudo mount --rbind /dev  /mnt/dev"
    #execute_command "sudo mount --rbind /proc /mnt/proc"
    #execute_command "sudo mount --rbind /sys  /mnt/sys"
    execute_command "sudo mount -t proc proc /mnt/proc"
    execute_command "sudo mount -t sysfs sys /mnt/sys"
    execute_command "sudo mount -o bind /dev /mnt/dev"
    execute_command "sudo mount --bind /run /mnt/run"
    add_env_for_chroot

    
    cat << EOF | sudo chroot ${root_path}
    grub-install --target=x86_64-efi /dev/mmcblk0 --force --recheck --efi-directory=/boot/efi 
    exit
EOF
}

create_fstab() {
    echo "create fstab"
    UUID1=$(sudo blkid | grep '^/dev/mmcblk0p1' | awk -F 'UUID="' '{print $2}' | awk -F '"' '{print $1}')
    UUID2=$(sudo blkid | grep '^/dev/mmcblk0p2' | awk -F 'UUID="' '{print $2}' | awk -F '"' '{print $1}')
    UUID3=$(sudo blkid | grep '^/dev/mmcblk0p3' | awk -F 'UUID="' '{print $2}' | awk -F '"' '{print $1}')
    #这里不替换sda
    devName="\/dev\/mmcblk0p1"
    UUIDStr="UUID=$UUID1"
    execute_command "sed -i \"s/${devName}/${UUIDStr}/g\"  /mnt/etc/fstab"
    
    devName="\/dev\/mmcblk0p2"
    UUIDStr="UUID=$UUID2"
    execute_command "sed -i \"s/${devName}/${UUIDStr}/g\"  /mnt/etc/fstab"
    
    devName="\/dev\/mmcblk0p3"
    UUIDStr="UUID=$UUID3"
    execute_command "sed -i \"s/${devName}/${UUIDStr}/g\"  /mnt/etc/fstab"
}

partition_format_mount() {
    echo "begin to part"
    ####partition####
    sudo /sbin/parted ${hdstr} mklabel gpt
    sudo /sbin/parted ${hdstr} <<EOT 1>/dev/null 2>/dev/null || exit 1
    rm 1
    rm 2
    rm 3
    rm 4
    mkpart primary fat32 1MiB 200MiB
    set 1 esp on
    mkpart primary ext4 200MiB 8200MiB
    mkpart primary ext4 8200MiB 100%
    quit
EOT
    echo ""
    ####format####
    echo "begin to format"
    #execute_command "sudo partx ${hdstr} 1>/dev/null"
    execute_command "sudo /sbin/mkfs.fat -F32 ${hdstr1} 1>/dev/null"
    execute_command "sudo /sbin/mkfs.ext4 ${hdstr2} 1>/dev/null"
    execute_command "sudo /sbin/mkfs.ext4 ${hdstr3} 1>/dev/null"
    
    execute_command "sudo /sbin/fsck.vfat -v -a -w ${hdstr1} 1>/dev/null"
    execute_command "sudo /sbin/fsck.ext4 ${hdstr2}"
    execute_command "sudo /sbin/fsck.ext4 ${hdstr3}"
    ####mount####
    execute_command "sudo mkdir -p ${root_path}"
    execute_command "sudo mount ${hdstr3} ${root_path} 1>/dev/null"
    execute_command "sudo mkdir ${boot_path} -p"
    execute_command "sudo mount ${hdstr2} ${boot_path} 1>/dev/null"
    execute_command "sudo mkdir ${efi_path} -p"
    execute_command "sudo mount ${hdstr1} ${efi_path} 1>/dev/null"
    echo "end part"
}

install_os(){
    echo "begin to install grub"
    execute_command "sudo mkdir -p ${grub_path}"
    execute_command "sudo cp ${current_path}/grub_CRA.cfg ${grub_path}/grub.cfg"


    #execute_command "sudo cp ${current_path}/x86_64-efi  /usr/lib/grub/  -raf"  #issue1
    execute_command "sudo cp \"${current_path}/bzImage\" ${boot_path}/"        #2024-1-19 修改bzImage路径
    execute_command "sudo cp \"${current_path}/initrd.img-6.4.0-rt8\" ${boot_path}"  #2024-1-19 修改initrd路径
    execute_command "sudo cp \"${current_path}/rootfs.tar.gz\" ${root_path}"
    cd ${root_path}
    execute_command "sudo tar -vxf rootfs.tar.gz"
    execute_command "echo \"y\" | sudo rm rootfs.tar.gz"
    echo "begin to changerooot" 
    change_root
    create_fstab
    
    echo "start to umount"  
    execute_command "sudo umount ${hdstr1} 1>/dev/null"
    execute_command "sudo umount ${hdstr2} 1>/dev/null"
    execute_command "sudo umount /mnt/run"
    #execute_command "sudo umount /mnt/{proc,sys,dev}"
    execute_command "sudo umount /mnt/proc"
    execute_command "sudo umount /mnt/sys"
    execute_command "sudo umount /mnt/dev"
    #cat << EOF | sudo chroot ${root_path}
    #echo "To successfully umount /mnt, I don't know why, but it is useful." 
    #exit
#EOF
    execute_command "sync"
#==========================my question in here=============================
    sudo umount /mnt 
#==========================================================================
#   execute_command "sudo umount ${hdstr3} 1>/dev/null"
    #sudo /sbin/fsck.vfat -a ${hdstr1}
    #sudo /sbin/fsck.ext4 -a ${hdstr2}
    #sudo /sbin/fsck.ext4 -a ${hdstr3}
    execute_command "sync" 
}

mainFunc(){
    echo "start to burn"
    partition_format_mount
    install_os    
}

mainFunc

ベストアンサー1

スクリプトの上にあるものと同等のものがあるので、cd /mnt削除したいディレクトリにあります。

ここで考慮すべきいくつかの他の重要な質問があります。

  1. スクリプト内のほぼすべてのコマンドはで実行されるため、その呼び出しをすべてsudo削除し、スクリプトsudo自体がで実行されるようにする必要がありますsudo

    #!/bin/bash
    if [ "$(id -u)" -ne 0 ]
    then
        echo "Please re-run with sudo" >&2
        exit 1
    fi
    

    または、スクリプトが自動的に再実行されるようにすることもできますsudo

    #!/bin/bash
    if [ "$(id -u)" -ne 0 ]
    then
        echo "Re-running with sudo" >&2
        exec sudo "$0" "$@"
        exit 1
    fi
    
  2. コマンドを変数に保存します。これは、データに変数を使用し、コマンドに関数を使用している間にあらゆる種類の参照関連の問題を引き起こす可能性がありますeval

    execute_command() {
        if "$@"
        then
            echo "ran $1 successfully" >&2
        else
            echo "failed to run $1" >&2
            exit 1
        fi
    }
    
    execute_command sudo umount /mnt/proc
    

    しかし、実際には、最初の行に設定-e(または行ごとのトレースを有効にするフラグ)を使用するか、すぐ後ろに使用するだけで-ex達成できます。例えば、-xset -eset -x

    #!/bin/bash -ex
    echo "this line succeeds"
    echo "the next line will fail"
    false
    echo "the script has already stopped so you will not see this"
    exit 0
    
  3. 変数を二重引用符で囲んでいません。つまり、使用するたびに分割とワイルドカードの影響を受けます。

  4. このスクリプトに777権限を追加しました。- 他のユーザーがこのスクリプトを編集できるようにする必要はないため、その権限を放棄しないでください。他のユーザーがスクリプトを実行できるようにすることはできません。

    chmod u=rwx,go=rx ./your_script    # Replace "./yourscript" with the script path
    

おすすめ記事