Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1056440
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-07 16:03:01

Ubuntu中添加新硬盘

       为系统扩充磁盘存储容量是一件非常常见的事情,特别是现在在云平台中。不像windows系统,当我们在系统中添加一块硬盘时,系统有界面提示有新硬件,你可以直接在新硬件上右键,然后格式化之后就能用了。但在Linux系统中,如果你没有安装桌面系统,要能把新的硬盘使用起来还是要做一些配置的。

这里我介绍一下在云主机ubuntu系统中如何处理新加的硬盘的,具体来说分为以下6步:

1)找到新添加的硬盘设备,这个需要从系统的log里找。

[html] view plaincopy
  1. stack@ubunt:~$ dmesg |grep vd  
  2. [    0.949797]  vda: vda1 vda2 < vda5 >  
  3. [    0.959385]  vdb: unknown partition table  
  4. [    1.159555] systemd-udevd[119]: starting version 204  
  5. [    1.442816] EXT4-fs (vda1): mounted filesystem with ordered data mode. Opts: (null)  
  6. [    2.189858] Adding 1046524k swap on /dev/vda5.  Priority:-1 extents:1 across:1046524k FS  
  7. [    2.416559] EXT4-fs (vda1): re-mounted. Opts: errors=remount-ro  
  8. [    4.663330] systemd-udevd[361]: starting version 204  


因为我这里使用的时虚拟的硬盘所以设备名是以“vd”开头的,如果你添加的是IDE接口的硬盘你可以grep “hd",如果是SATA/SAS等scsi的硬盘,可以grep “sd"。

2)使用fdisk在硬盘上创建分区。

[html] view plaincopy
  1. stack@ubunt:~$ fdisk /dev/vdb  
  2. fdisk: unable to open /dev/vdb: Permission denied  
  3. stack@ubunt:~$ sudo fdisk /dev/vdb  
  4. sudo: unable to resolve host ubunt  
  5. Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel  
  6. Building a new DOS disklabel with disk identifier 0x6e850859.  
  7. Changes will remain in memory only, until you decide to write them.  
  8. After that, of course, the previous content won't be recoverable.  
  9.   
  10. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)  
  11.   
  12. Command (m for help): n  
  13. Partition type:  
  14.    p   primary (0 primary, 0 extended, 4 free)  
  15.    e   extended  
  16. Select (default p): p  
  17. Partition number (1-4, default 1): 1  
  18. First sector (2048-209715199, default 2048):  
  19. 'Using default value 2048  
  20. Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):  
  21. Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):  
  22. Using default value 209715199  
  23.   
  24. Command (m for help): w  
  25. The partition table has been altered!  
  26.   
  27. Calling ioctl() to re-read partition table.  
  28. Syncing disks.  
"n"表示新建一个分区,"p"表示创建一个主分区,“1”表示只创建一个分区,“w“表示保存分区信息。在输入分区个数之后,系统会让你配置分区的大小,直接回车选择默认的数字就行。


3)时新建的分区在系统中生效。

stack@ubunt:~$ partprobe

4)格式化新建的分区。


[html] view plaincopy
  1. stack@ubunt:~$ sudo mkfs.ext4 /dev/vdb1  
  2. mke2fs 1.42.9 (4-Feb-2014)  
  3. Filesystem label=  
  4. OS type: Linux  
  5. Block size=4096 (log=2)  
  6. Fragment size=4096 (log=2)  
  7. Stride=0 blocks, Stripe width=0 blocks  
  8. 6553600 inodes, 26214144 blocks  
  9. 1310707 blocks (5.00%) reserved for the super user  
  10. First data block=0  
  11. Maximum filesystem blocks=4294967296  
  12. 800 block groups  
  13. 32768 blocks per group, 32768 fragments per group  
  14. 8192 inodes per group  
  15. Superblock backups stored on blocks:  
  16.     32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,  
  17.     4096000, 7962624, 11239424, 20480000, 23887872  
  18.   
  19. Allocating group tables: done  
  20. Writing inode tables: done  
  21. Creating journal (32768 blocks): done  
  22. Writing superblocks and filesystem accounting information: done  
5)创建硬盘挂载点,修改/etc/fstab使得硬盘能够一直挂载在系统中。


在新版本的ubuntu系统中/etc/fstab推荐使用分区的uuid来定义分区的挂载点,所以需要首先得到磁盘的uuid,这可以通过blkid命令得到。


[html] view plaincopy
  1. stack@ubunt:~$ sudo blkid -p /dev/vdb1  
  2. /dev/vdb1: UUID="7689119f-9ad7-4cf8-a7c5-0589147b3566" VERSION="1.0" TYPE="ext4" USAGE="filesystem" PART_ENTRY_SCHEME="dos" PART_ENTRY_TYPE="0x83" PART_ENTRY_NUMBER="1" PART_ENTRY_OFFSET="2048" PART_ENTRY_SIZE="209713152" PART_ENTRY_DISK="253:16"  
然后在/etc/fstab中加入下面的配置后保存,退出。


[html] view plaincopy
  1. UUID=7689119f-9ad7-4cf8-a7c5-0589147b3566 /usr/localext4  ext4    defaults   0  0  
6)重启系统,然后通过df命令确认。


[html] view plaincopy
  1. stack@ubunt:~$ df /dev/vdb1  
  2. Filesystem     1K-blocks  Used Available Use% Mounted on  
  3. /dev/vdb1      103080224 61044  97759968   1% /usr/localext4  
  4. stack@ubunt:~$ cd /usr/localext4/  
  5. stack@ubunt:/usr/localext4$ ls  
  6. lost+found  
  7. stack@ubunt:/usr/localext4$ touch testfile  
  8. touch: cannot touch ‘testfile’: Permission denied  
  9. stack@ubunt:/usr/localext4$ sudo touch testfile  
  10. stack@ubunt:/usr/localext4$ ls -la  
  11. total 24  
  12. drwxr-xr-x  3 root root  4096 Apr 15 22:53 .  
  13. drwxr-xr-x 11 root root  4096 Apr 15 22:03 ..  
  14. drwx------  2 root root 16384 Apr 15 21:56 lost+found  
  15. -rw-r--r--  1 root root     0 Apr 15 22:53 testfile  

Bingo!完成!
阅读(1227) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~