sabato 5 luglio 2014

Bootable SD Card Cloning

Bootable SD Card Cloning

(OpenSuse 11.4)

If you don't know what you do, you can seriously damage your system with the commands used in this post. Please DOUBLE CHECK any command you run, and be absolutely sure to understand what it does before hitting "ENTER" or copy/paste as a dumb. If you are in doubt, DON'T DO IT! You have been warned.
You want to clone on an SD card your bootable OpenSuse 11.4 Linux installation and be able to boot from the SD card itself. Or you simply want to clone a bootable SD card or USB flashpen into another one.
  • Solution 1: use clonezilla
  • Solution 2: you want to do it manually for some reason
This article is about solution 2. You should not do this on a running system, so your source SD card or hard disk can not be running while doing that. You must have another system running and use it. All commands used here need root privileges.

You must follow the steps below:
  • Partition the target SD card
  • Format the target SD card
  • Copy the system directories
  • /etc/fstab adapting
  • Install the bootloader
We suppose the target SD card being /dev/sdc and the source being dev/sdg.
My system language setting is Italian, so some of the outputs shown here are in italian.

Partitioning & Formatting

/dev/sdc must be unmounted.

Start fdisk:
  # fdisk /dev/sdc

Delete all the existing partitions (usually one):
  Comando (m per richiamare la guida): d
  Partizione selezionata 1

Create two partitions, one for the system and one for the swap:
  Comando (m per richiamare la guida): n
  Azione comando
      e   estesa
     p   partizione primaria (1-4)
  Numero della partizione (1-4, predefinito 1): 
  Utilizzo del valore predefinito 1
  Primo settore (2048-62333951, predefinito 2048): 
  Utilizzo del valore predefinito 2048
  Last settore, +settori or +size{K,M,G} (2048-62333951, predefinito 62333951): 61285375
  Comando (m per richiamare la guida): n
  Azione comando
      e   estesa
     p   partizione primaria (1-4)
  Numero della partizione (1-4, predefinito 2): 
  Utilizzo del valore predefinito 2
  Primo settore (61285376-62333951, predefinito 61285376): 
  Utilizzo del valore predefinito 61285376
  Last settore, +settori or +size{K,M,G} (61285376-62333951, predefinito 62333951): 
  Utilizzo del valore predefinito 62333951
  Comando (m per richiamare la guida): t
  Numero della partizione (1-4): 2
  Codice esadecimale (digitare L per elencare i codici): 82
  Modificato il tipo di sistema della partizione 2 in 82 (Linux swap / Solaris)
  Comando (m per richiamare la guida): a
  Numero della partizione (1-4): 1
  Comando (m per richiamare la guida): p
  Disk /dev/sdc: 31.9 GB, 31914983424 bytes
  64 testine, 32 settori/tracce, 30436 cilindri, totale 62333952 settori
  Unità = settori di 1 * 512 = 512 byte
  Sector size (logical/physical): 512 bytes / 512 bytes
  I/O size (minimum/optimal): 512 bytes / 512 bytes
  Identificativo disco: 0x00000000
  Dispositivo Boot      Start         End      Blocks   Id  System
  /dev/sdc1   *        2048    61285375    30641664   83  Linux
  /dev/sdc2        61285376    62333951      524288   82  Linux swap / Solaris

Save and exit:
  Comando (m per richiamare la guida): w
  La tabella delle partizioni è stata alterata!
  Chiamata di ioctl() per rileggere la tabella delle partizioni.
  Sincronizzazione dei dischi in corso.

Now format the partitions:
  # mkswap /dev/sdc2
  Setting up swapspace version 1, size = 524284 KiB
  nessuna etichetta, UUID=94e66236-e300-4b7d-83aa-c8baaf48c088
  # mkfs.ext3 /dev/sdc1
  mke2fs 1.41.14 (22-Dec-2010)
  Etichetta del filesystem=
  Tipo SO: Linux
  Dimensione blocco=4096 (log=2)
  Dimensione frammento=4096 (log=2)
  Stride=0 blocks, Stripe width=0 blocks
  1916928 inode, 7660416 blocchi
  383020 blocchi (5.00%) riservati per l'utente root
  Primo blocco dati=0
  Maximum filesystem blocks=0
  234 gruppi di blocchi
  32768 blocchi per gruppo, 32768 frammenti per gruppo
  8192 inode per gruppo
  Backup del superblocco salvati nei blocchi: 
          32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
  Scrittura delle tavole degli inode: fatto                           
  Creating journal (32768 blocks): fatto
  Scrittura delle informazioni dei superblocchi e dell'accounting del filesystem: fatto
  Questo filesystem verrà automaticamente controllato ogni 20 mount, o
  180 giorni, a seconda di quale venga prima. Usare tune2fs -c o -i per cambiare.

