x01.os.22: 填补一下

制作 LiveCD

  • 参考 ubuntu 官方
  • 推荐 一键制作
    在 deepin 上操作, 脚本需要更改为: RELEASE=bionic, makecd.sh 代码如下:
#!/bin/bash

# Author: Redbrother
# Email: 728083668@qq.com
# license: None
# data :2019 10
# scripts in /usr/share/debootstrap/scripts
############################################

ARCH=amd64
RELEASE=bionic
installer_url=http://mirrors.aliyun.com/ubuntu/
IMAGE=ubuntu-$RELEASE

set -e
#自定义版本项

if [ "`whoami`" != "root" ]; then
	echo "User must be root 需要root权限."
	exit 0
fi
apt install debootstrap genisoimage p7zip-full squashfs-tools -y
if [ "$?" != "0" ]; then
	echo "Commands required: mkisofs, 7z, wget."
	exit 0
fi
if [ "`which mkisofs`" == "" ]; then
	ln -s /usr/bin/genisoimage /usr/bin/mkisofs
fi

############################################

# debootstrap a base live
umount squashfs-root/* 2>/dev/null || true
rm -rf squashfs-root
mkdir -p squashfs-root
debootstrap --arch=$ARCH $RELEASE squashfs-root $installer_url

############################################

cd squashfs-root
echo "deb $installer_url $RELEASE main restricted universe multiverse"          >  etc/apt/sources.list
echo "deb $installer_url $RELEASE-updates main restricted universe multiverse"  >> etc/apt/sources.list
echo "deb $installer_url $RELEASE-security main restricted universe multiverse" >> etc/apt/sources.list
echo 'APT::Install-Recommends "false";' > etc/apt/apt.conf
chroot . chmod a+s /bin/ping
cp -f /etc/hosts etc/hosts
rm -f etc/resolv.conf
mkdir -p run/NetworkManager/
cp -f /etc/resolv.conf etc/
mount -o bind /dev dev/
mount -o bind /sys sys/
mount -o bind /proc proc/
mount -o bind /dev/pts dev/pts/
chroot . rm -f /sbin/initctl
chroot . dbus-uuidgen > /var/lib/dbus/machine-id
chroot . dpkg-divert --local --rename --add /sbin/initctl
chroot . ln -s /bin/true /sbin/initctl
chroot . apt-get update
chroot . apt-get upgrade --yes
chroot . locale-gen zh_CN.UTF-8
chroot . locale-gen en_US.UTF-8
# kernels and network
echo -e "install kernels and network …… "   
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends dbus sudo bash-completion grub-pc linux-image-generic xinit xserver-xorg network-manager apt-transport-https --yes
DEBIAN_FRONTEND=noninteractive chroot . apt-get download network-manager --yes
# Livecd necessary 
echo -e "install Livecd necessary  "  
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends ubuntu-standard casper lupin-casper discover laptop-detect os-prober --yes
# useful tools
echo -e "install useful tools  "  
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends vim nano gvfs lzma unrar rar unzip zip genisoimage p7zip-full wget aria2 zsync jigdo-file squashfs-tools debootstrap syslinux rsync dump parted gparted mdadm python3 python3-distutils --yes
# auto login
mkdir -p etc/lightdm/lightdm.conf.d
mkdir -p usr/share/lightdm/lightdm.conf.d
echo '. /etc/bash_completion'   >> etc/bash.bashrc
echo '[SeatDefaults]'           >  etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-user=ubuntu'    >> etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-user-timeout=0' >> etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-guest=false'    >> etc/lightdm/lightdm.conf.d/90-default.conf
echo 'allow-guest=false'        >> usr/share/lightdm/lightdm.conf.d/60-lightdm-gtk-greeter.conf
cp -f   ../tools/bin/*  usr/local/bin
chroot . chmod +755 -R /usr/local/bin
chroot . chmod +644 -R /boot
chroot . chmod +755 -R /root
chroot . xsession-config

############################################

# zh_CN language packs and timezone
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends language-pack-zh-hans language-pack-zh-hans-base language-pack-gnome-zh-hans language-pack-gnome-zh-hans-base --yes
chroot . cp -rf usr/share/locale-langpack/zh_CN/LC_MESSAGES/* usr/share/locale/zh_CN/LC_MESSAGES/
chroot . rm -rf usr/share/locale-langpack/
chroot . rm -f etc/timezone
chroot . rm -f etc/localtime
chroot . rm -f etc/adjtime
chroot . ln -s /usr/share/zoneinfo/Asia/Shanghai etc/localtime
echo '0.0 0 0' >  etc/adjtime
echo '0'       >> etc/adjtime
echo 'LOCAL'   >> etc/adjtime
echo 'Asia/Shanghai'             >  etc/timezone
echo 'export TZ=Asia/Shanghai'   >> etc/profile
echo 'export LANG=zh_CN.UTF-8'   >> etc/profile
echo 'export LC_ALL=zh_CN.UTF-8' >> etc/profile
# user settings
mkdir -p home/ubuntu/Desktop
mkdir -p home/ubuntu/.config
cp -rf ../tools/ubuntu/config/* home/ubuntu/.config/
chmod  +666 -R home/ubuntu/.config/
chmod  +666 -R home/ubuntu/Desktop/
chroot . rm -f home/ubuntu/.config/user-dirs.*
# fcitx-googlepinyin
echo -e "install fcitx-googlepinyin and fonts  " 
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends ttf-ubuntu-font-family fonts-wqy-microhei fcitx fcitx-ui-classic fcitx-frontend-gtk2 fcitx-config-gtk fcitx-googlepinyin --yes
# fcitx config
echo 'export GTK_IM_MODULE="xim"'    >  etc/X11/Xsession.d/96fcitx_start
echo 'export XMODIFIERS="@im=fcitx"' >> etc/X11/Xsession.d/96fcitx_start
cp -f usr/local/bin/97pinyin*      etc/X11/Xsession.d/
cp -f usr/local/bin/fcitx-pinyin*  usr/bin
cp ../tools/settings/*.desktop               usr/share/applications
cp ../tools/settings/fcitx-config.desktop    etc/xdg/autostart/
# mate desktop environment
echo -e " mate desktop environment " 
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends mate-desktop-environment-core lightdm pluma engrampa eom mate-calc --yes
# cd usr/share/themes/ && rm -rf `ls | grep -v Default Adwaita Black* Blue* Green* Menta* ` && cd ../../..
cd usr/share/themes/ && rm -rf ContrastHigh* Clearlook* Crux* eOS* HighContrast* Industria* Mist* Raleigh* Redmond* Traditional*  &&  cd ../../..
# add gonme applications Links
chroot . rm -f /usr/bin/gedit
chroot . rm -f /usr/bin/nautilus
chroot . rm -f /usr/bin/gnome-terminal
chroot . rm -f /usr/bin/file-roller
chroot . rm -f /usr/bin/eog
ln -s pluma usr/bin/gedit
ln -s caja  usr/bin/nautilus
ln -s eom   usr/bin/eog
ln -s engrampa      usr/bin/file-roller
ln -s mate-terminal usr/bin/gnome-terminal
#  modify some desktop
cd usr/share/applications
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' engrampa.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' mate-terminal.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' caja-browser.desktop
sed -i 's/Exec=fcitx/Exec=im-config/' fcitx.desktop
sed -i 's/Settings/System/' fcitx-con*.desktop
sed -i 's/NoDisplay\=true//' python*.desktop
sed -i 's/System\;//'  mate-terminal.desktop
sed -i 's/System\;//'  caja-browser.desktop
sed -i 's/MATE\ 之眼//' eom.desktop
cd ../../../
# 重新配置网络 and # clean up
chroot . rm -f etc/resolv.conf
chroot . dpkg -i network-manager*.deb
chroot . apt clean
chroot . apt-clean
chroot . apt-get clean
chroot . rm -rf tmp/*
chroot . rm -f network-manager*.deb
chroot . rm -f vmlinu*  initrd.im*
chroot . rm -f etc/init/tty3.conf etc/init/tty4.conf etc/init/tty5.conf
chroot . rm -f /var/lib/dbus/machine-id
chroot . rm -f /sbin/initctl
chroot . dpkg-divert --rename --remove /sbin/initctl
umount -lf sys/
umount -lf proc/
umount -lf dev/pts/
umount -lf dev/
cd ..

############################################

# ubuntu live base system is Success
  echo -e "ubuntu live base system is successful, you can diy it in squashfs-root contents "   
  echo -e ". Do you want to make an ISO image ? [y/n] "           
  read choice
  if [ "$choice" != "y" ]; then
      echo -e "you can run ' sudo bash mkiso.sh ' to build an iso LiveCD;successful "           
      exit 0
  fi

# make iso file ; build an iso image
mkdir -p cdimage/casper
rm -f cdimage/casper/*
rm -f $IMAGE.iso
cp -f squashfs-root/boot/vmlinuz-*-generic cdimage/casper/vmlinuz
cp -f squashfs-root/boot/initrd.img-*-generic cdimage/casper/initrd.lz
mksquashfs squashfs-root cdimage/casper/filesystem.squashfs -e boot
chmod +666 -R cdimage/casper
mkdir -p cdimage/.disk
touch cdimage/ubuntu
touch cdimage/.disk/base_installable
echo "full_cd/single"        > cdimage/.disk/cd_type
echo "Ubuntu Remix $RELEASE" > cdimage/.disk/info
cd cdimage && find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt && cd ..
mkisofs -o $IMAGE.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -r -V "$IMAGE" -cache-inodes -J -l cdimage/
chmod  +666 $IMAGE.iso
cd cdimage && rm -f ubuntu md5sum.txt  && cd ..
rm -rf cdimage/.disk
rm -rf cdimage/casper/*
echo -e "successfully done"

其他尝试

  1. 创建虚拟盘 c.img 使用 bximage 即可
  2. 创建分区 (m: 帮助, n: 创建新分区, a: 添加引导标记, p: 查看, w: 保存)
sudo fdisk c.img
  1. 映射到 /dev/mapper
sudo kpartx -av c.img
ls -l /dev/mapper
  1. 根据映射 loop 格式化
sudo mkfs.ext4 /dev/mapper/loop0p1
  1. 安装 grub
sudo mount /dev/mapper/loop0p1 /mnt
sudo grub-install --boot-directory=/mnt/ /dev/loop0
  1. 复制 vmlinz initrd.img
sudo cp /boot/vmlinuz-4.15.0-30deepin-generic /mnt/boot/vmlinuz
sudo cp /boot/initrd.img-4.15.0-30deepin-generic  /mnt/boot/initrd.img
  1. 创建配置文件 grub.cfg,复制到 /mnt/boot/grub/ 中
default=0
timeout=10
insmod ext2
set root='hd0,msdos1'
menuentry 'x01.treeos' {
    insmod gzio
	insmod part_msdos
	insmod ext2
	set root='hd0,msdos1'
	linux /boot/vmlinuz ro --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1
	initrd /boot/initrd.img 
}

Make LiveCD 脚本

# 取消注释即可下载
#wget http://kernel.org/pub/linux/kernel/v4.x/linux-4.10.9.tar.xz
#wget http://busybox.net/downloads/busybox-1.24.2.tar.bz2
#wget http://kernel.org/pub/linux/utils/boot/syslinux/syslinux-6.03.tar.xz
mkdir isoimage
tar -xvf linux-4.10.9.tar.xz
tar -xvf busybox-1.25.1.tar.bz2
tar -xvf syslinux-6.03.tar.gz
cd busybox-1.25.1
make distclean defconfig
sed -i "s/.*CONFIG_STATIC.*/CONFIG_STATIC=y/" .config
make busybox install
cd _install
rm -f linuxrc
mkdir dev proc sys
echo '#!/bin/sh' > init
echo 'dmesg -n 1' >> init
echo 'mount -t devtmpfs none /dev' >> init
echo 'mount -t proc none /proc' >> init
echo 'mount -t sysfs none /sys' >> init
echo 'setsid cttyhack /bin/sh' >> init
chmod +x init
find . | cpio -R root:root -H newc -o | gzip > ../../isoimage/rootfs.gz
cd ../../linux-4.10.9
make mrproper defconfig bzImage
cp arch/x86/boot/bzImage ../isoimage/kernel.gz
cd ../isoimage
cp ../syslinux-6.03/bios/core/isolinux.bin .
cp ../syslinux-6.03/bios/com32/elflink/ldlinux/ldlinux.c32 .
echo 'default kernel.gz initrd=rootfs.gz' > ./isolinux.cfg
xorriso \
    -as mkisofs \
    -o ../minimal_linux_live.iso \
    -b isolinux.bin \
    -c boot.cat \
    -no-emul-boot \
    -boot-load-size 4 \
    -boot-info-table \
    ./
