从 Ubuntu live usb 重新定义分区

从 Ubuntu live usb 重新定义分区

我创建了一个 Ubuntu live USB:

$ dd if=/path/to/ubuntu.iso of=/dev/sdb

我安装了我的发行版。现在我想将 USB 转回正常存储用途。

$ lsblk
sdb      8:16   1    15G  0 disk 
├─sdb1   8:17   1   1,4G  0 part  # Ubuntu is here
└─sdb2   8:18   1   2,3M  0 part  # Extra space I guess

$ sudo gdisk /dev/sdb
> p  # list patitions
Command (? for help): p
Disk /dev/sdb: 0 sectors, 0 bytes
Logical sector size: 512 bytes
Disk identifier (GUID): A9CA0E15-8539-47B8-9BD1-4942D18E789B
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 18446744073709551582
Partitions will be aligned on 2048-sector boundaries
Total free space is 0 sectors (0 bytes)

Number  Start (sector)    End (sector)  Size       Code  Name

如您所见,没有列出任何分区,并且磁盘显示为空。

尽管如此,我还是尝试创建一个新的:

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-18446744073709551582, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-18446744073709551582, default = 18446744073709551582) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

然后写入更改:

Command (? for help): w
Caution! Secondary header was placed beyond the disk's limits! Moving the
header, but other problems may occur!
Warning! The claimed last usable sector is incorrect! Do you want to correct
this problem? (Y/N): Y
Have adjusted the second header and last usable sector value.

Problem: partition 1 is too big for the disk.
Aborting write operation!
Aborting write of new partition table.

所以它不起作用。我究竟做错了什么?目前,他的 USB Sick 仍可用作 Live USB。


编辑1

好的,在收到答案之前我尝试格式化每个分区:

$ sudo mkfs.ext4 /dev/sdb1
$ sudo mkfs.ext4 /dev/sdb2

然后我检查了分区的 id:

$ blkid /dev/sdb*
/dev/sdb1: UUID="eea83bc3-408b-4287-8c35-0cceeee837bb" TYPE="ext4" 
/dev/sdb2: UUID="1138f1f8-0840-44fe-bb89-935993ed83af" TYPE="ext4" 

尝试使用fdisk

$ sudo fdisk /dev/sdb
fdisk: unable to read /dev/sdb: Invalid argument

然后我按照Alex Stragies建议删除文件签名:

$ dd if=/dev/zero of=/dev/sdb count=4 bs=1M

fdisk这次工作:

$ sudo fdisk /dev/sdb

我建立一个新的msdos partition table

> o  
Building a new DOS disklabel with disk identifier 0xb4151e51.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
You must set cylinders.
You can do this from the extra functions menu.

并保存更改并退出fdisk

>  w
The partition table has been altered!

Syncing disks.

现在,当我检查分区时,fdisk它告诉我我的设备大约有 4MB,而不是 16GB!我的 U 盘应该有 16GB 左右

$ sudo fdisk /dev/sdb
> p
Disk /dev/sdb: 4 MB, 4194304 bytes
131 heads, 2 sectors/track, 31 cylinders, total 8192 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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        8191        4095+  ee  GPT

使用gdisk:

$ sudo gdisk /dev/sdb
> p
Disk /dev/sdb: 8192 sectors, 4.0 MiB
Logical sector size: 512 bytes
Disk identifier (GUID): 5A262CEA-114A-4DF7-B2D3-99634D40398A
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 8158
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048            8158   3.0 MiB     8300  Linux filesystem

而使用lsblk它告诉我该设备的容量为 15GB,并且有两个卷或 1,4G 和 2,3M:

$ lsblck
sdb      8:16   1    15G  0 disk 
├─sdb1   8:17   1   1,4G  0 part 
└─sdb2   8:18   1   2,3M  0 part

到底是怎么回事?我完全迷失在这里


编辑2

我玩了一下dd的 count 和fdisk。我可以使用 count=2000 来dd复制 2GB:

$ dd if=/dev/zero of=/dev/sdb count=2000 bs=1M

但仅此而已。如果我指定count=3000,则只会复制 2GB。

澄清一下:我三年前就已经遇到过这个问题,我放弃了以前的 USB 记忆棒,因为我被告知它可能坏了。我发现它运气不好,因为我还把它用作 Ubuntu live USB 棒,然后对其进行格式化并损坏。现在,在同样的情况下,Ubuntu 也发生了这种情况。虽然我之前多次将它用作 Arch Linux 的 USB 盘,并且重新格式化它从未失败过。我在互联网上发现其他线程,其中用户描述了相同的问题,与制作实时 USB 棒(使用 Ubuntu)有关。但还没有找到明确的解决方案,我读到人们建议重新格式化、为未分配的空间创建新的分区等。当然有人已经经历过这个问题并找到了防弹解决方案,不是吗?或者我的 USB 记忆棒永久丢失了?

答案1

fdisk 可能无法打开该设备,因为它看到的是 iso9660 文件系统。

这可以通过 确认blkid /dev/sdb*

无论如何,我可能会尝试这个:

  • dd if=/dev/zero of=/dev/sdb count=4 bs=1M这将删除开始时的所有文件系统签名
  • 再试fdisk /dev/sdb一次
  • 开始操作o(创建新的MS-DOS分区表)

  • 然后,添加分区,按n,然后按p,然后按几次 Enter 选择最大大小。 (否则 fdisk 将创建一个小的虚拟分区)

  • t如果需要,使用 更改分区类型

您需要确保您的安装能够可靠地检测块设备更改,并为此刷新内核映射。如果不确定,请在和partprobe之间强制使用ddfdisk

答案2

您可以使用以下命令擦除 USB 闪存盘上的分区(以 root 身份运行以下所有命令):

dd if=/dev/zero of=/dev/sdb bs=1M count=1000
sync

这将从头开始写入 1Gb 的零,sdb因此肯定会擦除任何分区信息。
然后使用 fdisk 创建单个分区:

fdisk /dev/sdb
n
p
<enter> three times
w
partprobe
sync

然后检查lsblk分区,并使用 .在新分区上创建文件系统mkfs.ext4 /dev/sdb1

相关内容