System Copy

Let's mount the target SD card (being /dev/sdc) and the source (being dev/sdg):
  # mount /dev/sdc1 /mnt
  # mount /dev/sdg1 /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3
  # df
  File system        blocchi di 1K   Usati   Dispon. Uso% Montato su
  rootfs                15907324  12375740   2723520  82% /
  devtmpfs               1653368       308   1653060   1% /dev
  tmpfs                  1658796         4   1658792   1% /dev/shm
  /dev/sda10            15907324  12375740   2723520  82% /
  /dev/sda3              5162828   3684960   1215608  76% /documents
  /dev/sda11            23867088  19794348   2860236  88% /home
  /dev/sda7             15488716   8913140   5788796  61% /local
  /dev/sda8             10429552   3473932   6531828  35% /download
  /dev/sda9              1050152    653468    396684  63% /windoc
  /dev/sda2             25806300  17392260   7103136  72% /virtual
  /dev/sda11            23867088  19794348   2860236  88% /home/faumarz
  /dev/sdc1             30160428    176196  28452152   1% /mnt
  /dev/sdg1             30265344  23713308   5016656  83% /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3

I showed the source dev/sdg mounted into a /media directory because I let it be mounted by the system.

Now we can do the long copy of the system:
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/bin /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/boot /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/dev /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/etc /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/home /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/lib /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/lib64 /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/opt /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/root /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/sbin /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/srv /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/studio /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/tmp /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/usr /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/var /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/bootincluded_archives.filelist /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/success /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/.Trash-0 /mnt
  cp -av /media/ca11ec65-e12b-4e89-a39a-7cda9482e2d3/.gnupg /mnt
  mkdir /mnt/flash
  mkdir /mnt/media
  mkdir /mnt/mnt
  mkdir /mnt/proc
  mkdir /mnt/selinux
  mkdir /mnt/sys

(Your system can be different: check it)
This will take a long time. Let's take a coffee. (you can also use dd to do the copy, if you want to wait longer).

Bootloader & Friends

I assume you have grub bootloader. OpenSuse is very scared about the damages you can cause to your system using the grub-install script, and you should be scared, too. OpenSuse is so scared that renamed the script into grub-install.unsupported in order to be really sure that you know what you do when you call it.

Old grub versions (OpenSuse 11.4):
  grub-install.unsupported --recheck --root-directory=/mnt /dev/sdc

Newer grub versions:
  grub-install.unsupported --recheck --boot-directory=/mnt/boot /dev/sdc

The --recheck options is needed to avoid the following error:
  /dev/hdc does not have any corresponding BIOS drive

Another way to solve this error is to add the following line to the /mnt/boot/grub/
  (hd2) /dev/hdc

The important thing is that you have to edit the which is in <root-directory>/boot/grub, and NOT the file on the drive you booted from. If you modify the/mnt/boot/grub/ you can also use interactive grub shell instead of grub-install script:
  # grub --device-map=/mnt/boot/grub/
  grub> root (hd2,0)
  root (hd2,0)
  grub> setup (hd2)
  setup (hd2)
   Checking if "/boot/grub/stage1" exists... yes
   Checking if "/boot/grub/stage2" exists... yes
   Checking if "/boot/grub/fat_stage1_5" exists... no
   Running "install /boot/grub/stage1 (hd2) /boot/grub/stage2 p /boot/grub/menu.lst "... succeeded
  grub> quit

Now check the /mnt/etc/fstab file and be sure that its content are the desired mountings for the target system, in particular the swap partition should be /dev/sda2 (supposing that your system boots the SD card considering it as /dev/sda).

I suggest also to check the /mnt/boot/grub/menu.lst in order to be sure that the booting references to the hard disk partitions are correctly referred to /dev/sda (or whatever your system will consider as boot device).

Nessun commento:

Posta un commento