Liu*_* 刘研 41 linux filesystems cloning uuid
我在 VMware 中有一个 Fedora 来宾操作系统。我想扩展/boot分区,所以我向这个虚拟机添加了另一个虚拟磁盘,并尝试克隆该磁盘。
之后dd if=/dev/sda1 of=/dev/sdb1,blkid报告/dev/sda1和/dev/sdb1具有相同的 UUID/GUID。
奇怪的是,宇宙中有2个相同的UUID,如何将其中一个更改为另一个UUID值?
主题改变了,这里的 UUID 是指文件系统 UUID,而不是分区 UUID。
由于它是文件系统 UUID,因此需要特定于文件系统的实用程序来更改 UUID,或使用 hexeditor 修改磁盘上的原始数据(危险,除非您知道自己在做什么,否则不推荐)。
And*_*ndy 41
要生成一个随机的新 UUID,可以使用:
$ uuidgen
Run Code Online (Sandbox Code Playgroud)
实际更改 UUID 取决于文件系统。
# tune2fs -U <output of uuidgen> /dev/sdb1
Run Code Online (Sandbox Code Playgroud)
或者,如果您确信 uuidgen 会起作用:
# tune2fs -U $(uuidgen) /dev/sdb1
Run Code Online (Sandbox Code Playgroud)
# btrfstune -U $(uuidgen) /dev/sdb1
Run Code Online (Sandbox Code Playgroud)
UUID 存储在超级块中,因此文件系统的逐字节副本将具有相同的 UUID。
Zaz*_*Zaz 22
用:
tune2fs -U random /dev/sdb1
Run Code Online (Sandbox Code Playgroud)
如果它是ext文件系统,或者
xfs_admin -U generate /dev/sdb1
Run Code Online (Sandbox Code Playgroud)
如果它是一个xfs文件系统。
第二个分区具有相同 UUID 的原因是因为dd只是将数据从一个文件复制到另一个文件 ( dd if=/dev/sda1 of=/dev/sdb1= cat /dev/sda1 > /dev/sdb1);dd不知道分区是什么,或者如何生成 UUID,所以它只是按原样复制所有数据,包括 UUID,它存储在分区开头附近的 flysystem 超级块中。
顺便说一句,
我也认为 UUID(通用唯一标识符)是一个奇怪的名字。它应该被称为 PGUID(Possively Globally Unique IDentifier),或者甚至更好,只是 ARLN(一个合理的大数)。
Liu*_* 刘研 19
除了 ext2 /ext3 / ex4 和 xfs,您还可以更改以下文件系统或块设备的 UUID 或 GPT 分区的 PARTUUID。
swaplabel -U $NEW_UUID
对于MD RAID,必须先停止RAID,然后重新组装时更新UUID。因此,如果您的 RAID 已安装到/,则无法阻止它,您需要在离线模式下更新 UUID——使用 live CD/USB 来完成。
mdadm --stop $RAID_DEVICEmdadm --assemble $RAID_DEVICE --update=uuid --uuid=$NEW_UUID $MEMBER_DEVICES...例子:
mdadm --stop /dev/md127mdadm --assemble /dev/md127 --update=uuid --uuid=2A1D2A1D-2A1D-2A1D-2A1D-2A1D2A1D2A1D /dev/sda2 /dev/sdb2更新 UUID 后,您可能需要更新grub.cfg(rd.md.uuid=$NEW_UUID在linux/linuxefi/linux16命令行中)和/或fstab和/或mdadm.conf和/或update-initramfs(Debian 系列)和/或dracut --force(Red Hat 系列),以便您下次可以成功启动。
cryptsetup luksUUID --uuid=$NEW_UUID $LUKS_DEVICE
例子:
cryptsetup luksUUID --uuid=e0c0e0c0-e0c0-e0c0-e0c0-e0c0e0c0e0c0 ~/encrypted-LUKS.loop
有ntfslabel实用程序(感谢ntfs-3g)来更改 NTFS 文件系统的 UUID。请参阅abchk1234 的回答。
如果您想更改 NTFS 文件系统的整个 UUID 而不是--new-half-serial,请使用该--new-serial选项。
例子:
ntfslabel --new-serial=1122334455667788 /dev/sda3
这是另一个故事, 目前没有实用程序可以修改这些文件系统的文件系统 UUID(请参阅reichhart 的回答——mlabel工具),但我们可以修改磁盘上的原始数据来执行此操作。
警告,修改磁盘上的原始数据是很危险的,如果不小心操作可能会丢失数据。
以下是使用hexedit.
blkid,将文件系统的 UUID 写在纸上,或者记住它。FAT/exFAT 的 UUID 字符串看起来像1122-3344,NTFS 的 UUID 字符串看起来像1122334455667788。
使用hexedit打开分区设备
hexedit <partition device such as /dev/sda1>
按/,按相反顺序输入 UUID(如果blkid报告1234-ABCD,则搜索CDAB3412)以搜索磁盘上的 UUID 数据。
找到 UUID 后,更改它们,按F2保存并退出。
blkid 验证 UUID。
修改 UUID 后,您可能需要更新grub.cfg和/或fstab以便下次可以成功启动。
执行fsck <partition device such as /dev/sda1>并看到“引导扇区与其备份之间存在差异”消息。为了解决它,请选择“1) Copy original to backup”,如果系统询问您“Perform changes ? (y/n)”,请按y。
一个小故事:我从没想过我会需要触摸 GPT 分区的 PARTUUID,直到现在,当我尝试将 Windows 从小磁盘迁移到大磁盘时,Windows 无法在大磁盘上启动,甚至无法启动自动修复。原因是Windows+UEFI把NTFS文件系统的UUID改成原来的UUID是不够的,还需要改GPT分区的PARTUUID——PARTUUID的事。(将PARTUUID改成原来的,Windows从大盘启动成功。)
askubuntu.com上已经有一个答案来展示如何使用gdisk实用程序更改 GPT 分区的 PARTUUID 。
fdisk来自 util-linux的当前版本(编辑时在 Fedora 32 存储库中为 2.35.2)也可以在专家模式下执行此操作,实际上,从util-linux v2.23 开始,fdisk 已启用 GPT。
这是使用 util-linuxfdisk实用程序更改 PARTUUID 的示例:
# truncate -s 200M /tmp/file-as-a.disk
# LANG=en_US.UTF-8 fdisk /tmp/file-as-a.disk
Welcome to fdisk (util-linux 2.35.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xc0fc8503.
Command (m for help): g
Created a new GPT disklabel (GUID: 4CD23B97-80C5-BD42-8466-1B9476653A92).
Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-409566, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-409566, default 409566):
Created a new partition 1 of type 'Linux filesystem' and of size 199 MiB.
Command (m for help): i
Selected partition 1
Device: /tmp/file-as-a.disk1
Start: 2048
End: 409566
Sectors: 407519
Size: 199M
Type: Linux filesystem
Type-UUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4
UUID: 7265D7C3-6277-DE4B-956C-41E3BFFF8E0D
Command (m for help): w
The partition table has been altered.
Syncing disks.
# LANG=en_US.UTF-8 fdisk /tmp/file-as-a.disk
Welcome to fdisk (util-linux 2.35.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): x
Expert command (m for help): m
Help (expert commands):
GPT
i change disk GUID
n change partition name
u change partition UUID
l change table length
M enter protective/hybrid MBR
A toggle the legacy BIOS bootable flag
B toggle the no block IO protocol flag
R toggle the required partition flag
S toggle the GUID specific bits
Generic
p print the partition table
v verify the partition table
d print the raw data of the first sector from the device
D print the raw data of the disklabel from the device
f fix partitions order
m print this menu
Save & Exit
q quit without saving changes
r return to main menu
Expert command (m for help): u
Selected partition 1
New UUID (in 8-4-4-4-12 format): 11223344-5566-7788-9900-AABBCCDDEEFF
Partition UUID changed from 7265D7C3-6277-DE4B-956C-41E3BFFF8E0D to 11223344-5566-7788-9900-AABBCCDDEEFF.
Expert command (m for help): w
w: unknown command
Expert command (m for help): r
Command (m for help): w
The partition table has been altered.
Syncing disks.
# losetup --partscan /dev/loop101 /tmp/file-as-a.disk
# blkid /dev/loop101*
/dev/loop101: PTUUID="4cd23b97-80c5-bd42-8466-1b9476653a92" PTTYPE="gpt"
/dev/loop101p1: PARTUUID="11223344-5566-7788-9900-aabbccddeeff"
Run Code Online (Sandbox Code Playgroud)
小智 8
VFAT 的部分答案
\n对于 ext* 和 XFS 文件系统,已经有了很好的答案。\xc2\xa0\n但是对于 VFAT,我强烈建议使用mtools包的mlabel \n 。\xc2\xa0\n使用十六进制编辑器是危险的。
\n使用类似这样的东西来创建一个新的 UUID:
\necho \'drive d: file="/dev/sdb3" exclusive\' >> /etc/mtools.conf\nmlabel -n d:\nRun Code Online (Sandbox Code Playgroud)\n第一行将字母分配d:给/dev/sdb3\nby\xc2\xa0附加到配置文件,\n而\xc2\xa0第二行使用\xc2\ xa0选项/etc/mtools.conf将新的随机UUID分配给\xc2\xa0drive\n。\xc2\xa0\ n将\xc2\ xa0选项添加到\xc2\xa0告诉\xc2\xa0显示\xc2\xa0new\xc2\xa0UUID。d:-n-vmlabel
您可以使用选项\n(大写\xc2\xa0-N\xc2\xa0serialN后接\xc2\xa0a\xc2\xa0序列号)将UUID\n设置为\xc2\xa0a\xc2\xa0的\xc2\xa0编号\xc2\xa0您的\xc2\xa0选择\ xc2\xa0情况下,您需要一个\xc2\xa0特定编号,\ne.g.,\xc2\xa0如果有\xc2\xa0引导加载程序或其他一些工具\nis\xc2\xa0专门寻找该序列号。
小智 6
对于 XFS,请使用: xfs_admin -U <uuid> <device>
使用xfs_admin -u <device>,以查看一个UUID(注意小写选项视图,相对于上壳体选项集)。U&L 上的另一篇文章指出了blkid查看系统上所有或部分 UUID的命令。
| 归档时间: |
|
| 查看次数: |
70694 次 |
| 最近记录: |