设为首页 收藏本站
查看: 555|回复: 0

[经验分享] inode and its structure in linux

[复制链接]
发表于 2018-5-24 11:44:15 | 显示全部楼层 |阅读模式
  Let me begin this tutorial by a famous UNIX statement which emphasizes the main underlying working model of Linux operating system. The statement is that "Everything Is a File".
  If you are one of that person who spends a lot time with Linux or any NIX system, then you might have already understood that statement. Each and everything in Linux is accessed by a file. To make that much more clear, even block devices like Hard disks,and CD/DVD's are nothing other than a file(yeah but a special file..but a file).
  Now if we start digging a bit deeper into files, we will come to know that, to the operating system(Linux) files inside a file system(be it ext2,ext3 or ext4) are not really accessed by their name. Names are helpful to humans but, the file system recognizes a file not by its name but by a number. That number through which the operating system reaches the location and other attributes of that file is called as an inode number. Now don't worry DSC0000.jpg , this tutorial is all about Inodes and its structure.

What is an INODE in Linux?
  

  I must say that its a data structure that keeps track of all the information about a file.
  You store your information in a file, and the operating system stores the information about a file in an inode(sometimes called as an inode number).
  Information about files(data) are sometimes called metadata.So you can even say it in another way, "An inode is metadata of the data."
  Whenever a user or a program needs access to a file, the operating system first searches for the exact and unique inode (inode number), in a table called as an inode table. In fact the program or the user who needs access to a file, reaches the file with the help of the inode number found from the inode table.
  To reach a particular file with its "name" needs an inode number corresponding to that file. But to reach an inode number you dont require the file name. Infact with the inode number you can get the data.
  

How does the structure of an inode look like?
  

  This is the most important part to understand in terms of an inode. Here we will be discussing the contents of an inode.
Inode Structure of a Directory:
DSC0001.jpg

  Inode structure of a directory just consists of Name to Inode mapping of files and directories in that directory.
  So here in the above shown diagram you can see the first two entries of (.) and (..) dot dot. You might have seen them whenever you list the contents of a directory.(most of the times they are hidden. You will have to use -a option with "ls" command to see them).
  And people who are more into Linux or any NIX system, knows that the command "cd ." will change the directory to the current directory itself(which means it does nothing..because you are already in that directory.).
  And the command "cd .." will take you to the previous directory or call it the parent directory of the current directory. Now why that happens?
  Lets understand why this happens with an example. Imagine am in the directory /var/log on my system.
  

[root@slashroot1 log]# ls -ia3633723 .                3633786 faillog     3634889 rpmpkgs.33633697 ..               3634727 gdm         3634893 rpmpkgs.43634833 acpid            3633883 httpd       3633813 samba
  Now lets note down inode numbers of .(dot) and ..(dot dot).
  .(dot) = 3633723
  ..(dot dot) = 3633697
  Now lets do the directory listing of /var/ directory and see the inodes there.
[root@slashroot1 var]# ls -ia3633697 .        3634275 cvs    3633698 lib    3633733 nis       3633737 spool      2 ..       3633724 db     3633729 local  3633734 opt       3633700 tmp3633844 account  3633725 empty  3633730 lock   3633735 preserve  3634278 tux3633701 cache    3633726 games  3633723 log    3633838 racoon    3633884 www3634135 crash    3634624 gdm    3633732 mail   3633736 run       3633740 yp
  Now lets note down the inode numbers of log directory and .(dot) from the directory listing of /var
  .(dot) = 3633697
  log = 3633723
  So you can clearly note that inode of .(dot) inside /var/log directory is equal to inode of log directory. And inode of ..(dot dot ) inside /var/log/ is equal to inode of .(dot) inside /var/ directory.
  .(dot) always means the current directory just because its inode is same as the directory's inode. And ..(dot dot) means parent directory inode because its inode is same as the previous(parent) directory.

Inode Structure of a File
  

  Now lets see how the structure of an inode of a file look like.

  Mode:
  This keeps information about two things, one is the permission information, the other is the type of inode, for example an inode can be of a file, directory or a block device etc.
  Owner Info:Access details like owner of the file, group of the file etc.
  Size:  This location store the size of the file in terms of bytes.
  Time Stamps:  it stores the inode creation time, modification time, etc.
  Now comes the important thing to understand about how a file is saved in a partition with the help of an inode.
  Block Size:Whenever a partition is formatted with a file system.It normally gets formatted with a default block size. Now block size is the size of chunks in which data
will be spread. So if the block size is 4K, then for a file of 15K it will take 4 blocks(because 4K*4 16), and technically speaking you waste 1 K.


