Linux下磁盘挂载

1.查看是否已分配

[root@localhost ~]# fdisk -l

Disk /dev/vda: 536.9 GB, 536870912000 bytes
16 heads, 63 sectors/track, 1040253 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00062b97

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

2、发现有磁盘,路径为/dev/vda。然后使用fdisk命令进行分区

[root@localhost ~]# fdisk /dev/vda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/vda: 536.9 GB, 536870912000 bytes
16 heads, 63 sectors/track, 1040253 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00062b97

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1-1040253, default 1): 41611
Last cylinder, +cylinders or +size{K,M,G} (41611-1040253, default 1040253): 665776

Command (m for help): p

Disk /dev/vda: 536.9 GB, 536870912000 bytes
16 heads, 63 sectors/track, 1040253 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00062b97

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3           41611      665776   314579584   83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Selected partition 4
First cylinder (1-1040253, default 1): 4
Sector 3024 is already allocated
First cylinder (665777-1040253, default 665777): 665777
Last cylinder, +cylinders or +size{K,M,G} (665777-1040253, default 1040253):
Using default value 1040253

Command (m for help): p

Disk /dev/vda: 536.9 GB, 536870912000 bytes
16 heads, 63 sectors/track, 1040253 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00062b97

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3           41611      665776   314579584   83  Linux
/dev/vda4          665777     1040253   188736408   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

解释:

Partition number表示分区号,由于1、2号已经分区所有从3号分区开始

其中 n 新建分区 d删除分区 p打印分区 w将表写入磁盘保存并退出

3、然后使用fdisk -l 命令查看,已经新建的分区

[root@localhost ~]# fdisk -l

Disk /dev/vda: 536.9 GB, 536870912000 bytes
16 heads, 63 sectors/track, 1040253 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00062b97

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3           41611      665776   314579584   83  Linux
/dev/vda4          665777     1040253   188736408   83  Linux

红色部分为刚刚新建的分区

6.建好分区后对分区进行格式化,建立文件系统 mkfs.ext3

[root@localhost ~]# mkfs.ext3 /dev/vda3
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
19668992 inodes, 78644896 blocks
3932244 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
2401 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616

正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mkfs.ext3 /dev/vda4
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
11796480 inodes, 47184102 blocks
2359205 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
1440 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424, 20480000, 23887872

正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.


7.然后mount选择一个挂载点挂上

[root@localhost ~]# mount /dev/vda3 /var
[root@localhost ~]# mount /dev/vda4 /opt

8.使用 df -TH  查看文件大小


[root@localhost ~]# df -TH /opt
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/vda4      ext3  191G  197M  181G   1% /opt
[root@localhost ~]# df -TH /var
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/vda3      ext3  318G  201M  301G   1% /var

注意:

如果挂载错误可以使用umount取消挂载,如果找不到分区使用partprobe 刷新分区实在不行就reboot下

使用fdisk -l即可查看新的分区

 

