微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

更新:我得到了这个工作,但仍然不是 100% 确定为什么。我已将完整且一致的工作脚本附加到末尾以供参考。

我正在尝试使用 sgdiskmkfs.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,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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。