获得的 Windows 和 Linux 下的磁盘分区及大小

内容纲要

Windows 和 Linux 都会有一些隐藏的分区,例如 EFI、boot 等。在 Linux 中情况又复杂对得多,例如 Docker 映射的分区/卷等,ram 交换页文件等。
Linux 中 DriveType 嗨哟 Fixed、Ram、CD 等类型,如果要对其进行筛选,编写获取方法是一件较为麻烦的事情。

这时候可以使用 CZGL.SystemInfo 库,在 Nuget 上可以搜索。
使用 var disks = DiskInfo.GetDisks(); 可以直接获取所有磁盘信息,但是这些信息会有很多非物理磁盘。
使用 Linq 表达式可以筛选掉。

            var realDisk = disks.Where(x =>
            x.DriveType == System.IO.DriveType.Fixed &&
            x.TotalSize != 0 && x.FileSystem != "overlay"
            ).Distinct(new DiskInfoEquality());

全部示例代码如下:

using CZGL.SystemInfo;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;

namespace ConsoleApp7
{
    public class DiskInfoEquality : IEqualityComparer<DiskInfo>
    {
        public bool Equals([AllowNull] DiskInfo x, [AllowNull] DiskInfo y)
        {
            return x.Id == y.Id;
        }

        public int GetHashCode([DisallowNull] DiskInfo obj)
        {
            return obj.Id.GetHashCode();
        }
    }

