mkfs.vfat:无法打开{分区}:没有这样的文件或目录(命令成功,但抛出此错误并阻止脚本的其余部分)

问题描述 投票:0回答:2

更新:我已经成功了,但仍然不能 100% 确定原因。 我已将完整且一致的工作脚本附加到末尾以供参考。

我正在尝试使用

sgdisk
mkfs.vfat
编写一系列磁盘分区命令的脚本。 我正在使用 Live USB (NixOS 21pre),有一个空白的 1TB M.2 SSD,并正在创建一个 1GB EFI 启动分区和一个 999GB ZFS 分区。

一切正常,直到我尝试使用

mkfs.vfat
在 EFI 分区上创建 FAT32 文件系统,我在标题中得到了错误。

但是,奇怪的是,mkfs.vfat 命令成功,但仍然抛出该错误并阻止脚本的其余部分。 知道为什么这样做以及如何解决它吗?

从未格式化的 1TB M.2 SSD 开始:

$  sudo parted /dev/disk/by-id/wwn-0x5001b448b94488f8 print
Error: /dev/sda: unrecognised disk label
Model: ATA WDC WDS100T2B0B- (scsi)                                        
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags: 

脚本:

$ ls
total 4
drwxr-xr-x  2 nixos users   60 May 18 20:25 .
drwx------ 17 nixos users  360 May 18 15:24 ..
-rwxr-xr-x  1 nixos users 2225 May 18 19:59 partition.sh
$  cat partition.sh
#!/usr/bin/env bash
#make gpt partition table and boot & rpool partitions for ZFS on 1TB M.2 SSD

#error handling on
set -e

#wipe the disk with -Z, then create two partitions, a 1GB (945GiB) EFI boot partition, and a ZFS root partition consisting of the rest of the drive, then print the results
DISK=/dev/disk/by-id/wwn-0x5001b448b94488f8
sgdisk -Z $DISK
sgdisk -n 1:0:+954M -t 1:EF00 -c 1:efi $DISK
sgdisk -n 2:0:0 -t 2:BF01 -c 2:zroot $DISK
sgdisk -p /dev/sda

#make a FAT32 filesystem on the EFI partition, then mount it
#mkfs.vfat -F 32 ${DISK}-part1 (troubleshooting with hardcoded version below)
mkfs.vfat -F 32 /dev/disk/by-id/wwn-0x5001b448b94488f8-part1
mkdir -p /mnt/boot
mount ${DISK}-part1 /mnt/boot

结果(一切都很好,直到

mkfs.vfat
,它抛出错误并阻止脚本的其余部分):

$  sudo sh partition.sh
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.
Creating new GPT entries in memory.
Setting name!
partNum is 0
The operation has completed successfully.
Setting name!
partNum is 1
The operation has completed successfully.
Disk /dev/sda: 1953525168 sectors, 931.5 GiB
Model: WDC WDS100T2B0B-
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 77ED6A41-E722-4FFB-92EC-975A37DBCB97
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 1953525134
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         1955839   954.0 MiB   EF00  efi
   2         1955840      1953525134   930.6 GiB   BF01  zroot
mkfs.fat 4.1 (2017-01-24)
mkfs.vfat: unable to open /dev/disk/by-id/wwn-0x5001b448b94488f8-part1: No such file or directory

验证分区和 FAT32 创建命令是否有效:

$ sudo parted /dev/disk/by-id/wwn-0x5001b448b94488f8 print
Model: ATA WDC WDS100T2B0B- (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name   Flags
 1      1049kB  1001MB  1000MB  fat32        efi    boot, esp
 2      1001MB  1000GB  999GB                zroot

Fwiw,相同的命令在命令行上运行,没有错误:

$  sudo mkfs.vfat -F 32 /dev/disk/by-id/wwn-0x5001b448b94488f8-part1
mkfs.fat 4.1 (2017-01-24)

成功。 但为什么命令行上没有错误,但脚本中却出错了?

更新:完全一致的工作脚本:

#!/usr/bin/env bash
#make UEFI (GPT) partition table and two partitions (FAT32 boot and ZFS rpool) on 1TB M.2 SSD

#error handling on
set -e

#vars
DISK=/dev/disk/by-id/wwn-0x5001b448b94488f8
POOL='rpool'

#0. if /mnt/boot is mounted, umount it; if any NixOS filesystems are mounted, unmount them
if mount -l | grep -q '/mnt/boot'; then
  umount -f /mnt/boot
fi
if mount -l | grep -q '/mnt/nix'; then
  umount -fR /mnt
fi

#1. if a zfs pool exists, delete it
if zpool list | grep -q $POOL; then
  zfs unmount -a
  zpool export $POOL
  zpool destroy -f $POOL
fi

#2. wipe the disk
sgdisk -Z $DISK
wipefs -a $DISK

#3. create two partitions, a 1GB (945GiB) EFI boot partition, and a ZFS root partition consisting of the rest of the drive, then print the results
sgdisk -n 1:0:+954M -t 1:EF00 -c 1:efiboot $DISK
sgdisk -n 2:0:0 -t 2:BF01 -c 2:zfsroot $DISK
sgdisk -p /dev/sda

#4. notify the OS of partition updates, and print partition info
partprobe
parted ${DISK} print

#5. make a FAT32 filesystem on the EFI boot partition
mkfs.vfat -F 32 ${DISK}-part1

#6. notify the OS of partition updates, and print new partition info
partprobe
parted ${DISK} print

#mount the partitions in nixos-zfs-pool-dataset-create.sh script. Make sure to first mount the ZFS root dataset on /mnt before mounting and subdirectories of /mnt.
linux bash uefi zfs disk-partitioning
2个回答
0
投票

内核可能需要一段时间才能收到有关分区更改的通知。尝试在

partprobe
之前调用
mkfs
,请求内核重新读取分区表。


0
投票

据推测,这是 sgdisk 等工具修改分区表与内核更新其内部结构之间的竞争条件。因此,人们可能会认为“只需调用partprobe即可完成”。事实并非如此,正如以下脚本摘录所示:

DISK="/dev/disk/by-id/mmc-SD128"
PART="${DISK}-part2"
# partition created with sgdisk
# sync
partprobe "$DISK"
# sync
blockdev --rereadpt "$DISK"
# while loop testing for block device 
# existence ever second: not fencing 
# against "mkfs unable to open" condition
# sleep 1
#if ! test -b "$PART"; then
#  exit 2;
#fi
mkdosfs "$PART"
mkdosfs: unable to open /dev/disk/by-id/mmc-SD128-part2: No such file or directory

顺便说一句,不,那些为了演示而插入的同步在这种情况下也不有利。

令人惊讶的是,测试块设备是否存在是不够的。

注意:这种竞争情况可能发生在 50% 的调用中。

我强烈怀疑添加

parted print
调用是否可靠且明确地调解了这种竞争条件,而不是仅仅随着后续
mkdosfs
调用之前经过更多时间而减少其发生。

到目前为止,最简单(尽管可以说不是最令人满意)的解决方案似乎是循环调用 mkdosfs 直到成功。

© www.soinside.com 2019 - 2024. All rights reserved.