fdisk入门

来源:互联网 发布:短信端口号查询 编辑:程序博客网 时间:2024/06/16 07:35

添加磁盘并挂载:
设置虚拟机系统里添加磁盘,重启虚拟机后,虚拟机目录/dev下会增加一个sdb设备。
使用fdisk /dev/sdb 管理磁盘分区,首先p 查看磁盘分区信息,然后n增加一个主分区,然后在敲入p进行分区操作,再输入1表示命名分区为sdb1,接着会提示输入开始扇区,直接回车跳过,然后在最后扇区那里输入+2G表示该分区大小设置为2G,使用p再次查看分区是否成功创建,显示有新的分区后,敲入w键表示写入磁盘。命令执行完成后,会自动退出fdisk。现在file /dev/sdb1查询分区信息,如果出错,敲入命令partprobe或者直接重启虚拟机即可。
因为Linux系统无法对没有格式化的磁盘进行读写操作,所以接下来还需要格式化创建好的分区。输入命令mkfs.xfs /dev/sdb1将其格式化为xfs文件系统格式,如果需要换成其他格式可以在敲完mkfs后再使用两次tab键来显示其他文件系统格式。
那么,现在我们只需要再挂载该分区到系统里就可以了,首先mkdir /newFS 在根目录创建文件夹用来挂载,然后就是mount /dev/sdb1 /newFS。现在就可以正常使用该分区了。但每次重启虚拟机后都需要再挂载一遍,所以我们可以在每次开机时自动挂载该分区,vi /etc/fstab 然后在文件末尾添加 /dev/sdb1 /newFS xfs defaults 0(表示是否开机磁盘自检) 0

下面是我在虚拟机里的操作过程记录

[root@localhost /]# fdisk /dev/sdbWelcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.Be careful before using the write command.Device does not contain a recognized partition tableBuilding a new DOS disklabel with disk identifier 0xf15825cf.Command (m for help): pDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0xf15825cf   Device Boot      Start         End      Blocks   Id  SystemCommand (m for help): nPartition type:   p   primary (0 primary, 0 extended, 4 free)   e   extendedSelect (default p): pPartition number (1-4, default 1): 1First sector (2048-41943039, default 2048): Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2GPartition 1 of type Linux and of size 2 GiB is setCommand (m for help): pDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0xf15825cf   Device Boot      Start         End      Blocks   Id  System/dev/sdb1            2048     4196351     2097152   83  LinuxCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[root@localhost /]# ls /dev/sdb1 /dev/sdb1[root@localhost /]# mkfs.ext2 /dev/sdb1mke2fs 1.42.9 (28-Dec-2013)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks131072 inodes, 524288 blocks26214 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=53687091216 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks:     32768, 98304, 163840, 229376, 294912Allocating group tables: done                            Writing inode tables: done                            Writing superblocks and filesystem accounting information: done [root@localhost /]# mount /dev/sdb1 /bp[root@localhost /]# dfFilesystem     1K-blocks    Used Available Use% Mounted on/dev/sda3       18555904 5090696  13465208  28% /devtmpfs          485292       0    485292   0% /devtmpfs             499968     144    499824   1% /dev/shmtmpfs             499968    7224    492744   2% /runtmpfs             499968       0    499968   0% /sys/fs/cgroup/dev/sda1         303780  154756    149024  51% /boottmpfs              99996       8     99988   1% /run/user/0/dev/sdb1        2064208    3072   1956280   1% /bp[root@localhost /]# df -T Filesystem     Type     1K-blocks    Used Available Use% Mounted on/dev/sda3      xfs       18555904 5090676  13465228  28% /devtmpfs       devtmpfs    485292       0    485292   0% /devtmpfs          tmpfs       499968     144    499824   1% /dev/shmtmpfs          tmpfs       499968    7224    492744   2% /runtmpfs          tmpfs       499968       0    499968   0% /sys/fs/cgroup/dev/sda1      xfs         303780  154756    149024  51% /boottmpfs          tmpfs        99996       8     99988   1% /run/user/0/dev/sdb1      ext2       2064208    3072   1956280   1% /bp[root@localhost /]# 
原创粉丝点击