# % Options Settings: https://orgmode.org/manual/Export-Settings.html
#+OPTIONS: timestamp:nil ^:nil <:nil p:t prop:t tags:t tasks:t todo:t
#+LATEX_CLASS: article
#+LaTeX_CLASS_OPTIONS: [a4paper,12pt]
#+LATEX_HEADER: \usepackage{booktabs}
# % to include pdf/eps/png files
#+LATEX_HEADER: \usepackage{indentfirst}
#+LATEX_HEADER: \usepackage{graphicx}
# % useful to add 'todo' markers
#+LaTeX_HEADER: \usepackage{todonotes}
#+LaTeX_HEADER: \setlength{\marginparwidth}{2cm} % fix todonotes warning
# % hyperrefs
#+LaTeX_HEADER: \usepackage{hyperref}
# % ----------------- Code blocks ----------------
# % Dependencies: pip install pygments
# % nice source code formatting
#+LaTeX_HEADER: \usepackage{xcolor}
#+LaTeX_HEADER: \definecolor{bg}{rgb}{0.98,0.98,0.98}
#+LaTeX_HEADER: \usepackage[cache=false]{minted}
#+LaTeX_HEADER: \setminted{
#+LaTeX_HEADER:   fontsize=\small,
#+LaTeX_HEADER:   baselinestretch=1,
#+LaTeX_HEADER:   xleftmargin=4em,
#+LaTeX_HEADER:   breaklines,
#+LaTeX_HEADER:   mathescape,
#+LaTeX_HEADER:   linenos,
#+LaTeX_HEADER:   numbersep=5pt,
#+LaTeX_HEADER:   frame=leftline,
#+LaTeX_HEADER:   framesep=2mm,
#+LaTeX_HEADER:   autogobble,
#+LaTeX_HEADER:   style=tango,
#+LaTeX_HEADER:   bgcolor=bg
#+LaTeX_HEADER: }
# % change style of section headings
#+LaTeX_HEADER: \usepackage{sectsty}
#+LaTeX_HEADER: \allsectionsfont{\sffamily}
# % only required for orgmode ticked TODO items, can remove
#+LaTeX_HEADER: \usepackage{amssymb}
# % only required for underlining text
#+LaTeX_HEADER: \usepackage[normalem]{ulem}
# % often use this in differential operators:
#+LaTeX_HEADER: \renewcommand{\d}{\ensuremath{\mathrm{d}}}
# % allow more reasonable text width for most documents than LaTeX default
#+LaTeX_HEADER: \setlength{\textheight}{21cm}
#+LaTeX_HEADER: \setlength{\textwidth}{16cm}
# % reduce left and right margins accordingly
#+LaTeX_HEADER: \setlength{\evensidemargin}{-0cm}
#+LaTeX_HEADER: \setlength{\oddsidemargin}{-0cm}
# % reduce top margin
#+LaTeX_HEADER: \setlength{\topmargin}{0cm}
# % references formats
#+LaTeX_HEADER: \usepackage[round]{natbib}
# % Chinese supported
#+LATEX_HEADER: \usepackage{ctex}
# % Increase default line spacing a little
#+LATEX_HEADER: \usepackage{setspace}
#+LATEX_HEADER: \renewcommand{\baselinestretch}{1.5}
#+LATEX_HEADER: \setlength{\parskip}{0.8em}
# % Line & paragraph space end
# % item list margin
#+LATEX_HEADER: \usepackage{enumitem}
# % Breaking Page Between Title and Toc
#+LATEX_HEADER: \makeatletter \def\@maketitle{\null \begin{center} {\vskip 5em \Huge \@title} \vskip 30em {\LARGE \@author} \vskip 3em {\LARGE \@date} \end{center} \newpage} \makeatother
# % End of Breaking Page Between Title and Toc
#+LATEX_HEADER: \renewcommand\contentsname{目录}
# Generate Tex File: C-c C-e l l; then replace verbatim with minted, and must special the code language
#+LATEX_HEADER: % Generate PDF: xelatex -shell-escape <tex file>
#+AUTHOR: jouyouyun
#+EMAIL: yanbowen717@gmail.com
#+TITLE: 更新流程

** Prepare
+ 已安装更新管理程序： =deepin-upgrade-manager=
+ 已初始化本地 =repo=
+ 已下载更新文件
  - 若是增量更新，则本地 =repo= 中应存在已提交的版本