Direct Block Pointers:
  In an ext2 file system an inode consists of only 15 block pointers. The first 12 block pointers are called as Direct Block pointers. Which means that these pointers point to the address of the blocks containing the data of the file. 12 Block pointers can point to 12 data blocks. So in total the Direct Block pointers can address only 48K(12 * 4K) of data. Which means if the file is only of 48K or below in size, then inode itself can address all the blocks
containing the data of the file.

  Now What if the file size is above 48K?
Indirect Block Pointers:
  whenever the size of the data goes above 48k(by considering the block size as 4k), the 13th pointer in the inode will point to the very next block after the data(adjacent block after 48k of data), which inturn will point to the next block address where data is to be copied.
  Now as we have took our block size as 4K, the indirect block pointer, can point to 1024 blocks containing data(by taking the size of a block pointer as 4bytes, one 4K block can point to 1024 blocks because 4 bytes * 1024 = 4K).
  which means an indirect block pointer can address, upto 4MB of data(4bytes of block pointer in 4K block, can point and address 1024 number of 4K blocks which makes the data size of 4M)
Double indirect Block Pointers:
  Now if the size of the file is above 4MB + 48K then the inode will start using Double Indirect Block Pointers, to address data blocks. Double Indirect Block pointer in an inode will point to the block that comes just after 4M + 48K data, which intern will point to the blocks where the data is stored.
  Double Indirect block pointer also is inside a 4K block as every blocks are 4K, Now block pointers are 4 bytes  in size, as mentioned previously, so Double indirect block pointer can address 1024 Indirect Block pointers(which means 1024 * 4M =4G). So with the help of a double indirect Block Pointer the size of the data can go upto 4G.
Triple Indirect Block Pointers:
  Now this triple Indirect Block Pointers can address upto 4G * 1024 = 4TB, of file size. The fifteenth block pointer in the inode will point to the block just after the 4G of data, which intern will point to 1024 Double Indirect Block Pointers.
  So after the 12 direct block pointers, 13th block pointer in inode is for Indirect block pointers, and 14th block pointer is for double indirect block pointers, and 15th block pointer is for triple indirect block pointers.
  Now this is the main reason why there are limits to the full size of a single file that you can have in a file system.
  

  Now an interesting fact to understand is that the total no of inodes are created at the time of creating a file system. Which means there is an upper limit in the number of inodes you can have in a file system. Now after that limit has reached you will not be able to create any more files on the file system, even if you have space left on the partition.
How to check Inode Utilization?
  Inode utilization can be checked by using the beow command in linux.
[root@slashroot1 ~]# df -iFilesystem            Inodes   IUsed   IFree IUse% Mounted on/dev/sda1            10246368  124075 10122293    2% /tmpfs                  64431       1   64430    1% /dev/shm
  

  As you can clearly see from the above output that the max number of inodes that can be created on /dev/sda1 is 10246368.
[root@slashroot1 ~]# tune2fs -l /dev/sda1tune2fs 1.39 (29-May-2006)Filesystem volume name:   /1Last mounted on:          <not available>Filesystem UUID:          86898399-4550-4b08-8196-7444ea953c96Filesystem magic number:  0xEF53Filesystem revision #:    1 (dynamic)Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_fileDefault mount options:    user_xattr aclFilesystem state:         cleanErrors behavior:          ContinueFilesystem OS type:       LinuxInode count:              10246368Block count:              10239421Reserved block count:     511971



How to find a file using inode?
You can find a file using its inode and do actions on it, like list the file name of the inode, delete the file using its inode etc.

[root@slashroot1 log]# find /var/ -inum 3634906 -exec ls -l {} \;-rw------- 1 root root 1272 Dec  2 10:13 /var/log/maillog.1[root@slashroot1 log]#

You can find and delete a file using its inode using the below command.

[root@slashroot1 log]# find /var/ -inum 3634906 -exec rm -f {} \;[root@slashroot1 log]#

How to change directory using an inode?
  You can change to a directory using its inode number as shown below.
[root@slashroot1 log]# cd $(find -inum 3633883)[root@slashroot1 httpd]#

you can see that i have changed the directory to httpd by using its inode number.

I will posting a couple of another posts, regarding hardlinks in inode, how to deal with inode limit, What happens to the inode when a file gets deleted in ext2 and ext3 filesystem etc.

Hope this introductory guide in understanding inode was helpful..



  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-480635-1-1.html 上篇帖子: Linux之lnmmp实现 下篇帖子: 从零学LINUX
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表