sudo mount -t ext4 -o loop失败的另一方法

命令:sudo mount -t ext4 -o loop rootfs.img ./rootfs
[sudo] password for hw: 
mount:文件系统类型错误、选项错误、/dev/loop2 有坏超级块,
       缺少代码页或助手程序,或其他错误
       有些情况下在 syslog 中可以找到一些有用信息- 请尝试
       dmesg | tail  这样的命令看看。

需要转换:ext4转为yaffs2格式

使用./simg2img.py rootfs.img rootfs_simg2img.img

挂载:sudo mount rootfs_simg2img.img rootfs

#!/usr/bin/env python
#encoding:utf8
#===============================================================================
#
#      FILE:  simg2img.py
# 
#      USAGE:  ./simg2img.py rootfs.img rootfs_simg2img.img
# 
#      DESCRIPTION:  
# 
#      AUTHOR: wh 
#      COMPANY: wh
#      CREATED: 2020年01月10日
#      REVISION:  ---
#===============================================================================
#!/usr/bin/python
import sys

#encoding=utf-8
__author__ = 'addy.ke@rock-chips.com'

import traceback
import os
import sys
import threading
import time
import math
import getopt
import struct

targeImg = 'rootfs_simg2img.img '
if( len(sys.argv) > 2):
    targeImg =sys.argv[2]

print 'default path is:'#(targeImg)
print(targeImg)

if( len(sys.argv) > 3):
    print 'parameter error. eg. ./simg2img.py rootfs.img rootfs_simg2img.img'
    sys.exit()

class ext4_file_header:
    def __init__(self, buf):
        self.magic, \
                self.major, \
                self.minor, \
                self.file_header_size, \
                self.chunk_header_size, \
                self.block_size, \
                self.total_blocks, \
                self.total_chunks, \
                self.crc32, \
                = struct.unpack('<IHHHHIIII', buf)

class ext4_chunk_header:
    def __init__(self, buf):
        self.type,\
                self.reserved,\
                self.chunk_size,\
                self.total_size,\
                = struct.unpack('<HHII', buf)

if len(sys.argv) > 1:
    filename = sys.argv[1]
else:
    print "No file is designated!!"
    sys.exit(1)
ifd = open(filename, "rb")

buf=""

# get filelen
ifd.seek(0, 2)
file_len = ifd.tell()
print file_len
ifd.seek(0, 0)

buf = ifd.read(28)
#print repr(buf)
file_header = ext4_file_header(buf)

EXT4_FILE_HEADER_MAGIC = 0xED26FF3A
EXT4_CHUNK_HEADER_SIZE = 12

if file_header.magic != EXT4_FILE_HEADER_MAGIC:
    print "Not a compressed ext4 file!!"
    sys.exit(1)

print "file_header chunks:%X"%(file_header.total_chunks)

total_chunks = file_header.total_chunks
print("total chunk = %d "%(total_chunks))

ofd = open(targeImg, "wb")

sector_base = 82528
output_len = 0

while total_chunks > 0:
    buf = ifd.read(EXT4_CHUNK_HEADER_SIZE)
    chunk_header = ext4_chunk_header(buf)
    sector_size = (chunk_header.chunk_size * file_header.block_size) >> 9;
    #print "ct:%X, cs:%X, ts:%X, ss:%X"%(chunk_header.type, chunk_header.chunk_size, chunk_header.total_size, sector_size)

    data = ""
    if chunk_header.type == 0xCAC1:  # raw type 
        data = ifd.read(chunk_header.total_size - EXT4_CHUNK_HEADER_SIZE)
        if len(data) != (sector_size << 9):
            print("len data:%d, sector_size:%d"%(len(data), (sector_size << 9)))
            sys.exit(1)
        else:
            print ("len data:%d, sector_size:%d"%(len(data), sector_size << 9))
            ofd.write(data)
            output_len += len(data)
            print("raw_chunk ")
            print("write raw data in %d size %d \n"%(sector_base, sector_size))
            print("output len:%x"%(output_len))

            sector_base += sector_size
    else:
        if chunk_header.type == 0xCAC2:  # TYPE_FILL
            data = '\0' * (sector_size << 9);
            ofd.write(data) 
            output_len += len(data)
            print("fill_chunk \n")
            print("chunk_size:%x"%(chunk_header.chunk_size))
            print("output len:%x"%(output_len))
            sector_base += sector_size
        else:
            if chunk_header.type == 0xCAC3:  # TYPE_DONT_CARE
                print "none chunk at chunk:%d"%(file_header.total_chunks - total_chunks)
                print("data_size:0x%x, chunk_size:%d, block_size:%d"%(sector_size << 9, chunk_header.chunk_size, file_header.block_size))
                data = '\0' * (sector_size << 9);
                ofd.write(data) 
                output_len += len(data)
                sector_base += sector_size
            else:
                data = '\0' * (sector_size << 9);
                ofd.write(data)
                print "unknown type!!"
                output_len += len(data)
                print("output len:%x"%(output_len))
                sector_base += sector_size

    total_chunks -= 1 
    print("remain chunks = %d "%(total_chunks));

print "write done"

ifd.close()
ofd.close()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值