Merge ~alextu/pc-enablement/+git/oem-scripts:recovery-from-jenkins-iso into ~oem-solutions-engineers/pc-enablement/+git/oem-scripts:master

Proposed by Alex Tu
Status: Merged
Merged at revision: a985900a169bb6bc4b7a5968df23725d5601f867
Proposed branch: ~alextu/pc-enablement/+git/oem-scripts:recovery-from-jenkins-iso
Merge into: ~oem-solutions-engineers/pc-enablement/+git/oem-scripts:master
Diff against target: 195 lines (+189/-0)
1 file modified
recovery-from-iso.sh (+189/-0)
Reviewer Review Type Date Requested Status
Shih-Yuan Lee Approve
Review via email: mp+399837@code.launchpad.net

Commit message

╰─>./recovery-from-iso.sh --help
usage:
recovery-from-iso.sh -j <jenkins-job-name> -b <jenkins-job-build-no> -t <target-ip> [-h|--help] [--dry-run]
recovery-from-iso.sh --local-iso <path to local iso file> -t <target-ip> [-h|--help] [--dry-run]

Limition:
    It will failed when target recovery partition size smaller than target iso file.

The assumption of using this tool:
 - An root account 'ubuntu' on target machine.
 - The root account 'ubuntu' can execute command with root permission with `sudo` without password.
 - Host executing this tool can access target machine without password over ssh.

OPTIONS:
    -j|--jenkins-job Get iso from jenkins-job. The default is "dell-bto-focal-fossa-edge-alloem".
    -b|--jenkins-job-build-no The build number of the Jenkins job assigned by -j|--jenkins-job.
    -t|--target-ip The IP address of target machine. It will be used for ssh accessing.
                    Please put your ssh key on target machine. This tool no yet support keyphase for ssh.
    -h|--help Print this message

    Usage:
    recovery-from-iso.sh -j dell-bto-focal-fossa-edge-alloem -b 3 -t 192.168.101.68

    recovery-from-iso.sh --local-iso ./dell-bto-focal-fossa-edge-alloem-X73-20210302-3.iso

the result of testing:
https://pastebin.canonical.com/p/VzXJQCZZvV/

To post a comment you must log in.
Revision history for this message
Shih-Yuan Lee (fourdollars) wrote :

Please hide those internal information

review: Needs Fixing
Revision history for this message
jeremyszu (os369510) wrote :

@Alex,

Would you please help to try 'efibootmgr -n ${ubuntu recovery}' or modify grub order for testing stella/sutton?

