-1

I am working on a raspberry pi project and I am having trouble duplicating the OS image. What I am trying to accomplish is creating an exact bootable copy of the working OS with its files and settings intact. I am running Raspian Lite and I used this video tutorial to create an image of the working OS.

The program created an .img, and it could boot. However, it failed to find the root user directory which made it boot into safe mode. Anyone successful in imaging working copies of Raspbian Lite that could help me out? It would be greatly appreciated.

Two

0

I have encountered the emergency mode once for a simple mistake in /etc/fstab (default instead of defaults). In emergency mode there is a suggestion to run journalctl -xb to view system logs. In my case after running that command and on the last page of many the error was somthing like ... unknown option 'default' in fstab ....

I can show you how to make a bootable backup of Raspbian Lite on the Pi itself if you wish. Requirements would be SD card with USB reader, USB port on the Pi, and the command rsync which is not included in Raspbian Lite (sudo apt-get install rsync).

With the SD card on the Pi, run the command lsblk to verify that only the SD card that you want the clone/backup to be is sda. The following script will check that rsync is installed, use sudo where required, unmount /dev/sda?*, format /dev/sda into two partition, one for boot (FAT32 66M) and the other for root (Linux the reminder of the card). It will make filesystems in the partitions for boot and root. Directories /mnt/d1 and /mnt/d2 will be made to mount the filesystems (/media/pi directories will create errors). The rsync command will copy the /boot and / (root) filesystems to the SD card, known as /dev/sda1 and /dev/sda2, with flags (axvHAXW) that will not cross filesystem boundary, system generated (/dev, /proc, ...) and mounted filesystems (/mnt/d1, /mnt/d2) will not be copied (very good thing). If you wish to copy NOOBS or other filesystems, see How can I backup my whole SD to an img file usable with Etcher to restore later? for details. The last thing is getting root partition (/dev/mmcblk0p2) set correctly in /boot/cmdline.txt and in /etc/fstab, with /boot (/dev/mmcblk0p1).

#!/bin/bash
# Script for Raspbian-Lite to clone/backup boot and root filesystems.

ls /dev/sda > /dev/null || exit 2
which rsync > /dev/null
if [ $? -gt 0 ]; then
    echo -n "'rsync' command not installed, install with "
    echo "'sudo apt-get install rsync'."
    exit 1
fi

echo "WARNING: Everything on SD Card SDA will be DELETED!"
echo -n "Do you wish to continue? (Y/n) "
read answer
[ "$answer" == "Y" ] || exit 1

sudo umount /dev/sda?* 2> /dev/null
echo -e "o\nn\n\n\n\n+66M\nt\nc\nn\n\n\n\n\nw" | sudo fdisk /dev/sda

sudo mkfs.vfat -n boot /dev/sda1
sudo mkfs.ext4 -L root /dev/sda2

sudo mkdir /mnt/d1 /mnt/d2 2> /dev/null
# Backup part after /dev/sda has been clone
sudo mount /dev/sda1 /mnt/d1
sudo mount /dev/sda2 /mnt/d2

# With 'rsync' always use trailing '/' on directories.
sudo rsync -axvHAXW --delete /boot/ /mnt/d1/
sudo rsync -axvHAXW --delete / /mnt/d2/

awk 'BEGIN{ boot = "/dev/mmcblk0p1"; root = "/dev/mmcblk0p2" }{
    if ( $1 == "proc" || $1 == "#" ) print $0
    if ( $2 == "/boot" ) print boot "  " substr($0, index($0, "/boot"))
    if ( $2 == "/" ) print root "  " substr($0, index($0, "/ "))
    }' /mnt/d2/etc/fstab > /tmp/new_fstab
sudo cp /tmp/new_fstab /mnt/d2/etc/fstab
rm /tmp/new_fstab

awk '{
    for (i=1; i<=NF; i++)
        if ( index($i, "root=")) $i = "root=/dev/mmcblk0p2"    
    print $0
    }' /mnt/d1/cmdline.txt > /tmp/new_cmdline
sudo cp /tmp/new_cmdline /mnt/d1/cmdline.txt
rm /tmp/new_cmdline
sudo umount /mnt/d[12]

Done, now you can power down, remove power and swap the sda card in place of the system sd card and power up. Your cloned system should be up and running as before.

To update your backup on the cloned sda card, use the bottom part of the script starting at the "# Backup part ...". If your orginal sd card has root as /dev/mmcblk0p2 as boot as /dev/mmcblk0p1, the awk, sudo cp, and rm commands are not needed.

Note: If downloading this script, you may need to run tr -d '\r' < download_file > file_without_cr to convert from Dos crlf to Unix lf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.