linux 学习笔记之 Files and Directories

本文介绍了文件系统的几个关键方面,包括使用stat、fstat和lstat获取文件信息的方法;文件访问权限规则及其对目录和文件操作的影响;特殊权限位如set-user-id的作用;文件截断的操作;硬链接、软链接及删除文件的过程;改变当前工作目录的功能;以及设备特殊文件的管理。
  1.  file stat
int stat(const char * pathname, struct stat *info); 
int fstat(int fd, struct stat *info); 
int lstat(const char *pathname, struct stat *info);

The fstat function obtains information about the file that is already open on the descriptor filedes. The lstat function is similar to stat, but when the named file is a  symbolic link, lstat returns information about the symbolic link, not the file referenced by the symbolic link.

      2.      file access permissions

  • The file rule is that whenever we want to open any type of file by name, we must have execute permission in each directory mentioned in the name, including the current directory, if it is implied.   Read permission lets us read the directory(use ls command), write permission lets us create files below the directory and execute permission lets us pass through the directory when it is a component of a pathname what we are trying to access.
  • We must have write permission for a file to specify the O_TRUNC flag in the open function.
  • To delete an existing file, we need write permission and execute permission in the directory containing the file.

      3.  set-user-id, set-group-id, set-sticky-id

  • set-user-id(S_ISUID) : used in some file like /etc/passwd and print queue, the S_ISUID tell kernel run program as file owner
  • set-group-id(S_IGUID): tell kernel run program as file's group user
  • set-sticky-id(S_ISVTX): if the bits is set for a directory, the file in the directory can be removed or renamed only if the user has the write permission for the directory and one of the fellowing:  owns this file, owns the directory, is the superuser.

       4.   file truncation

int truncate(const char *pathname, off_t length);
int ftrruncate(int fd, off_t length);
  •  If the previous size of the file was greater than length, the data byond length is nolonger accessible.
  • If the previous sizeof the file was less than length, the effect is system dependent , but XSI-confofrmation system will increase the file size. If the implementation does extend a file, data between the old end of file and the new end of file will read as 0.

         5.

int link(const char *pathname, const char * newpath);
int symlink(const char *actualpath, const char * sympath);
int unlink(const char *pathname);
int remove(const char *pathname);
 
  • Many system disallow hard link to directory to prevent loops in the file system.
  • When a file is closed, kernel first checks the count of the number of process that have the file open, If this count has reached 0, the kernel then checks the link count; if  it is 0, file's contents are delete. If the set-sticky-id is set in this directory  we must have enough permission(see above).
  •  If the pathname is a symbolic link unlink removes the symbolic link, not the references by the link. There is no function to remove the file referenced by a symbolic link given the name of the link.
  •  We can also unlink a file or a directory with the remove function. For a file, remove is identical to unlink. For directory, remove is identical rmdir.
  • It is not required that actualpath exist when the symbolic link is created.

        6.  chdir, fchdir

int chdir(const char *pathname);
int fchdir(int fd);

            Each program if run is a separate process, so the current working directory of the shell is unaffected by the call to chdir in the program.

         7.    device special files

             Each file system on the same disk drive would usually have the same number, but a different minor number.

 

 

 

 

 

 

 

 

 

 

<think>嗯,用户遇到了Linux启动时卡在“starting createvolatile filesand directories”的问题,我需要帮他们解决。首先,我应该回忆一下这个错误通常是什么原因引起的。可能跟systemd的服务有关,特别是systemd-tmpfiles-setup.service,因为它负责创建临时文件和目录。接下来,用户可能需要进入恢复模式或者单用户模式来检查服务状态。我记得可以通过在启动时按Esc或者Shift进入GRUB菜单,然后选择恢复选项。进入恢复模式后,可能需要挂载文件系统为可写,因为默认可能是只读的。可以用mount -o remount,rw /命令。然后,检查服务的日志是关键。用journalctl-u systemd-tmpfiles-setup.service应该能显示相关日志,看看有没有错误信息。比如权限问题、磁盘空间不足或者文件系统错误。如果是权限问题,可能需要运行fsck检查文件系统,或者手动清理/tmp目录下的文件。 另外,用户可能需要检查磁盘空间,使用df-h命令看看是否有分区满了,特别是/tmp所在的挂载点。如果空间不足,删除一些大文件或者清理日志可能有帮助。还有可能是文件系统损坏,这时候建议运行fsck命令。需要注意umount分区后再运行,否则可能会有风险。如果用户不太熟悉,可能需要指导他们正确的步骤,比如在恢复模式下运行fsck/dev/sdXn。还有可能的问题是与特定的tmpfiles配置有关,检查/etc/tmpfiles.d/下的配置文件是否有错误。如果有自定义的配置,可能需要临时移除,看看是否解决了问题。最后,如果问题依然存在,可能需要重新安装systemd相关的包,或者更新系统。对于不同的发行版,命令可能不同,比如Ubuntu用apt,CentOS用yum。 生成相关问题时,要考虑用户可能的后续疑问,比如如何进入恢复模式,如何检查其他服务状态,或者如何预防这类问题再次发生。需要确保问题覆盖了故障排除的各个方面,帮助用户深入理解。</think>### 解决Linux启动卡在"starting create volatile files and directories"的方法 #### 1. 定位问题根源 该提示属于systemd的`systemd-tmpfiles-setup.service`服务,负责创建临时文件和目录[^1]。卡顿可能由以下原因导致: - 文件系统权限错误 - 磁盘空间不足(使用$df -h$检查) - /tmp目录挂载异常 - 文件系统损坏 #### 2. 进入恢复模式 1) 重启时按Esc/Shift进入GRUB菜单 2) 选择"Advanced options" 3) 选择"Recovery mode" 4) 选择"root"进入命令行 ```bash mount -o remount,rw / # 重新挂载为可写模式 ``` #### 3. 检查服务状态 ```bash systemctl status systemd-tmpfiles-setup.service journalctl -u systemd-tmpfiles-setup.service # 查看完整日志 ``` #### 4. 常见修复操作 - **磁盘空间清理**: ```bash du -sh /tmp/* 2>/dev/null | sort -hr # 查找大文件 rm -rf /tmp/large_file ``` - **文件系统修复**: ```bash umount /dev/sdXn # 先卸载分区 fsck -y /dev/sdXn ``` - **权限修复**: ```bash chmod 1777 /tmp # 确保/tmp有sticky位 ls -ld /tmp # 验证权限应为drwxrwxrwt ``` #### 5. 临时绕过检测(紧急情况) ```bash systemctl mask systemd-tmpfiles-setup.service # 慎用!仅临时解决方案 ``` #### 6. 验证修复效果 ```bash systemctl daemon-reload systemctl restart systemd-tmpfiles-setup.service ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值