在Alpine Linux上进行磁盘管理,你可以执行多种操作,包括查看磁盘使用情况、挂载和卸载文件系统、分区和格式化磁盘等。这些操作和其他发行版本的操作基本一致,差别不大。以下是一些基本的磁盘管理任务和相应的命令:
查看磁盘使用情况:
使用df
命令可以查看磁盘的挂载情况和使用空间:
localhost:~# df -h
Filesystem Size Used Available Use% Mounted on
devtmpfs 10.0M 0 10.0M 0% /dev
shm 484.5M 0 484.5M 0% /dev/shm
/dev/vg0/lv_root 36.9G 1.6G 33.4G 5% /
tmpfs 193.8M 208.0K 193.6M 0% /run
/dev/sda1 271.1M 35.5M 216.6M 14% /boot
tmpfs 484.5M 0 484.5M 0% /tmp
/dev/vg0/lv_root 36.9G 1.6G 33.4G 5% /var/lib/docker
这将以人类可读的格式(例如MB、GB)显示磁盘空间的使用情况 。
查看所有分区信息
你可以通过fdisk -l
查看所有分区信息 。
localhost:~# fdisk -l
Disk /dev/sda: 40 GB, 42949672960 bytes, 83886080 sectors
5221 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes
Device Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type
/dev/sda1 * 0,32,33 38,94,56 2048 616447 614400 300M 83 Linux
/dev/sda2 38,94,57 1023,254,63 616448 83886079 83269632 39.7G 8e Linux LVM
Disk /dev/dm-0: 1940 MB, 2034237440 bytes, 3973120 sectors
247 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes
Disk /dev/dm-0 doesn't contain a valid partition table
Disk /dev/dm-1: 38 GB, 40596668416 bytes, 79290368 sectors
4935 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes
Disk /dev/dm-1 doesn't contain a valid partition table
Disk /dev/sdb: 8192 MB, 8589934592 bytes, 16777216 sectors
1044 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes
Disk /dev/sdb doesn't contain a valid partition table
分区和格式化:
使用fdisk
命令进行分区:
localhost:~# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI, OSF or GPT disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that the previous content
won't be recoverable.
The number of cylinders for this disk is set to 1044.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): n
Partition type
p primary partition (1-4)
e extended
p
Partition number (1-4): 1
First sector (63-16777215, default 63):
Using default value 63
Last sector or +size{,K,M,G,T} (63-16777215, default 16777215):
Using default value 16777215
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table
然后使用mke2fs
命令格式化分区为ext4文件系统:
localhost:~# mke2fs -t ext4 /dev/sdb1
mke2fs 1.47.0 (5-Feb-2023)
/dev/sdb1 contains a ext4 file system
created on Mon Oct 21 21:29:16 2024
Proceed anyway? (y,N) y
Discarding device blocks: done
Creating filesystem with 2097144 4k blocks and 524288 inodes
Filesystem UUID: 1758cb0c-731e-450a-bc04-3f1204d05cb7
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
或者直接使用mkfs.ext4
格式化为ext4文件格式
localhost:~# mkfs.ext4 /dev/sdb1
mke2fs 1.47.0 (5-Feb-2023)
Discarding device blocks: done
Creating filesystem with 2097144 4k blocks and 524288 inodes
Filesystem UUID: b1efc461-42a5-4f1e-9222-e28509e171e2
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
挂载文件系统:
使用mount
命令将设备挂载到文件系统:
localhost:~# mount /dev/sdb1 /mnt
其中/dev/sdb1
是设备文件,/mnt
是挂载点 。
挂载完成后的效果:
localhost:~# df -h
Filesystem Size Used Available Use% Mounted on
devtmpfs 10.0M 0 10.0M 0% /dev
shm 484.5M 0 484.5M 0% /dev/shm
/dev/vg0/lv_root 36.9G 1.6G 33.4G 5% /
tmpfs 193.8M 208.0K 193.6M 0% /run
/dev/sda1 271.1M 35.5M 216.6M 14% /boot
tmpfs 484.5M 0 484.5M 0% /tmp
/dev/vg0/lv_root 36.9G 1.6G 33.4G 5% /var/lib/docker
/dev/sdb1 7.8G 2.0M 7.4G 0% /mnt
卸载文件系统:
使用umount
命令卸载已挂载的文件系统:
localhost:~# umount /dev/sdb1
卸载后的结果:
localhost:~# df -h
Filesystem Size Used Available Use% Mounted on
devtmpfs 10.0M 0 10.0M 0% /dev
shm 484.5M 0 484.5M 0% /dev/shm
/dev/vg0/lv_root 36.9G 1.6G 33.4G 5% /
tmpfs 193.8M 208.0K 193.6M 0% /run
/dev/sda1 271.1M 35.5M 216.6M 14% /boot
tmpfs 484.5M 0 484.5M 0% /tmp
/dev/vg0/lv_root 36.9G 1.6G 33.4G 5% /var/lib/docker
查看块设备的UUID:
使用blkid
命令列出所有可用的块设备及其挂载点:
localhost:~# blkid
/dev/sda1: UUID="24edfcfd-38ca-4697-8dee-c49222ff23e2" BLOCK_SIZE="1024" TYPE="ext4" PARTUUID="c22a01bb-01"
/dev/sda2: UUID="fVUdUn-9C4B-Em8m-NZOy-kcb1-Lqvn-z3AqKY" TYPE="LVM2_member" PARTUUID="c22a01bb-02"
/dev/mapper/vg0-lv_root: UUID="9be48d84-8113-4423-b7e2-7cd668a6fe56" BLOCK_SIZE="4096" TYPE="ext4"
/dev/mapper/vg0-lv_swap: UUID="a0d465d3-c3fe-4817-bc52-a4c2d4def262" TYPE="swap"
/dev/sdb1: UUID="1758cb0c-731e-450a-bc04-3f1204d05cb7" BLOCK_SIZE="4096" TYPE="ext4"
永久挂载:
要使分区在重启后自动挂载,需要编辑/etc/fstab
文件并添加相应的挂载信息:
localhost:~# echo "UUID=1758cb0c-731e-450a-bc04-3f1204d05cb7 /mnt ext4 defaults 0 0" >> /etc/fstab
然后可以使用mount -a
命令应用更改
localhost:~# mount -a
localhost:~# df -h
Filesystem Size Used Available Use% Mounted on
devtmpfs 10.0M 0 10.0M 0% /dev
shm 484.5M 0 484.5M 0% /dev/shm
/dev/vg0/lv_root 36.9G 1.6G 33.4G 5% /
tmpfs 193.8M 208.0K 193.6M 0% /run
/dev/sda1 271.1M 35.5M 216.6M 14% /boot
tmpfs 484.5M 0 484.5M 0% /tmp
/dev/vg0/lv_root 36.9G 1.6G 33.4G 5% /var/lib/docker
/dev/sdb1 7.8G 2.0M 7.4G 0% /mnt
磁盘I/O统计:
使用iostat
命令监视系统输入/输出设备负载:
localhost:~# iostat
Linux 6.6.52-0-lts (localhost) 10/21/24 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.52 0.00 0.96 0.12 0.00 98.40
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
sda 1.38 132.23 2.51 320990 6082
dm-0 0.04 2.23 0.00 5416 0
dm-1 1.59 122.85 2.50 298210 6064
sdb 0.85 9.28 226.48 22534 549796
sdb1 0.80 7.22 226.48 17526 549788
这些是Alpine Linux中一些基本的磁盘管理操作。对于更高级的磁盘管理任务,如LVM、RAID或加密,这里就详细展开了