Liunx 挂载磁盘

查看已经挂载的分区和文件系统类型

[root@VM_101_18212122_centos /]# df -T     
Filesystem    Type   1K-blocks      Used Available Use% Mounted on
/dev/vda1     ext3    20641404  13449448   6143440  69% /

 

fdisk -l 可以显示出所有挂载和未挂载的分区,但不显示文件系统类型

[root@VM_101_18212122_centos /]# fdisk -l // 查看磁盘

Disk /dev/vda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units
= cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical
/physical): 512 bytes / 512 bytes
I
/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier:
0x00072b28

Device Boot Start End Blocks Id System // 设备启动端块 ID 系统
/dev/vda1 * 1 2611 20970496 83 Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes      // 这块磁盘没有设备 是因为没有挂载
16 heads, 63 sectors/track, 208050 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:
0x00000000

 

fdisk 命令进行建立分区

磁盘分区分为:主分区、扩展分区、逻辑分区;主分区至少有 1 个,最多 4 个;扩展分区可以没有,最多 1 个

主分区 + 扩展分区 最多不得超过 4 个,如果需要多个分区可以先建立扩展分区,在扩展分区中划分多个逻辑分区

执行分区步骤可以根据 :fdisk 命令   Linux 下磁盘挂载  这 2 篇博客文章操作

自定义当前第几块分区注意:Partition number(1-4) :number  这个数字是指在当前这块硬盘上第几个分区

[root@VM_101_18212122_centos ~]# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf3de3926.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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): n
Command action
e extended
p primary partition (
1-4)
p
Partition number (
1-4): 1 // 第一个分区
First cylinder (1-208050, default 1):
Using
default value 1
Last cylinder,
+cylinders or +size{K,M,G} (1-208050, default 208050): +50G

Command (m for help): p

Disk /dev/vdb: 107.4 GB, 107374182400 bytes
16 heads, 63 sectors/track, 208050 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:
0xf3de3926

Device Boot Start End Blocks Id System
/dev/vdb1 1 104026 52429072+ 83 Linux

Command (m for help): n
Command action
e extended
p primary partition (
1-4)
e
Partition number (
1-4): 1 // 这里必须至少从 2 开始
Partition 1 is already defined. Delete it before re-adding it.

Command (m for help): n
Command action
e extended
p primary partition (
1-4)
e
Partition number (
1-4): 2
First cylinder (
104027-208050, default 104027):
Using
default value 104027
Last cylinder,
+cylinders or +size{K,M,G} (104027-208050, default 208050):
Using
default value 208050

Command (m for help): p

Disk /dev/vdb: 107.4 GB, 107374182400 bytes
16 heads, 63 sectors/track, 208050 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:
0xf3de3926

Device Boot Start End Blocks Id System
/dev/vdb1 1 104026 52429072+ 83 Linux
/dev/vdb2 104027 208050 52428096 5 Extended

Command (m for help): p // 打印磁盘情况

Disk
/dev/vdb: 107.4 GB, 107374182400 bytes
16 heads, 63 sectors/track, 208050 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:
0xf2295fd0

Device Boot Start End Blocks Id System
/dev/vdb1 1 104026 52429072+ 83 Linux
/dev/vdb2 104027 208050 52428096 83 Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@VM_101_182121222_centos
~]# fdisk -l

Disk /dev/vda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units
= cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical
/physical): 512 bytes / 512 bytes
I
/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier:
0x00072b28

Device Boot Start End Blocks Id System
/dev/vda1 * 1 2611 20970496 83 Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes
16 heads, 63 sectors/track, 208050 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:
0xf2295fd0

Device Boot Start End Blocks Id System
/dev/vdb1 1 104026 52429072+ 83 Linux
/dev/vdb2 104027 208050 52428096 83 Linux

View Code

如果第一个分区划分为主分区,第二个分区划分为扩展分区,这个地方就至少填 2,如果填写 1,会提示 Partition 1 is already defined. Delete it before re-adding it.

 

