linux磁盘挂载

本文经过多次编辑,存在数据上下文不一致情况,大家理解操作即可,无须在意数据。

下面的所有操作,记得注意权限问题,可切换到root用户

一 、挂载到新目录

查看磁盘

[root@localhost ~]# fdisk -l

Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000c81ed

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   104857566    52427759+  83  Linux

Disk /dev/vdb: 2199.0 GB, 2199023255552 bytes, 4294967296 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

磁盘/dev/vdb尚有2199GB未分配

查看已挂载的磁盘分区

[root@localhost ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  7.8G     0  7.8G   0% /dev
tmpfs          tmpfs     7.8G     0  7.8G   0% /dev/shm
tmpfs          tmpfs     7.8G  488K  7.8G   1% /run
tmpfs          tmpfs     7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/vda1      ext4       50G  2.0G   45G   5% /
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/0

磁盘/dev/vdb实际并没有挂载

查看磁盘分区情况

使用lsblk命令确认/dev/vdb设备是否存在并可用:

root@kjb-shxm-server:/# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0                       7:0    0   62M  1 loop /snap/core20/1611
loop1                       7:1    0 63.5M  1 loop /snap/core20/2015
loop2                       7:2    0 67.8M  1 loop /snap/lxd/22753
loop3                       7:3    0 91.9M  1 loop /snap/lxd/24061
loop4                       7:4    0   47M  1 loop /snap/snapd/16292
sr0                        11:0    1 1024M  0 rom  
vda                       252:0    0  100G  0 disk 
├─vda1                    252:1    0    1M  0 part 
├─vda2                    252:2    0    2G  0 part /boot
└─vda3                    252:3    0   98G  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0   98G  0 lvm  /
vdb                       252:16   0  1.5T  0 disk

如果/dev/vdb设备没有分区,您需要先对其进行分区。

创建磁盘新分区(根据情况而定)

fdisk /dev/vdb

注意:

# 执行"fdisk /dev/vdb"后,出现"Command (m for help)"提示时,依次输入下面指令
n -> p -> 1 -> 回车 -> 回车 -> w

详情:

[root@localhost ~]# fdisk /dev/vdb
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x11e106cf.

WARNING: The size of this disk is 2.2 TB (2199023255552 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID 
partition table format (GPT).


Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-4294967295, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4294967294, default 4294967294): 
Using default value 4294967294
Partition 1 of type Linux and of size 2 TiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.

说明:

n:添加新分区

p:设为主分区

1: 分区序号

两个回车指是开始和结束的磁盘大小;

w:写入磁盘

再次查看磁盘分区情况

使用lsblk命令确认/dev/vdb设备是否存在并可用:

root@kjb-shxm-server:/# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0                       7:0    0   62M  1 loop /snap/core20/1611
loop1                       7:1    0 63.5M  1 loop /snap/core20/2015
loop2                       7:2    0 67.8M  1 loop /snap/lxd/22753
loop3                       7:3    0 91.9M  1 loop /snap/lxd/24061
loop4                       7:4    0   47M  1 loop /snap/snapd/16292
sr0                        11:0    1 1024M  0 rom  
vda                       252:0    0  100G  0 disk 
├─vda1                    252:1    0    1M  0 part 
├─vda2                    252:2    0    2G  0 part /boot
└─vda3                    252:3    0   98G  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0   98G  0 lvm  /
vdb                       252:16   0  1.5T  0 disk 
└─vdb1                    252:17   0  1.5T  0 part

发现新分区为/dev/vdb1

格式化新分区

格式化分区为所需的文件系统类型(例如ext4):这里假设分区为/dev/vdb1,如果分区号不同,请相应调整。

[root@localhost ~]# mkfs.ext4 /dev/vdb1
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
134217728 inodes, 536870655 blocks
26843532 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
16384 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, 78675968, 
    102400000, 214990848, 512000000

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

挂载磁盘

将分区挂载到创建的挂载点:

mkdir /mnt
mount /dev/vdb1 /mnt

检查确认磁盘

[root@localhost ~]# fdisk -l 

Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000c81ed

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   104857566    52427759+  83  Linux

Disk /dev/vdb: 2199.0 GB, 2199023255552 bytes, 4294967296 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: 0x11e106cf

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048  4294967294  2147482623+  83  Linux

检查确认分区

[root@localhost ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  7.8G     0  7.8G   0% /dev
tmpfs          tmpfs     7.8G     0  7.8G   0% /dev/shm
tmpfs          tmpfs     7.8G  492K  7.8G   1% /run
tmpfs          tmpfs     7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/vda1      ext4       50G  2.0G   45G   5% /
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/0
/dev/vdb1      ext4      2.0T   81M  1.9T   1% /mnt

开机启动

为了让系统在启动时自动挂载该设备,您可以将相应的挂载配置添加到/etc/fstab文件中。打开文件并在末尾添加以下行(假设使用ext4文件系统):

echo "/dev/vdb1 /mnt ext4 defaults 0 0" >> /etc/fstab

二 、挂载到旧目录

如果你要用来挂载的目录里面并不是空的,那么挂载了文件系统之后,原目录下的东西就会暂时的消失。并不是被覆盖掉,而是暂时的隐藏了起来,等到新分割槽被卸除之后,则原目录原本的内容就会再次出来。

如果要永久挂载已有目录,可以在新硬盘创建文件系统后,先挂载到一个临时目录,然后把要扩展的目录复制到这临时目录,然后删除要扩展的目录,再卸载临时挂载点,重新挂载到要扩展的目录上。举例:

# 比如要扩充 /var

# 在创建好文件系统后 新建临时挂载点 mnt
mkdir /mnt

# 将/dev/sdb1挂载到/mnt下
mount /dev/vdb1 /mnt

# 拷贝/var下的所有内容到新的硬盘
cp -pdr /var/* /mnt

# 删除当前/var目录下的内容
rm -rf /var/*

# 卸载磁盘
umount /dev/vdb1

# 重新挂载磁盘到/var目录
mount /dev/vdb1 /var

# 过程中若提示磁盘忙,使用fuser找出将正在使用磁盘的程序并结束掉;
fuser -m -v /var
fuser -m -v -i -k /var

# 开机启动
echo "/dev/vdb1 /var ext4 defaults 0 0" >> /etc/fstab
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值