我想把我的v3s中的系统做备份,在网上各种寻找,开始了制作img。

1.安装工具

sudo apt-get install dosfstools
sudo apt-get install dump
sudo apt-get install parted
sudo apt-get install kpartx

2.生成新的img文件
先df -h查看下实际使用空间然后在决定生成文件大小

zqh@linux:~/lichee/MyImage$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           787M  9.4M  778M   2% /run
/dev/sda1       451G   21G  408G   5% /
tmpfs           3.9G  184K  3.9G   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs           787M   68K  787M   1% /run/user/1000
/dev/sdb2        15G  371M   14G   3% /media/zqh/rootfs
/dev/sdb1        32M   12M   21M  37% /media/zqh/BOOT

可以看到我一共用了371+32=403M的空间,所以我弄个400M多的大小的img

sudo dd if=/dev/zero of=licheebackup.img bs=1M count=420
sync

这里要写1M,1MiB也就是1024kb,这样大小不会错,如果写kb可能太小就会出错。

还有/dev/zero这个文件的输出全是0,所以我现在只是有个了一个空img文件而已。

3.分割虚拟磁盘

利用parted将刚才生成的虚拟盘分割成两个分区

zqh@linux:~/lichee/MyImage$ sudo fdisk -l
Disk /dev/sdb: 15 GiB, 16106127360 bytes, 31457280 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: 0x78e2e610

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048    67583    65536  32M  6 FAT16
/dev/sdb2       67584 31457279 31389696  15G 83 Linux

先看一下内存卡的扇区分布,然后就开始了

sudo parted licheebackup.img --script -- mklabel msdos #制作一个msdos分区
sudo parted licheebackup.img --script -- mkpart primary fat16 2048s 67583s
sudo parted licheebackup.img --script -- mkpart primary ext4 67584s -1

这里可以查看一下。这里的size指的是MB 我分区的时候是要 32M 这里就是33.6MB。

zqh@linux:~/lichee/MyImage$ sudo parted licheebackup.img
GNU Parted 3.2
Using /home/zqh/lichee/MyImage/licheebackup.img
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model:  (file)
Disk /home/zqh/lichee/MyImage/licheebackup.img: 419MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  34.6MB  33.6MB  primary               lba
 2      34.6MB  418MB   384MB   primary

4.挂载虚拟磁盘并格式化

首先建立虚拟磁盘并分区

sudo losetup /dev/loop0 licheebackup.img
sudo kpartx -av /dev/loop0

格式化虚拟磁盘分区

zqh@linux:~/lichee/MyImage$ sudo mkfs.msdos /dev/mapper/loop0p1
zqh@linux:~/lichee/MyImage$ sudo mkfs.ext4 /dev/mapper/loop0p2
mke2fs 1.42.13 (17-May-2015)
Discarding device blocks: done
Creating filesystem with 374784 1k blocks and 93840 inodes
Filesystem UUID: b4f3a95e-ffe2-417e-843c-f4c40daf9ad5
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729, 204801, 221185

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

5.开始备份

备份uboot

sudo dd if=u-boot-sunxi-with-spl.bin of=/dev/loop0 bs=1024 seek=8
sync

因为我是在我的电脑上复制内存卡中的内容,所以我还要挂载我的内存卡

当然这里可能会出错。最后我直接把我的dtb 和 zimage拉进去了

