11#! /bin/bash
22set -e
33
4+ device=" $1 "
5+ source_device=" $2 "
6+
47cd " ` dirname \" $0 \" ` "
58
69list_devices () {
@@ -13,7 +16,6 @@ new_devices_attached() {
1316
1417echo " Hello, I am a friendly program which likes to duplicate"
1518echo " this operating system to other USB devices!"
16- device=" $1 "
1719if [ -z " $device " ]; then
1820 echo " Please attach a USB device to this computer!"
1921
@@ -53,18 +55,73 @@ if [ -z "$description" ]; then
5355 exit 1
5456fi
5557
56- ./partition-sticks.sh " $device "
57-
58- dir= " /tmp/usb-stick "
58+ if [ -z " $source_device " ] ; then
59+ source_device= " /dev/sda "
60+ fi
5961partition=" ${device} 1"
6062
61- mkdir -p " $dir "
62- sudo mount " $partition " " $dir "
63+ end=" ` sudo parted -s /dev/sdb ' unit b print' | awk ' /^ / {print $3}' ` "
64+ bytes=" ${end% B} "
65+ bytes=" ${bytes% b} "
66+ start=" ` sudo parted -s /dev/sdb ' unit b print' | awk ' /^ / {print $2}' ` "
67+
68+ echo " Getting partitions of $device "
69+ partitions=" ` sudo parted -s \" $device \" print| awk ' /^ / {print $1}' ` "
70+ echo " Deleting partitions $partitions "
71+ for i in $partitions ; do
72+ sudo umount " $device$i " || true
73+ sudo parted -s " $device " rm " $i "
74+ done
75+
76+ echo " Creating a new partition table"
77+
78+ sudo parted -s " $device " mklabel msdos
79+
80+ part_num=" 1"
81+ partition=" $device$part_num "
82+
83+ sudo partprobe || true
84+
85+ echo " Waiting for partition to disappear"
86+ timeout 1s bash -c " while [ -e \" $partition \" ]; do echo -n .; sleep 0.2; done" || {
87+ echo " $partition exists"
88+ exit 1
89+ }
90+ echo " $partition vanished."
91+
92+
93+ echo " Creating partition:"
94+ sudo parted -s " $device " mkpart primary " $start " " $end " || true
95+
96+ echo " Rereading the partition table"
97+ echo " see http://serverfault.com/a/36047"
98+ sudo partprobe || true
99+
100+ echo " Waiting for partition to appear"
101+ timeout 1s bash -c " while ! [ -e \" $partition \" ]; do echo -n .; sleep 0.2; done" || {
102+ echo " $partition does not exist"
103+ exit 1
104+ }
105+ echo " $partition exists."
106+
107+ echo " Bytes to copy: $bytes "
63108
64- sudo cp -v -r -T " /cdrom/" " $dir "
109+ steps=100
110+ start=0
111+ sec_start=" ` date +%s` "
112+ for i in ` seq 1 100` ; do
113+ stop=$(( bytes * i / steps))
114+ count=" $(( stop - start)) "
115+ sudo dd if=" $source_device " of=" $device " count=$count seek=$start skip=$start oflag=seek_bytes iflag=skip_bytes,count_bytes
116+ start=$stop
117+ sec_now=" ` date +%s` "
118+ sec_remaining=$(( (sec_now - sec_start) * ( 100 - i) / i ))
119+ echo " $i % - $(( sec_remaining / 60 )) m $(( sec_remaining % 60 )) s remaining"
120+ done
65121
66- sudo umount " $dir "
122+ echo " Syncing... "
67123sync
124+ sudo partprobe || true
68125
69126echo " Copied all contents successfully!"
70127echo " You can now remove the device $device ."
0 commit comments