cd ..

图片黑白处理

import os 
from PIL import Image 

def blackwhite_image(filename, threshold=160):
    image = Image.open(filename)
    gray = image.convert("L")
    binary = gray.point(lambda x: 255 if x>threshold else x)
    dir = os.path.dirname(filename)
    name = os.path.basename(filename)
    temppath = os.path.join(dir,"黑白_"+name)
    binary.save(temppath)
    print("OK!")

学习文档

x01.docs

Hello Django

Django for Beginner 学习笔记

  1. 安装 django
pip install django
  1. 新建目录 hello-django, 终端进入该目录分别运行以下命令
django-admin startproject django_project .
python manage.py migrate
python manage.py startapp pages
  1. 在 django_project/settings.py 的 INSTALLED_APPS 中添加 "pages":
INSTALLED_APPS = [
    ...
    "pages",
]
  1. 修改 pages/views.py 内容如下:
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def homne_page_view(request):
    return HttpResponse("hello world!")
  1. 新增 pages/urls.py 内容如下:
from django.urls import path 
from .views import homne_page_view

urlpatterns = [
    path("", homne_page_view, name="home"),
]
  1. 修改 django_project/urls.py 内容如下:
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("admin/", admin.site.urls),
    path('', include('pages.urls')),
]

  1. OK! 运行如下命令后, 可在浏览器中输入地址: http://127.0.0.1:8000 进行浏览.
python manage.py runserver

Pages App

  1. 添加 templates/pages/base.html, home.html, about.html
  • base.html
<header>
    <a href="{% url 'home' %}">Home</a> |
    <a href="{% url 'about' %}">About</a>
</header>

{% block content %} {% endblock content %}
  • home.html
{% extends "base.html" %}

{% block content %}
<h1>Home Page</h1>
{% endblock content %}
  • about.html
{% extends "base.html" %}

{% block content %}
<h1>About Page</h1>
{% endblock content %}
  1. 修改 django_project/settings.py
TEMPLATES = [
    {
        ...
        "DIRS": [BASE_DIR / "templates/pages"], # new
        ...
    },
]
  1. 修改 pages/views.py
from django.views.generic import TemplateView

class HomePageView(TemplateView):
    template_name = "home.html"

class AboutPageView(TemplateView):
    template_name = "about.html"
  1. 修改 pages/urls.py
from django.urls import path 
from .views import HomePageView, AboutPageView

urlpatterns = [
    path("", HomePageView.as_view(), name="home"),
    path("about", AboutPageView.as_view(), name="about")
]
  1. OK! python manage.py runserver 运行之.

Posts App

  1. 新建应用 python manage.py startapp posts, 修改 django_project/settings.py
INSTALLED_APPS = [
    ...
    "posts",
]
  1. 修改 posts/models.py
