3 # Copyright 2013-2014 Intel Corporation
4 # Author: Artem Bityutskiy
7 PROG="setup-ivi-clone-service"
10 srcdir="$(readlink -ev -- ${0%/*})"
11 PATH="/usr/share/setup-ivi:$srcdir:$PATH"
13 if [ -f "$srcdir/setup-ivi-sh-functions" ]; then
14 . "$srcdir/setup-ivi-sh-functions"
15 . "$srcdir/installerfw-sh-functions"
17 . /usr/share/setup-ivi/setup-ivi-sh-functions
18 . /usr/share/setup-ivi/installerfw-sh-functions
21 # This is a small trick which I use to make sure my scripts are portable -
22 # check if 'dash' is present, and if yes - use it.
23 if can_switch_to_dash; then
24 exec dash -euf -- "$srcdir/$PROG" "$@"
28 # Find the first non-removable device and prints its device node name.
29 find_an_internal_disk()
33 ls -1 "/sys/block" | while IFS= read -r name; do
34 local removable="$(cat "/sys/block/$name/removable")"
35 local size="$(cat "/sys/block/$name/size")"
37 if [ "$removable" -eq "0" ] && [ "$size" -ne "0" ]; then
38 verbose "found removable disk \"$name\""
39 printf "%s" "/dev/$name"
45 # Get user-readable information about a disk.
49 local path="/sys/block/$name"
51 local size="$(LC_ALL=C sed -n -e "s/[[:blank:]]*$//p" -- "$path/size")"
52 local info="size $(($size/2048))MiB"
54 if [ -f "$path/device/vendor" ]; then
55 local vendor="$(LC_ALL=C sed -n -e "s/[[:blank:]]*$//p" -- \
56 "$path/device/vendor")"
57 info="${info}, $vendor"
60 if [ -f "$path/device/model" ]; then
61 local model="$(LC_ALL=C sed -n -e "s/[[:blank:]]*$//p" -- \
62 "$path/device/model")"
69 # Get user confirmation about whether it is OK to proceed.
72 printf "%s\n" "WARNING! All the disk data will be destroyed!"
75 printf "%s\n" "Type \"yes\" to proceed, type \"no\" to exit"
80 if printf "%s" "$input" | grep -q -I -e "^yes$"; then
82 elif printf "%s" "$input" | grep -q -I -e "^no$"; then
91 Usage: $PROG [options]
93 This program is a wrapper over the 'setup-ivi-clone' program and it is intended
94 to be executed from a systemd service when the system boots up.
96 By default, this program finds the first non-removable device in the system and
97 installs the OS there. However, if the 'ivi-clone-target=<devnode>' kernel boot
98 option is present (see "/proc/cmdline"), then this program installs the OS to
99 the disk represented by "<devnode>" (e.g., "/dev/sda").
101 The 'ivi-clone-target=autodetect' kernel option causes the default behaviour.
104 -f, --force do not ask for confirmation, just proceed with cloning the OS
105 --version show the program version and exit
106 -v, --verbose be verbose
107 -h, --help show this text and exit
113 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
119 tmp=`getopt -n $PROG -o f,v,h --long force,verbose,version,help -- "$@"` ||
120 show_usage_fail "cannot parse command-line options"
143 *) show_usage_fail "unrecognized option \"$1\""
150 show_usage_fail "this program does not take any arguments"
152 # Sleep for a while to make sure other tasks finish and our messages appear the
153 # last on the console.
156 devnode="$(sed -n -e "s/^.*ivi-clone-target=\([^ ]\+\).*$/\1/p" /proc/cmdline)"
157 if [ -z "$devnode" ] || [ "$devnode" = "autodetect" ]; then
158 devnode="$(find_an_internal_disk)"
160 [ -n "$devnode" ] || fatal "cannot find an internal disk"
163 # Make sure all device nodes are created
164 udevadm settle > /dev/null 2>&1 ||:
166 [ -b "$devnode" ] || fatal "intended to clone the OS to \"$devnode\"," \
167 "but it does not exist"
169 disk_info="$(get_disk_info "$devnode")"
170 if [ -n "$disk_info" ]; then
171 disk_info="$devnode ($disk_info)"
176 printf "%s\n" "cloning the Tizen IVI OS to \"$disk_info\""
177 [ -z "$force" ] && get_confirmation
178 printf "%s\n" "this may take a while, be patient"
180 setup-ivi-clone $verbose "$devnode" || fatal "failed to clone to $devnode"