Epics@GSI Webhome

Dreamplug Create Bootable USB Device From Local System

Requirements

How-to

Create your bootable USB / SD card device.

  1. "script based"
    • download the script dreamplugCreateBootDeviceFunctionSet.sh
    • NOTE: our dreamplug devices, already contain this script at ~/dreamplugCreateBootDeviceFunctionSet.sh
    • set variables:
      • target, target device, e.g. sdd
      • source, uimage source device, e.g. sda
      • tmpusr=restore, temporary user to be created
      • tmppwd=nosoup4u, its password
      • master=root@ee-dev004, system to be exported
    • source the script and use its functions:
      SINGLE action
      createBootDevicePartition partitions your device into 2 partitions:
      * first partition: FAT16, ≈ 30MB
      * second partition: ext3, the rest
      createBootDevicePrepareSystem on the ssh remote machine defined by master
      * mounts uimage source device,
      * creates temporary account,
      * assigns its password,
      * adds it to the /etc/sudoers,
      * adds a cronjob to delete this user at reboot
      createBootDeviceMounts mounts local target devices
      createBootDeviceRsync rsyncs master sources to target mounts
      createBootDeviceUnmounts unmounts devices
      createBootDeviceRestoreSource restore system:
      * deleting temporary user
      * removing crontab entry
      * removing /etc/sudores entry
      * unmounts uimage source device
      actions SET
      createBootDevice combines all:
          createBootDevicePartition
          createBootDevicePrepareSystem
          createBootDeviceMounts
          createBootDeviceRsyncs
          createBootDeviceUmounts
          createBootDeviceRestoreSystem
      createBootDeviceMountRsyncUmount combines:
          createBootDeviceMounts
          createBootDeviceRsyncs
          createBootDeviceUmounts
      functions are only executed if precursor results ok
      allows multiple target setting before restoring the source
      createBootDeviceNoPartitioning combines:
          createBootDevicePrepareSystem
          createBootDeviceMounts
          createBootDeviceRsyncs
          createBootDeviceUmounts
          createBootDeviceRestoreSystem

Example "all"
target=sdd; source=sda; tmpusr=restore; tmppwd=nosoup4u; master=root@ee-dev004;
createBootDevice;
Example "no partitioning"
target=sdd; source=sda; tmpusr=restore; tmppwd=nosoup4u; master=root@ee-dev004;
createBootDeviceNoPartioning;
Example "no partitioning, multiple targets"
target=sdd; source=sda; tmpusr=restore; tmppwd=nosoup4u; master=root@ee-dev004;
createBootDevicePrepareSystem &&

createBootDeviceMountRsyncUmount &&
target=sdb &&
createBootDeviceMountRsyncUmount &&

createBootDeviceRestoreSource

    Script code
    dreamplugCreateBootDeviceFunctionSet.sh
    #!/bin/bash
       # Dreamplug: create Boot Device Function set
       #            for system transfer local -> /dev/${target}
       #--------------------------
    
       function createBootDevicePartition()
       {
           
          # First partitioning:
          # using fdisk (delete all partitions, create first:FAT16, primary,30MB, second:ext3, the rest)
       
          echo "d.1.d.2.d.3.d.4.n....+30M.n.....t.1.6.w." | tr '.' '\n' | fdisk /dev/${target} || return 1 && :;
       
          #Format, don't give vfat partition a different name!!!
    
          mkfs.vfat -c -v -n " " /dev/${target}1 || return 1; 
          mkfs.ext3 -c -v -L "system" /dev/${target}2 || return 1;
    
          return 0;
       }
    
       function createBootDevicePrepareSystem()
       { 
          #Prepare system source:
          #   - mount uimage device
          #   - create temporary restore account and add its removal to the crontab '@reboot'
          #   - reset /etc/hostname to 'dreamplug'
    
          targetScript=~/.remove_${tmpusr}.sh
    
          rebootScript=$(mktemp) && 
             echo -e '#!/bin/bash' > $rebootScript
             echo -e " id ${tmpusr} 1>/dev/null 2>&1 && " >> $rebootScript
             echo -e " echo ${tmpusr}_REMOVAL|: && " >> $rebootScript
             echo -e " grep -q root=/dev/sda /proc/cmdline && " >> $rebootScript
             echo -e " /usr/sbin/deluser $tmpusr --remove-home && " >> $rebootScript
             echo -e ' tmp=$(mktemp) && cat /etc/sudoers | grep -v -w '${tmpusr}' > $tmp && mv $tmp /etc/sudoers && ' >> $rebootScript
             echo -e ' crontab -l | grep -v '${tmpusr}'_REMOVAL  | crontab - &&' >> $rebootScript 
             echo -e " rm $targetScript" >> $rebootScript 
    
          scp  $rebootScript ${master:?undefined}:$targetScript && 
          rm $rebootScript && 
        
          ssh ${master:?undefined}  "echo dreamplug > /etc/hostname;                                  mkdir -p /mnt/${source}1 &&                                  mount /dev/${source}1 /mnt/${source}1;                                  adduser ${tmpusr} --disabled-password --gecos \"\" --home /home/.$(basename $(mktemp -u -d)) &&                                  echo ${tmpusr}:${tmppwd} | chpasswd &&                                  eval tmp=\$\(mktemp\) && cat /etc/sudoers | grep -v -w ${tmpusr} > \${tmp} && mv \$tmp /etc/sudoers &&                                  echo -e \"\n\n#${tmpusr} \n${tmpusr} ALL=(ALL) NOPASSWD:ALL\" >> /etc/sudoers &&                                  echo -e \"\n\n#${tmpusr} becomes immediately root                                              \necho ATTENTION: be aware you are now ROOT!                                              \nsudo su - \" >> ~${tmpusr}/.bashrc &&                                 chmod 700 $targetScript &&                                  echo -e                                          \"\$(crontab -l | grep -v ${tmpusr}_REMOVAL)                                         \n\n#${tmpusr}_REMOVAL: remove $tmpusr account @reboot from local /dev/sda and afterwards remove this crontab entry                                          \n@reboot echo ${tmpusr}_REMOVAL |: && /bin/bash -c $targetScript\"                                  | tr -s '[:space:]'                                  | crontab - && crontab -l" 
       }
    
       function createBootDeviceMounts()
       {
          #Mount local filesystems:
          for item in ${target}1 ${target}2
          do
            if [ -b /dev/${item} ];
            then 
              mkdir -p /mnt/${item} &&
          echo mounting: /dev/${item} '->' /mnt/${item} && 
              mount /dev/${item} /mnt/${item} ||
              return 1    
            else
          echo "\"/dev/$item\" is not an available (block) device"
          return 1;
            fi
          done
       }
    
       function createBootDeviceRsync()
       {
          # Rsync system and uImage
          # 
          # using the following options
          # -x, --one-file-system: don't cross filesystem boundaries
          # -a, --archive        : archive mode; equals -rlptgoD (no -H,-A,-X)
          #                          -r, --recursive  recurse into directories
          #                          -l, --links      copy symlinks as symlinks
          #                          -t, --times      preserve modification times
          #                          -g, --group      preserve group
          #                          -o, --owner      preserve owner (super-user only)
          #                          -D               same as --devices --specials
          #                                           --devices   preserve device files (super-user only)
          #                                           --specials  preserve special files
          # -A, --acls           : preserve ACLs (implies --perms)
          # -X, --xattrs         : preserve extended attributes
          # -H, --hard-links     : preserve hard links
          #     --delete         : delete extraneous files from destination dirs
    
          time { grep -q /mnt/${target}1 /etc/mtab &&              rsync --verbose --one-file-system --archive --hard-links --acls --xattrs --delete ${master:?undefined}:/mnt/${source}1/ /mnt/${target}1/              || return 1; }
          time { grep -q /mnt/${target}1 /etc/mtab &&              rsync --verbose --one-file-system --archive --hard-links --acls --xattrs --delete ${master:?undefined}:/ /mnt/${target}2/              || return 1; }
    
          return 0; 
       }
    
    
       function createBootDeviceUnmounts()
       {
    
          # Unmount targets
          cd / 
          umount /mnt/${target}1 /mnt/${target}2 
          for item in ${target}1 ${target}2 
          do
            if [ -d /mnt/${item} ];
             then 
           grep -q /mnt/${item} /etc/mtab &&
               echo unmounting: /mnt/${item} &&
               umount /mnt/${item} 
            fi
          done
    
          return 0; 
       }
    
       function createBootDeviceRestoreSource()
       {
          # Delete temporary restore account
       
          ssh ${master:?undefined} "deluser ${tmpusr} --remove-home;                                 echo -e \"\$(crontab -l | grep -v ${tmpusr}_REMOVAL) \" | crontab - ;                                 echo -e \"\$(cat /etc/sudoers | grep -v -w ${tmpusr})\" > /etc/sudoers ;                                umount /mnt/${source}1;" || return 1
    
         return 0; 
       }
    
       function createBootDeviceMountRsyncUmount
       {
         createBootDeviceMounts &&
         createBootDeviceRsync &&
         createBootDeviceUnmounts
       }
    
       function createBootDeviceNoPartitioning()
       {
         createBootDevicePrepareSystem &&
         createBootDeviceMountRsyncUmount &&
         createBootDeviceRestoreSource
       }
    
       function createBootDevice()
       {
         createBootDevicePartition &&
         createBootDeviceNoPartitioning    
       }
    
       echo "Available Functions:
       
       # ALL: partition && prepare && mount && rsync && umount && restore
       createBootDevice 
    
                   SINGLE            |                                SETS
       -------------------------------|------------------------------------------------------------------
       createBootDevicePartition        
    
       createBootDevicePrepareSystem                                    |
                                                                        | # prepare && mount && rsync && 
       createBootDeviceMounts        | # mount && rsync && umount       | # umount && restore
       createBootDeviceRsync         |                                  | 
       createBootDeviceUnmounts      | createBootDeviceMountRsyncUmount | createBootDeviceNoPartitioning 
                                                                        |
       createBootDeviceRestoreSource                                    |
       "
       echo "EXAMPLE:
    
       target=${target:=sdd}; source=sda; tmpusr=restore; tmppwd=nosoup4u; master=root@ee-dev004;
       createBootDevice; 
       unset source target tmpusr tmppwd"
    
    

-- PeterZumbruch - 22 Feb 2016
-- PeterZumbruch - 2016-02-26

Topic revision: r25 - 2016-02-26, PeterZumbruch
 
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding GSI Wiki? Send feedback
Imprint (in German)
Privacy Policy (in German)