Robust and user friendly SDC should also be able to restore USB drive for data storage

Nio Wiklund nio.wiklund at gmail.com
Sun Feb 28 00:08:10 UTC 2016


Hi Marc and Nicholas,

We are discussing the new version of the Ubuntu Startup Disk Creator,
SDC, in the following thread at the Ubuntu Forums and the bug report
#1549603 at Launchpad

http://ubuntuforums.org/showthread.php?t=2314875

https://bugs.launchpad.net/ubuntu/+source/usb-creator/+bug/1549603

We are happy that there is a robust and user friendly SDC program. So
why do we want more?

mc4man started the discussion because he is concerned that it is more
difficult to restore pendrives that have been used as cloned boot drives
(with iso9660 file systems) back to standard drives for storage and
transfer of files. And it is not straightforward to find out how to do
it. He wanted information directly available at the SDC.

There is information at the Ubuntu wiki and help pages, but not directly
available from the SDC or Disks or Gparted (or other tools available in
the Ubuntu family flavours except mkusb, which is only available via PPA).

During the discussion I suggested that it would be a good alternative to
supply a tool to do it, to repartition and reformat the pendrive. We
think the best method would be to build that tool into the SDC, and to
access it via a button in the main window. The button can have the text
'Restore a USB drive for data storage', and the target, the USB drive is
simply selected by the same box as is selectíng the target when flashing
the content of the iso file. It would be possible (but not necessary) to
let the user select a label.

So I took the bash script function mk_msdos from mkusb:

- modified it for this purpose
- wrapped it into a main program to make a verbose command line demo
program, 'restore-pendrive'

The intention was to let some people test it. Four people have tested
this demo program, and it does what we expect it should do.

*Now it is your turn to test it*

If you think that it does what it should do, we can figure out how to
use it. And I am prepared to help with what I can do.

- One alternative is to use the function mk_msdos with a minimal main
program around it (not the verbose demo program we have now), and to
call it via a system call from the SDC.

- Another and maybe better alternative is to build it into the SDC. I
guess it means that it should be translated to python, and the echo
command lines should be removed or replaced.

- Of course it is still an alternative to provide information via that
button (or another button), but if the tool is there, we need no
detailed information.

-----
$ md5sum restore-pendrive
78f8727194210ab633eaff1b4fd1f5a7  restore-pendrive
-----

Grab the attached file and run it in a bash shell :-) See the
screenshots at the following link (post #13)

http://ubuntuforums.org/showthread.php?t=2314875&p=13446485#post13446485

Best regards
Nio
-------------- next part --------------
#! /bin/bash

#-----------------------------------------------------------------------------
#
## Copyright 2016 Nio Wiklund
#
# GPLv3: GNU GPL version 3
# <http://gnu.org/licenses/gpl.html>.
#
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.

# date        editor   comment
# 20160226    sudodus  created from mk_mkdos in mkusb
# 20160227    sudodus  accepting only mass storage devices as target
# 20160227    sudodus  version 1.1

version=1.1

inversvid="\0033[7m"
resetvid="\0033[0m"
redtext="\0033[31;47m"

#######################################################################
#######################################################################

function mk_msdos {

# make an MSDOS partition table and a FAT32 file system

target="$1"
label1="$2"

label1="${label1:0:11}"
label1="${label1^^}"
#echo "Label (name) for the FAT32 partition: $label1"

echo "Trying to unmount partitions if mounted on the target device"
umount "$target"*
df | grep "$target"
if [ $? -eq 0 ]
then
 echo "mk_msdos: could not unmount a partition on the target device"
 exit
fi
echo "------------------------------------------------------------"
dd if=/dev/zero of="$target" bs=1024 count=1024 2>&1
parted -s "$target" mklabel msdos
sleep 0.5
parted -s "$target" mkpart primary 1048576b 100%
sleep 0.5
parted -s "$target" set 1 boot on  # set boot flag on partition 1
sleep 0.5
mkfs.vfat -v -F 32 -n "$label1" "${target}1"
if [ $? -eq 0 ]
then
 success=true
else
 success=false
fi
sleep 0.5
sync
if $success
then
 /bin/echo -e "$inversvid created MSDOS partition table and FAT file system $resetvid"
else
 /bin/echo -e "$inversvid mk_msdos: could not create MSDOS partition table and FAT file system $resetvid"
fi
}

########################################################################
########################################################################
#
# main program
#
########################################################################
########################################################################

# print version on demand

if [ "$1" == "-v" ]
then
 echo "$0 version $version"
 exit
fi 

# identify usb devices

usb_dev=$(ls -l /dev/disk/by-id|grep usb|grep -v 'part.*->' \
|sed -e 's/.*usb-/usb-/' -e 's#../..#/dev#' -e 's/^/ /' -e 's/$/ /'|sort -k3)

# usage text

leng=${#1}
leng1=$((leng - 1))
trunk=${1:0:leng1}

if [ "$(whoami)" != "root" ] || [ $# -gt 2 ] || ! test -b "$1" || test -b "$trunk" || [ "$1" == "-h" ]
then
 echo "Restore a USB install device to a device with MSDOS partition table
and FAT32 partition (to store and transfer files)"
 echo "Usage:"
 echo "sudo $0 <mass storage device> [label]  # max 11 characters in label"
 echo "Example:"
 echo "sudo $0 /dev/sdx usb-123"
 echo "Help:"
 echo "$0 -h"
 echo "Version:"
 echo "$0 -v"
 /bin/echo -e "${inversvid}${usb_dev}${resetvid}"
 exit
fi

# checkpoint

/bin/echo -en "$redtext$inversvid Warning! $resetvid$redtext
This shellsript is only a $inversvid demo $resetvid$redtext for a function to be built into a program,
that is selecting the target device in a safe way to avoid overwriting     
valuable data by mistake. This simple script is not really safe.           
$inversvid Use it very carefully and only for testing! $resetvid$redtext [press Enter to continue] $resetvid"
read -n1 tmp
lsblk -o NAME,MODEL,FSTYPE,LABEL,MOUNTPOINT,SIZE,NAME

/bin/echo -e "${inversvid}${usb_dev}${resetvid}"
echo ''
echo "$usb_dev"|grep -m1 "$1" > /dev/null
if [ $? -ne 0 ]
then
 /bin/echo -en "$redtext Not a USB device. Do you really want to overwrite $inversvid ${1} $resetvid$redtext ? (y/N)$resetvid "
 read ans
 if [ "$ans" != "y" ]
 then
  exit
 fi
fi

/bin/echo -e "$inversvid Final checkpoint $resetvid"
/bin/echo -en "Do you want to restore $inversvid ${1} $resetvid to a FAT32 partition? (y/N) "
read ans
if [ "$ans" == "y" ]
then
# do it
 mk_msdos "$1" "$2"
# check it
 parted -s "$target" print
fi


More information about the Ubuntu-quality mailing list