大网数据广告活动
服务器运维问答

How to clone Linux hard drive to smaller flash drive

· 52CE 我爱测

Currently, I'm trying to clone a hard drive that has Linux installed to a 16GB flash drive. The hard drive is 233GB, but df -h shows that only 3.5G is in use. Can I use Clonezilla to create an image using just the allocated amoun…

问题原文

原帖链接 · AndreasKralj · CC BY-SA 4.0

Currently, I'm trying to clone a hard drive that has Linux installed to a 16GB flash drive. The hard drive is 233GB, but df -h shows that only 3.5G is in use. Can I use Clonezilla to create an image using just the allocated amount of space and copy it to a flash drive, where I can deploy the image to other Linux laptops?

Edit: I tried using Clonzilla with Parted Magic, and tried to do device-image and the following error is thrown:

split /home/partimag/lubuntu.img/lubuntu-vg-root.ext4.ptcl-img.gz.aa: Input/output error
Checking the disk space...
Failed to save partition /dev/lubuntu-vg/root.
Press Enter to continue

已采纳解答

原帖链接 · Aaron Copley · CC BY-SA 4.0

You haven't specified much about your disk layout so I am going to make some assumptions. Please modify for your application, or provide the additional details and I can edit this.

Assumptions:

  • 1 physical disk
  • 2 partitions (boot + LVM PV)
  • 2 LV (root + swap)

This is pretty much a default install of CentOS/Red Hat and is very, very common.

You can use GParted as your boot media, but let's try this without the actual GParted tool:

Start from the bottom of the stack. Shrink your LVM Logical Volume and its filesystem.

lvreduce -rL 8G /dev/mapper/centos-root

I specified 8G here because it's smaller than your 16G available and you may still have a small swap volume.

Replace centos-root with your LV name. Run lvs/lvdisplay/ls /dev/mapper if you are unsure.)

Then, you should be able to shrink the LVM PV.

pvresize --setphysicalvolumesize 14G /dev/vda2

Replace /dev/vda2 with your disk.

I chose 14G because it's just under your 16G limit and we won't have to do any exact math this way. If your swap was allocated before your root volume, things should work. This command may fail if swap was allocated behind the root volume that you just resized. If that is the case, there's now a gap between root and swap and you need to slide swap into that free area so that the PV can shrink. I ran into this but overcame it with the next steps.

/dev/vda2: cannot resize to *n* extents as later ones are allocated.

First, find what extents need to move.

pvs -v --segments /dev/vda2

You'll see the LV with a range of extents occurring after the target boundary n above.

pvmove --alloc anywhere /dev/vda2:n-n

This command will rearrange the layout such that the volume occupies the free space you just created from shrinking root. You can then attempt to pvresize again and it should work now.

Lastly, shrink your LVM partition.

fdisk /dev/vda2

First, to print (p) your partition table. (You'll need this later.) The one you want to delete (d) is your LVM partition and hopefully it's at the end of the disk. Then, recreate it with the new smaller size. (n) Be absolutely sure that it begins in the same sector as it did before. (Scroll up to where you printed the original table.) Make it 15G, again, because math, and we know it will fit on the 16G flash drive that's not quite really 16G. Finish this operation with (w) to write your changes.

Verify everything is < 16G and retry your CloneZilla process.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-06-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。