Skip to content

Instantly share code, notes, and snippets.

@tpu01yzx
Last active November 6, 2023 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpu01yzx/48cc13518fb19aaa00708a0551049082 to your computer and use it in GitHub Desktop.
Save tpu01yzx/48cc13518fb19aaa00708a0551049082 to your computer and use it in GitHub Desktop.
#!/bin/bash
# 确保脚本以 root 用户执行
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root."
fi
echo "mounting /dev/vda1 to /mnt"
# 挂载根分区
mount /dev/vda1 /mnt
# 检查挂载是否成功
if [ $? -ne 0 ]; then
echo "Failed to mount /dev/vda1 to /mnt."
fi
echo "enabling swap on /dev/vda2"
# 启用交换分区
swapon /dev/vda2
# 检查 swapon 是否成功
if [ $? -ne 0 ]; then
echo "Failed to enable swap on /dev/vda2."
fi
echo "generating fstab file"
# 保留 /mnt/etc/fstab 文件中的注释内容
grep '^#' /mnt/etc/fstab > /mnt/etc/fstab.new
# 生成新的 fstab 记录并追加到 /mnt/etc/fstab
genfstab -U /mnt >> /mnt/etc/fstab.new
# 检查 genfstab 是否成功
if [ $? -ne 0 ]; then
echo "Failed to generate fstab."
fi
# 替换旧的 fstab 文件
mv /mnt/etc/fstab /mnt/etc/fstab.old
mv /mnt/etc/fstab.new /mnt/etc/fstab
echo "mouting other system direcotries :dev dev/pts proc sys run; "
# 挂载必要的系统文件夹
for dir in dev dev/pts proc sys run; do
if mountpoint -q "/mnt/$dir"; then
echo "/mnt/$dir is already mounted."
else
mount --bind "/$dir" "/mnt/$dir"
# 如果要挂载的是 /dev,那么我们需要确保 /dev/pts 也被挂载
if [ "$dir" == "dev" ]; then
mount --bind /dev/pts /mnt/dev/pts
fi
fi
done
echo "entering chroot"
# 使用 arch-chroot 进入新环境
#arch-chroot /mnt
chroot /mnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment