添加新磁盘并开机自动挂载


:Mr.zhou  阅读: 1,351 次

  为虚拟机增加一块 2G 的磁盘。

add-hard-disk1

add-hard-disk2

add-hard-disk3

add-hard-disk4

add-hard-disk5

add-hard-disk6

add-hard-disk7

 

add-hard-disk8

 

  按以上步骤为虚拟机添加完一块新磁盘后,开机进入系统。对新增的磁盘进行分区、格式化、挂载、开机自动挂载。

  通过 fdisk –l 命令查看系统识别到的新增磁盘:

[root@mrzhou ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x00025055

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26          91      524288   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3              91        1306     9755648   83  Linux

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x00000000

[root@mrzhou ~]# 

  /dev/sdb 为新增加的磁盘,目前该磁盘还未进行分区。下面对 sdb 进行分区,划分为一个主分区。

[root@mrzhou ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x3ea44d1f.
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-261, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): 
Using default value 261

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

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

  查看分区后的磁盘。

[root@mrzhou ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x3ea44d1f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         261     2096451   83  Linux
[root@mrzhou ~]# 

  新增的磁盘 sdb 已被分为一个主分区 /dev/sdb1。下面将 /dev/sdb1 分区格式化。

[root@mrzhou ~]# mkfs.ext4 /dev/sdb1
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
131072 inodes, 524112 blocks
26205 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912

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

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@mrzhou ~]# tune2fs -c 0 /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Setting maximal mount count to -1
[root@mrzhou ~]#  

  将分区表的改变告知内核。

[root@mrzhou ~]# partprobe /dev/sdb1
[root@mrzhou ~]# 

  分区已格式化完毕。创建挂载点 /data ,将新增的磁盘分区挂载到 /data 用于网站数据的存储。

[root@mrzhou /]# mkdir /data
[root@mrzhou data]# mount /dev/sdb1 /data
[root@mrzhou data]# df -h /data
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       2.0G  3.0M  1.9G   1% /data
[root@mrzhou data]# df -hi /data
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/sdb1        128K    11  128K    1% /data
[root@mrzhou data]# 

  新增磁盘分区已成功挂载。下面编辑 /etc/fstab 文件 ,让系统启动时自动挂载该磁盘分区。

[root@mrzhou data]# lsblk -f        
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sr0                                                      
sda                                                      
├─sda1 ext4         054fd0b1-1624-43d4-8955-1ced9914f2be /boot
├─sda2 swap         92ed7802-e438-416c-ac9a-88a8afa1f6eb [SWAP]
└─sda3 ext4         7612a4dd-9cda-4e64-82db-4cde0f8ab2b8 /
sdb                                                      
└─sdb1 ext4         1c6c582e-8b7c-4dc7-9405-c6ed6634c9a5 
[root@mrzhou data]# 

  在 /etc/fstab 配置文件中,需要挂载的设备可以使用设备名 /dev/sdb1 也可以使用 该设备的 UUID,以下使用UUID进行演示。获取 /dev/sdb1 的UUID。

[root@mrzhou data]# lsblk -f|grep "sdb1"|sed -r 's/^.*ext4 *(.*)/\1/g'
1c6c582e-8b7c-4dc7-9405-c6ed6634c9a5 
[root@mrzhou data]# 

  将要挂载的信息写入 /etc/fstab

[root@mrzhou data]# echo "UUID=`lsblk -f|grep "sdb1"|sed -r 's/^.*ext4 *(.*)/\1/g'` /data ext4 defaults 0 0" >> /etc/fstab
[root@mrzhou data]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu May 14 17:16:26 2015
#
# 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
#
UUID=7612a4dd-9cda-4e64-82db-4cde0f8ab2b8 /                       ext4    defaults        1 1
UUID=054fd0b1-1624-43d4-8955-1ced9914f2be /boot                   ext4    defaults        1 2
UUID=92ed7802-e438-416c-ac9a-88a8afa1f6eb swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
UUID=1c6c582e-8b7c-4dc7-9405-c6ed6634c9a5  /data ext4 defaults 0 0
[root@mrzhou data]# 

  使用 mount –a 选项检查 /etc/fstab 配置文件是否有错误。在 Linux 中没有信息就是最好的信息。>_<

[root@mrzhou ~]# mount -a
[root@mrzhou ~]# 

  重启系统检查。

[root@mrzhou data]# init 6
[root@mrzhou ~]# mount
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/sdb1 on /data type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
[root@mrzhou ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       9.1G  1.6G  7.0G  19% /
tmpfs           491M     0  491M   0% /dev/shm
/dev/sda1       190M   27M  153M  16% /boot
/dev/sdb1       2.0G  3.0M  1.9G   1% /data
[root@mrzhou ~]# 

  到此经过重启测试,新增磁盘已能开机自动挂载至 /data。

  /etc/fstab 配置文件 详解 http://www.z-dig.com/fstab-xiangjie.html


转载请注明原文链接:http://www.z-dig.com/add-new-disks-and-power-on-the-mount.html



正文部分到此结束