Linux 将加密驱动器克隆到较小的驱动器

Linux 将加密驱动器克隆到较小的驱动器

我想升级笔记本电脑的驱动器,使其速度更快,所以我想将我当前的硬盘克隆到 SSD。不幸的是,它们的大小不一样。

我已经找到了这个问题,但我对目前的答案并不满意。

以下是我所拥有的(sda = HDD,sdb = SSD):

NAME                               MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda                                  8:0    0 232.9G  0 disk  
|-sda1                               8:1    0   243M  0 part  /boot
|-sda2                               8:2    0     1K  0 part  
`-sda5                               8:5    0 232.7G  0 part  
  `-sda5_crypt (dm-0)              252:0    0 232.7G  0 crypt 
    |-elementary--vg-root (dm-1)   252:1    0 230.8G  0 lvm   /
    `-elementary--vg-swap_1 (dm-2) 252:2    0   1.9G  0 lvm   [SWAP]
sdb                                  8:16   0 111.8G  0 disk  
`-sdb1                               8:17   0 111.8G  0 part  

使用实时 CD 将当前 HDD 分区缩小到 SSD 可以处理的确切大小然后dd再处理,这不是最好的方法吗?我必须使用什么命令?

答案1

我将亲自回答这个问题,但总体来说: 也许从头开始是最快的,步骤复杂且耗时!

  • 使用您最喜欢的磁盘克隆/备份工具进行备份
  • 关注 Ubuntu Wiki 文章调整加密分区大小
  • 确保分区与较小磁盘的大小完全匹配(因此最后一个分区后的可用空间未分配)
  • 使用 dd 克隆磁盘,直到达到确切大小

详细的

  • /dev/sda 是我想要缩小的当前磁盘
  • /dev/sdb 是我的新磁盘
  • 使用您的磁盘大小、块大小等的精确值,我的计算只是对我如何做到的一点线索。
  • 小心使用选项参数(例如,b 有时表示字节,有时表示块等等)。务必先检查手册页。
  • 查看上面链接的文章,了解有关合理步骤的其他信息

启动 livecd、加载模块、解密磁盘、发现分区:

sudo modprobe dm-crypt
sudo cryptsetup luksOpen /dev/sda5 crypt1
sudo vgscan --mknodes
sudo vgchange -ay

打印出当前分区布局及其大小(以字节为单位):

ubuntu@ubuntu:~$ lsblk -b

NAME                               MAJ:MIN RM         SIZE RO TYPE  MOUNTPOINT
sda                                  8:0    0 250059350016  0 disk 
├─sda1                               8:1    0    254803968  0 part 
├─sda2                               8:2    0         1024  0 part 
└─sda5                               8:5    0 249802260480  0 part 
  └─crypt1 (dm-0)                  252:0    0 249800163328  0 crypt
    ├─elementary--vg-root (dm-1)   252:1    0 247757537280  0 lvm  
    └─elementary--vg-swap_1 (dm-2) 252:2    0   2038431744  0 lvm  
sdb                                  8:16   0 120034123776  0 disk 
└─sdb1                               8:17   0 120034091520  0 part  /media/ubuntu/ff08402

我们需要知道新的根分区应该有多大,我们做一些计算:

find how much space is used without root:
  250059350016
- 247757537280
=   2301812736

calculate new wished size from the new disk:
  120034123776
-   2301812736
= 117732311040

Since we can specify it only in kibibytes, we divide it by 1024
  117732311040
/         1024
=    114972960

使用 resize2fs 调整分区大小(我使用的是 ext4):

sudo resize2fs -p /dev/mapper/elementary--vg-root 114972960k

将逻辑卷设置为计算的大小:

sudo lvreduce -L 114972960k /dev/mapper/elementary--vg-root

打印新的分区设置,我们需要一些值:

ubuntu@ubuntu:~$ lsblk -b
NAME                               MAJ:MIN RM         SIZE RO TYPE  MOUNTPOINT
sda                                  8:0    0 250059350016  0 disk 
├─sda1                               8:1    0    254803968  0 part 
├─sda2                               8:2    0         1024  0 part 
└─sda5                               8:5    0 249802260480  0 part 
  └─crypt1 (dm-0)                  252:0    0 249800163328  0 crypt
    ├─elementary--vg-root (dm-1)   252:1    0 117734113280  0 lvm  
    └─elementary--vg-swap_1 (dm-2) 252:2    0   2038431744  0 lvm  
sdb                                  8:16   0 120034123776  0 disk 
└─sdb1                               8:17   0 120034091520  0 part  /media/ubuntu/ff08402

我们需要知道新的 crypt1 分区应该有多大,我们做一些计算:

