3 # Copyright (c) 2012, Intel Corporation.
6 # install.sh [device_name] [rootfs_name]
9 PATH=/sbin:/bin:/usr/sbin:/usr/bin
11 # figure out how big of a boot partition we need
12 boot_size=$(du -ms /run/media/$1/ | awk '{print $1}')
13 # remove rootfs.img ($2) from the size if it exists, as its not installed to /boot
14 if [ -e /run/media/$1/$2 ]; then
15 boot_size=$(( boot_size - $( du -ms /run/media/$1/$2 | awk '{print $1}') ))
17 # remove initrd from size since its not currently installed
18 if [ -e /run/media/$1/initrd ]; then
19 boot_size=$(( boot_size - $( du -ms /run/media/$1/initrd | awk '{print $1}') ))
21 # add 10M to provide some extra space for users and account
22 # for rounding in the above subtractions
23 boot_size=$(( boot_size + 10 ))
28 # Get a list of hard drives
30 live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'`
31 live_dev_name=${live_dev_name#\/dev/}
32 # Only strip the digit identifier if the device is not an mmc
33 case $live_dev_name in
39 live_dev_name=${live_dev_name%%[0-9]*}
43 echo "Searching for hard drives ..."
45 # Some eMMC devices have special sub devices such as mmcblk0boot0 etc
46 # we're currently only interested in the root device so pick them wisely
47 devices=`ls /sys/block/ | grep -v "mmcblk\|md\|dm"` || true
48 mmc_devices=`ls /sys/block/ | grep "mmcblk[0-9]\{1,\}$"` || true
49 md_devices=`cat /proc/mdstat |grep -w active |awk -F":" '{print $1}'` || true
50 devices="$devices $mmc_devices $md_devices"
52 for device in $devices; do
64 # skip the device LiveOS is on
65 # Add valid hard drive name to the list
68 # skip the device we are running from
71 hdnamelist="$hdnamelist $device"
78 if [ -z "${hdnamelist}" ]; then
79 echo "You need another device (besides the live device /dev/${live_dev_name}) to install the image. Installation aborted."
84 for hdname in $hdnamelist; do
85 # Display found hard drives and their basic info
86 echo "-------------------------------"
88 if [ -r /sys/block/$hdname/device/vendor ]; then
90 cat /sys/block/$hdname/device/vendor
92 if [ -r /sys/block/$hdname/device/model ]; then
94 cat /sys/block/$hdname/device/model
96 if [ -r /sys/block/$hdname/device/uevent ]; then
98 cat /sys/block/$hdname/device/uevent
105 echo "Please select an install target or press n to exit ($hdnamelist ): "
107 if [ "$answer" = "n" ]; then
108 echo "Installation manually aborted."
111 for hdname in $hdnamelist; do
112 if [ "$answer" = "$hdname" ]; then
113 TARGET_DEVICE_NAME=$answer
117 if [ -n "$TARGET_DEVICE_NAME" ]; then
122 if [ -n "$TARGET_DEVICE_NAME" ]; then
123 echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
125 echo "No hard drive selected. Installation aborted."
129 device=/dev/$TARGET_DEVICE_NAME
132 # The udev automounter can cause pain here, kill it
134 #rm -f /etc/udev/rules.d/automount.rules
135 #rm -f /etc/udev/scripts/mount*
138 # Unmount anything the automounter had mounted
140 #umount ${device}* 2> /dev/null || /bin/true
142 for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts | grep $TARGET_DEVICE_NAME`; do
148 # Create /etc/mtab if not present
149 if [ ! -e /etc/mtab ] && [ -e /proc/mounts ]; then
150 ln -sf /proc/mounts /etc/mtab
153 disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
155 swap_size=$((disk_size*swap_ratio/100))
156 rootfs_size=$((disk_size-boot_size-swap_size))
158 rootfs_start=$((boot_size))
159 rootfs_end=$((rootfs_start+rootfs_size))
160 swap_start=$((rootfs_end))
162 # MMC devices are special in a couple of ways
163 # 1) they use a partition prefix character 'p'
164 # 2) they are detected asynchronously (need rootwait)
167 if [ ! "${device#/dev/mmcblk}" = "${device}" ] || \
168 [ ! "${device#/dev/nvme}" = "${device}" ]; then
173 # MD raid device use partition prefix charater 'p'
174 # and it need a larger capacity to store initrd,
175 # considering some debug purpose, enlarge it to 1G.
176 if [ ! "${device#/dev/md}" = "${device}" ]; then
181 # USB devices also require rootwait
182 if [ -n `readlink /dev/disk/by-id/usb* | grep $TARGET_DEVICE_NAME` ]; then
186 bootfs=${device}${part_prefix}1
187 rootfs=${device}${part_prefix}2
188 swap=${device}${part_prefix}3
190 echo "*****************"
191 echo "Boot partition size: $boot_size MB ($bootfs)"
192 echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
193 echo "Swap partition size: $swap_size MB ($swap)"
194 echo "*****************"
195 echo "Deleting partition table on ${device} ..."
196 dd if=/dev/zero of=${device} bs=512 count=35
198 echo "Creating new partition table on ${device} ..."
199 parted ${device} mklabel gpt
201 echo "Creating boot partition on $bootfs"
202 parted ${device} mkpart boot fat32 0% $boot_size
203 parted ${device} set 1 boot on
205 echo "Creating rootfs partition on $rootfs"
206 parted ${device} mkpart root ext4 $rootfs_start $rootfs_end
208 echo "Creating swap partition on $swap"
209 parted ${device} mkpart swap linux-swap $swap_start 100%
211 parted ${device} print
213 echo "Waiting for device nodes..."
215 while [ $C -ne 3 ] && [ ! -e $bootfs -o ! -e $rootfs -o ! -e $swap ]; do
220 echo "Formatting $bootfs to vfat..."
223 echo "Formatting $rootfs to ext4..."
226 echo "Formatting swap partition...($swap)"
233 # Handling of the target root partition
234 mount $rootfs /tgt_root
236 if [ ! -f /run/media/$1/$2 ]; then
237 mkdir -p /run/media/$1
238 mount /dev/$1 /run/media/$1
240 mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
241 echo "Copying rootfs files..."
242 cp -a /src_root/* /tgt_root
243 if [ -d /tgt_root/etc/ ] ; then
244 boot_uuid=$(blkid -o value -s UUID ${bootfs})
245 swap_part_uuid=$(blkid -o value -s PARTUUID ${swap})
246 echo "/dev/disk/by-partuuid/$swap_part_uuid swap swap defaults 0 0" >> /tgt_root/etc/fstab
247 echo "UUID=$boot_uuid /boot vfat defaults 1 2" >> /tgt_root/etc/fstab
248 # We dont want udev to mount our root device while we're booting...
249 if [ -d /tgt_root/etc/udev/ ] ; then
250 echo "${device}" >> /tgt_root/etc/udev/mount.blacklist
256 # Handling of the target boot partition
258 echo "Preparing boot partition..."
260 EFIDIR="/boot/EFI/BOOT"
262 # Copy the efi loader
263 cp /run/media/$1/EFI/BOOT/*.efi $EFIDIR
265 if [ -f /run/media/$1/EFI/BOOT/grub.cfg ]; then
266 root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
267 GRUBCFG="$EFIDIR/grub.cfg"
268 cp /run/media/$1/EFI/BOOT/grub.cfg $GRUBCFG
269 # Update grub config for the installed image
270 # Delete the install entry
271 sed -i "/menuentry 'install/,/^}/d" $GRUBCFG
272 # initrd is necessary to boot from MD device
273 if [ ! "${device#/dev/md}" = "${device}" ]; then
274 cp /run/media/$1/initrd /boot
275 cp /run/media/$1/startup.nsh /boot
277 # Delete the initrd lines
278 sed -i "/initrd /d" $GRUBCFG
280 # Delete any LABEL= strings
281 sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG
282 # Replace root= and add additional standard boot options
283 # We use root as a sentinel value, as vmlinuz is no longer guaranteed
284 sed -i "s/ root=[^ ]*/ root=PARTUUID=$root_part_uuid rw $rootwait quiet /g" $GRUBCFG
287 if [ -d /run/media/$1/loader ]; then
288 rootuuid=$(blkid -o value -s PARTUUID ${rootfs})
289 SYSTEMDBOOT_CFGS="/boot/loader/entries/*.conf"
290 # copy config files for systemd-boot
291 cp -dr /run/media/$1/loader /boot
292 # delete the install entry
293 rm -f /boot/loader/entries/install.conf
294 # delete the initrd lines
295 sed -i "/initrd /d" $SYSTEMDBOOT_CFGS
296 # delete any LABEL= strings
297 sed -i "s/ LABEL=[^ ]*/ /" $SYSTEMDBOOT_CFGS
298 # delete any root= strings
299 sed -i "s/ root=[^ ]*/ /" $SYSTEMDBOOT_CFGS
300 # add the root= and other standard boot options
301 sed -i "s@options *@options root=PARTUUID=$rootuuid rw $rootwait quiet @" $SYSTEMDBOOT_CFGS
306 # Copy kernel artifacts. To add more artifacts just add to types
307 # For now just support kernel types already being used by something in OE-core
308 for types in bzImage zImage vmlinux vmlinuz fitImage; do
309 for kernel in `find /run/media/$1/ -name $types*`; do
318 echo "Installation successful. Remove your installation media and press ENTER to reboot."