Dev/Embedded/Rootfs: Difference between revisions

From Embeded Linux (and more) Wiki by Nathael
< Dev‎ | Embedded
Jump to navigation Jump to search
No edit summary
No edit summary
Line 5: Line 5:
* [[Dev/Embedded/Rootfs/Devuan/Config|Devuan configuration]]
* [[Dev/Embedded/Rootfs/Devuan/Config|Devuan configuration]]


== Rootfs Backup (or Copy) ==
== Rootfs (and more) Backup (or copy part 1) ==
First of all, avoid any site which tells you to use ''dd'' to backup or clone your system, as it's author either does not want to take some time to explain how things work, or simply doesn't know.<br />
First of all, avoid any site which tells you to use ''dd'' to backup or clone your system, as it's author either does not want to take some time to explain how things work, or simply doesn't know.<br />
When using ''dd'' :
When using ''dd'' :
Line 14: Line 14:
* and you will also copy any junk found on the SD card, as the empty space is not necessarily filled with zeroes, making a bigger image than required, even when compressed !!
* and you will also copy any junk found on the SD card, as the empty space is not necessarily filled with zeroes, making a bigger image than required, even when compressed !!


The right way to do it is to use tar.
The right way to do it is to use '''tar'''.


For each partition you need to backup, you must mount it on an empty directory, possibly with ''bind'' mount option, and then create an archive :
For each partition you need to backup, you must run one of the following tar commands :
* Store the archive on a separate local partition :
tar --one-file-system cjf /somewhere/else/backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2 *
* Combine tar and ssh to directly transfer the archive to your host system :
tar --one-file-system cjf - * | ssh user@devhost "cat > backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2"
Or with compression on the host system :
tar --one-file-system cf - * | ssh user@devhost "bzip2 | cat > backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2"
 
It is then a good idea to create a README file to store with your archive with indications of partitions, sizes, filesystems, labels, bootable flag and so on (The output of sfdisk may be useful). If your system requires more than one archive you can then place them together with the README file in the same directory, and even create an archive of this directory :)
 
If your tar does not have the "--one-file-system" option (busybox one for example) you must mount each partition you need to backup on an empty directory (possibly with ''bind'' mount option), and then create an archive :
  mntpoint=/mnt/tmp
  mntpoint=/mnt/tmp
  mkdir ${mntpoint}
  mkdir ${mntpoint}
  disk=mmcblk0  # Use the right disk name (sda, sdb, ... mmcblk1, ...)
  disk=mmcblk0  # Use the right disk name (sda, sdb, ... mmcblk1, ...)
  partition=p1  # Use the right partition indicator (p1, p2, p3, ... for mmcblk* or simply 1, 2, 3, ... for sd*)
  partition=p1  # Use the right partition indicator (p1, p2, p3, ... for mmcblk* or simply 1, 2, 3, ... for sd*)
  mount -o bind /dev/${disk}${partition} ${mntpoint}
  mount /dev/${disk}${partition} ${mntpoint}
  cd ${mntpoint}
  cd ${mntpoint}
  tar cjf /somewhere/else/backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2 *
  tar cjf /somewhere/else/backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2 *
  cd && umount ${mntpoint}
  cd && umount ${mntpoint}
You can of course chose the backup image name according to your preferences, this is only an example, but the Archive file '''MUST be on another partition'''.<br />
 
Alternatively you can combine tar and ssh to directly transfer the archive to your host system :
You can of course chose the backup image name according to your preferences, this is only an example.
 
