setup-gummiboot-conf: Set background to black
[platform/adaptation/setup-scripts.git] / setup-ivi-clone-service
1 #!/bin/sh -euf
2
3 # Copyright 2013-2014 Intel Corporation
4 # Author: Artem Bityutskiy
5 # License: GPLv2
6
7 PROG="setup-ivi-clone-service"
8 VER="1.0"
9
10 srcdir="$(readlink -ev -- ${0%/*})"
11 PATH="/usr/share/setup-ivi:$srcdir:$PATH"
12
13 if [ -f "$srcdir/setup-ivi-sh-functions" ]; then
14         . "$srcdir/setup-ivi-sh-functions"
15         . "$srcdir/installerfw-sh-functions"
16 else
17         .  /usr/share/setup-ivi/setup-ivi-sh-functions
18         .  /usr/share/setup-ivi/installerfw-sh-functions
19 fi
20
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" "$@"
25         exit $?
26 fi
27
28 # Find the first non-removable device and prints its device node name.
29 find_an_internal_disk()
30 {
31         local dir
32
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")"
36
37                 if [ "$removable" -eq "0" ] && [ "$size" -ne "0" ]; then
38                         verbose "found removable disk \"$name\""
39                         printf "%s" "/dev/$name"
40                         break
41                 fi
42         done
43 }
44
45 # Get user-readable information about a disk.
46 get_disk_info()
47 {
48         local name="${1##*/}"
49         local path="/sys/block/$name"
50
51         local size="$(LC_ALL=C sed -n -e "s/[[:blank:]]*$//p" -- "$path/size")"
52         local info="size $(($size/2048))MiB"
53
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"
58         fi
59
60         if [ -f "$path/device/model" ]; then
61                 local model="$(LC_ALL=C sed -n -e "s/[[:blank:]]*$//p" -- \
62                                                   "$path/device/model")"
63                 info="${info} $model"
64         fi
65
66         printf "%s" "$info"
67 }
68
69 # Get user confirmation about whether it is OK to proceed.
70 get_confirmation()
71 {
72         printf "%s\n" "WARNING! All the disk data will be destroyed!"
73
74         while true; do
75                 printf "%s\n" "Type \"yes\" to proceed, type \"no\" to exit"
76
77                 local input
78                 read input
79
80                 if printf "%s" "$input" | grep -q -I -e "^yes$"; then
81                         return
82                 elif printf "%s" "$input" | grep -q -I -e "^no$"; then
83                         exit 0
84                 fi
85         done
86 }
87
88 show_usage()
89 {
90         cat <<-EOF
91 Usage: $PROG [options]
92
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.
95
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").
100
101 The 'ivi-clone-target=autodetect' kernel option causes the default behaviour.
102
103 Options:
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
108 EOF
109 }
110
111 show_usage_fail()
112 {
113         IFS= printf "%s\n\n" "$PROG: error: $*" >&2
114         show_usage >&2
115         exit 1
116 }
117
118 # Parse the options
119 tmp=`getopt -n $PROG -o f,v,h --long force,verbose,version,help -- "$@"` ||
120         show_usage_fail "cannot parse command-line options"
121 eval set -- "$tmp"
122
123 verbose=
124 force=
125 while true; do
126         case "$1" in
127         -f|--force)
128                 force="-f"
129                 ;;
130         --version)
131                 printf "%s\n" "$VER"
132                 exit 0
133                 ;;
134         -v|--verbose)
135                 verbose="-v"
136                 ;;
137         -h|--help)
138                 show_usage
139                 exit 0
140                 ;;
141         --) shift; break
142                 ;;
143         *) show_usage_fail "unrecognized option \"$1\""
144                 ;;
145         esac
146         shift
147 done
148
149 [ "$#" -eq 0 ] || \
150         show_usage_fail "this program does not take any arguments"
151
152 # Sleep for a while to make sure other tasks finish and our messages appear the
153 # last on the console.
154 sleep 2
155
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)"
159
160         [ -n "$devnode" ] || fatal "cannot find an internal disk"
161 fi
162
163 # Make sure all device nodes are created
164 udevadm settle > /dev/null 2>&1 ||:
165
166 [ -b "$devnode" ] || fatal "intended to clone the OS to \"$devnode\"," \
167                            "but it does not exist"
168
169 disk_info="$(get_disk_info "$devnode")"
170 if [ -n "$disk_info" ]; then
171         disk_info="$devnode ($disk_info)"
172 else
173         disk_info="$devnode"
174 fi
175
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"
179
180 setup-ivi-clone $verbose "$devnode" || fatal "failed to clone to $devnode"