基于Ubuntu 20.04环境测试验证。
一、磁盘空间查看
1.1 磁盘分区管理 - fdisk
fdisk通常被用来查看系统磁盘的分区信息,同时fdisk也支持对磁盘空间进行分区(下一章节介绍)。
命令参数:
Usage:
fdisk [options] <disk> change partition table
fdisk [options] -l [<disk>] list partition table(s)
Display or manipulate a disk partition table.
Options:
-b, --sector-size <size> physical and logical sector size
-B, --protect-boot don't erase bootbits when creating a new label
-c, --compatibility[=<mode>] mode is 'dos' or 'nondos' (default)
-L, --color[=<when>] colorize output (auto, always or never)
colors are enabled by default
-l, --list display partitions and exit
-o, --output <list> output columns
-t, --type <type> recognize specified partition table type only
-u, --units[=<unit>] display units: 'cylinders' or 'sectors' (default)
-s, --getsz display device size in 512-byte sectors [DEPRECATED]
--bytes print SIZE in bytes rather than in human readable format
-w, --wipe <mode> wipe signatures (auto, always or never)
-W, --wipe-partitions <mode> wipe signatures from new partitions (auto, always or never)
-C, --cylinders <number> specify the number of cylinders
-H, --heads <number> specify the number of heads
-S, --sectors <number> specify the number of sectors per track
-h, --help display this help
-V, --version display version
Available output columns:
gpt: Device Start End Sectors Size Type Type-UUID Attrs Name UUID
dos: Device Start End Sectors Cylinders Size Type Id Attrs Boot End-C/H/S Start-C/H/S
bsd: Slice Start End Sectors Cylinders Size Type Bsize Cpg Fsize
sgi: Device Start End Sectors Cylinders Size Type Id Attrs
sun: Device Start End Sectors Cylinders Size Type Id Flags
For more details see fdisk(8).
命令示例:
# 查看所有磁盘分区信息
fdisk -l
# 查看指定磁盘分区信息
fdisk -l /dev/sda
输出示例:
root@ubuntu:~$ fdisk -l /dev/sda
Disk /dev/sda: 238.49 GiB, 256060514304 bytes, 500118192 sectors
Disk model: YRUIS SSDSCE880/
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 3F6BASDF-336F-48DF-B248-A6SD9EEE4F98
Device Start End Sectors Size Type
/dev/sda1 2048 1050623 1048576 512M EFI System
/dev/sda2 1050624 500117503 499066880 238G Linux filesystem
1.2 文件系统磁盘占用 - df
df命令可以被用来显示linux系统上的文件系统的磁盘使用情况。
命令参数:
Usage: df [OPTION]... [FILE]...
Show information about the file system on which each FILE resides,
or all file systems by default.
Mandatory arguments to long options are mandatory for short options too.
-a, --all include pseudo, duplicate, inaccessible file systems
-B, --block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'-BM' prints sizes in units of 1,048,576 bytes;
see SIZE format below
-h, --human-readable print sizes in powers of 1024 (e.g., 1023M)
-H, --si print sizes in powers of 1000 (e.g., 1.1G)
-i, --inodes list inode information instead of block usage
-k like --block-size=1K
-l, --local limit listing to local file systems
--no-sync do not invoke sync before getting usage info (default)
--output[=FIELD_LIST] use the output format defined by FIELD_LIST,
or print all fields if FIELD_LIST is omitted.
-P, --portability use the POSIX output format
--sync invoke sync before getting usage info
--total elide all entries insignificant to available space,
and produce a grand total
-t, --type=TYPE limit listing to file systems of type TYPE
-T, --print-type print file system type
-x, --exclude-type=TYPE limit listing to file systems not of type TYPE
-v (ignored)
--help display this help and exit
--version output version information and exit
Display values are in units of the first available SIZE from --block-size,
and the DF_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).
The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
FIELD_LIST is a comma-separated list of columns to be included. Valid
field names are: 'source', 'fstype', 'itotal', 'iused', 'iavail', 'ipcent',
'size', 'used', 'avail', 'pcent', 'file' and 'target' (see info page).
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/df>
or available locally via: info '(coreutils) df invocation'
命令示例:
# 以人性化方式显示各文件系统容量
df -h
输出示例:
root@ubuntu:~$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.8G 0 3.8G 0% /dev
tmpfs 774M 3.6M 770M 1% /run
/dev/sda2 234G 73G 150G 33% /
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/loop0 128K 128K 0 100% /snap/bare/5
/dev/loop1 64M 64M 0 100% /snap/core20/2264
/dev/loop3 75M 75M 0 100% /snap/core22/1380
/dev/loop6 506M 506M 0 100% /snap/gnome-42-2204/176
/dev/loop7 347M 347M 0 100% /snap/gnome-3-38-2004/115
/dev/loop5 64M 64M 0 100% /snap/core20/2318
/dev/loop4 350M 350M 0 100% /snap/gnome-3-38-2004/143
/dev/loop8 92M 92M 0 100% /snap/gtk-common-themes/1535
/dev/loop12 13M 13M 0 100% /snap/snap-store/1113
/dev/loop13 39M 39M 0 100% /snap/snapd/21759
/dev/loop11 13M 13M 0 100% /snap/snap-store/959
/dev/loop9 497M 497M 0 100% /snap/gnome-42-2204/141
/dev/loop10 39M 39M 0 100% /snap/snapd/21465
/dev/sda1 511M 6.1M 505M 2% /boot/efi
tmpfs 774M 16K 774M 1% /run/user/125
tmpfs 774M 44K 774M 1% /run/user/1000
/dev/loop14 75M 75M 0 100% /snap/core22/1439
1.3 文件/目录空间使用 - du
命令参数:
Usage: du [OPTION]... [FILE]...
or: du [OPTION]... --files0-from=F
Summarize disk usage of the set of FILEs, recursively for directories.
Mandatory arguments to long options are mandatory for short options too.
-0, --null end each output line with NUL, not newline
-a, --all write counts for all files, not just directories
--apparent-size print apparent sizes, rather than disk usage; although
the apparent size is usually smaller, it may be
larger due to holes in ('sparse') files, internal
fragmentation, indirect blocks, and the like
-B, --block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'-BM' prints sizes in units of 1,048,576 bytes;
see SIZE format below
-b, --bytes equivalent to '--apparent-size --block-size=1'
-c, --total produce a grand total
-D, --dereference-args dereference only symlinks that are listed on the
command line
-d, --max-depth=N print the total for a directory (or file, with --all)
only if it is N or fewer levels below the command
line argument; --max-depth=0 is the same as
--summarize
--files0-from=F summarize disk usage of the
NUL-terminated file names specified in file F;
if F is -, then read names from standard input
-H equivalent to --dereference-args (-D)
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
--inodes list inode usage information instead of block usage
-k like --block-size=1K
-L, --dereference dereference all symbolic links
-l, --count-links count sizes many times if hard linked
-m like --block-size=1M
-P, --no-dereference don't follow any symbolic links (this is the default)
-S, --separate-dirs for directories do not include size of subdirectories
--si like -h, but use powers of 1000 not 1024
-s, --summarize display only a total for each argument
-t, --threshold=SIZE exclude entries smaller than SIZE if positive,
or entries greater than SIZE if negative
--time show time of the last modification of any file in the
directory, or any of its subdirectories
--time=WORD show time as WORD instead of modification time:
atime, access, use, ctime or status
--time-style=STYLE show times using STYLE, which can be:
full-iso, long-iso, iso, or +FORMAT;
FORMAT is interpreted like in 'date'
-X, --exclude-from=FILE exclude files that match any pattern in FILE
--exclude=PATTERN exclude files that match PATTERN
-x, --one-file-system skip directories on different file systems
--help display this help and exit
--version output version information and exit
Display values are in units of the first available SIZE from --block-size,
and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).
The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/du>
or available locally via: info '(coreutils) du invocation'
命令示例:
# 以人性化方式显示当前目录所有文件/目录空间占用
df -h
# 以人性化方式显示特定目录所有文件/目录空间占用
df -h <path>
# 指定目录深度显示所有文件/目录空间占用
df -h -d <n> <path>
输出示例:
root@ubuntu:~$ du -h -d 1 /usr
54M /usr/sbin
17M /usr/lib32
4.0K /usr/libx32
28M /usr/local
4.0K /usr/games
2.3G /usr/share
13M /usr/libexec
235M /usr/include
4.0K /usr/lib64
725M /usr/bin
50M /usr/arm-linux-gnueabihf
12K /usr/arm-linux-gnueabi
428M /usr/src
5.4G /usr/lib
9.2G /usr
二、Linux磁盘管理
2.1 fdisk
前文提到fdisk可以查看磁盘的分区信息,但fdisk更强大的功能在于支持磁盘分区。
执行“fdisk [options] <disk>”后,会弹出shell命令行交付界面:
root@ubuntu:~$ fdisk /dev/sdb
Welcome to fdisk (util-linux 2.34).
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 (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
分区示例:将一个磁盘划分为256M的FAT32格式的boot分区和ext4格式的根目录
jyl@dev:~$ sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
# 查看当前磁盘分区信息
Command (m for help): p
Disk /dev/sdb: 7.22 GiB, 7746879488 bytes, 15130624 sectors
Disk model: Storage Device
Geometry: 239 heads, 62 sectors/track, 1021 cylinders
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xb5805fa2
# 创建一个新的分区表
Command (m for help): o
Created a new DOS disklabel with disk identifier 0x5973a5ec.
# 创建一个新分区
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
# 分区类型为primary
Select (default p): p
# 接回车,number使用默认值
Partition number (1-4, default 1):
# 直接回车,使用默认扇区位置
First sector (62-15130623, default 62):
# +256M,分配256M空间给第一个分区
Last sector, +/-sectors or +/-size{K,M,G,T,P} (62-15130623, default 15130623): +256M
# 创建完成
Created a new partition 1 of type 'Linux' and of size 256 MiB.
# 创建另一个分区
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
# 分区类型为primary
Select (default p): p
# 直接回车,number使用默认值
Partition number (2-4, default 2):
# 直接回车,使用默认扇区位置
First sector (524351-15130623, default 524351):
# 直接回车,使用默认值,分配全部空间
Last sector, +/-sectors or +/-size{K,M,G,T,P} (524351-15130623, default 15130623):
# 创建完成
Created a new partition 2 of type 'Linux' and of size 7 GiB.
# 查看新的磁盘分区信息
Command (m for help): p
Disk /dev/sdb: 7.22 GiB, 7746879488 bytes, 15130624 sectors
Disk model: Storage Device
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc0ccc46e
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 526335 524288 256M 83 Linux
/dev/sdb2 526336 15130623 14604288 7G 83 Linux
Filesystem/RAID signature on partition 1 will be wiped.
# 保存table并退出
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
注:尝试对磁盘进行分区前建议储备些文件系统基础知识,避免损坏盘区丢失数据。
2.2 gparted
命令安装:
sudo apt install gparted
软件启动:
gparted
这是一个带UI的工具,使用方法可以自行摸索或参考其他网络资料,很容易上手。
三、制作image文件
嵌入式linux开发中,软件组件分为boot、kernel、rootfs三部分,当我们部署到目标板时需要把这三个组件分别放置到存储器内相应位置。为了方便使用,通常我们会将这三个组件打包为image(系统镜像),在硬件生产时只需要把image烧录在目标板的EMMC或SD卡,就可以完成系统软件的部署。
3.1 创建空白image文件
dd:用指定大小的块拷贝一个文件,并在拷贝的同时支持进行指定的转换
sync:将内存中的数据同步至硬盘
sudo dd if=/dev/zero of="demo.img" bs=1M count=4096
sync
3.2 image文件分区
这里我们使用fdisk命令对demo.img进行分区,便于脚本化执行我们使用"(echo <command>)"代替fdisk的命令行shell交互。
(
echo o
echo n
echo p
echo 1
echo
echo +256M
echo n
echo p
echo
echo
echo
echo t
echo 1
echo c
echo a
echo 1
echo w
) | sudo fdisk demo.img
sync
执行成功后:
root@ubuntu:~$ fdisk -l demo.img
Disk demo.img: 4 GiB, 4294967296 bytes, 8388608 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
Disklabel type: dos
Disk identifier: 0xc1af88bb
Device Boot Start End Sectors Size Id Type
demo.img1 * 2048 526335 524288 256M c W95 FAT32 (LBA)
demo.img2 526336 8388607 7862272 3.8G 83 Linux
3.3 挂载为loop设备
kpartx: 用来挂载带有分区表的映像文件,挂载为loop设备
awk: 文本分析工具,配合正则表达式用来获取loop设备的编号
LOOP_NUM=$(sudo kpartx -av demo.img | awk 'NR==1{ sub(/p[0-9]$/, "", $3); print $3 }')
# 使用shell脚本执行该命令时,建议sleep一段时间后再对/dev/loop设备进行操作
sleep 1
3.4 向分区中写入数据
分区创建好了,下面就可以向分区中分别写入数据了:使用dd复制二进制文件;mount&cp复制rootfs。
# 复制boot image
sudo dd if=${OUTDIR}/boot.img of=/dev/mapper/${LOOP_NUM}p1 bs=1M
# 格式化分区
(echo y) | sudo mkfs -t ext4 -I 512 -L rootfs /dev/mapper/${LOOP_NUM}p2
# 挂载到文件系统
mkdir ~/${LOOP_NUM}p2 && sudo mount /dev/mapper/${LOOP_NUM}p2 ~/${LOOP_NUM}p2
# 从文件系统卸载
sudo umount ~/${LOOP_NUM}p2
# 其他数据操作
# ...
sync
3.5 完成,卸载设备
sudo kpartx -d "/dev/${LOOP_NUM}"