An alternative to specifying the device you want to backup (/dev/***), you can perform a "bind mount" and specify the directory (for example the system root directory "/") :
mount -o bind / /mnt/tmp
This will bind only the filesystem mounted on "/", and not any other filesystem mounted for example on /proc, /sys or /dev.
 
Note that by using the ''mount'' solution you can combine different filesystems in a single archive. Let's say that your home directory is on a separate partition, then you can combine both by doing so :
mount -o bind / /mnt/tmp
mount -o bind /home /mnt/tmp/home
cd /mnt/tmp && tar cf - * | ssh user@devhost "bzip2 | cat > backup_full.tar.bz2"
 
== Rootfs restoration (or copy part 2) ==
* Start by partitioning your disk / SD card / whatever storage support. My preferred tool for this is cfdisk, but any other partitioning tool should do the job :)
* Then create the filesystem (mkfs.*** /dev/*** [options])
* Mount the partition somewhere (mount /dev/*** /somewhere)
* And finally decompress your archive (cd /somewhere && tar xf /somewhere/else/archive.tar.bz2)
* Do not forget to umount (cd - && umount /somewhere)

Revision as of 16:28, 16 October 2019

Busybox rootfs

Devuan rootfs

Rootfs (and more) Backup (or copy part 1)

First of all, avoid any site which tells you to use dd to backup or clone your system, as it's author either does not want to take some time to explain how things work, or simply doesn't know.
When using dd :

  • you won't be able to extract the content to explore it on your development system
  • you will not be able to change the partition sizes or layout when creating a copy
  • you will not be able to change the filesystem type
  • even if you compress the result, extracting it will require as much space as your SD card had, using 4GB (or 8, or 16 ?), and no way to extract to a smaller µSD card or you may loose data and get a broken filesystem.
  • and you will also copy any junk found on the SD card, as the empty space is not necessarily filled with zeroes, making a bigger image than required, even when compressed !!

The right way to do it is to use tar.

For each partition you need to backup, you must run one of the following tar commands :

  • Store the archive on a separate local partition :
tar --one-file-system cjf /somewhere/else/backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2 *
  • Combine tar and ssh to directly transfer the archive to your host system :
tar --one-file-system cjf - * | ssh user@devhost "cat > backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2"

Or with compression on the host system :

tar --one-file-system cf - * | ssh user@devhost "bzip2 | cat > backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2"

It is then a good idea to create a README file to store with your archive with indications of partitions, sizes, filesystems, labels, bootable flag and so on (The output of sfdisk may be useful). If your system requires more than one archive you can then place them together with the README file in the same directory, and even create an archive of this directory :)

If your tar does not have the "--one-file-system" option (busybox one for example) you must mount each partition you need to backup on an empty directory (possibly with bind mount option), and then create an archive :

mntpoint=/mnt/tmp
mkdir ${mntpoint}
disk=mmcblk0  # Use the right disk name (sda, sdb, ... mmcblk1, ...)
partition=p1  # Use the right partition indicator (p1, p2, p3, ... for mmcblk* or simply 1, 2, 3, ... for sd*)
mount /dev/${disk}${partition} ${mntpoint}
cd ${mntpoint}
tar cjf /somewhere/else/backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2 *
cd && umount ${mntpoint}

You can of course chose the backup image name according to your preferences, this is only an example.

An alternative to specifying the device you want to backup (/dev/***), you can perform a "bind mount" and specify the directory (for example the system root directory "/") :

mount -o bind / /mnt/tmp

This will bind only the filesystem mounted on "/", and not any other filesystem mounted for example on /proc, /sys or /dev.

Note that by using the mount solution you can combine different filesystems in a single archive. Let's say that your home directory is on a separate partition, then you can combine both by doing so :

mount -o bind / /mnt/tmp
mount -o bind /home /mnt/tmp/home
cd /mnt/tmp && tar cf - * | ssh user@devhost "bzip2 | cat > backup_full.tar.bz2"

Rootfs restoration (or copy part 2)

  • Start by partitioning your disk / SD card / whatever storage support. My preferred tool for this is cfdisk, but any other partitioning tool should do the job :)
  • Then create the filesystem (mkfs.*** /dev/*** [options])
  • Mount the partition somewhere (mount /dev/*** /somewhere)
  • And finally decompress your archive (cd /somewhere && tar xf /somewhere/else/archive.tar.bz2)
  • Do not forget to umount (cd - && umount /somewhere)