<think>我们正在处理一个关于Linux磁盘挂载的问题。根据用户提供的引用和问题描述,用户需要一份详细的Linux磁盘挂载指南。我们将结合引用内容,构建一个完整的磁盘挂载流程。引用内容要点:-引用[1]:提供了磁盘挂载的完整流程,包括检查硬盘状态、分区、格式化、挂载和设置自动挂载(/etc/fstab)。强调了操作需谨慎,特别是编辑fstab时。-引用[2]:提到了一本Linux书籍,其中包含磁盘挂载实验案例,但具体内容未详细给出。-引用[3]:展示了使用blkid命令获取磁盘UUID的示例,并提到修改磁盘挂载配置文件。用户需求:查找磁盘挂载的解决方案或指南。因此,我们将按照以下步骤构建回答:1.检查磁盘状态(使用lsblk、fdisk等命令)2.磁盘分区(使用fdisk或parted)3.格式化分区(使用mkfs)4.挂载分区(使用mount)5.设置开机自动挂载(通过编辑/etc/fstab)6.验证挂载注意:在关键步骤给出示例命令,并提醒用户谨慎操作(特别是fstab)。另外,用户之前的问题是关于创建普通用户的,但当前问题转向了磁盘挂载,因此我们专注于磁盘挂载。我们将生成相关问题:1.如何卸载已挂载磁盘?2.如何调整磁盘分区大小?3.挂载磁盘时遇到权限问题如何解决?开始编写回答。</think>### Linux 磁盘挂载详细教程 以下是完整的磁盘挂载流程,涵盖**分区创建 → 格式化 → 挂载 → 开机自动挂载**四个关键步骤。操作需`root`权限(使用`sudo`或切换至root用户)。 --- #### 1. **检查磁盘状态** 查看系统识别到的磁盘设备: ```bash lsblk # 显示所有块设备(树状结构) fdisk -l # 查看磁盘分区详情 ``` 输出示例: ``` NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 253:0 0 50G 0 disk └─vda1 253:1 0 50G 0 part / vdb 253:16 0 100G 0 disk <--挂载的新磁盘 ``` > 此处`vdb`为待挂载磁盘(无分区挂载点)[^1]。 --- #### 2. **创建分区** 使用`fdisk`对新磁盘分区(以`/dev/vdb`为例): ```bash fdisk /dev/vdb ``` 交互式操作流程: 1. 输入 `n` → 创建新分区 2. 输入 `p` → 选择主分区类型 3. 分区号默认(回车) 4. 起始/结束扇区默认(全盘分区直接回车) 5. 输入 `w` → 保存分区表 > 完成后通过 `fdisk -l /dev/vdb` 验证分区(如`/dev/vdb1`)[^1]。 --- #### 3. **格式化分区** 选择文件系统类型(推荐`ext4`或`xfs`),格式化新分区: ```bash # 格式化为ext4 mkfs.ext4 /dev/vdb1 # 或格式化为xfs mkfs.xfs /dev/vdb1 ``` > ⚠️ 格式化会清除所有数据!确认分区名称无误[^1]。 --- #### 4. **临时挂载分区** 创建挂载目录并挂载: ```bash mkdir /data # 创建挂载点目录 mount /dev/vdb1 /data # 挂载分区 ``` 验证挂载: ```bash df -hT | grep /data # 查看挂载状态 ``` 输出示例: ``` /dev/vdb1 ext4 100G 0% /data ``` --- #### 5. **设置开机自动挂载** 通过`/etc/fstab`配置文件实现永久挂载: ```bash # 获取分区的UUID(推荐使用UUID防止设备名变更) blkid /dev/vdb1 # 输出:/dev/vdb1: UUID="c28be586-c293..." TYPE="ext4" [^3] # 编辑fstab文件 vim /etc/fstab ``` 在文件末尾添加: ``` UUID=c28be586-c293-41b0-bcba-4890db06ea9e /data ext4 defaults 0 0 ``` 字段说明: - `UUID`:分区唯一标识 - `/data`:挂载点路径 - `ext4`:文件系统类型 - `defaults`:挂载参数 - `0 0`:dump和fsck选项 > **关键安全步骤**: > 执行 `mount -a` 测试配置是否正确,若报错需立即修正,否则可能导致系统无法启动[^1][^3]。 --- #### 6. **验证结果** ```bash # 重启后检查自动挂载 reboot df -hT | grep /data # 测试读写权限 touch /data/testfile rm /data/testfile ``` --- ### ⚠️ 注意事项 1. **操作前备份数据**:分区和格式化会清除磁盘内容。 2. **谨慎编辑/etc/fstab**:错误配置可能导致系统无法启动。 3. **使用UUID挂载**:设备名(如`/dev/vdb1`)可能变动,UUID更稳定[^3]。 4. **磁盘权限**:若普通用户需访问挂载点,用`chmod`设置目录权限(如`chmod 755 /data`)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值