为了描述方便:定义原始磁盘为 磁盘A;新的磁盘为 磁盘B
1. 启动磁盘A 操作系统,并登录操作系统
2. 查看磁盘A 系统分区和分区磁盘格式
root@ac810:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 119.2G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 118.8G 0 part /
root@ac810:~# df -T /dev/sda1
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda1 vfat 523248 5348 517900 2% /boot/efi
root@ac810:~# df -T /dev/sda2
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda2 ext4 122030736 11532192 104256704 10% /
原始磁盘的总大小是128G;有两个分区:
/dev/sda1的格式是 vfat ,分区大小是512M
/dev/sda2的格式是ext4, 分区大小是118.8G
3. 查看磁盘A 系统分区信息
root@ac810:~# fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sda: 119.25 GiB, 128035676160 bytes, 250069680 sectors
Disk model: 128GB SSD
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: B59DCAAC-F032-462B-84C0-A0FAC80399D1
Device Start End Sectors Size Type
/dev/sda1 2048 1050623 1048576 512M EFI System
/dev/sda2 1050624 250068991 249018368 118.8G Linux filesystem
Command (m for help): q
Device Start End Sectors Size Type
/dev/sda1 2048 1050623 1048576 512M EFI System
/dev/sda2 1050624 250068991 249018368 118.8G Linux filesystem
磁盘A的分区类型是:gpt
磁盘B做分区,需要和老盘在盘符的个数上保持一致,系统引导盘的大小保持一致;
4. 查看磁盘A 目前已经使用的容量
root@ac810:~# df -h
df: /mnt/udisk/sdb1: Transport endpoint is not connected
Filesystem Size Used Avail Use% Mounted on
udev 1.8G 0 1.8G 0% /dev
tmpfs 362M 1.6M 361M 1% /run
/dev/sda2 117G 11G 100G 10% /
tmpfs 1.8G 0 1.8G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup
/dev/sda1 511M 5.3M 506M 2% /boot/efi
tmpfs 362M 12K 362M 1% /run/user/1000
观察Used这一列,目前磁盘A已使用的磁盘大小大概是12G;所以可以使用32G或者64G的磁盘B来进行系统数据的拷贝
5. 插入磁盘B,并查看系统是否识别
root@ac810:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 119.2G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 118.8G 0 part /
sdb 8:16 1 28.9G 0 disk
└─sdb1 8:17 1 28.9G 0 part /mnt/udisk/sdb1
此时发现系统中多出来一个磁盘 sdb ,说明系统已经识别到新插入的磁盘B
6. 卸载磁盘B
root@ac810:~# umount /mnt/udisk/sdb1
7. 对磁盘B进行分区
删除已有分区:
root@ac810:~# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sdb: 28.92 GiB, 31029460992 bytes, 60604416 sectors
Disk model: DataTraveler 3.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xdb7c1084
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 60602367 60600320 28.9G 7 HPFS/NTFS/exFAT
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
Command (m for help): w
The partition table has been altered.
Failed to remove partition 1 from system: Device or resource busy
The kernel still uses the old partitions. The new table will be used at the next reboot.
Syncing disks.
开始按照磁盘A的分区,对磁盘B进行分区:
root@ac810:~# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): g
Created a new GPT disklabel (GUID: 31A35DC7-BADB-FB4E-AFCA-E85C0B6027CF).
Command (m for help): p
Disk /dev/sdb: 28.92 GiB, 31029460992 bytes, 60604416 sectors
Disk model: DataTraveler 3.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 31A35DC7-BADB-FB4E-AFCA-E85C0B6027CF
Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-60604382, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-60604382, default 60604382): +512M
Created a new partition 1 of type 'Linux filesystem' and of size 512 MiB.
Command (m for help): t
Selected partition 1
Partition type (type L to list all types): 1
Changed type of partition 'Linux filesystem' to 'EFI System'.
Command (m for help): n
Partition number (2-128, default 2):
First sector (1050624-60604382, default 1050624):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1050624-60604382, default 60604382):
Created a new partition 2 of type 'Linux filesystem' and of size 28.4 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
8. 对磁盘B的每个分区进行格式化
root@ac810:~# mkfs.vfat /dev/sdb1
mkfs.fat 4.1 (2017-01-24)
root@ac810:~# mkfs.ext4 /dev/sdb2
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 7444219 4k blocks and 1864128 inodes
Filesystem UUID: 5ae3f3d6-f8ab-4c20-bf3b-05fa4e349e52
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks):
done
Writing superblocks and filesystem accounting information:
done
9. 向磁盘B的第一个分区中拷贝数据
挂载/dev/sdb1 到 /mnt 目录:
root@ac810:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 119.2G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 118.8G 0 part /
sdb 8:16 1 28.9G 0 disk
├─sdb1 8:17 1 512M 0 part
└─sdb2 8:18 1 28.4G 0 part
root@ac810:~# mount -t vfat /dev/sdb1 /mnt
拷贝数据到/mnt 目录:就是把/dev/sda1 对应的目录的内容拷贝到 /mnt/
root@ac810:~# cp -rfa /boot/efi/* /mnt/
卸载/dev/sdb1
root@ac810:~# umount /mnt
10. 向磁盘B的第二个分区中拷贝数据
挂载/dev/sdb2 到 /mnt 目录:
```
root@ac810:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 119.2G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 118.8G 0 part /
sdb 8:16 1 28.9G 0 disk
├─sdb1 8:17 1 512M 0 part
└─sdb2 8:18 1 28.4G 0 part
root@ac810:~# mount -t ext4 /dev/sdb2 /mnt
拷贝数据到/mnt 目录:就是把/dev/sda2 对应的目录的内容拷贝到 /mnt/
root@ac810:~# ls -al /
total 2097256
drwxr-xr-x 23 root root 4096 Feb 19 20:39 .
drwxr-xr-x 23 root root 4096 Feb 19 20:39 ..
drwxr-xr-x 5 root root 4096 Feb 22 13:45 App
lrwxrwxrwx 1 root root 7 Feb 17 18:48 bin -> usr/bin
drwxr-xr-x 4 root root 4096 May 19 2023 boot
drwxrwxr-x 2 root root 4096 Feb 17 18:50 cdrom
drwxr-xr-x 15 root root 4100 Apr 7 09:58 dev
drwxr-xr-x 133 root root 12288 Apr 7 09:43 etc
drwxr-xr-x 3 root root 4096 Feb 17 18:50 home
-rw-r--r-- 1 root root 29 Feb 20 14:18 .ini
lrwxrwxrwx 1 root root 7 Feb 17 18:48 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Feb 17 18:48 lib32 -> usr/lib32
lrwxrwxrwx 1 root root 9 Feb 17 18:48 lib64 -> usr/lib64
lrwxrwxrwx 1 root root 10 Feb 17 18:48 libx32 -> usr/libx32
drwx------ 2 root root 16384 Feb 17 18:48 lost+found
drwxr-xr-x 3 root root 4096 Feb 17 11:42 media
drwxr-xr-x 3 root root 4096 Apr 7 10:00 mnt
drwxr-xr-x 6 root root 4096 Feb 19 06:38 opt
dr-xr-xr-x 277 root root 0 Jan 2 2021 proc
drwxr-xr-x 3 root root 4096 Jan 11 2019 qtx11
drwx------ 10 root root 4096 Apr 7 2023 root
drwxr-xr-x 30 root root 820 Apr 7 09:40 run
lrwxrwxrwx 1 root root 8 Feb 17 18:48 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Feb 18 02:25 ScreenShots
drwxr-xr-x 9 root root 4096 Aug 31 2022 snap
drwxr-xr-x 2 root root 4096 Aug 31 2022 srv
-rw------- 1 root root 2147483648 Feb 17 18:48 swapfile
dr-xr-xr-x 11 root root 0 Jan 2 2021 sys
drwxrwxrwt 15 root root 4096 Apr 7 09:40 tmp
drwxr-xr-x 14 root root 4096 Aug 31 2022 usr
drwxr-xr-x 14 root root 4096 Aug 31 2022 var
root@ac810:~# cp -rfa /App/ /mnt/
root@ac810:~# cp -rfa /boot/ /mnt/
root@ac810:~# cp -rfa /cdrom/ /mnt/
root@ac810:~# cp -rfa /dev/ /mnt/
root@ac810:~# cp -rfa /etc/ /mnt/
root@ac810:~# cp -rfa /home/ /mnt/
root@ac810:~# cp -rfa /.ini /mnt/
root@ac810:~# cp -rfa /media/ /mnt/
root@ac810:~# cp -rfa /opt/ /mnt/
root@ac810:~#
root@ac810:~# cp -rfa /qtx11/ /mnt/
root@ac810:~# cp -rfa /root/ /mnt/
root@ac810:~# cp -rfa /run/ /mnt/
cp: cannot stat '/run/user/1000/gvfs': Permission denied
root@ac810:~# cp -rfa /ScreenShots/ /mnt/
root@ac810:~# cp -rfa /snap/ /mnt/
root@ac810:~# cp -rfa /srv/ /mnt/
root@ac810:~# cp -rfa /sys/ /mnt/
root@ac810:~# cp -rfa /tmp/ /mnt/
root@ac810:~# cp -rfa /usr/ /mnt/
root@ac810:~# cp -rfa /var/ /mnt/
root@ac810:~# cp -rfa /proc/ /mnt/
root@ac810:~# cp -rfa /swapfile /mnt/
//建立软连接
root@ac810:~# cd /mnt/
root@ac810:/mnt# ln -sf usr/bin/ bin
root@ac810:/mnt# ln -sf usr/sbin/ sbin
root@ac810:/mnt# ln -sf usr/lib/ lib
root@ac810:/mnt# ln -sf usr/lib32/ lib32
root@ac810:/mnt# ln -sf usr/lib64/ lib64
root@ac810:/mnt# ln -sf usr/libx32/ libx32
卸载 /dev/sdb2
root@ac810:/mnt# umount /mnt
umount: /mnt: target is busy.
root@ac810:/mnt# cd
root@ac810:~# umount /mnt
11. 查看磁盘A和磁盘B的UUID(和磁盘挂载和grup引导加载有关)
root@ac810:~# blkid -s UUID
/dev/sda2: UUID="4e87d18b-3223-4f9d-aa48-2bd450ad33d0"
/dev/sda1: UUID="C095-90BB"
/dev/sdb1: UUID="9A5A-29E3"
/dev/sdb2: UUID="5ae3f3d6-f8ab-4c20-bf3b-05fa4e349e52"
12. 修改磁盘B的开机自动挂载磁盘文件
挂载/dev/sdb2 到 /mnt 目录:
root@ac810:~# mount -t ext4 /dev/sdb2 /mnt/
修改/mnt/etc/fstab文件:
root@ac810:~# cat /mnt/etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during installation
UUID=4e87d18b-3223-4f9d-aa48-2bd450ad33d0 / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/sda1 during installation
UUID=C095-90BB /boot/efi vfat umask=0077 0 1
/swapfile none swap sw 0 0
root@ac810:~# sed -i 's/4e87d18b-3223-4f9d-aa48-2bd450ad33d0/5ae3f3d6-f8ab-4c20-bf3b-05fa4e349e52/g' /mnt/etc/fstab
root@ac810:~# sed -i 's/C095-90BB/9A5A-29E3/g' /mnt/etc/fstab
root@ac810:~# cat /mnt/etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during installation
UUID=5ae3f3d6-f8ab-4c20-bf3b-05fa4e349e52 / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/sda1 during installation
UUID=9A5A-29E3 /boot/efi vfat umask=0077 0 1
/swapfile none swap sw 0 0
13. 修改磁盘B的grub引导相关文件的uuid
修改 /mnt/boot/efi/EFI/ubuntu/grub.cfg /mnt/boot/grub/x86_64-efi/load.cfg /mnt/boot/grub/grub.cfg 三个文件中的UUID
root@ac810:/mnt/boot# sed -i 's/4e87d18b-3223-4f9d-aa48-2bd450ad33d0/5ae3f3d6-f8ab-4c20-bf3b-05fa4e349e52/g' /mnt/boot/efi/EFI/ubuntu/grub.cfg
root@ac810:/mnt/boot# sed -i 's/4e87d18b-3223-4f9d-aa48-2bd450ad33d0/5ae3f3d6-f8ab-4c20-bf3b-05fa4e349e52/g' /mnt/boot/grub/x86_64-efi/load.cfg
root@ac810:/mnt/boot# sed -i 's/4e87d18b-3223-4f9d-aa48-2bd450ad33d0/5ae3f3d6-f8ab-4c20-bf3b-05fa4e349e52/g' /mnt/boot/grub/grub.cfg
卸载 /dev/sdb2 ;加载 /dev/sdb1
root@ac810:/mnt/boot# cd
root@ac810:~# umount /mnt
root@ac810:~# mount -t vfat /dev/sdb1 /mnt/
修改 /mnt/EFI/ubuntu/grub.cfg 文件中的UUID
root@ac810:~# cd /mnt/
root@ac810:/mnt# grep -nr 4e87d18b-3223-4f9d-aa48-2bd450ad33d0
EFI/ubuntu/grub.cfg:1:search.fs_uuid 4e87d18b-3223-4f9d-aa48-2bd450ad33d0 root hd0,gpt2
root@ac810:/mnt# sed -i 's/4e87d18b-3223-4f9d-aa48-2bd450ad33d0/5ae3f3d6-f8ab-4c20-bf3b-05fa4e349e52/g' /mnt/EFI/ubuntu/grub.cfg
卸载 /dev/sdb2
root@ac810:/mnt# cd
root@ac810:~# umount /mnt
断电然后用新的磁盘B开机就可以了。