给大家介绍一种方法调试文件系统
本帖最后由 amarant 于 2012-11-14 22:39 编辑先建立一个img
dd if=/dev/zero of=./busybox.img bs=1M count=50
给该镜像分区
fdisk busybox.img
用n命令新建分区,注意建立两个分区,一个分区做/目录,一个分区供自己调查跟踪zh@PC:~/work/linux$ sudo fdisk busybox.img
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x09997232.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-102399, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-102399, default 102399): 20M
Value out of range.
Last sector, +sectors or +size{K,M,G} (2048-102399, default 102399): 20m
Value out of range.
Last sector, +sectors or +size{K,M,G} (2048-102399, default 102399): +20M
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (1-4, default 2):
Using default value 2
First sector (43008-102399, default 43008):
Using default value 43008
Last sector, +sectors or +size{K,M,G} (43008-102399, default 102399):
Using default value 102399
Command (m for help): w
The partition table has been altered!
Syncing disks.
然后用kpartx工具给这两个分区创建文件系统:zh@PC:~/work/linux$ sudo kpartx -av busybox.img
add map loop1p1 (252:0): 0 40960 linear /dev/loop1 2048
add map loop1p2 (252:1): 0 59392 linear /dev/loop1 43008
zh@PC:~/work/linux$ sudo mkfs.ext2 /dev/mapper/loop1p1
mke2fs 1.42 (29-Nov-2011)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
5136 inodes, 20480 blocks
1024 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=20971520
3 block groups
8192 blocks per group, 8192 fragments per group
1712 inodes per group
Superblock backups stored on blocks:
8193
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
zh@PC:~/work/linux$ sudo mkfs.ext2 /dev/mapper/loop1p2
mke2fs 1.42 (29-Nov-2011)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
7424 inodes, 29696 blocks
1484 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=30408704
4 block groups
8192 blocks per group, 8192 fragments per group
1856 inodes per group
Superblock backups stored on blocks:
8193, 24577
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done这一步,看看上面的命令,大家就都懂了。
关于用busybox跑起来内核估计就不用我介绍了。
跑起来内核以后,可以直接通过dd if=/dev/sda2 of=file bs=? count=? seek=? 来读指定快的内容。观察其数据在磁盘中存的状态。
有网友就会有疑问了,那為什麼要用两个分区呢。一个分区不也可以吗?
如果一个分区的话,那么根目录下太多的文件就会造成干扰,让我们无法知道哪些数据是我们留下的,哪些数据是根目录里本来就有的。
先写到这,有时间再补充 loop device 是个好东西。
http://wiki.osdev.org/Loopback_Device 回复 1# amarant
多谢分享:mrgreen:
感谢分享 fallocate disk.img -l 128M
sudo losetup /dev/loop0 disk.img
sudo fdisk /dev/loop0
sudo mkfs -t ext2 /dev/loop0
sudo mount /dev/loop0 /mnt 回复 5# tempname2
呵呵,我主要是不想只有一个分区 今天继续
如果用qemu模拟器跑起来内核
接着昨天的,创建好了分区后,就可以通过
mount /dev/mapper/loop1p2 /mnt
挂载好分区。
busybox代码可以从网上获取,编译也很简单,make menuconfig
配置上交叉工具链,选上静态编译(static)
直接make
成功后,就把当前目录下的_install 全部拷贝到/mnt下
cp -rf _install/* /mnt
然后在/mnt下创建dev文件夹:cd /mnt
mkdir dev
cp /dev/tty dev/ -af
cp /dev/tty /dev/ -af
mkdir etc/init.d
touch etc/init.d/rcS
chmod +x etc/init.d/rcS
busybox基本就这样就可以了
现在再说说内核的配置,内核的配置大家都很熟了吧,make menuconfig 选选选,make就可以了
我这里用的是mips的内核,我用的是malta_defconfig,然后配置成64位的mips
写了一个简单脚本#!/bin/bash
export ARCH=mips
export CROSS_COMPILE=mips64el-unknown-linux-gnu-
if [ "X$1" != "XX" ]; then
make -j8
fi
if [ $? -eq 0 ]; then
${CROSS_COMPILE}objdump -D vmlinux > vmlinux.hex
../qemu-build/mips64el-softmmu/qemu-system-mips64el -M malta -kernel vmlinux -nographic -cpu 5kc -hda busybox.img -append 'root=/dev/hda1'
else
echo "Make error!"
fi
不出意外就可以跑完了 这里是完整的log:zh@PC:~/work/linux$ ../qemu-build/mips64el-softmmu/qemu-system-mips64el -M malta -kernel vmlinux -nographic -cpu 5kc -hda busybox.img -append 'root=/dev/hda1'
Linux version 3.7.0-rc5-00014-g9924a19 (zh@PC) (gcc version 4.6.0 (GCC for Cross-LFS 4.6.0.20110517) ) #19 SMP Thu Nov 15 20:36:09 CST 2012
Config serial console: console=ttyS0,38400n8r
bootconsole enabled
CPU revision is: 00018100 (MIPS 5Kc)
Checking for the multiply/shift bug... no.
Checking for the daddiu bug... no.
Determined physical RAM map:
memory: 0000000000001000 @ 0000000000000000 (reserved)
memory: 00000000000ef000 @ 0000000000001000 (ROM data)
memory: 0000000000534000 @ 00000000000f0000 (reserved)
memory: 00000000079dc000 @ 0000000000624000 (usable)
Wasting 88032 bytes for tracking 1572 unused pages
Zone ranges:
DMA
Normal
Movable zone start for each node
Early memory node ranges
node 0:
Primary instruction cache 8kB, VIPT, 2-way, linesize 32 bytes.
Primary data cache 8kB, 2-way, VIPT, no aliases, linesize 32 bytes
PERCPU: Embedded 10 pages/cpu @98000000011c3000 s9728 r8192 d23040 u40960
Built 1 zonelists in Zone order, mobility grouping on.Total pages: 32320
Kernel command line: root=/dev/hda1 console=ttyS0,38400n8r
PID hash table entries: 512 (order: 0, 4096 bytes)
Dentry cache hash table entries: 16384 (order: 5, 131072 bytes)
Inode-cache hash table entries: 8192 (order: 4, 65536 bytes)
__ex_table already sorted, skipping sort
Cache parity protection disabled
Memory: 122736k/124784k available (3746k kernel code, 2048k reserved, 1117k data, 252k init, 0k highmem)
Hierarchical RCU implementation.
CONFIG_RCU_FANOUT set to non-default value of 32
RCU restricting CPUs from NR_CPUS=2 to nr_cpu_ids=1.
NR_IRQS:256
CPU frequency 200.00 MHz
Console: colour dummy device 80x25
Calibrating delay loop... 944.53 BogoMIPS (lpj=4722688)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 256
Checking for the daddi bug... no.
Brought up 1 CPUs
NET: Registered protocol family 16
bio: create slab <bio-0> at 0
vgaarb: loaded
PCI host bridge to bus 0000:00
pci_bus 0000:00: root bus resource
pci_bus 0000:00: root bus resource
pci_bus 0000:00: No busn resource found for root bus, will use
pci 0000:00:0a.3: no compatible bridge window for
vgaarb: device added: PCI:0000:00:12.0,decodes=io+mem,owns=none,locks=none
pci 0000:00:0a.3: BAR 8: has bogus alignment
pci 0000:00:12.0: BAR 0: assigned
pci 0000:00:0b.0: BAR 6: assigned
pci 0000:00:12.0: BAR 6: assigned
pci 0000:00:12.0: BAR 1: assigned
pci 0000:00:0a.2: BAR 4: assigned
pci 0000:00:0b.0: BAR 0: assigned
pci 0000:00:0b.0: BAR 1: assigned
pci 0000:00:0a.1: BAR 4: assigned
Switching to clocksource MIPS
NET: Registered protocol family 2
TCP established hash table entries: 4096 (order: 4, 65536 bytes)
TCP bind hash table entries: 4096 (order: 4, 65536 bytes)
TCP: Hash tables configured (established 4096 bind 4096)
TCP: reno registered
UDP hash table entries: 256 (order: 1, 8192 bytes)
UDP-Lite hash table entries: 256 (order: 1, 8192 bytes)
NET: Registered protocol family 1
RPC: Registered named UNIX socket transport module.
RPC: Registered udp transport module.
RPC: Registered tcp transport module.
RPC: Registered tcp NFSv4.1 backchannel transport module.
PCI: Enabling device 0000:00:0a.2 (0000 -> 0001)
VFS: Disk quotas dquot_6.5.2
Dquot-cache hash table entries: 512 (order 0, 4096 bytes)
Installing knfsd (copyright (C) 1996 okir@monad.swb.de).
msgmni has been set to 239
Block layer SCSI generic (bsg) driver version 0.4 loaded (major 253)
io scheduler noop registered
io scheduler deadline registered
io scheduler cfq registered (default)
PCI: Enabling device 0000:00:12.0 (0000 -> 0002)
cirrusfb 0000:00:12.0: Cirrus Logic chipset on PCI bus, RAM (4096 kB) at 0x10000000
Console: switching to colour frame buffer device 80x30
Serial: 8250/16550 driver, 4 ports, IRQ sharing disabled
serial8250.0: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
console enabled, bootconsole disabled
console enabled, bootconsole disabled
serial8250.0: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A
serial8250.0: ttyS2 at MMIO 0x1f000900 (irq = 18) is a 16550A
brd: module loaded
Uniform Multi-Platform E-IDE driver
piix 0000:00:0a.1: IDE controller (0x8086:0x7111 rev 0x00)
PCI: Enabling device 0000:00:0a.1 (0000 -> 0001)
piix 0000:00:0a.1: not 100% native mode: will probe irqs later
ide0: BM-DMA at 0x2040-0x2047
ide1: BM-DMA at 0x2048-0x204f
hda: QEMU HARDDISK, ATA DISK drive
hda: UDMA/33 mode selected
hdc: QEMU DVD-ROM, ATAPI CD/DVD-ROM drive
hdc: UDMA/33 mode selected
ide0 at 0x1f0-0x1f7,0x3f6 on irq 14
ide1 at 0x170-0x177,0x376 on irq 15
ide_generic: please use "probe_mask=0x3f" module parameter for probing all legacy ISA IDE ports
ide-gd driver 1.18
hda: max request size: 512KiB
hda: 102400 sectors (52 MB) w/256KiB Cache, CHS=101/255/63
hda: cache flushes supported
hda: hda1 hda2
ide-cd driver 5.00
ide-cd: hdc: ATAPI 4X DVD-ROM drive, 512kB Cache
cdrom: Uniform CD-ROM driver Revision: 3.20
physmap platform flash device: 00400000 at 1e000000
pflash_write: Unimplemented flash cmd sequence (offset 0000000000000000, wcycle 0x0 cmd 0x0 value 0xf000f0)
physmap-flash.0: Found 1 x32 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
Intel/Sharp Extended Query Table at 0x0031
pflash_write: Unimplemented flash cmd sequence (offset 0000000000000000, wcycle 0x0 cmd 0x0 value 0xf0)
Using buffer write method
Creating 3 MTD partitions on "physmap-flash.0":
0x000000000000-0x000000100000 : "YAMON"
0x000000100000-0x0000003e0000 : "User FS"
0x0000003e0000-0x000000400000 : "Board Config"
pcnet32: pcnet32.c:v1.35 21.Apr.2008 tsbogend@alpha.franken.de
PCI: Enabling device 0000:00:0b.0 (0000 -> 0003)
pcnet32: PCnet/PCI II 79C970A at 0x2020, 52:54:00:12:34:56 assigned IRQ 10
pcnet32: eth0: registered as PCnet/PCI II 79C970A
pcnet32: 1 cards_found
mousedev: PS/2 mouse device common for all mice
rtc_cmos rtc_cmos: rtc core: registered rtc_cmos as rtc0
rtc0: alarms up to one day, 242 bytes nvram
TCP: cubic registered
NET: Registered protocol family 17
NET: Registered protocol family 15
rtc_cmos rtc_cmos: setting system clock to 2012-11-15 12:48:40 UTC (1352983720)
VFS: Mounted root (ext2 filesystem) readonly on device 3:1.
Freeing prom memory: 956k freed
Freeing unused kernel memory: 252k freed
Algorithmics/MIPS FPU Emulator v1.5
Please press Enter to activate this console.
/ # ls
bin dev etc init lost+foundsbin
/ #
qemu的代码可以在网上下载,编译的步骤也很简单
./configure
make
也可通过configure选定只编译选定的目标
./configure --target-list=mips64el-softmmu
make
可能会少一些库,少了什么安装上就可以了。 本帖最后由 amarant 于 2012-11-19 21:43 编辑
关于cpu以及架构,如果你没有一种比较熟悉的,建议使用openrisc,这个处理器非常简单。如果了解一个处理器的指令集以及异常处理等一些原理。发生kernel panic的时候,可以比较快定位原因。关于处理器以及环境就介绍这么多了。
页:
[1]
2