    class Program
    {
        private static (string Name, int Size) Get(long size)
        {
            if (size < 1024)
            {
                return ("Byte", (int)(size >> 10));
            }
            if (size < 1000 * 1000)
            {
                return ("KB", (int)size / 1000);
            }
            if (size < 1000 * 1000 * 1000)
            {
                return ("MB", (int)(size >> 20));
            }

            if (size < (long)1000 * 1000 * 1000 * 1000)
            {
                return ("GB", (int)(size >> 30));
            }

            if (size < (long)1000 * 1000 * 1000 * 1000 * 1000)
            {
                return ("TB", (int)(size >> 40));
            }
            throw new Exception();
        }
        static void Main(string[] args)
        {
            var disks = DiskInfo.GetDisks();
            foreach (var item in disks)
            {
                var tmpa = Get(item.FreeSpace);
                var tmpb = Get(item.TotalSize);
                var tmpc = Get(item.UsedSize);
                Console.WriteLine(@$"ID:{item.Id}
Name:{item.Name}
DriveType:{item.DriveType}
FileSystem:{item.FileSystem}
FreeSpace:{tmpa.Size} {tmpa.Name}
TotalSize:{tmpb.Size} {tmpb.Name}
UsedSize: {tmpc.Size} {tmpc.Name}
");
            }

            Console.WriteLine("经过检查后,发现以下磁盘可能是磁盘分区,请确认是否正确:");
            var realDisk = disks.Where(x =>
            x.DriveType == System.IO.DriveType.Fixed &&
            x.TotalSize != 0 && x.FileSystem != "overlay"
            ).Distinct(new DiskInfoEquality());

            foreach (var item in realDisk)
            {
                var tmpa = Get(item.FreeSpace);
                var tmpb = Get(item.TotalSize);
                var tmpc = Get(item.UsedSize);
                Console.WriteLine(@$"ID:{item.Id}
Name:{item.Name}
DriveType:{item.DriveType}
FileSystem:{item.FileSystem}
FreeSpace:{tmpa.Size} {tmpa.Name}
TotalSize:{tmpb.Size} {tmpb.Name}
UsedSize: {tmpc.Size} {tmpc.Name}
");
            }

            Console.WriteLine("CD,USB等存储设备不计入此列表,Docker的虚拟磁盘也不考虑");
            Console.ReadKey();
        }
    }
}

Windows 下:

ID:C:\
Name:C:\
DriveType:Fixed
FileSystem:NTFS
FreeSpace:11 GB
TotalSize:116 GB
UsedSize: 105 GB

ID:D:\
Name:D:\
DriveType:Fixed
FileSystem:NTFS
FreeSpace:69 GB
TotalSize:235 GB
UsedSize: 165 GB

ID:E:\
Name:E:\
DriveType:Fixed
FileSystem:NTFS
FreeSpace:73 GB
TotalSize:99 GB
UsedSize: 26 GB

ID:F:\
Name:F:\
DriveType:Fixed
FileSystem:FAT32
FreeSpace:511 MB
TotalSize:511 MB
UsedSize: 356 KB

ID:G:\
Name:G:\
DriveType:Fixed
FileSystem:NTFS
FreeSpace:45 GB
TotalSize:69 GB
UsedSize: 24 GB

经过检查后,发现以下磁盘可能是磁盘分区,请确认是否正确:
ID:C:\
Name:C:\
DriveType:Fixed
FileSystem:NTFS
FreeSpace:11 GB
TotalSize:116 GB
UsedSize: 105 GB

ID:D:\
Name:D:\
DriveType:Fixed
FileSystem:NTFS
FreeSpace:69 GB
TotalSize:235 GB
UsedSize: 165 GB

ID:E:\
Name:E:\
DriveType:Fixed
FileSystem:NTFS
FreeSpace:73 GB
TotalSize:99 GB
UsedSize: 26 GB

ID:F:\
Name:F:\
DriveType:Fixed
FileSystem:FAT32
FreeSpace:511 MB
TotalSize:511 MB
UsedSize: 356 KB

ID:G:\
Name:G:\
DriveType:Fixed
FileSystem:NTFS
FreeSpace:45 GB
TotalSize:69 GB
UsedSize: 24 GB

CD,USB等存储设备不计入此列表,Docker的虚拟磁盘也不考虑

Linux 下:

ID:/sys
Name:/sys
DriveType:Ram
FileSystem:sysfs
FreeSpace:0 Byte
TotalSize:0 Byte
UsedSize: 0 Byte

ID:/proc
Name:/proc
DriveType:Ram
FileSystem:proc
FreeSpace:0 Byte
TotalSize:0 Byte
UsedSize: 0 Byte

ID:/dev
Name:/dev
DriveType:Ram
FileSystem:tmpfs
FreeSpace:63 GB
TotalSize:63 GB
UsedSize: 0 Byte

ID:/sys/kernel/security
Name:/sys/kernel/security
DriveType:Ram
FileSystem:securityfs
FreeSpace:0 Byte
TotalSize:0 Byte
UsedSize: 0 Byte

ID:/dev/shm
Name:/dev/shm
DriveType:Ram
FileSystem:tmpfs
FreeSpace:63 GB
TotalSize:63 GB
UsedSize: 65 KB

ID:/dev/pts
Name:/dev/pts
DriveType:Ram
FileSystem:devpts
FreeSpace:0 Byte
TotalSize:0 Byte
UsedSize: 0 Byte

ID:/run
Name:/run
DriveType:Ram
FileSystem:tmpfs
FreeSpace:61 GB
TotalSize:63 GB
UsedSize: 1 GB

ID:/sys/fs/cgroup
Name:/sys/fs/cgroup
DriveType:Ram
FileSystem:tmpfs
FreeSpace:63 GB
TotalSize:63 GB
UsedSize: 0 Byte

ID:/sys/fs/cgroup/cpuset
Name:/sys/fs/cgroup/cpuset
DriveType:Ram
FileSystem:cgroupfs
FreeSpace:0 Byte
TotalSize:0 Byte
UsedSize: 0 Byte

ID:/sys/fs/cgroup/pids
Name:/sys/fs/cgroup/pids
DriveType:Ram
FileSystem:cgroupfs
FreeSpace:0 Byte
TotalSize:0 Byte
UsedSize: 0 Byte

ID:/
Name:/
DriveType:Fixed
FileSystem:xfs
FreeSpace:19 GB
TotalSize:49 GB
UsedSize: 30 GB

ID:/sys/fs/selinux
Name:/sys/fs/selinux
DriveType:Ram
FileSystem:selinux
FreeSpace:0 Byte
TotalSize:0 Byte
UsedSize: 0 Byte

ID:/dev/mqueue
Name:/dev/mqueue
DriveType:Ram
FileSystem:mqueue
FreeSpace:0 Byte
TotalSize:0 Byte
UsedSize: 0 Byte

ID:/dev/hugepages
Name:/dev/hugepages
DriveType:Ram
FileSystem:hugetlbfs
FreeSpace:0 Byte
TotalSize:0 Byte
UsedSize: 0 Byte

ID:/boot
Name:/boot
DriveType:Fixed
FileSystem:xfs
FreeSpace:892 MB
TotalSize:0 GB
UsedSize: 121 MB

ID:/boot/efi
Name:/boot/efi
DriveType:Fixed
FileSystem:msdos
FreeSpace:190 MB
TotalSize:199 MB
UsedSize: 8 MB

ID:/home
Name:/home
DriveType:Fixed
FileSystem:xfs
FreeSpace:9 TB
TotalSize:9 TB
UsedSize: 1 GB

ID:/var/lib/docker/overlay2/c9e4edfb0ea712587958eaf9c9c458ce9a9348bca8f29bccbbf4abb4e4dbd0e1/merged
Name:/var/lib/docker/overlay2/c9e4edfb0ea712587958eaf9c9c458ce9a9348bca8f29bccbbf4abb4e4dbd0e1/merged
DriveType:Fixed
FileSystem:overlay
FreeSpace:19 GB
TotalSize:49 GB
UsedSize: 30 GB

ID:/run/user/0
Name:/run/user/0
DriveType:Ram
FileSystem:tmpfs
FreeSpace:12 GB
TotalSize:12 GB
UsedSize: 0 Byte

ID:/var/lib/docker/containers/b8085a8567be7b251028bc121488b5d62e9defeadc857fe71440b9acd721e025/mounts/shm
Name:/var/lib/docker/containers/b8085a8567be7b251028bc121488b5d62e9defeadc857fe71440b9acd721e025/mounts/shm
DriveType:Ram
FileSystem:tmpfs
FreeSpace:64 MB
TotalSize:64 MB
UsedSize: 0 Byte

ID:/run/docker/netns/20b636138662
Name:/run/docker/netns/20b636138662
DriveType:Unknown
FileSystem:
FreeSpace:0 Byte
TotalSize:0 Byte
UsedSize: 0 Byte

ID:/proc/sys/fs/binfmt_misc
Name:/proc/sys/fs/binfmt_misc
DriveType:Ram
FileSystem:binfmt_misc
FreeSpace:0 Byte
TotalSize:0 Byte
UsedSize: 0 Byte

经过检查后,发现以下磁盘可能是磁盘分区,请确认是否正确:
ID:/
Name:/
DriveType:Fixed
FileSystem:xfs
FreeSpace:19 GB
TotalSize:49 GB
UsedSize: 30 GB

ID:/boot
Name:/boot
DriveType:Fixed
FileSystem:xfs
FreeSpace:892 MB
TotalSize:0 GB
UsedSize: 121 MB

ID:/boot/efi
Name:/boot/efi
DriveType:Fixed
FileSystem:msdos
FreeSpace:190 MB
TotalSize:199 MB
UsedSize: 8 MB

ID:/home
Name:/home
DriveType:Fixed
FileSystem:xfs
FreeSpace:9 TB
TotalSize:9 TB
UsedSize: 1 GB
点赞

发表评论

邮箱地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!