lsblk -f 也可以查看未挂载的文件系统类型

[root@VM_101_18212122_centos /]# lsblk -f
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
vda                                                      
`-vda1 ext3         dddcd0d3-764f-4013-93f6-2037ab1294f7 /  // 已挂载
vdb    ext3         0f1a7f8b-cbea-47e8-b19a-84e28c57ad2a   // 未分区挂载

 

parted -l 可以查看未挂载的文件系统类型,以及哪些分区尚未格式化

[root@VM_101_18212122_centos ~]# parted -l 
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 1049kB 21.5GB 21.5GB primary ext3 boot

Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 32.3kB 53.7GB 53.7GB primary
2 53.7GB 107GB 53.7GB primary

 

格式化分区   mkfs. 文件系统格式   分区名

[root@VM_101_182121222_centos ~]# mkfs.ext3 /dev/vdb2  // 执行格式化命令
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13107024 blocks
655351 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
400 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

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@VM_101_18212122_centos
~]# lsblk -f // 查看磁盘情况
NAME FSTYPE LABEL UUID MOUNTPOINT
vda
</span>-vda1 ext3 dddcd0d3-764f-<span style="color: rgba(128, 0, 128, 1)">4013</span>-93f6-2037ab1294f7 /<span style="color: rgba(0, 0, 0, 1)"> vdb ext3 0f1a7f8b</span>-cbea-47e8-b19a-<span style="color: rgba(0, 0, 0, 1)">84e28c57ad2a </span>|-<span style="color: rgba(0, 0, 0, 1)">vdb1
-vdb2 ext3 c064d156-6d83-4040-a20e-ef15e4d084f3

注意:格式化前看一下文件系统类型

格式化分区注意: 不是格式化的硬盘,直接格式化扩展分区是不允许的,只能格式化主分区和逻辑分区

 

挂载磁盘

[root@VM_101_18212122_centos /]# mount /dev/vdb2 /data  // 挂载目录, 有的系统需要通知系统内核分区表的变化,不然内核不知道分区(或重启系统)执行 partprobe 命令
[root@VM_101_182_centos /]# df -TH /data    // 查看是否挂载成功
Filesystem    Type     Size   Used  Avail Use% Mounted on
/dev/vdb2     ext3      53G   189M    50G   1% /data
[root@VM_101_1821_centos /]# echo '/dev/vdb2 /data ext3 defaults 0 0' >> /etc/fstab   // 添加数据盘挂载信息至 /etc/fstab,实现开机自动挂载
[root@VM_101_182_centos /]# cat /etc/fstab   // 查看写入
/dev/vda1            /                    ext3       noatime,acl,user_xattr 1 1
proc                 /proc                proc       defaults              0 0
sysfs                /sys                 sysfs      noauto                0 0
debugfs              /sys/kernel/debug    debugfs    noauto                0 0
devpts               /dev/pts             devpts     mode=0620,gid=5       0 0
/dev/vdb2            /data                ext3       defaults              0 0

 

卸载挂载点

[root@VM_101_18212122_centos /]# umount /dev/vdb2  // 通过设备卸载 或者 通挂载点卸载 umount /data
[root@VM_101_18212122_centos /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda1              20G   13G  5.9G  69% /

 注意:如果设备正忙,卸载即告失败。原因一般是你当前在挂载点的某个目录中;也可以用 lsof 列出已打开文件,然后搜索列表查找待卸载的挂载点:lsof | grep data  查找 data 分区里打开的文件

 

删除分区

[root@VM_101_18212122_centos /]# fdisk /dev/vdb

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): d
Partition number (
1-4): 1 // 删除第一个分区

Command (m for help): d
Selected partition
2 // 我这个磁盘一共分了 2 个区,执行 2 次 d 就是选择 2 个分区

Command (m for help): w // 保存退出

 记得修改 vim /etc/fstab  文件