Block devices and block sizes

本文详细解析了硬盘分区的基本概念及其计算方法,并通过具体实例展示了如何计算分区占用的硬盘空间。此外,还介绍了如何利用命令行工具查看硬盘和内存的使用情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       block size是一个笼统的概念,不是一个固定值,在硬盘(一般是512byte),文件系统(分为1kbyte,2kbyte,4kbyte等)及kernel(为1kbyte)中大小不一定

详细可以参考

Block devices and block sizes

block size--file hole


yuebao@yuebao-G31M-ES2C:~$ sudo fdisk -l

Disk /dev/sda: 500.1 GB, 500106780160 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes   //这是指硬盘上1个cylinder(柱面)的大小
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x9d899d89

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       12748   102398278+   7  HPFS/NTFS
/dev/sda2           12749       44618   255995744+   f  W95 Ext'd (LBA)
/dev/sda3           44619       45605     7919616   82  Linux swap / Solaris
/dev/sda4           45605       60802   122070016   83  Linux
/dev/sda5           12749       25496   102398278+   7  HPFS/NTFS
/dev/sda6           25497       44618   153597433+   7  HPFS/NTFS

以sda3  是swap分区的大小,依次为例,此分区占用的大小计算方法,

1.  总共占用的cylinders个数:(60802-45605)=986个

   1个cylinders的大小8225280byte/1024/1024=7.8MB

   占用空间=986x7.8MB=7.5GB

2.直接用Blocks数计算

   1个block size 1024byte,即1k

   则7919616个bloack

  占用空间=7919616x1k=7.5GB

两种方法计算结果是一样的。

另外df是disk free,可以查看硬盘的空间使用情况

yuebao@yuebao-G31M-ES2C:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda4             115G   96G   14G  88% /
none                  2.0G  656K  2.0G   1% /dev
none                  2.0G  7.5M  2.0G   1% /dev/shm
none                  2.0G  356K  2.0G   1% /var/run
none                  2.0G     0  2.0G   0% /var/lock
/dev/sda5              98G   77G   21G  79% /media/d:
/dev/sda6             147G   45G  103G  31% /media/e:

free则是查看内存空间(包括硬盘上的虚拟内存)的使用情况,-m以MB显示大小
yuebao@yuebao-G31M-ES2C:~$ free -m
             total       used       free     shared    buffers     cached
Mem:          3951       3877         74          0        357       1317
-/+ buffers/cache:       2202       1749
Swap:         7733          0       7733




 

// Configuration provided during initialization of the littlefs struct lfs_config { // Opaque user provided context that can be used to pass // information to the block device operations void *context; // Read a region in a block. Negative error codes are propagated // to the user. int (*read)(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size); // Program a region in a block. The block must have previously // been erased. Negative error codes are propagated to the user. // May return LFS_ERR_CORRUPT if the block should be considered bad. int (*prog)(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size); // Erase a block. A block must be erased before being programmed. // The state of an erased block is undefined. Negative error codes // are propagated to the user. // May return LFS_ERR_CORRUPT if the block should be considered bad. int (*erase)(const struct lfs_config *c, lfs_block_t block); // Sync the state of the underlying block device. Negative error codes // are propagated to the user. int (*sync)(const struct lfs_config *c); #ifdef LFS_THREADSAFE // Lock the underlying block device. Negative error codes // are propagated to the user. int (*lock)(const struct lfs_config *c); // Unlock the underlying block device. Negative error codes // are propagated to the user. int (*unlock)(const struct lfs_config *c); #endif // Minimum size of a block read in bytes. All read operations will be a // multiple of this value. lfs_size_t read_size; // Minimum size of a block program in bytes. All program operations will be // a multiple of this value. lfs_size_t prog_size; // Size of an erasable block in bytes. This does not impact ram consumption // and may be larger than the physical erase size. However, non-inlined // files take up at minimum one block. Must be a multiple of the read and // program sizes. lfs_size_t block_size; // Number of erasable blocks on the device. Defaults to block_count stored // on disk when zero. lfs_size_t block_count; // Number of erase cycles before littlefs evicts metadata logs and moves // the metadata to another block. Suggested values are in the // range 100-1000, with large values having better performance at the cost // of less consistent wear distribution. // // Set to -1 to disable block-level wear-leveling. int32_t block_cycles; // Size of block caches in bytes. Each cache buffers a portion of a block in // RAM. The littlefs needs a read cache, a program cache, and one additional // cache per file. Larger caches can improve performance by storing more // data and reducing the number of disk accesses. Must be a multiple of the // read and program sizes, and a factor of the block size. lfs_size_t cache_size; // Size of the lookahead buffer in bytes. A larger lookahead buffer // increases the number of blocks found during an allocation pass. The // lookahead buffer is stored as a compact bitmap, so each byte of RAM // can track 8 blocks. lfs_size_t lookahead_size; // Threshold for metadata compaction during lfs_fs_gc in bytes. Metadata // pairs that exceed this threshold will be compacted during lfs_fs_gc. // Defaults to ~88% block_size when zero, though the default may change // in the future. // // Note this only affects lfs_fs_gc. Normal compactions still only occur // when full. // // Set to -1 to disable metadata compaction during lfs_fs_gc. lfs_size_t compact_thresh; // Optional statically allocated read buffer. Must be cache_size. // By default lfs_malloc is used to allocate this buffer. void *read_buffer; // Optional statically allocated program buffer. Must be cache_size. // By default lfs_malloc is used to allocate this buffer. void *prog_buffer; // Optional statically allocated lookahead buffer. Must be lookahead_size. // By default lfs_malloc is used to allocate this buffer. void *lookahead_buffer; // Optional upper limit on length of file names in bytes. No downside for // larger names except the size of the info struct which is controlled by // the LFS_NAME_MAX define. Defaults to LFS_NAME_MAX or name_max stored on // disk when zero. lfs_size_t name_max; // Optional upper limit on files in bytes. No downside for larger files // but must be <= LFS_FILE_MAX. Defaults to LFS_FILE_MAX or file_max stored // on disk when zero. lfs_size_t file_max; // Optional upper limit on custom attributes in bytes. No downside for // larger attributes size but must be <= LFS_ATTR_MAX. Defaults to // LFS_ATTR_MAX or attr_max stored on disk when zero. lfs_size_t attr_max; // Optional upper limit on total space given to metadata pairs in bytes. On // devices with large blocks (e.g. 128kB) setting this to a low size (2-8kB) // can help bound the metadata compaction time. Must be <= block_size. // Defaults to block_size when zero. lfs_size_t metadata_max; // Optional upper limit on inlined files in bytes. Inlined files live in // metadata and decrease storage requirements, but may be limited to // improve metadata-related performance. Must be <= cache_size, <= // attr_max, and <= block_size/8. Defaults to the largest possible // inline_max when zero. // // Set to -1 to disable inlined files. lfs_size_t inline_max; #ifdef LFS_MULTIVERSION // On-disk version to use when writing in the form of 16-bit major version // + 16-bit minor version. This limiting metadata to what is supported by // older minor versions. Note that some features will be lost. Defaults to // to the most recent minor version when zero. uint32_t disk_version; #endif }; litterfs的这个context有什么用 目前litterfs的代码中没有看到有使用到的地方
最新发布
06-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值