+ 系统至少存在根分区和数据分区
+ 系统目录的关系及挂载方式如下
  - =ln -svf /usr/{boot,bin,sbin,lib,lib<qual>} /=
  - =/,/usr, /osroot= 挂载到根分区
  - =/osroot/data= 挂载到数据分区
  - =mount -o bind /osroot/data/opt /opt=
  - =mount -o bind /osroot/data/var /var=
  - =mount -o bind /osroot/data/home /home=
  - =mount -o bind /osroot/data/roothome /root=
  - =/etc= 由 =/usr/etc= 生成，并将 =/var/etc= 作为 =overlayfs upperdir=
    - ~overlay /etc overlay x-systemd.requires=/var,lowerdir=/etc,upperdir=/var/etc,workdir=/var/overlay 0 1~

** Upgrade
安装主要分为几个部分：
+ 解压并验证更新文件
+ =commit= 数据
  - =/usr=
  - =/boot=
+ 切换系统状态
  - =/usr=
  - =/etc=
  - =var=

详细流程如下：
1. 解压并验证更新文件
  - 解压更新文件
  - 读取文件中的 =manifest.xml= ，对以下文件进行签名验证
    - =os_data.squashfs=
    - =os_desc.xml=
    - =os_file.diff=
    - =after install scripts=
2. =commit= 数据
  - 挂载 =os_data.squashfs= 到 =/usr-squashfs-xxx=
  - 读取 =manifest.xml= 获取更新方式，生成 =/usr-snap-xxx=
    - =repo Commit=
      - 全量更新
        - 复制 =/usr-squashfs-xxx= 到 =/usr-snap-xxx=
      - 增量更新
        - 获取 =repo= 中的 =latest version=
        - =checkout latest version= 到 =/usr-snap-xxx=
        - 合并 =/usr-squashfs-xxx= 中的文件到 =/usr-snap-xxx=
  - =make rootfs=
    - =change active version to new version=
    - 合成 =rootfs/usr=
      - =merge /boot to snap/boot=
      - =combine snap and vendor/usr to rootfs/usr=
    - =rootfs mount=
      - =osroot, sys, proc, dev=
        - =mount -o bind /osroot osroot=
        - =mount -t proc proc proc=
        - =mount -t sysfs sys sys=
        - =mount -o bind /dev dev=
        - =mount -t devpts pts dev/pts=
      - =make etc=
        - =cp -r rootfs/usr/etc rootfs/etc=
        - =cp -r /var/etc rootfs/etc=
      - =symlink boot,bin,sbin,lib,lib<qual>,var=
        - =ln -svf usr/{boot,bin,sbin,lib,lib<qual>,var} ./=
    - =chroot and do command=
      - =update-initramfs=
      - =update-grub=
      - 执行 =after install scripts=
        - 主要用于执行文件替换、 =postinst= 等
    - 更新 =snapshot=
      - =cp -r rootfs/usr/boot snap/boot=
      - =cp -r /etc/fstab snap/etc/fstab=
    - =change active version to old version=
  - =commit again=
    - =repo Commit=
    - =repo Snapshot=
      - =remove exists snapshot=
  - =clean rootfs and upgrade file=
3. 切换到新版本
  - =combine /usr=
    - =Snapshot new version=
    - =combine snapshot and vendor/usr to /usr-tmp-xxx=
    - =replace /usr with /usr-tmp-xxx=
  - =merge /var=
    - =merge /usr/var to /var= 跳过已存在的文件
  - =combine /etc=
    - =umount /etc=
    - =cp -r /usr/etc /etc-tmp-xxx=
    - =replace /etc-tmp-xxx with /etc=
    - =remount /etc overlay=
      =mount -t overlay overlay -o lowerdir=/etc,upperdir=/var/etc,workdir=/var/overlay /etc=
  - =merge dpkg status=
    - =repo Cat old version var/lib/dpkg/status to /tmp/status-tmp=
    - =merge /tmp/status-tmp and /usr/var/lib/dpkg/status to /var/lib/dpkg/status=
  - =change active version to new version=

=merge dir= 时，默认跳过已存在的文件， =copy dir= 时则默认覆盖存在的文件。
若 =commit= 之前失败，则清理 =usr-tmp-xxx= 和 =rootfs= 。

** Rollback
回滚时需指定 =version= ，回滚步骤参见 **Upgrade** 中的第 =3= 步。

注意： *在 =initrd.img= 中进行 =rollback= 时，需要挂载 =/osroot/data= 和 =/var=* 。
