Linux磁盘挂载操作


文章目录

  1.  实验目标
  2. 实验步骤

实验目标

  • 挂载一块10G的硬盘
  • 将硬盘分成1个3G的主分区、1个1G的swap分区和1个1G的扩展分区
  • 将其中一个主分区和扩展分区分别格式化为ext4,并进行挂载
  • 实现开机自动挂载

 

实验步骤:

1、虚拟机添加硬盘

2、重启服务器,并输入lsblk查看服务器上的磁盘信息

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
...
sdb               8:16   0   10G  0 disk
sr0              11:0    1 1024M  0 rom

3、分区

fdisk /dev/sdb        #对目标分区设备文件进行分区
[root@localhost nginx]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n           #新增一个3G的主分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +3G
Partition 1 of type Linux and of size 3 GiB is set

Command (m for help): n          #新增一个1G的swap分区
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2):
First sector (6293504-20971519, default 6293504):
Using default value 6293504
Last sector, +sectors or +size{K,M,G} (6293504-20971519, default 20971519): +1G
Partition 2 of type Linux and of size 1 GiB is set

Command (m for help): t                    #改变分区ID为82
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): n                    #将剩余空间都给扩展分区
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): e
Partition number (3,4, default 3):
First sector (8390656-20971519, default 8390656):
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-20971519, default 20971519):
Using default value 20971519
Partition 3 of type Extended and of size 6 GiB is set

Command (m for help): n                    #新增一个1G大小的逻辑分区
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (8392704-20971519, default 8392704):
Using default value 8392704
Last sector, +sectors or +size{K,M,G} (8392704-20971519, default 20971519): +1G                        #设置大小
Partition 5 of type Linux and of size 1 GiB is set

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xede96e76

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     6293503     3145728   83  Linux
/dev/sdb2         6293504     8390655     1048576   82  Linux swap / Solaris
/dev/sdb3         8390656    20971519     6290432    5  Extended
/dev/sdb5         8392704    10489855     1048576   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: Device or resource busy.
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.
      
分区的操作过程

4、格式化

# mkfs -t ext4 /dev/sdb1               #对主分区1以ext4文件系统格式进行格式化
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
196608 inodes, 786432 blocks
39321 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=805306368
24 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
输出
 mkswap /dev/sdb2                      #将分区格式化为swap分区
mkswap: /dev/sdb2: warning: don't erase bootbits sectors
        (dos partition table detected). Use -f to force.
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=6b5eee00-73a6-4d10-bc33-bfab0d6911b2
swap分区格式化输出
 mkfs -t ext4 /dev/sdb5                #对逻辑分区进行格式化操作
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
输出

 5、使用分区【挂载】

  • 临时挂载
mount /dev/sdb1 /share_test/             #临时挂载主分区
mount -o rw /dev/sdb5 /logical_test/     #以读写的方式挂载逻辑分区
swapon /dev/sdb2                         #启动新的swap分区
  • 自动挂载
  1. 修改/etc/fstab文件,添加如下内容
/dev/sdb1       /share_test                     ext4    defaults    0 0
/devsdb5        /logical_test                   ext4    defaults    0 0
/dev/sdb2       swap    swap    defaults        0       0

  2.使用mount -a命令自动挂载/etc/fstab中未挂载选项

mount -a                    #此命令会自动挂载/etc/fstab中未挂载项

6、停止挂载

[root@localhost nginx]# umount /dev/sdb1         #停止挂载/dev/sdb1
[root@localhost nginx]# umount /dev/sdb5         #停止挂载/dev/sdb5    
[root@localhost nginx]# swapoff /dev/sdb2        #停止使用此swap分区

【注意】最后还需要把/etc/fstab中停止挂载的内容清除


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM