---
# System prepended metadata

title: NASA-HW6

---

# NASA-HW6
資工二 B07902046 高長聖
## NFS:
### 1.
reference: https://linuxize.com/post/how-to-mount-an-nfs-share-in-linux/
#### (1)
先在lab12的VM中，創造新的資料夾/hw6
#### (2)
在/etc/fstab中，加入: 
```
 10.217.44.92:/e/NASA_flag  /hw6   nfs  defaults   0   0
```
#### (3)
```bash=
mount /hw6
mount 10.217.44.92:/e/NASA_flag
```
#### (4)
最後，進入/nfs，裡面flag的內容: NASA{M0un71n6_NF5!}
![](https://i.imgur.com/uCblUME.png)
### 2.
reference: http://linux.vbird.org/linux_server/0330nfs.php#What_NFS_perm
NFS的permission checking mechanism是當user想要存取server端的filesystem的時候，server端會自主或者以NIS來確認user身分，並檢查是否有permission可以進行操作。

但如果要mount別人的資料夾，需要在server端更改/etc/fstab以及/etc/exports的檔案，因此沒有權限的人無法進行這種行為。再者，即使擁有更改以上兩個檔案的權限，受制於想要mount的directory的權限，也不一定有機會。

## Fix the VM:
### Disk Corruption:
reference:
https://www.cgsecurity.org/wiki/TestDisk_Step_By_Step
http://www.iitk.ac.in/LDP/LDP/Linux-Filesystem-Hierarchy/html/mnt.html
https://wiki.archlinux.org/index.php/Chroot#Using_arch-chroot
https://superuser.com/questions/595818/add-usr-local-sbin-to-the-path-of-a-user
https://docs.pagure.org/docs-fedora/the-grub2-bootloader.html
#### (1)
啟動VM:
```bash=
qemu-img create -f qcow2 -b /tmp2/nasa-hw6/VMfix.qcow2 vm0.qcow2
         #將VMfix.qcow2複製到資料夾
qemu-system-x86_64 \
        -enable-kvm \
        -smp 2,cores=1 \
        -m 2G \
        -hda vm0.qcow2 \
        -cdrom /tmp2/nasa-hw6/archlinux-2020.05.01-x86_64.iso \
        -netdev user,id=n0 \
        -device rtl8139,netdev=n0 \
        -vnc :23,password \
        -monitor stdio
        #啟動VM，並用VNC連線進去
```
#### (2)
下載testdisk，並進入其console:
```bash=
pacman -Syu testdisk
testdisk
```
#### (3)
立用[Create] $\rightarrow$ [Disk /dev/sda] $\rightarrow$ [EFI GPT] $\rightarrow$ [Analyze] $\rightarrow$ [Quick search]來檢查/dev/sda下的partition:
![](https://i.imgur.com/4RB80Li.png)
最後用[write]寫入
#### (4)
將所有的sda\*都mount到一個自創的資料夾，查看裡面的內容後之後umount，在/dev/sda2的裡面看到/etc/fstab:
![](https://i.imgur.com/qUE7pju.png)
#### (5)
但由於根據之前所看的內容，BIOS boot partition在原本應該是sda1，其餘的必須往後移一個位置，因此更改/etc/fstab為:
![](https://i.imgur.com/88XB3RA.png)
#### (6)
由於/mnt具有此vm的root directory的性質，因此，先將對應到/mnt的/dev/sda2 mount上去，之後再對mount好的/mnt，將/dev/sda\* mount到相應的位置:
```bash=
mount /dev/sda2 /mnt
mount /dev/sda1 /mnt/boot
mount /dev/sda5 /mnt/home
mount /dev/sda4 /mnt/usr
mount /dev/sda3 /mnt/var
```
#### (7)
利用fdisk創造新的BIOS boot partition /dev/sda6:
```bash=
fdisk
n #add a new partition, and all the settings are difault
t #change the type of partition to bios boot (4)
w #write changes
```
#### (8)
利用arch-chroot，進入原本要修復的VM:
```bash=
arch-chroot /mnt /usr/bin/bash
```
#### (9)
安裝並啟動grub2:
```bash=
yum install grub2
export PATH=$PATH:/usr/sbin >> ~/.bashrc #由於grub2是裝在sbin 因此要將sbin加入PATH
grub2-install /dev/sda #install grub2 to sda
grub2-mkconfig -o /boot/grub2/grub.cfg #make configuration file for grub2
```
#### (10)
關掉VM，並用以下指令重新啟動，並用VNC連線:
```bash=
qemu-system-x86_64 \
        -enable-kvm \
        -smp 2,cores=1 \
        -m 2G \
        -hda vm0.qcow2 \
        -netdev user,id=n0 \
        -device rtl8139,netdev=n0 \
        -vnc :23,password \
        -monitor stdio
```
#### (11)
可以開機，並且裡面的partition都已經修復
![](https://i.imgur.com/G6lhrGV.png)
### Broken Pacman
#### 1.
reference:
https://unix.stackexchange.com/questions/364439/how-to-manually-boot-arch-linux-from-preboot-emergency-shell
此error message的發生，通常是在更新或者是安裝的時候，遺失某些檔案所造成的。而此題的情況，可能是因為朋友太久沒有更新此系統，而導致boot partition以及pacman library無法正常安裝或者遺失。
#### 2.
reference:
https://wiki.archlinux.org/index.php/Pacman_(%E6%AD%A3%E9%AB%94%E4%B8%AD%E6%96%87)
https://www.ostechnix.com/fix-pacman-error-loading-shared-libraries-error-arch-linux/
#### (1)
啟動VM:
```bash=
qemu-img create -f qcow2 /tmp2/nasa-hw6/arch-packages-fix.qcow2 vm1.qcow2
        #將.qcow2複製到資料夾
qemu-system-x86_64 \
        -enable-kvm \
        -smp 1,cores=1 \
        -m 2G \
        -hda vm1.qcow2 \
        -cdrom /tmp2/nasa-hw6/archlinux-2020.05.01-x86_64.iso \
        -netdev user,id=n0 \
        -device rtl8139,netdev=n0 \
        -vnc :29,password \
        -monitor stdio \
        -boot d
        #啟動VM，並用VNC連線進去
```
#### (2)
用lsblk發現有兩個partition sda1、sda2，先將其mount在自創的一個資料夾，查看裡面內容，再umount，發現:
![](https://i.imgur.com/p6ILIw5.png)

#### (3)
將sda1和sda2 mount到/mnt對應的位置:
```bash=
mount /dev/sda1 /mnt
mount /dev/sda2 /mnt/boot
```
#### (4)
利用arch-chroot，進入原本要修復的VM:
```bash=
arch-chroot /mnt /usr/bin/bash
```
#### (5)
嘗試重整軟體包後更新:
```bash=
pacman -Syyu
```
到一半時，跳出:
![](https://i.imgur.com/5m5QaFx.png)
#### (6)
嘗試安裝libp11-kit:
```bash=
pacman -S libp11-kit
```
#### (7)
重新建立一份 initramfs 映像:
```bash=
mkinitcpio -p linux
```
#### (8)
重新開機，並用以下指令開啟VM:
```bash=
qemu-system-x86_64 \
        -enable-kvm \
        -smp 1,cores=1 \
        -m 2G \
        -hda vm1.qcow2 \
        -netdev user,id=n0 \
        -device rtl8139,netdev=n0 \
        -bios /usr/share/ovmf/x64/OVMF.fd \
        -vnc :31,password \
        -monitor stdio
```
#### (9)
測試pacman可否使用:
$\Rightarrow$ 原本VM裡面沒有git指令:
![](https://i.imgur.com/JOXyVLe.png)
利用:
```bash=
pacman -Syu git
```
結果順利下載git指令:
![](https://i.imgur.com/bwfjcOG.png)
(10) 用nasa的身分登入，得到flag:
![](https://i.imgur.com/1DpxaaB.png)
### Arch Linux Installation with PXE
#### 1.
path: /home/student/07/b07902046/nasa/hw_6/2.3
##### (1)
利用./start.sh，啟動PXE server，自己更改vnc password，然後用vnc連線進去
##### (2)
啟動PXE服務:
```bash=
# username root password nasa
darkhttpd /mnt/archiso
```
##### (3)
create新的img並啟動:
```bash=
qemu-img create -f qcow2 <vm-name> <vm-size>
./start2.sh <vm-name>
```
#### 2.
reference:
https://wiki.archlinux.org/index.php/Preboot_Execution_Environment
https://www.simplified.guide/ssh/copy-file
https://bbs.archlinux.org/viewtopic.php?id=187250
##### (1)
啟動VM:
```bash=
qemu-img create -f qcow2 -b /tmp2/nasa-hw6/arch.qcow2 vm2.qcow2
qemu-system-x86_64 \
        -enable-kvm \
        -smp 1,cores=1 \
        -m 2G \
        -hda vm2.qcow2 \
        -netdev user,id=n0 \
        -device rtl8139,netdev=n0 \
        -netdev socket,id=vlan,mcast=230.0.0.1:1257 \
        -bios /usr/share/ovmf/x64/OVMF.fd \
        -vnc :31,password \
        -monitor stdio
        #啟動VM，並用VNC連線進去
```
##### (2)
建立一個image的mount point，並將工作站上/tmp2/nasa-hw6/的arch linux iso複製到VM:
```bash=
mkdir -p /mnt/archiso
scp linux1.csie.org:/tmp2/nasa-hw6/archlinux-2020.05.01-x86_64.iso arch.iso
```
##### (3)
重新啟動，之後將arch.iso mount在/mnt/archiso
```bash=
reboot
mount -o loop,ro /root/arch.iso /mnt/archiso
```
##### (4)
設定網卡:
```bash=
ip link set ens3 up
ip addr add 192.168.0.1/24 dev ens3
```
##### (5)
利用dnsmasq架設dhcp和TFTP server:
```bash=
pacman -Syu dnsmasq
vim /etc/dnsmasq.conf
###
port=0
interface=ens3
bind-interfaces
dhcp-range=192.168.0.50,192.168.0.150,12h
dhcp-boot=/arch/boot/syslinux/lpxelinux.0
dhcp-option-force=209,boot/syslinux/archiso.cfg
dhcp-option-force=210,/arch/
dhcp-option-force=66,192.168.0.1
enable-tftp
tftp-root=/mnt/archiso
###
systemctl start dnsmasq.service
```
##### (6)
利用darkhttpd，使filesystem得以轉移，啟動PXE服務:
```bash=
pacman -Syu darkhttpd
darkhttpd /mnt/archiso
```
得到:
![](https://i.imgur.com/1FsZy6c.png)




