Calculate difference between logical volume sizes and crypt1 (from before we made any changes):
    2038431744
+ 247757537280
= 249795969024

  249800163328
- 249795969024
=      4194304

calculate the new crypt1 size (with the new changes):
    2038431744
+ 117734113280
+      4194304
= 119776739328

later we need the crypt1 size in blocks:
  119776739328
/          512
=    233938944

在我们减少墓穴容量之前,请检查容量是否有序:

ubuntu@ubuntu:~$ sudo pvs -v --segments /dev/mapper/crypt1
    Using physical volume(s) on command line
  PV                 VG            Fmt  Attr PSize   PFree   Start SSize LV     Start Type   PE Ranges                    
  /dev/mapper/crypt1 elementary-vg lvm2 a--  232.64g 121.09g     0 28070 root       0 linear /dev/mapper/crypt1:0-28069   
  /dev/mapper/crypt1 elementary-vg lvm2 a--  232.64g 121.09g 28070 31000            0 free                                
  /dev/mapper/crypt1 elementary-vg lvm2 a--  232.64g 121.09g 59070   486 swap_1     0 linear /dev/mapper/crypt1:59070-59555

不,所以我需要将交换移动到最佳可用空间(实际上意味着阻止 28070):

sudo pvmove --alloc anywhere /dev/mapper/crypt1:59070-59555

然后重新检查您的分配,以便可用空间位于最后:

sudo pvs -v --segments /dev/mapper/crypt1

为 crypt1 设置新的卷大小

sudo pvresize --setphysicalvolumesize 119776739328b /dev/mapper/crypt1
sudo cryptsetup -b 233938944 resize crypt1

继续分区表,卸载加密分区:

sudo vgchange -an
sudo cryptsetup luksClose crypt1

打印出来并记下您当前的分区表布局:

ubuntu@ubuntu:~$ sudo fdisk -l

Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00082d72

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758   488396799   243947521    5  Extended
/dev/sda5          501760   488396799   243947520   83  Linux

我们需要知道,新的分区表应该有多大,我们再做一些计算:

reverse engineering blocks setup from current /dev/sda:
     243947521
*         1024
-         1024
= 249802260480

size of other uses:
  250059350016
- 249802260480
=    257089536

size for the new disk:
  120034123776
-    257089536
= 119777034240

blocks for new disk:
  119777034240
+         1024
/         1024
=    116969761

calculating end sector with new size:
     488396799
+            1
-       501758
/            2
=    243947521

     116969761
*            2
+       501758
-            1
=    234441279

现在我们可以使用新的大小重新定义分区表:

ubuntu@ubuntu:~$ sudo fdisk /dev/sda

Command (m for help): d
Partition number (1-5): 5

Command (m for help): d
Partition number (1-5): 2

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2):
Using default value 2
First sector (499712-488397167, default 499712):
Using default value 499712
Last sector, +sectors or +size{K,M,G} (499712-488397167, default 488397167): 234441279

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (501760-234441279, default 501760):
Using default value 501760
Last sector, +sectors or +size{K,M,G} (501760-234441279, default 234441279):
Using default value 234441279

Command (m for help): p

Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00082d72

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          499712   234441279   116970784    5  Extended
/dev/sda5          501760   234441279   116969760   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

小测试:尝试加载新的加密卷(或不使用 livecd 重新启动)并检查是否可以访问数据(如果不能,则说明您在某处犯了错误,需要使用备份重新启动):

sudo cryptsetup luksOpen /dev/sda5 crypt1
sudo vgscan --mknodes
sudo vgchange -ay

通过将结束块与较小磁盘的块大小进行比较来检查您的计算是否正确:

ubuntu@ubuntu:~$ sudo fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00082d72

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          499712   234441279   116970784    5  Extended
/dev/sda5          501760   234441279   116969760   83  Linux


ubuntu@ubuntu:~$ sudo fdisk /dev/sdb

Command (m for help): p

Disk /dev/sdb: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders, total 234441648 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f335e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1              63   234441647   117220792+  83  Linux

使用新的分区表重新检查您的计算:

/dev/sda total - sda5 end-block (in the beginning):
  488397168
- 488396799
=       369

/dev/sdb total - sda5 end-block (after shrink):
  234441648
- 234441279
=       369

一切顺利,大小正是我们想要的。所以下一步是从 0 到 234441648(= /dev/sdb 的总大小)

sudo dd if=/dev/sda of=/dev/sdb bs=1b count=234441648

您已准备就绪,收缩已完成。此 dd 命令将需要一些时间,并且您看不到任何输出。就我而言,它运行了大约 6 个小时。

相关内容