前置作業

系統裝好後先執行下面指令清除 dnf cache
dnf clean all
dnf autoremove

然後重開機進入其他Linux系統或live系統
掛載要備份的分割區,例如要備份 sda4
將 sda4 掛載到 /mnt
sudo -s
mount /dev/sda4 /mnt

清除垃圾
* boot clean
rm -rf /mnt/boot/grub2/grub.cfg
rm -rf /mnt/boot/initramfs-0-rescue-*
rm -rf /mnt/boot/vmlinuz-0-rescue-*
* etc clean
rm -rf /mnt/etc/nsswitch.conf.bak
rm -rf /mnt/etc/group-
rm -rf /mnt/etc/gshadow-
rm -rf /mnt/etc/passwd-
rm -rf /mnt/etc/shadow-
rm -rf /mnt/etc/subgid-
rm -rf /mnt/etc/subuid-
rm -rf /mnt/etc/fstab
* home clean
rm -rf /mnt/home/*/*
rm -rf /mnt/home/*/.*
rm -rf /mnt/root/*
rm -rf /mnt/root/.*
* misc clean
rm -rf /mnt/media/*
rm -rf /mnt/tmp/*
rm -rf /mnt/tmp/.*
rm -rf /mnt/lost+found
* var clean
rm -rf /mnt/var/cache/dnf/*
rm -rf /mnt/var/log/*
rm -rf /mnt/var/cache/apt/archives/*
rm -rf /mnt/var/lib/blueman/*
rm -rf /mnt/var/lib/bluetooth/*
rm -rf /mnt/var/lib/dbus/*
rm -rf /mnt/var/lib/dhclient/*
rm -rf /mnt/var/lib/dkms/*
rm -rf /mnt/var/lib/dnf/yumdb/*
rm -rf /mnt/var/lib/dnf/history/*
rm -rf /mnt/var/lib/dnsmasq/*
rm -rf /mnt/var/lib/logrotate/*
rm -rf /mnt/var/lib/NetworkManager/*
rm -rf /mnt/var/lib/rsyslog/*
rm -rf /mnt/var/lib/systemd/catalog/*
rm -rf /mnt/var/lib/systemd/coredump/*
rm -rf /mnt/var/lib/upower/*
rm -rf /mnt/var/tmp/*
mkdir -p /mnt/var/log/audit
chmod -R 700 /mnt/var/log/audit
mkdir -p /mnt/var/log/journal
chown -R root:systemd-journal /mnt/var/log/journal
chmod -R 2755 /mnt/var/log/journal
mkdir -p /mnt/var/log/lightdm
chown -R lightdm:lightdm /mnt/var/log/lightdm
mkdir -p /mnt/var/log/pluto
chmod -R 700 /mnt/var/log/pluto
mkdir -p /mnt/var/log/ppp
chmod -R 700 /mnt/var/log/ppp
mkdir -p /mnt/var/log/samba
chmod -R 700 /mnt/var/log/samba
mkdir -p /mnt/var/log/sssd
chmod -R 750 /mnt/var/log/sssd
mkdir -p /mnt/var/log/tor
chmod -R 750 /mnt/var/log/tor

修改主機名稱
名稱可隨意修改,範例: localhosts
echo localhosts > /mnt/etc/hostname

開始製作 LiveOS Image

例如: 存放路徑在 /media/fedora/DATA/ 目錄,要備份的分割區為sda4

SAVEDIR=$/media/fedora/DATA/
TARGET=sda4

  • 建立目錄
    mkdir -p $SAVEDIR/squashfs-root/LiveOS

  • 計算sda4已使用區塊大小(Live檔案系統的容量)
    USED=$(df | grep /dev/$TARGET | awk '{print $3 }')

  • 區塊大小增加1GB容量,因為Live系統啟動時會用掉約400MB的空間,所以啟動後Live系統還有約600MB的可用空間
    BS=$(expr $USED + 1048576)

  • 製作 空白的 image
    dd if=/dev/zero of=$SAVEDIR/squashfs-root/LiveOS/rootfs.img bs=$BS count=1024 status=progress

  • 格式化為 ext4
    mkfs.ext4 -j -L Fedora $SAVEDIR/squashfs-root/LiveOS/rootfs.img

  • 掛載 rootfs.img
    mkdir -p /tmp/rootfs
    mount -o loop $SAVEDIR/squashfs-root/LiveOS/rootfs.img /tmp/rootfs

  • 將 sda4 內容複製到 rootfs.img
    SRC=/mnt
    DEST=/tmp/rootfs
    cd $SRC; (tar cf - *) | (cd $DEST ; tar xf -) > /dev/null 2>&1

  • 打包 rootfs.img 為 squashfs.img
    cd $SAVEDIR
    mksquashfs squashfs-root squashfs.img

  • 完成後重建 fstab
    UUID=$(blkid -s UUID -o value /dev/sdb1)
    cat < /mnt/etc/fstab
    UUID=$UUID / ext4 defaults 1 1
    EOF

或使用 genfstab 重建 fstab
wget https://raw.githubusercontent.com/Mint-Fans/linux-package/master/genfstab4
chmod +x genfstab
./genfstab -U /mnt > /mnt/etc/fstab

  • 卸載sda4與rootfs
    umount /mnt
    umount /tmp/rootfs

到此基本上 live 檔案系統製作完成,接下來製作可引導的 Live 系統的 initramfs。

製作 Live initramfs

  • 安裝 dracut-live
    sudo dnf install dracut-live

dracut-live 套件不支援 NTFS 分割區的引導,可以安裝 NTFS Loop 補釘

  • dracut NTFS Loop Boot Patch
    git clone https://github.com/rgcjonas/dracut-ntfsloop.git9
    sudo cp -r dracut-ntfsloop/90ntfsloop /usr/lib/dracut/modules.d/
    sed -i 's/$(find_binary "ntfs-3g")/\/usr\/bin\/ntfs-3g/g' /usr/lib/dracut/modules.d/90dmsquash-live/dmsquash-live-root.sh

  • 建立可引導 Live 系統的 initramfs
    sudo dracut --nomdadmconf --nolvmconf --xz \
    --add 'livenet dmsquash-live ntfsloop convertfs pollcdrom qemu qemu-net' \
    --omit 'plymouth' --no-hostonly --debug \
    --no-early-microcode --force \
    ~/initrd.img

  • 複製 vmlinuz
    cp /boot/vmlinuz-$(uname -r) ~/vmlinuz

製作ISO映像檔

下載iso文件 (取自fedora-27-xfce iso,內容文字標題自行修改)
wget https://github.com/Mint-Fans/linux-package/raw/fedora/Fedora-mkiso.tar.gz22
tar zxvf Fedora-mkiso.tar.gz

解壓縮後,將 squashfs.img、initrd.img、vmlinuz 放到 Fedora/Fedora64/LiveOS/ 目錄內

然後開始執行腳本打包iso
cd Fedora
./fedora64-mkiso

USB或HD Grub4dos引導參數

範例: 分割區標籤為 INST
title Fedora
find --set-root --ignore-floppies --ignore-cd /LiveOS/vmlinuz
kernel /LiveOS/vmlinuz root=live:LABEL=INST rd.live.image
initrd /LiveOS/initrd.img
boot

將ISO映像檔寫到USB的方法

lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 465.8G 0 disk
├─sda1 8:1 0 201M 0 part
├─sda2 8:2 0 201M 0 part /boot
├─sda3 8:3 0 15G 0 part /
└─sda4 8:4 0 15G 0 part /mnt
sdb 8:16 1 979.8M 0 disk
└─sdb1 8:17 1 978M 0 part /run/media/fedora/USB
sr0 11:0 1 1024M 0 rom

例如USB裝置為sdb
dd if=fedora.iso of=/dev/sdb

  • 已建立

    3月 '18
  • 最新回覆

    3月 '18
  • 1

    回覆

  • 5.7 千

    觀看

  • 2

    用戶

  • 3

    鏈結

The Fedora Project is mainatined by the community and sponsored by Red Hat.
This website also is maintained by its community but not affiliated with or endorsed by the Fedora Project.