linux 分区

今天,在整理图片时,发现根目录硬盘空间已经所剩无几了,而网站和所有相关文件主要放置在根下data目录(/data)里,但/home下是有空间,而又不想把文件放到/home下面去,只有再加一个新硬盘,对以后/data目录的文件管理也比较好。

那么就先关掉主机,再加上新硬盘(其它电脑使用过的500G硬盘),插上后再启动电脑。

接下来,就开始工作了:

1. 查看硬盘信息:
  
  
  1. feigo@figo:~$ sudo df -h
  2. 文件系统 容量 已用 可用 已用% 挂载点
  3. rootfs 102G 92G 4.7G 96% /
  4. udev 10M 0 10M 0% /dev
  5. tmpfs 799M 1.8M 798M 1% /run
  6. /dev/disk/by-uuid/3640f24a-4870-42e2-ae07-109cb9c35164 102G 92G 4.7G 96% /
  7. tmpfs 5.0M 0 5.0M 0% /run/lock
  8. tmpfs 4.6G 224K 4.6G 1% /run/shm
  9. /dev/sda7 92M 26M 62M 30% /boot
  10. /dev/sda9 201G 23G 169G 12% /home
  
  
  1. feigo@figo:~$ sudo fdisk -l
  2.  
  3. Disk /dev/sdb: 500.1 GB, 500107862016 bytes
  4. 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
  5. Units = sectors of 1 * 512 = 512 bytes
  6. Sector size (logical/physical): 512 bytes / 512 bytes
  7. I/O size (minimum/optimal): 512 bytes / 512 bytes
  8. Disk identifier: 0x3f1e271a
  9.  
  10. Device Boot Start End Blocks Id System
  11. /dev/sdb1 63 102414374 51207156 7 HPFS/NTFS/exFAT
  12. /dev/sdb2 102414375 976768064 437176845 f W95 Ext'd (LBA)
  13. /dev/sdb5 102414438 409625369 153605466 b W95 FAT32
  14. /dev/sdb6 409625433 716836364 153605466 b W95 FAT32
  15. /dev/sdb7 716836428 976768064 129965818+ 7 HPFS/NTFS/exFAT
  16.  
  17. Disk /dev/sda: 500.1 GB, 500107862016 bytes
  18. 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
  19. Units = sectors of 1 * 512 = 512 bytes
  20. Sector size (logical/physical): 512 bytes / 512 bytes
  21. I/O size (minimum/optimal): 512 bytes / 512 bytes
  22. Disk identifier: 0xc35aebd4
  23.  
  24. Device Boot Start End Blocks Id System
  25. /dev/sda1 * 63 104872319 52436128+ 7 HPFS/NTFS/exFAT
  26. /dev/sda2 104872381 976771071 435949345+ f W95 Ext'd (LBA)
  27. /dev/sda5 104872383 231962534 63545076 7 HPFS/NTFS/exFAT
  28. /dev/sda6 231962598 300929579 34483491 b W95 FAT32
  29. /dev/sda7 300931072 301125631 97280 83 Linux
  30. /dev/sda8 301127680 517726207 108299264 83 Linux
  31. /dev/sda9 517728256 945461247 213866496 83 Linux
  32. /dev/sda10 945463296 976771071 15653888 82 Linux swap / Solaris
我们可以看到有两个硬盘,sda和sdb,其中sda是目前使用中的硬盘,sdb是新加上去的硬盘。

2. 针对sdb创建新硬盘分区:
先说明一下,fdisk 可以用 m 命令来看 fdisk 命令的内部命令:
a :命令指定启动分区
d :命令删除一个存在的分区
l :命令显示分区 ID 号的列表
m :查看 fdisk 命令帮助
n :命令创建一个新分区
p :命令显示分区列表
t :命令修改分区的类型 ID 号
w :命令是将对分区表的修改存盘让它发生作用
  
  
  1. feigo@figo:~$ su
  2. 密码:
  3. root@figo:/home/feigo# fdisk /dev/sdb
  4.  
  5. Command (m for help): m
  6. Command action
  7. a toggle a bootable flag
  8. b edit bsd disklabel
  9. c toggle the dos compatibility flag
  10. d delete a partition
  11. l list known partition types
  12. m print this menu
  13. n add a new partition
  14. o create a new empty DOS partition table
  15. p print the partition table
  16. q quit without saving changes
  17. s create a new empty Sun disklabel
  18. t change a partition's system id
  19. u change display/entry units
  20. v verify the partition table
  21. w write table to disk and exit
  22. x extra functionality (experts only)
由于sdb硬盘上已经有几个分区,人们需要先删除这些分区:
  
  
  1. Command (m for help): d # 删除分区,依次输入数字
  2. Partition number (1-7): 7
  3.  
  4. Command (m for help): d
  5. Partition number (1-6): 6
  6.  
  7. Command (m for help): d
  8. Partition number (1-5): 5
  9.  
  10. Command (m for help): d
  11. Partition number (1-5): 2
  12.  
  13. Command (m for help): d
  14. Selected partition 1
  15.  
  16. Command (m for help): w # 保存
  17. The partition table has been altered!
  18.  
  19. Calling ioctl() to re-read partition table.
  20. Syncing disks.

