自动 chroot 进入损坏的系统

自动 chroot 进入损坏的系统

每次你必须用 live-boot-CD 挂载一个系统并chroot进入系统来修复 grub 时,你必须输入很多行来挂载到正确的分区:

fdisk -l

例如找到正确的分区/dev/sda1

mount /dev/sda1 /mnt/
mount -t proc none /mnt/proc
mount -o bind /dev /mnt/dev
mount -t sysfs sys /mnt/sys
chroot /mnt/

有没有办法用单个脚本来优化它?

答案1

将此脚本复制到mount-root您的 USB 设备上

#!/bin/bash
if [ "$(whoami &2>/dev/null)" != "root" ] && [ "$(id -un &2>/dev/null)" != "root" ] ; then
 echo "You must be root to run this script!"; echo "use 'sudo !!'"; exit 1
fi


if [ $# -ne 2 ]; then
 echo "calling this script with only two options with the device and mountpoint"
 echo "for example"
 echo "mount-root /dev/sda1 /media/other/"
 set -x
 fdisk -l
 lsblk -f
 exit
fi

D=$(echo $1 | sed 's:/$::')
M=$(echo $2 | sed 's:/$::')

echo mounting $D to $M

mkdir -p $M

mount $D $M
mount -t proc none $M/proc
mount -o bind /dev $M/dev
mount -t sysfs sys $M/sys
mount --bind /dev/pts $M/dev/pts
cp /proc/mounts $M/etc/mtab
chroot $M/ /bin/bash

来源:https://gist.github.com/rubo77/eac3313ea5302acfca60

相关内容