本文目录导读:

要获取硬盘启动顺序,通常需要访问系统的固件设置(BIOS/UEFI),但这在操作系统层面没有标准的直接API,根据不同操作系统,可以通过以下脚本方式间接获取或控制:
Windows 系统
使用 PowerShell 查看启动配置
# 查看 UEFI 启动项(需要管理员权限) Get-WmiObject -Namespace root\wmi -Class MSI_SystemBootInfo | Select-Object -Property BootOrder # 通过 BCDEdit 查看当前启动配置 bcdedit /enum | Select-String "device"
使用 WMI 查询硬盘信息
# 获取硬盘启动顺序(按磁盘编号)
Get-WmiObject -Class Win32_DiskDrive |
Select-Object -Property Index, Model, Size,
@{Name="BootDrive";Expression={$_.Index -eq 0 ? "Primary" : "Secondary"}}
Linux 系统
查看启动顺序(需要 root 权限)
# 查看 EFI 启动项(UEFI 系统) efibootmgr -v | grep "BootOrder" # 查看磁盘挂载顺序 lsblk -o NAME,MOUNTPOINT,FSTYPE,SIZE | grep -E "boot|efi" # 查看 grub 配置中的启动顺序 grep -E "^menuentry|^submenu" /boot/grub/grub.cfg
脚本获取硬盘启动顺序
#!/bin/bash
# 获取按启动顺序排序的硬盘设备
for disk in /sys/block/sd[a-z]; do
if [ -f "$disk/queue/rotational" ]; then
echo "Disk: $(basename $disk) - $(cat $disk/device/model 2>/dev/null)"
echo " Boot flag: $(cat $disk/boot/status 2>/dev/null || echo 'Unknown')"
fi
done
macOS 系统
查看启动磁盘信息
# 查看当前启动磁盘 system_profiler SPStorageDataType | grep "Boot" -A 2 # 查看所有硬盘设备 diskutil list | grep -E "^/dev/disk" # 查看启动顺序(需要管理员权限) sudo bless --info / | grep "BootOrder"
跨平台 Python 脚本
import platform
import subprocess
import sys
def get_boot_order():
system = platform.system().lower()
if system == "windows":
try:
result = subprocess.run(['bcdedit', '/enum'],
capture_output=True, text=True)
# 解析输出中的设备信息
lines = result.stdout.split('\n')
boot_devices = []
for line in lines:
if 'device' in line.lower() and 'partition' in line.lower():
boot_devices.append(line.strip())
return boot_devices
except:
return "需要管理员权限"
elif system == "linux":
try:
# UEFI 系统
result = subprocess.run(['efibootmgr', '-v'],
capture_output=True, text=True)
# 解析启动顺序
for line in result.stdout.split('\n'):
if line.startswith('BootOrder'):
return line.strip()
return "非 UEFI 系统或需要 root 权限"
except:
return "未安装 efibootmgr"
elif system == "darwin": # macOS
try:
result = subprocess.run(['system_profiler', 'SPStorageDataType'],
capture_output=True, text=True)
return result.stdout
except:
return "无法获取信息"
else:
return f"不支持的平台: {system}"
# 使用
print(get_boot_order())
重要说明
- 权限要求:大多数操作需要管理员/root权限
- 系统差异:UEFI和Legacy BIOS的获取方式不同
- 间接获取:操作系统通常无法直接读取BIOS设置,只能通过其他方式推断
推荐工具
- Windows: EasyBCD 或
diskpart命令 - Linux:
efibootmgr,grub2-mkconfig - macOS: 系统偏好设置 > 启动磁盘
如果你需要具体的某个操作系统的详细脚本,请告诉我你的系统类型和具体需求。