再次查看硬盘分区信息,将会看到所有分区已被删除:
  
  
  1. root@figo:/home/feigo# fdisk -l
  2.  
  3. Disk /dev/sdb: 500.1 GB, 500107862016 bytes
  4. 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
  5. Units = sectors of 1 * 512 = 512 bytes
  6. Sector size (logical/physical): 512 bytes / 512 bytes
  7. I/O size (minimum/optimal): 512 bytes / 512 bytes
  8. Disk identifier: 0x3f1e271a
  9.  
  10. Device Boot Start End Blocks Id System
  11.  
  12. Disk /dev/sda: 500.1 GB, 500107862016 bytes
  13. 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
  14. Units = sectors of 1 * 512 = 512 bytes
  15. Sector size (logical/physical): 512 bytes / 512 bytes
  16. I/O size (minimum/optimal): 512 bytes / 512 bytes
  17. Disk identifier: 0xc35aebd4
  18.  
  19. Device Boot Start End Blocks Id System
  20. /dev/sda1 * 63 104872319 52436128+ 7 HPFS/NTFS/exFAT
  21. /dev/sda2 104872381 976771071 435949345+ f W95 Ext'd (LBA)
  22. /dev/sda5 104872383 231962534 63545076 7 HPFS/NTFS/exFAT
  23. /dev/sda6 231962598 300929579 34483491 b W95 FAT32
  24. /dev/sda7 300931072 301125631 97280 83 Linux
  25. /dev/sda8 301127680 517726207 108299264 83 Linux
  26. /dev/sda9 517728256 945461247 213866496 83 Linux
  27. /dev/sda10 945463296 976771071 15653888 82 Linux swap / Solaris

在之前的命令列表上面看到,n代表创建新硬盘分区,那么接下我们输入n命令来创建新分区:
  
  
  1. root@figo:/home/feigo# fdisk /dev/sdb
  2.  
  3. Command (m for help): n
  4. Partition type:
  5. p primary (0 primary, 0 extended, 4 free)
  6. e extended
  7. Select (default p): p
  8. Partition number (1-4, default 1): 1
  9. First sector (2048-976773167, default 2048):
  10. Using default value 2048
  11. Last sector, +sectors or +size{K,M,G} (2048-976773167, default 976773167):
  12. Using default value 976773167
  13.  
  14. Command (m for help): w
  15. The partition table has been altered!
  16.  
  17. Calling ioctl() to re-read partition table.
  18. Syncing disks.
完成,再查看,已经发现多了一个sdb1的分区:
  
  
  1. root@figo:/home/feigo# fdisk -l
  2.  
  3. Disk /dev/sdb: 500.1 GB, 500107862016 bytes
  4. 81 heads, 63 sectors/track, 191411 cylinders, total 976773168 sectors
  5. Units = sectors of 1 * 512 = 512 bytes
  6. Sector size (logical/physical): 512 bytes / 512 bytes
  7. I/O size (minimum/optimal): 512 bytes / 512 bytes
  8. Disk identifier: 0x3f1e271a
  9.  
  10. Device Boot Start End Blocks Id System
  11. /dev/sdb1 2048 976773167 488385560 83 Linux
  12.  
  13. Disk /dev/sda: 500.1 GB, 500107862016 bytes
  14. 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
  15. Units = sectors of 1 * 512 = 512 bytes
  16. Sector size (logical/physical): 512 bytes / 512 bytes
  17. I/O size (minimum/optimal): 512 bytes / 512 bytes
  18. Disk identifier: 0xc35aebd4
  19.  
  20. Device Boot Start End Blocks Id System
  21. /dev/sda1 * 63 104872319 52436128+ 7 HPFS/NTFS/exFAT
  22. /dev/sda2 104872381 976771071 435949345+ f W95 Ext'd (LBA)
  23. /dev/sda5 104872383 231962534 63545076 7 HPFS/NTFS/exFAT
  24. /dev/sda6 231962598 300929579 34483491 b W95 FAT32
  25. /dev/sda7 300931072 301125631 97280 83 Linux
  26. /dev/sda8 301127680 517726207 108299264 83 Linux
  27. /dev/sda9 517728256 945461247 213866496 83 Linux
  28. /dev/sda10 945463296 976771071 15653888 82 Linux swap / Solaris
3. 格式化分区为ext4:
  
  
  1. root@figo:/home/feigo# mkfs.ext4 /dev/sdb1
  2. mke2fs 1.42.5 (29-Jul-2012)
  3. 文件系统标签=
  4. OS type: Linux
  5. 块大小=4096 (log=2)
  6. 分块大小=4096 (log=2)
  7. Stride=0 blocks, Stripe width=0 blocks
  8. 30531584 inodes, 122096390 blocks
  9. 6104819 blocks (5.00%) reserved for the super user
  10. 第一个数据块=0
  11. Maximum filesystem blocks=4294967296
  12. 3727 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, 71663616, 78675968,
  18. 102400000
  19.  
  20. Allocating group tables: 完成
  21. 正在写入inode表: 完成
  22. Creating journal (32768 blocks): 完成
  23. Writing superblocks and filesystem accounting information: 完成