from django.db import models

class Post(models.Model):
    text = models.TextField()

    def __str__(self) -> str:
        return self.text[:50]
  1. 分别运行
python manage.py makemigrations posts
python manage.py migrate
python manage.py createsuperuser
  1. 修改 posts/admin.py
from django.contrib import admin
from .models import Post

admin.site.register(Post)
  1. 修改 posts/views.py
from django.views.generic import ListView, TemplateView
from .models import Post

class HomePageView(ListView):
    model = Post 
    template_name = "posts/home.html"

class AboutPageView(TemplateView):
    template_name = "posts/about.html"
  1. 新建 templates/posts/base.html, home.html, about.html 等模板和URL: posts/urls.py, 修改 django_project/urls.py, 内容参考 Pages App, 不再赘述. OK! 运行无误.

Blog App

  1. 参考以上, 运行以下:
python manage.py startapp blog
# 添加 blog 到 settings.py => INSTALLED_APPS 中
  1. blog/models.py
from django.db import models
from django.urls import reverse

class Post(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey(
        "auth.User",
        on_delete = models.CASCADE,
    )
    body = models.TextField()

    def __str__(self) -> str:
        return self.title

    def get_absolute_url(self):
        return reverse("post_detail", kwargs={'pk': self.pk})
  • shell
python manage.py makemigrations blog
python manage.py migrate
  1. blog/admin.py
from django.contrib import admin
from .models import Post

admin.site.register(Post)
  1. 新增 templates/blog/*, static, 修改 url, view 等
  • django_project/settings.py
STATIC_URL = "static/"
STATICFILES_DIRS = [BASE_DIR / "static"]
  • static/css/base.css
body {
    font-family: 'Source Sans Pro', sans-serif;
    font-size: 18px;
}

header {
    border-bottom: 1px solid #999;
    margin-bottom: 2rem;
    display: flex;
}

header h1 a {
    color: red;
    text-decoration: none;
    margin: 10px;
}

.nav-left {
    margin-right: auto;
}

.nav-right {
    display: flex;
    padding-top: 2rem;
}

.post-entry {
    margin-bottom: 2rem;
}

.post-entry h2 {
    margin: 0.5rem 0;
}

.post-entry h2 a,
.post-entry h2 a:visited {
    color: blue;
    text-decoration: none;
}

.post-entry p {
    margin: 0;
    font-weight: 400;
}
  • django_project/urls.py
urlpatterns = [
    ...
    path("blog/", include('blog.urls')),
]
  • blog/urls.py
from django.urls import path 
from .views import BlogListView, PostDetailView

urlpatterns = [
    path("", BlogListView.as_view(), name='home'),
    path("post/<int:pk>/", PostDetailView.as_view(), name='post_detail')
]
  • blog/views.py
from django.views.generic import ListView, DetailView
from .models import Post

class BlogListView(ListView):
    model = Post 
    template_name = "blog/home.html"

class PostDetailView(DetailView):
    model = Post 
    template_name = 'blog/post_detail.html'

  1. Create, Update, Delete 等详见代码, 不过是添加修改模板, view, url 而已. OK!

  2. login, logout, signin

  • 修改 django_project/urls.py
urlpatterns = [
    ...
    path('accounts/', include('django.contrib.auth.urls')), 
]
  • 新建目录: mkdir templates/registration 和文件 login.html
{% extends "blog/base.html" %}
{% block content %}

<h2>Log In</h2>
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Log In</button>
</form>

{% endblock content %}
  • blog/views.py 添加 logout
def logout_view(request):
    logout(request=request)
    return render(request=request, template_name='blog/home.html')
  • 注册 signup: 新建应用 accounts

运行

  1. 删除 db.sqlite3
  2. 运行数据迁移 python manage.py migrate
  3. 创建超级用户 python manage.py createsuperuser
  4. 执行命令 python manage.py runserver
  5. OK! 在浏览器中输入 http://127.0.0.1:8000/blog 即可.

posted on 2023-06-11 22:02  x01  阅读(20)  评论(0编辑  收藏  举报

导航