Revision history for this message
Shih-Yuan Lee (fourdollars) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/recovery-from-iso.sh b/recovery-from-iso.sh
2new file mode 100755
3index 0000000..c8a5b1c
4--- /dev/null
5+++ b/recovery-from-iso.sh
6@@ -0,0 +1,189 @@
7+#!/bin/bash
8+set -ex
9+
10+jenkins_job_for_iso=""
11+jenkins_job_build_no="lastSuccessfulBuild"
12+script_on_target_machine="inject_recovery_from_iso.sh"
13+user_on_target="ubuntu"
14+SSH="ssh -o StrictHostKeyChecking=no"
15+SCP="scp -o StrictHostKeyChecking=no"
16+#TAR="tar -C $temp_folder"
17+temp_folder="$(mktemp -d -p "$PWD")"
18+GIT="git -C $temp_folder"
19+
20+clear_all() {
21+ rm -rf "$temp_folder"
22+}
23+trap clear_all EXIT
24+# shellcheck disable=SC2046
25+eval set -- $(getopt -o "hj:t:b:u:" -l "help,target-ip:,jenkins-job:,local-iso:,url:" -- "$@")
26+
27+usage() {
28+cat << EOF
29+usage:
30+$(basename "$0") -u <jenkins url> -j <jenkins-job-name> -b <jenkins-job-build-no> -t <target-ip> [-h|--help] [--dry-run]
31+$(basename "$0") --local-iso <path to local iso file> -t <target-ip> [-h|--help] [--dry-run]
32+
33+Limition:
34+ It will failed when target recovery partition size smaller than target iso file.
35+
36+The assumption of using this tool:
37+ - An root account 'ubuntu' on target machine.
38+ - The root account 'ubuntu' can execute command with root permission with \`sudo\` without password.
39+ - Host executing this tool can access target machine without password over ssh.
40+
41+OPTIONS:
42+ -u|--url The url of jenkins server.
43+ -j|--jenkins-job Get iso from jenkins-job. The default is "dell-bto-focal-fossa-edge-alloem".
44+ -b|--jenkins-job-build-no The build number of the Jenkins job assigned by -j|--jenkins-job.
45+ -t|--target-ip The IP address of target machine. It will be used for ssh accessing.
46+ Please put your ssh key on target machine. This tool no yet support keyphase for ssh.
47+ -h|--help Print this message
48+
49+Usage:
50+
51+ $(basename "$0") -u 10.101.46.50 -j dell-bto-focal-fossa-edge-alloem -b 3 -t 192.168.101.68
52+
53+ $(basename "$0") --local-iso ./dell-bto-focal-fossa-edge-alloem-X73-20210302-3.iso -t 192.168.101.68
54+
55+EOF
56+exit 1
57+}
58+
59+download_preseed() {
60+ echo " == download_preseed == "
61+ # get checkbox pkgs and prepare-checkbox
62+ # get pkgs to skip OOBE
63+ $GIT clone https://git.launchpad.net/~oem-solutions-engineers/pc-enablement/+git/oem-fix-misc-cnl-no-secureboot --depth 1
64+ $GIT clone https://git.launchpad.net/~oem-solutions-engineers/pc-enablement/+git/oem-fix-misc-cnl-skip-oobe --depth 1
65+ $GIT clone https://git.launchpad.net/~oem-solutions-engineers/pc-enablement/+git/oem-fix-misc-cnl-skip-storage-selecting --depth 1
66+ $GIT clone https://git.launchpad.net/~oem-solutions-engineers/pc-enablement/+git/pack-fish.openssh-fossa --depth 1
67+
68+ # get pkgs for ssh key and skip disk checking.
69+ $GIT clone https://git.launchpad.net/~oem-solutions-engineers/pc-enablement/+git/oem-fix-misc-cnl-misc-for-automation --depth 1 misc_for_automation
70+
71+ return 0
72+}
73+push_preseed() {
74+ echo " == download_preseed == "
75+ $SSH "$user_on_target"@"$target_ip" rm -rf push_preseed
76+ $SSH "$user_on_target"@"$target_ip" mkdir -p push_preseed
77+ $SSH "$user_on_target"@"$target_ip" touch push_preseed/SUCCSS_push_preseed
78+ $SSH "$user_on_target"@"$target_ip" sudo rm -f /cdrom/SUCCSS_push_preseed
79+
80+ for folder in pack-fish.openssh-fossa misc_for_automation oem-fix-misc-cnl-no-secureboot oem-fix-misc-cnl-skip-oobe oem-fix-misc-cnl-skip-storage-selecting; do
81+ tar -C "$temp_folder"/$folder -zcvf "$temp_folder"/$folder.tar.gz .
82+ $SCP "$temp_folder/$folder".tar.gz "$user_on_target"@"$target_ip":~
83+ $SSH "$user_on_target"@"$target_ip" tar -C push_preseed -zxvf $folder.tar.gz || $SSH "$user_on_target"@"$target_ip" sudo rm -f push_preseed/SUCCSS_push_preseed
84+ done
85+
86+ $SSH "$user_on_target"@"$target_ip" sudo cp -r push_preseed/* /cdrom/
87+ return 0
88+}
89+inject_preseed() {
90+ echo " == inject_preseed == "
91+ $SSH "$user_on_target"@"$target_ip" rm -rf /tmp/SUCCSS_inject_preseed
92+ download_preseed && \
93+ push_preseed
94+ scp -o StrictHostKeyChecking=no "$user_on_target"@"$target_ip":/cdrom/SUCCSS_push_preseed "$temp_folder" || usage
95+ $SSH "$user_on_target"@"$target_ip" touch /tmp/SUCCSS_inject_preseed
96+}
97+
98+inject_recovery_iso() {
99+ if [ -n "$local_iso" ]; then
100+ img_name="$(basename "$local_iso")"
101+ scp -o StrictHostKeyChecking=no "$local_iso" "$user_on_target"@"$target_ip":~/
102+cat <<EOF > "$temp_folder/$script_on_target_machine"
103+#!/bin/bash
104+set -ex
105+sudo umount /cdrom /mnt || true
106+sudo mount -o loop $img_name /mnt && \
107+sudo mount /dev/\$(lsblk -l | grep efi | cut -d ' ' -f 1 | sed 's/.$/2'/) /cdrom && \
108+df | grep "cdrom\|mnt" | awk '{print \$2" "\$6}' | sort | tail -n1 | grep -q cdrom && \
109+sudo mkdir -p /var/tmp/rsync && \
110+sudo rsync -alv /mnt/ /cdrom/ --exclude=factory/grub.cfg* --exclude=efi/boot --exclude=.disk/casper-uuid --exclude=.disk/info --exclude=.disk/info.recovery --exclude=efi.factory --delete --exclude=casper/filesystem.squashfs --temp-dir=/var/tmp/rsync && \
111+sudo cp /mnt/.disk/ubuntu_dist_channel /cdrom/.disk/ && \
112+touch /tmp/SUCCSS_inject_recovery_iso
113+EOF
114+ scp -o StrictHostKeyChecking=no "$temp_folder"/"$script_on_target_machine" "$user_on_target"@"$target_ip":~/
115+ ssh -o StrictHostKeyChecking=no "$user_on_target"@"$target_ip" chmod +x "\$HOME/$script_on_target_machine"
116+ ssh -o StrictHostKeyChecking=no "$user_on_target"@"$target_ip" "\$HOME/$script_on_target_machine"
117+ scp -o StrictHostKeyChecking=no "$user_on_target"@"$target_ip":/tmp/SUCCSS_inject_recovery_iso "$temp_folder" || usage
118+ else
119+ img_jenkins_out_url="ftp://$jenkins_url/jenkins_host/jobs/$jenkins_job_for_iso/builds/$jenkins_job_build_no/archive/out"
120+ img_name="$(wget -q "$img_jenkins_out_url/" -O - | grep -o 'href=.*iso"' | awk -F/ '{print $NF}' | tr -d \")"
121+ pushd "$temp_folder" || usage
122+ wget "$img_jenkins_out_url/$img_name".md5sum
123+ md5sum -c "$img_name".md5sum || wget "$img_jenkins_out_url"/"$img_name"
124+ md5sum -c "$img_name".md5sum || usage
125+ local_iso="$PWD/$img_name"
126+ popd
127+ inject_recovery_iso
128+ fi
129+}
130+prepare() {
131+ echo "prepare"
132+ inject_recovery_iso
133+ inject_preseed
134+}
135+
136+poll_recovery_status() {
137+ while(:); do
138+ if $SSH "$user_on_target"@"$target_ip" ubuntu-report show; then
139+ break
140+ fi
141+ sleep 180
142+ done
143+}
144+
145+do_recovery() {
146+ ssh -o StrictHostKeyChecking=no "$user_on_target"@"$target_ip" sudo dell-restore-system -y &
147+ sleep 300 # sleep to make sure the target system has been rebooted to recovery mode.
148+ poll_recovery_status
149+}
150+
151+main() {
152+ while [ $# -gt 0 ]
153+ do
154+ case "$1" in
155+ --local-iso)
156+ shift
157+ local_iso="$1"
158+ ;;
159+ -u | --url)
160+ shift
161+ jenkins_url="$1"
162+ ;;
163+ -j | --jenkins-job)
164+ shift
165+ jenkins_job_for_iso="$1"
166+ ;;
167+ -b | --jenkins-job-build-no)
168+ shift
169+ jenkins_job_build_no="$1"
170+ ;;
171+ -t | --target-ip)
172+ shift
173+ target_ip="$1"
174+ ;;
175+ -h | --help)
176+ usage 0
177+ exit 0
178+ ;;
179+ --)
180+ ;;
181+ *)
182+ echo "Not recognize $1"
183+ usage
184+ ;;
185+ esac
186+ shift
187+ done
188+ prepare
189+ do_recovery
190+ clear_all
191+}
192+
193+if [ "${BASH_SOURCE[0]}" = "$0" ]; then
194+ main "$@"
195+fi

Subscribers

People subscribed via source and target branches