sudo mount /dev/mapper/loop0p1 /media/  #挂载 
sudo mount /dev/sdb1 /mnt/  #把sd卡的boot目录也挂载起来
sudo cp -rf /mnt/* /media/     #内容拷贝
sudo umount /media /mnt        #卸载

ext4 linux虚拟磁盘分区用dump & restore 备份

sudo mount -t ext4 /dev/mapper/loop0p2 /mnt/ #挂载虚拟磁盘
sudo dump -0au -f ~/lichee/MyImage/roofts.dump /dev/sdb2 #从插着的sd卡中dump数据
#-0uaf 优先级0  u成功后记录到文件中 a自动写到磁盘末尾  f备份写入文件
cd /mnt/
sudo restore rf ~/lichee/MyImage/roofts.dump  #恢复系统
cd
sudo umount /mnt

6.卸载虚拟磁盘

sudo kpartx -d licheebackup.img

 7.dd烧录系统

sudo umount /dev/sdb
sudo dd if=licheebackup.img of=/dev/sdb bs=1M

接下来就成功了。烧录到sd卡中去。

如果插入一张空卡,接下来

sudo umount /dev/sdb
sudo parted /dev/sdb --script -- mklabel msdos
sudo parted /dev/sdb --script -- mkpart primary fat16 2048s 67583s
sudo parted /dev/sdb --script -- mkpart primary ext4 67584s -1
zqh@linux:~$ sudo parted /dev/sdb  #查看一下
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: Mass Storage Device (scsi)
Disk /dev/sdb: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  34.6MB  33.6MB  primary               lba
 2      34.6MB  16.1GB  16.1GB  primary  ext4

(parted) q
sudo mkfs.msdos /dev/sdb1
sudo mkfs.ext4 /dev/sdb2
sudo dd if=licheebackup.img of=/dev/sdb bs=1M

 我写的打包脚本:

使用时把这些文件放在一个文件夹即可

# ls
build_img.sh            rootfs.dump                       u-boot-sunxi-with-spl.bin
lichee_zero_backup.img  sun8i-v3s-licheepi-zero-dock.dtb  zImage

 

#!/bin/bash

function echo_log()
{
       echo -e "\033[32m ------------------------------------------ \033[0m"
        echo $1
        echo -e "\033[32m ------------------------------------------\n \033[0m"
}

function echo_error()
{
        echo -e "\033[33m ------------------------------------------ \033[0m"
        echo $1
        echo -e "\033[33m ------------------------------------------\n \033[0m"
}

set -e


_PWD=$(pwd)
if [ -s ./sun8i-v3s-licheepi-zero-dock.dtb -a -s ./u-boot-sunxi-with-spl.bin -a -s ./zImage -a -s ./rootfs.d    ump ]
    then
        echo_log "Checking the files ok!"
    else
        echo_error "Can not find boot.scr, zImage, uboot or the modules file!"
fi

echo_log "Calculating the files's total size..."
_SIZE_ROOTFS=$(du -s -BM rootfs.dump | cut -d 'M' -f -1)
_IMG_SIZE=$(($_SIZE_ROOTFS+200))
echo_log "Creating dd img file... size is $_IMG_SIZE"

_NAME="lichee_zero_backup.img"

if [ ! -e ./$_NAME ]
    then 
        dd if=/dev/zero of=./$_NAME bs=1M count=$_IMG_SIZE
fi

if [ $? -ne 0 ]
   then echo_error "getting error in creating dd img!"
   exit
fi

echo_log "creating partitions..."
sudo parted $_NAME --script -- mklabel msdos #制作一个msdos分区
sudo parted $_NAME --script -- mkpart primary fat16 2048s 67583s
sync
sudo parted $_NAME --script -- mkpart primary ext4 67584s -1
sync



echo_log "losetup dev..."
_LOOP_DEV=$(losetup -f)
if [ -z $_LOOP_DEV ]
    then echo_error "can not find a loop device!"
    exit
fi

sudo losetup $_LOOP_DEV ./$_NAME
if [ $? -ne 0 ]
     then echo_error "dd img --> $_LOOP_DEV error!"
    sudo losetup -d $_LOOP_DEV >/dev/null 2>&1 && exit
fi

_MAP=$(sudo kpartx -av $_LOOP_DEV | sed -n '1p;1q' | cut -c 9-13)
sync

_MNT_DEV="/dev/mapper/${_MAP}"
sleep 20
sudo mkfs.msdos ${_MNT_DEV}p1 ||exit
sudo mkfs.ext4 ${_MNT_DEV}p2 ||exit

if [ $? -ne 0 ]
    then echo_error "error in creating partitions"
    sudo kpartx -d $_NAME && exit 
fi

echo_log "writing u-boot-sunxi-with-spl to $_LOOP_DEV"

sudo dd if=./u-boot-sunxi-with-spl.bin of=$_LOOP_DEV bs=1024 seek=8
if [ $? -ne 0 ]
    then echo_error "writing u-boot error!"
    sudo kpartx -d $_NAME && exit
fi

sudo sync

sudo mount ${_MNT_DEV}p1 /mnt  #挂载 
echo_log "copy dtb and zImage files..."
sudo cp ./sun8i-v3s-licheepi-zero-dock.dtb /mnt     #内容拷贝
sudo cp ./zImage /mnt     #内容拷贝
if [ $? -ne 0 ]
    then echo_error "copy files error! "
    sudo losetup -d $_LOOP_DEV >/dev/null 2>&1
     sudo umount ${_MNT_DEV}p1  >/dev/null 2>&1
    exit
fi
sudo sync
echo_log "dtb and zImage install successful!"
sudo umount  /mnt        #卸载
if [ $? -ne 0 ]
    then echo_error "umount fialed!"
    sudo losetup -d $_LOOP_DEV >/dev/null 2>&1
     sudo umount ${_MNT_DEV}p1  >/dev/null 2>&1
    exit
fi


sudo mount  ${_MNT_DEV}p2 /mnt  #挂载 
echo_log "restore rootfs..."
cd /mnt/
sudo restore rf ${_PWD}/rootfs.dump
sudo sync
echo_log "rootfs restore successfull!"
sleep 10
sudo umount /mnt

if [ $? -ne 0 ]
    then echo_error "umount or losetup -d error!!"
    sudo kpartx -dv $_LOOP_DEV
    exit
fi

sudo kpartx -d $_NAME 
sudo losetup -d $_LOOP_DEV

echo_log "${_NAME} has been created successfully!"
cd $_PWD
exit