格式化完成,可以使用parted命令查看/dev/sdb的分区格式:
  
  
  1. root@figo:/home/feigo# parted
  2. GNU Parted 2.3
  3. Using /dev/sda
  4. Welcome to GNU Parted! Type 'help' to view a list of commands.
  5. (parted) help
  6. align-check TYPE N check partition N for TYPE(min|opt) alignment
  7. check NUMBER do a simple check on the file system
  8. cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to another partition
  9. help [COMMAND] print general help, or help on COMMAND
  10. mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
  11. mkfs NUMBER FS-TYPE make a FS-TYPE file system on partition NUMBER
  12. mkpart PART-TYPE [FS-TYPE] START END make a partition
  13. mkpartfs PART-TYPE FS-TYPE START END make a partition with a file system
  14. move NUMBER START END move partition NUMBER
  15. name NUMBER NAME name partition NUMBER as NAME
  16. print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition
  17. quit exit program
  18. rescue START END rescue a lost partition near START and END
  19. resize NUMBER START END resize partition NUMBER and its file system
  20. rm NUMBER delete partition NUMBER
  21. select DEVICE choose the device to edit
  22. set NUMBER FLAG STATE change the FLAG on partition NUMBER
  23. toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
  24. unit UNIT set the default unit to UNIT
  25. version display the version number and copyright information of GNU Parted
  26. (parted) select /dev/sdb
  27. Using /dev/sdb
  28. (parted) print
  29. Model: ATA ST3500413AS (scsi)
  30. Disk /dev/sdb: 500GB
  31. Sector size (logical/physical): 512B/512B
  32. Partition Table: msdos
  33.  
  34. Number Start End Size Type File system Flags
  35. 1 1049kB 500GB 500GB primary ext4
  36.  
  37. (parted) quit
其中上面parted的内部命令:
help 代表帮助,将会打印所有内部命令
select /dev/sdb 代表切换到硬盘sdb
print 代表打印分区文件系统类型
quit 代表退出

那么我们已经知道了sdb已被格式化为ext4,接下来就要挂载硬盘来目录了:
  
  
  1. root@figo:/home/feigo# mkdir /data1
  2. root@figo:/home/feigo# mount /dev/sdb1 /data1
  3. root@figo:/home/feigo# df -Th
  4. 文件系统 类型 容量 已用 可用 已用% 挂载点
  5. rootfs rootfs 102G 93G 4.5G 96% /
  6. udev devtmpfs 10M 0 10M 0% /dev
  7. tmpfs tmpfs 799M 3.0M 796M 1% /run
  8. /dev/disk/by-uuid/3640f24a-4870-42e2-ae07-109cb9c35164 ext4 102G 93G 4.5G 96% /
  9. tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
  10. tmpfs tmpfs 4.6G 376K 4.6G 1% /run/shm
  11. /dev/sda7 ext4 92M 26M 62M 30% /boot
  12. /dev/sda9 ext4 201G 23G 169G 12% /home
  13. /dev/sdb1 ext4 459G 198M 435G 1% /data1
可以看到/dev/sdb1已经被挂载到/data1下了,剩余的工作就是迁移数据了。

4. 最后一步就是实现开机自动挂载/dev/sdb1到/data1下:
编辑/etc/fstab文件,加入下面两行内容:
  
  
  1. root@figo:/home/feigo# vi /etc/fstab
  2. # load /dev/sdb1 to /data1 when the system startup
  3. /dev/sdb1 /data1 ext4 defaults 1 2

然后再用mount -a确定/etc/fstab设置没有错误,再用mount查看已挂载的设备:
  
  
  1. root@figo:/home/feigo# mount -a
  2. root@figo:/home/feigo# mount
  3. sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
  4. proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
  5. udev on /dev type devtmpfs (rw,relatime,size=10240k,nr_inodes=1020978,mode=755)
  6. devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
  7. tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=818060k,mode=755)
  8. /dev/disk/by-uuid/3640f24a-4870-42e2-ae07-109cb9c35164 on / type ext4 (rw,relatime,errors=remount-ro,user_xattr,barrier=1,data=ordered)
  9. tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
  10. tmpfs on /run/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=4766880k)
  11. /dev/sda7 on /boot type ext4 (rw,relatime,user_xattr,barrier=1,data=ordered)
  12. /dev/sda9 on /home type ext4 (rw,relatime,user_xattr,barrier=1,data=ordered)
  13. rpc_pipefs on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
  14. binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,nosuid,nodev,noexec,relatime)
  15. /dev/sdb1 on /data1 type ext4 (rw,relatime,user_xattr,barrier=1,data=ordered)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值