Linux 硬盘容量不足挂载新硬盘至旧文件夹中

运维的坑 2016-02-16 19:28 暂无评论

有一台服务器在阿里云中,一般阿里云送一个 20GB 的系统盘,如果数据大了,那必须要准备一个数据盘了!这台服务器起初使用的时候并没有挂载好新的硬盘,导致一直在使用的时候放的数据都是在系统盘中!20GB 的系统盘,几个备份就没了 :(

看一下当前硬盘挂载以及硬盘使用请看:dh -TH

[root@localhost ~]# df -TH
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/xvda1     ext4    22G  6.1G   15G  30% /
tmpfs          tmpfs  4.2G     0  4.2G   0% /dev/shm

检查新设备是否连接:fdisk -l

[root@localhost ~]# fdisk -l

Disk /dev/xvda: 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: 0x00078f9c

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

Disk /dev/xvdb: 536.9 GB, 536870912000 bytes
255 heads, 63 sectors/track, 65270 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: 0xdb787533

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1               1       65271   524284928    7  HPFS/NTFS

一块新的硬盘:/dev/xvdb 容量是 536.9GB,接下来给它进行分区,格盘并且挂载起来!

给硬盘分区格式化:fdisk /dev/xvdb

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

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): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
Partition 1 is already defined.  Delete it before re-adding it.

Command (m for help): d
Selected partition 1

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

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

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

注意看帮助文档 m / help 分区使用 n 选项,在分区的时候使用主分区 p 选项。分区的时候选择 1-4 个主分区,一般默认是 1 就可以了。如果出现提示 Partition 1 is already defined. Delete it before re-adding it. 的时候使用 d 选项将分区删除后重复操作,知道提示起始磁盘熟,一般没必要选择默认回车即可。最后提示你分区大小,格式一般是 +size{K,M,G} 如果我需要分一个 100GB 的分区那么输入 +100G 即可。最后 w 选项写入保存!

接着给分区格式化:mkfs -t ext3 -c /dev/xvdb1

[root@localhost ~]# mkfs -t ext3 -c /dev/xvdb1
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
32768000 inodes, 131070310 blocks
6553515 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
4000 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

Checking for bad blocks (read-only test):   0.53% done, 0:36 elapsed

直到 100% 格式化完毕即可。

格式完毕后,如果你挂载的目录下有存储文件,先将文件移动吧,或者重命名 mv xxx oldxxx 然后新建一个文件夹 mkdir xxx 然后编辑 /etc/fstab 文件使硬盘开机自动挂载。如果不需要开机自动挂载那么直接使用 mount /dev/xvdb1 /home/xxx 即可挂载!

#
# /etc/fstab
# Created by anaconda on Thu Jan  7 16:30:08 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=9da5ac4d-b6ec-49c4-9c8d-f2e7576780b5 /boot                   xfs     defaults        0 0
/dev/mapper/cl-swap     swap                    swap    defaults        0 0
LABEL=/dev/xvdb1        /home/xxx       ext3    defaults        1 2

写入到 /etc/fstab 以后,使用 mount -a 测试一下写入配置是否有误,没有错误的话重启就可以了。重启后可以直接 dh -TH 就可以看到挂载的路径了。

如果是原本存储有文件的话,需要挂载上去,移动文件夹后将硬盘挂载成功之后将内容复制回去就可以了(也可以试试移动)。如果你是直接挂载的话,文件会找不到,把硬盘取消挂载就又可以看到内容了 XD

暂无评论