玩转linux硬盘加载
1.挂上硬盘之后用fdisk -l来看硬盘的编号:
[root@management ~]# fdisk -l
Disk /dev/xvda: 107.4 GB, 107374182400 bytes
//硬盘名为/dev/xvda,一共107.4GB
97 heads, 17 sectors/track, 127177 cylinders
Units = cylinders of 1649 * 512 = 844288 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003827e
Device Boot Start End Blocks Id System
/dev/xvda1 * 2 7632 6291456 83 Linux
//只有一个分区,这个分区从第2个cylinders到7632个cylinders
Disk /dev/xvdb: 322.1 GB, 322122547200 bytes
255 heads, 63 sectors/track, 39162 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
Disk /dev/xvda: 107.4 GB, 107374182400 bytes
//硬盘名为/dev/xvda,一共107.4GB
97 heads, 17 sectors/track, 127177 cylinders
Units = cylinders of 1649 * 512 = 844288 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003827e
Device Boot Start End Blocks Id System
/dev/xvda1 * 2 7632 6291456 83 Linux
//只有一个分区,这个分区从第2个cylinders到7632个cylinders
Disk /dev/xvdb: 322.1 GB, 322122547200 bytes
255 heads, 63 sectors/track, 39162 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
可以看出此硬盘有107G,但系统分区(/dev/xvda1)只使用了6G,还有100G没有挂靠到文件系统。
2.fdisk剩余容量分区
写道
[root@management ~]# fdisk /dev/xvda
//fdisk 硬盘名
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
//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
//p为主分区,最多只能有4个主分区
Partition number (1-4): 2
//前面已经有一个主分区了,这块选2
First cylinder (1-127177, default 1): 7633
Last cylinder, +cylinders or +size{K,M,G} (7633-127177, default 127177):
Using default value 127177
//因为第一个分期已经使用了从第2个cylinders到第7632个cylinders,所以这个分期的开始cylinders为7633,截至到最后一个就行
Command (m for help): w
//保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
//警告,这次分区变更并没有同步到系统中,需要用partprobe(8) or kpartx(8)同步,或者重启。我这次用partporbe没有成功,还是重启后才搞定。
//fdisk 硬盘名
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
//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
//p为主分区,最多只能有4个主分区
Partition number (1-4): 2
//前面已经有一个主分区了,这块选2
First cylinder (1-127177, default 1): 7633
Last cylinder, +cylinders or +size{K,M,G} (7633-127177, default 127177):
Using default value 127177
//因为第一个分期已经使用了从第2个cylinders到第7632个cylinders,所以这个分期的开始cylinders为7633,截至到最后一个就行
Command (m for help): w
//保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
//警告,这次分区变更并没有同步到系统中,需要用partprobe(8) or kpartx(8)同步,或者重启。我这次用partporbe没有成功,还是重启后才搞定。
3.mkfs格式化分区
在系统没有获取最新分区信息之前,mkfs的时候会报找不到此分区的错误。
[root@management ~]# mkfs -t ext4 /dev/xvda2
mke2fs 1.41.12 (17-May-2010)
Could not stat /dev/xvda2 --- No such file or directory
The device apparently does not exist; did you specify it correctly?
mke2fs 1.41.12 (17-May-2010)
Could not stat /dev/xvda2 --- No such file or directory
The device apparently does not exist; did you specify it correctly?
重启之后
写道
[root@management ~]# mkfs -t ext4 /dev/xvda2
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
6160384 inodes, 24641213 blocks
1232060 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
752 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
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
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
6160384 inodes, 24641213 blocks
1232060 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
752 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
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
4. mount
mount后将home下所有数数据迁到这个分区
mkdir /tmpmnt
mount /dev/xvda2 /tmp/mnt
mv /home/* /tmp/mnt
umount /dev/xvda2
mount /dev/xvda2 /home
搞定。
还有一点需要注意,所有mount到文件系统的分区,都需要到/etc/fstab注册一下,让系统重启的时候把这个分区自动load进来。
下面是我们服务器的fstab配置细节,其中最后两行是我们加上的。
写道
#
# /etc/fstab
# Created by anaconda on Mon Jun 9 15:02:26 2014
#
# 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=23cecb9c-baa3-4407-9485-ddf59583c3ed / ext4 defaults 1 1
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
/dev/xvda2 /home auto defaults,nofail,comment=cloudconfig 0 2
/dev/xvdf /mnt auto defaults,nofail,comment=cloudconfig 0 2
# /etc/fstab
# Created by anaconda on Mon Jun 9 15:02:26 2014
#
# 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=23cecb9c-baa3-4407-9485-ddf59583c3ed / ext4 defaults 1 1
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
/dev/xvda2 /home auto defaults,nofail,comment=cloudconfig 0 2
/dev/xvdf /mnt auto defaults,nofail,comment=cloudconfig 0 2