www.colben.cn/content/post/centos6-migrate.md
2021-11-14 14:32:08 +08:00

64 lines
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Centos6 系统盘迁移"
date: 2019-10-30T13:34:47+08:00
lastmod: 2019-10-30T13:34:47+08:00
tags: ["centos", "系统盘迁移"]
categories: ["os"]
---
# 环境
- Linux 物理机,已安装 VirtualBox 虚拟机软件
- CentOS6.9 live 启动 U 盘
- CentOS6.9 iso 镜像文件
- 待安装笔记本 IBM x32
# VirtualBox 创建 Redhat6 虚拟机
- VMware 没用过,建议硬件配置尽量和目标设备一致
- Thinkpad X32 的处理器只有一核,无 PAE内存 1024MB
- 虚拟硬盘 8G 就够了,使用 CentOS6.9 iso 装好虚拟机后,禁用 selinux可能需要重启生效。
# 打包操作系统根目录
```
cd /
tar cvpzf backup.tgz --exclude=/backup.tgz --one-file-system /
```
# 导出 /backup.tgz 文件
- 返回物理机操作系统,通过 ssh、http 或其他方式把虚拟机的 /backup.tgz 拷贝至物理机中 /root 下
# 格式化磁盘
- 取出待安装笔记本的硬盘,通过 USB 或其他方式挂载到该物理机上fdisk 分区,格式化
```
#fdisk 分成俩个分区前面一个大的根分区后面一个2G的 swap 分区,其他情况自己决定
#假设刚挂载的这个目标磁盘设备是 sdg
mkfs.ext3 /dev/sdg1
mkswap /dev/sdg2
```
# 部署操作系统
- 挂载待部署磁盘的根分区,解压操作系统文件,修改启动相关参数
```
mount /dev/sdg1 /mnt
tar xvpzf /root/backup.tgz -C /mnt/
#查看目标磁盘根分区的 uuid替换 /mnt/boot/grub/grub.conf 和 /mnt/etc/fstab 中对应项
ls -lh /dev/disk/by-uuid/|grep sdg1
#查看目标磁盘 swap 分区的uuid替换 /mnt/etc/fstab 中对应项
ls -lh /dev/disk/by-uuid/|grep sdg2
#检查 /mnt/etc/mtab 中列出的信息是否正确
```
# 安装 grub2
- 取消挂载,把部署好的磁盘安装回待安装笔记本中,插上 CentOS6.9 的启动 U 盘,从 U 盘启动待安装笔记本,进入 live 模式(安装盘可以在安装界面开始时按下 Alt + F1 切换到 live 模式下),安装 grub2
```
#在 live 模式下切换到 root
sudo -i
#挂载(假设 live 识别到的硬盘根分区是 /dev/sda1)
mount /dev/sda1 /mnt
#安装 grub2
grub-install --root-directory=/mnt/ /dev/sda
#如果显示 “no error”即可退出取消挂载
exit
umount -f /mnt
```
- 此时重启就可以正常进入系统了