常用的磁盘操作命令有fdisk, cfdisk, sfdisk, mkfs, parted, partprobe kpartx, 在Linux中挂载一个新磁盘时,常用到如下操作:
1. fdisk
fdisk可以用于查看指定硬盘的分区或对指定硬盘进行分区:
如显示所有分区:
# fdisk -l Disk /dev/vda: 26.8 GB, 26843545600 bytes, 52428800 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0008f170 Device Boot Start End Blocks Id System /dev/vda1 * 2048 52428749 26213351 83 Linux
查看帮助信息:
# fdisk -h Usage: fdisk [options] change partition table fdisk [options] -l list partition table(s) fdisk -s give partition size(s) in blocks Options: -b sector size (512, 1024, 2048 or 4096) -c[=] compatible mode: 'dos' or 'nondos' (default) -h print this help text -u[=] display units: 'cylinders' or 'sectors' (default) -v print program version -C specify the number of cylinders -H specify the number of heads -S specify the number of sectors per track
根据向导对硬盘/dev/sdb进行分区:
# fdisk /dev/sdb Welcome 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. Command (m for help): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition g create a new empty GPT partition table G create an IRIX (SGI) partition table 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)
2. mkfs
用ext4格式格式化硬盘分区/dev/sdb1
# mkfs -t ext4 /dev/sdb1
或
# mkfs.ext4 /dev/sdb1
详细帮助信息:
# mkfs --help Usage: mkfs [options] [-t ] [fs-options] [] Options: -t, --type= filesystem type; when unspecified, ext2 is used fs-options parameters for the real filesystem builder path to the device to be used number of blocks to be used on the device -V, --verbose explain what is being done; specifying -V more than once will cause a dry-run -V, --version display version information and exit; -V as --version must be the only option -h, --help display this help text and exit
3. df
查看磁盘使用情况:
# df -h
4. blkid
显示block devices信息:
# blkid /dev/sda2: UUID="cc648f16-2695-451d-a133-e90b5ea8add3" TYPE="ext3" /dev/sda1: UUID="3cb0a414-123d-4728-aca2-6d18e24e272e" TYPE="ext3" /dev/sda3: UUID="dc6e8463-90c7-419b-8ce0-0f6adf6d870f" TYPE="swap" /dev/sdb: UUID="840d4ffe-00ce-4a2e-83c8-b8b94e6d005b" TYPE="ext4"
5. mount
装载分区到指定目录。如装载ext4分区/dev/sdb1到/data目录:
# mount -t ext4 /dev/sdb1 /data
/data目录要事先存在。
[source]支持多种标识,如:# mount -t ext4 -U 840d4ffe-00ce-4a2e-83c8-b8b94e6d005b /data
或
# mount -t ext4 UUID="840d4ffe-00ce-4a2e-83c8-b8b94e6d005b" /data
mount –all则装载/etc/fstab中的所有配置。
5. /etc/fstab
mount指令装载在重启后会丢失,修改/etc/fstab文件可在系统重启后保持装载:
UUID=cc648f16-2695-451d-a133-e90b5ea8add3 / ext3 defaults 1 1 UUID=3cb0a414-123d-4728-aca2-6d18e24e272e /boot ext3 defaults 1 2 UUID=dc6e8463-90c7-419b-8ce0-0f6adf6d870f swap swap defaults 0 0 UUID=840d4ffe-00ce-4a2e-83c8-b8b94e6d005b /var/opt ext4 defaults 1 0
fstab文件也支持Label和UUID多种分区标识。详见:http://man7.org/linux/man-pages/man5/fstab.5.html