自用chroot-system脚本

@Ta 2021-01-14发布,05-21 16:13修改 7632点击
mount --rbind /dev dev
mount --rbind /proc proc
mount --rbind /sys sys
mount --rbind /run run
mount --rbind /tmp tmp
chroot . /bin/bash -l

警告:该脚本退出时虽然会尝试自动 umount 分区,但经常会 umount 失败。如果看到 umount 失败的提示,请勿用 rm -rf 删除 rootfs 文件夹,否则 mount 进去的文件夹会被删除。


用于在livecd中切换到已经挂载好的根文件系统去执行修复操作。

使用方法(请根据你的磁盘实际情况修改sdx1sdx2):

# 切换到root
sudo -i
# 挂载EFI分区(重要,之前未进行)
mount /dev/sdx1 /boot/efi
# 挂载根文件系统
mount /dev/sdx2 /mnt
# 进入根文件系统
chroot-system /mnt
# 挂载系统需要的其他分区
mount -a
# 查看mount是否成功
mount
# 重建initramfs
update-initramfs -k all -u
# 更新GRUB
update-grub
# 修复并升级软件包
apt install -f
apt update
apt dist-upgrade#!/bin/sh

if [ "$1" = "" ]; then
    chroot
    exit
fi

cd "$1"
mount --bind /dev dev
mount --bind /dev/pts dev/pts
mount --bind /dev/shm dev/shm
mount --bind /dev/snd dev/snd

mount --bind /proc proc
mount --bind /sys sys
mount --bind /run run
mount --bind /tmp tmp

mount --bind /boot/efi boot/efi

if ! [ -f "etc/resolv.conf" ]; then
        rm etc/resolv.conf
        touch etc/resolv.conf
fi
mount --bind /etc/resolv.conf etc/resolv.conf

shift
chroot . "$@"

umount etc/resolv.conf boot/efi tmp run sys proc dev/shm dev/pts dev/snd dev

内容,注意创建文件后要给执行权限才能用:

chmod +x /usr/local/bin/chroot-system

#!/bin/sh

if [ "$1" = "" ]; then
    chroot
    exit
fi

cd "$1"
mount --bind /dev dev
mount --bind /dev/pts dev/pts
mount --bind /dev/shm dev/shm
mount --bind /dev/snd dev/snd

mount --bind /proc proc
mount --bind /sys sys
mount --bind /run run
mount --bind /tmp tmp

mount --bind /boot/efi boot/efi

if ! [ -f "etc/resolv.conf" ]; then
        rm etc/resolv.conf
        touch etc/resolv.conf
fi
mount --bind /etc/resolv.conf etc/resolv.conf

shift
chroot . "$@"

umount etc/resolv.conf boot/efi tmp run sys proc dev/shm dev/pts dev/snd dev
回复列表(1|隐藏机器人聊天)
添加新回复
回复需要登录